OSDN Git Service

* include/std/condition_variable (condition_variable_any): Remove
[pf3gnuchains/gcc-fork.git] / libgcc / generic-morestack-thread.c
1 /* Thread library support for -fsplit-stack.  */
2 /* Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
3    Contributed by Ian Lance Taylor <iant@google.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
20
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 <http://www.gnu.org/licenses/>.  */
25
26 #include "tconfig.h"
27 #include "tsystem.h"
28 #include "coretypes.h"
29 #include "tm.h"
30
31 /* If inhibit_libc is defined, we can not compile this file.  The
32    effect is that people will not be able to use -fsplit-stack.  That
33    is much better than failing the build particularly since people
34    will want to define inhibit_libc while building a compiler which
35    can build glibc.  */
36
37 #ifndef inhibit_libc
38
39 #include <errno.h>
40 #include <pthread.h>
41
42 #include "generic-morestack.h"
43
44 /* We declare the pthread functions we need as weak, so that
45    libgcc_s.so does not need to be linked against -lpthread.  */
46
47 extern int pthread_once (pthread_once_t *, void (*) (void))
48   __attribute__ ((weak));
49
50 extern int pthread_key_create (pthread_key_t *, void (*) (void *))
51   __attribute__ ((weak));
52
53 extern int pthread_setspecific (pthread_key_t, const void *)
54   __attribute__ ((weak));
55
56 /* The key for the list of stack segments to free when the thread
57    exits.  This is created by pthread_key_create.  */
58
59 static pthread_key_t segment_list_key;
60
61 /* Used to only run create_key once.  */
62
63 static pthread_once_t create_key_once = PTHREAD_ONCE_INIT;
64
65 /* Release all the segments for a thread.  This is the destructor
66    function used by pthread_key_create, and is called when a thread
67    exits.  */
68
69 static void
70 free_segments (void* arg)
71 {
72   __morestack_release_segments ((struct stack_segment **) arg, 1);
73 }
74
75 /* Set up the key for the list of segments.  This is called via
76    pthread_once.  */
77
78 static void
79 create_key (void)
80 {
81   int err;
82
83   err = pthread_key_create (&segment_list_key, free_segments);
84   if (err != 0)
85     {
86       static const char msg[] = "pthread_key_create failed: errno ";
87       __morestack_fail (msg, sizeof msg - 1, err);
88     }
89 }
90
91 /* Pass information from the pthread_create wrapper to
92    stack_split_initialize_thread.  */
93
94 struct pthread_create_args
95 {
96   void *(*start_routine) (void *);
97   void *arg;
98 };
99
100 /* Initialize a thread.  This is called via pthread_create.  It calls
101    a target dependent function to set up any required stack guard.  */
102
103 static void* stack_split_initialize_thread (void *)
104   __attribute__ ((no_split_stack));
105
106 static void *
107 stack_split_initialize_thread (void *varg)
108 {
109   struct pthread_create_args *args = (struct pthread_create_args *) varg;
110   int err;
111   void *(*start_routine) (void *);
112   void *arg;
113
114   __stack_split_initialize ();
115
116   err = pthread_setspecific (segment_list_key, (void *) &__morestack_segments);
117   if (err != 0)
118     {
119       static const char msg[] = "pthread_setspecific failed: errno ";
120       __morestack_fail (msg, sizeof msg - 1, err);
121     }
122
123   start_routine = args->start_routine;
124   arg = args->arg;
125   free (args);
126   return (*start_routine) (arg);
127 }
128
129 /* This function wraps calls to pthread_create to make sure that the
130    stack guard is initialized for new threads.  FIXME: This hack will
131    not be necessary if glibc supports -fsplit-stack directly.  */
132
133 int __wrap_pthread_create (pthread_t *, const pthread_attr_t *,
134                            void *(*start_routine) (void *), void *)
135   __attribute__ ((visibility ("hidden")));
136
137 extern int __real_pthread_create (pthread_t *, const pthread_attr_t *,
138                                   void *(*start_routine) (void *), void *)
139   __attribute__ ((weak));
140
141 int
142 __wrap_pthread_create (pthread_t *tid, const pthread_attr_t *attr,
143                        void *(*start_routine) (void *), void *arg)
144 {
145   int err;
146   struct pthread_create_args* args;
147
148   err = pthread_once (&create_key_once, create_key);
149   if (err != 0)
150     {
151       static const char msg[] = "pthread_once failed: errno ";
152       __morestack_fail (msg, sizeof msg - 1, err);
153     }
154
155   args = malloc (sizeof (struct pthread_create_args));
156   if (args == NULL)
157     return EAGAIN;
158   args->start_routine = start_routine;
159   args->arg = arg;
160   return __real_pthread_create (tid, attr, stack_split_initialize_thread, args);
161 }
162
163 #endif /* !defined (inhibit_libc) */