Optimize for kernels which are known to have the vfork syscall.
[glibc/pb-stable.git] / nptl / pthreadP.h
blob3223291dab913586eb78411aaff7b2ed9be9cb54
1 /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifndef _PTHREADP_H
21 #define _PTHREADP_H 1
23 #include <pthread.h>
24 #include <setjmp.h>
25 #include <sys/syscall.h>
26 #include "descr.h"
27 #include <tls.h>
28 #include <lowlevellock.h>
29 #include <stackinfo.h>
30 #include <internaltypes.h>
31 #include <pthread-functions.h>
34 /* Internal variables. */
37 /* Default stack size. */
38 extern size_t __default_stacksize attribute_hidden;
40 /* Size and alignment of static TLS block. */
41 extern size_t __static_tls_size attribute_hidden;
42 extern size_t __static_tls_align attribute_hidden;
44 /* Thread descriptor handling. */
45 extern list_t __stack_user;
46 hidden_proto (__stack_user)
48 /* Attribute handling. */
49 extern struct pthread_attr *__attr_list attribute_hidden;
50 extern lll_lock_t __attr_list_lock attribute_hidden;
52 /* First available RT signal. */
53 extern int __current_sigrtmin attribute_hidden;
54 /* Last available RT signal. */
55 extern int __current_sigrtmax attribute_hidden;
57 /* Concurrency handling. */
58 extern int __concurrency_level attribute_hidden;
60 /* Thread-local data key handling. */
61 extern struct pthread_key_struct __pthread_keys[PTHREAD_KEYS_MAX];
62 hidden_proto (__pthread_keys)
64 /* The library can run in debugging mode where it performs a lot more
65 tests. */
66 extern int __pthread_debug attribute_hidden;
67 #define DEBUGGING_P __builtin_expect (__pthread_debug, 0)
70 /* Cancellation test. */
71 #define CANCELLATION_P(self) \
72 do { \
73 int cancelhandling = THREAD_GETMEM (self, cancelhandling); \
74 if (CANCEL_ENABLED_AND_CANCELED (cancelhandling)) \
75 { \
76 THREAD_SETMEM (self, result, PTHREAD_CANCELED); \
77 __do_cancel (); \
78 } \
79 } while (0)
81 /* Set cancellation mode to asynchronous. */
82 #define CANCEL_ASYNC() \
83 __pthread_enable_asynccancel ()
84 /* Reset to previous cancellation mode. */
85 #define CANCEL_RESET(oldtype) \
86 __pthread_disable_asynccancel (oldtype)
88 #if !defined NOT_IN_libc
89 /* Same as CANCEL_ASYNC, but for use in libc.so. */
90 # define LIBC_CANCEL_ASYNC() \
91 __libc_enable_asynccancel ()
92 /* Same as CANCEL_RESET, but for use in libc.so. */
93 # define LIBC_CANCEL_RESET(oldtype) \
94 __libc_disable_asynccancel (oldtype)
95 # define LIBC_CANCEL_HANDLED() \
96 __asm (".globl " __SYMBOL_PREFIX "__libc_enable_asynccancel"); \
97 __asm (".globl " __SYMBOL_PREFIX "__libc_disable_asynccancel")
98 #elif defined NOT_IN_libc && defined IS_IN_libpthread
99 # define LIBC_CANCEL_ASYNC() CANCEL_ASYNC ()
100 # define LIBC_CANCEL_RESET(val) CANCEL_RESET (val)
101 # define LIBC_CANCEL_HANDLED() \
102 __asm (".globl " __SYMBOL_PREFIX "__pthread_enable_asynccancel"); \
103 __asm (".globl " __SYMBOL_PREFIX "__pthread_disable_asynccancel")
104 #else
105 # define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */
106 # define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */
107 # define LIBC_CANCEL_HANDLED() /* Nothing. */
108 #endif
111 /* This function is responsible for calling all registered cleanup
112 handlers and then terminate the thread. This includes dellocating
113 the thread-specific data. The implementation is complicated by the
114 fact that we have to handle to cancellation handler registration
115 methods: exceptions using try/finally and setjmp.
117 The setjmp method is always available. The user might compile some
118 code which uses this method because no modern compiler is
119 available. So we have to handle these first since we cannot call
120 the cleanup handlers if the stack frames are gone. At the same
121 time this opens a hole for the register exception handler blocks
122 since now they might be in danger of using an overwritten stack
123 frame. The advise is to only use new or only old style cancellation
124 handling. */
125 static inline void
126 __do_cancel (void)
128 struct pthread *self = THREAD_SELF;
130 /* Throw an exception. */
131 // XXX TBI
133 /* If throwing an exception didn't work try the longjmp. */
134 __libc_longjmp (self->cancelbuf, 1);
136 /* NOTREACHED */
140 /* Test whether stackframe is still active. */
141 #ifdef _STACK_GROWS_DOWN
142 # define FRAME_LEFT(frame, other) ((char *) frame >= (char *) other)
143 #elif _STACK_GROWS_UP
144 # define FRAME_LEFT(frame, other) ((char *) frame <= (char *) other)
145 #else
146 # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
147 #endif
150 /* Internal prototypes. */
152 /* Thread list handling. */
153 extern struct pthread *__find_in_stack_list (struct pthread *pd)
154 attribute_hidden;
156 /* Deallocate a thread's stack after optionally making sure the thread
157 descriptor is still valid. */
158 extern void __free_tcb (struct pthread *pd) attribute_hidden;
160 /* Free allocated stack. */
161 extern void __deallocate_stack (struct pthread *pd) attribute_hidden;
163 /* Mark all the stacks except for the current one as available. This
164 function also re-initializes the lock for the stack cache. */
165 extern void __reclaim_stacks (void) attribute_hidden;
167 /* longjmp handling. */
168 extern void __pthread_cleanup_upto (__jmp_buf target, char *targetframe);
171 /* Functions with versioned interfaces. */
172 extern int __pthread_create_2_1 (pthread_t *newthread,
173 const pthread_attr_t *attr,
174 void *(*start_routine) (void *), void *arg);
175 extern int __pthread_create_2_0 (pthread_t *newthread,
176 const pthread_attr_t *attr,
177 void *(*start_routine) (void *), void *arg);
178 extern int __pthread_attr_init_2_1 (pthread_attr_t *attr);
179 extern int __pthread_attr_init_2_0 (pthread_attr_t *attr);
182 /* Event handlers for libthread_db interface. */
183 extern void __nptl_create_event (void);
184 extern void __nptl_death_event (void);
185 hidden_proto (__nptl_create_event)
186 hidden_proto (__nptl_death_event)
188 /* Register the generation counter in the libpthread with the libc. */
189 extern void __libc_pthread_init (unsigned long int *ptr,
190 void (*reclaim) (void),
191 const struct pthread_functions *functions);
194 /* Namespace save aliases. */
195 extern int __pthread_getschedparam (pthread_t thread_id, int *policy,
196 struct sched_param *param);
197 extern int __pthread_setschedparam (pthread_t thread_id, int policy,
198 const struct sched_param *param);
199 extern int __pthread_setcancelstate (int state, int *oldstate);
200 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
201 __const pthread_mutexattr_t *__mutexattr);
202 extern int __pthread_mutex_init_internal (pthread_mutex_t *__mutex,
203 __const pthread_mutexattr_t *__mutexattr);
204 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
205 extern int __pthread_mutex_destroy_internal (pthread_mutex_t *__mutex);
206 extern int __pthread_mutex_trylock (pthread_mutex_t *_mutex);
207 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
208 extern int __pthread_mutex_lock_internal (pthread_mutex_t *__mutex);
209 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
210 extern int __pthread_mutex_unlock_internal (pthread_mutex_t *__mutex);
211 extern int __pthread_mutexattr_init (pthread_mutexattr_t *attr);
212 extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *attr);
213 extern int __pthread_mutexattr_settype (pthread_mutexattr_t *attr, int kind);
214 extern int __pthread_attr_destroy (pthread_attr_t *attr);
215 extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
216 int *detachstate);
217 extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
218 int detachstate);
219 extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
220 int *inherit);
221 extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
222 extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
223 struct sched_param *param);
224 extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
225 const struct sched_param *param);
226 extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
227 int *policy);
228 extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
229 extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
230 extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
231 extern int __pthread_attr_getstackaddr (__const pthread_attr_t *__restrict
232 __attr, void **__restrict __stackaddr);
233 extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
234 void *__stackaddr);
235 extern int __pthread_attr_getstacksize (__const pthread_attr_t *__restrict
236 __attr,
237 size_t *__restrict __stacksize);
238 extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
239 size_t __stacksize);
240 extern int __pthread_attr_getstack (__const pthread_attr_t *__restrict __attr,
241 void **__restrict __stackaddr,
242 size_t *__restrict __stacksize);
243 extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
244 size_t __stacksize);
245 extern int __pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
246 __const pthread_rwlockattr_t *__restrict
247 __attr);
248 extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
249 extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
250 extern int __pthread_rwlock_rdlock_internal (pthread_rwlock_t *__rwlock);
251 extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
252 extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
253 extern int __pthread_rwlock_wrlock_internal (pthread_rwlock_t *__rwlock);
254 extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
255 extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
256 extern int __pthread_rwlock_unlock_internal (pthread_rwlock_t *__rwlock);
257 extern int __pthread_cond_broadcast (pthread_cond_t *cond);
258 extern int __pthread_cond_destroy (pthread_cond_t *cond);
259 extern int __pthread_cond_init (pthread_cond_t *cond,
260 const pthread_condattr_t *cond_attr);
261 extern int __pthread_cond_signal (pthread_cond_t *cond);
262 extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
263 extern int __pthread_cond_timedwait (pthread_cond_t *cond,
264 pthread_mutex_t *mutex,
265 const struct timespec *abstime);
266 extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
267 extern int __pthread_condattr_init (pthread_condattr_t *attr);
268 extern int __pthread_key_create (pthread_key_t *key, void (*destr) (void *));
269 extern int __pthread_key_create_internal (pthread_key_t *key,
270 void (*destr) (void *));
271 extern void *__pthread_getspecific (pthread_key_t key);
272 extern void *__pthread_getspecific_internal (pthread_key_t key);
273 extern int __pthread_setspecific (pthread_key_t key, const void *value);
274 extern int __pthread_setspecific_internal (pthread_key_t key,
275 const void *value);
276 extern int __pthread_once (pthread_once_t *once_control,
277 void (*init_routine) (void));
278 extern int __pthread_once_internal (pthread_once_t *once_control,
279 void (*init_routine) (void));
280 extern int __pthread_atfork (void (*prepare) (void), void (*parent) (void),
281 void (*child) (void));
282 extern pthread_t __pthread_self (void);
283 extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
284 extern int __pthread_kill (pthread_t threadid, int signo);
285 extern void __pthread_exit (void *value);
286 extern int __pthread_setcanceltype (int type, int *oldtype);
287 extern int __pthread_enable_asynccancel (void) attribute_hidden;
288 extern void __pthread_disable_asynccancel (int oldtype)
289 internal_function attribute_hidden;
291 extern int __pthread_cond_broadcast_2_0 (pthread_cond_2_0_t *cond);
292 extern int __pthread_cond_destroy_2_0 (pthread_cond_2_0_t *cond);
293 extern int __pthread_cond_init_2_0 (pthread_cond_2_0_t *cond,
294 const pthread_condattr_t *cond_attr);
295 extern int __pthread_cond_signal_2_0 (pthread_cond_2_0_t *cond);
296 extern int __pthread_cond_timedwait_2_0 (pthread_cond_2_0_t *cond,
297 pthread_mutex_t *mutex,
298 const struct timespec *abstime);
299 extern int __pthread_cond_wait_2_0 (pthread_cond_2_0_t *cond,
300 pthread_mutex_t *mutex);
304 /* The two functions are in libc.so and not exported. */
305 extern int __libc_enable_asynccancel (void) attribute_hidden;
306 extern void __libc_disable_asynccancel (int oldtype)
307 internal_function attribute_hidden;
309 #ifdef IS_IN_libpthread
310 /* Special versions which use non-exported functions. */
311 extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
312 void (*routine) (void *), void *arg)
313 attribute_hidden;
314 # undef pthread_cleanup_push
315 # define pthread_cleanup_push(routine,arg) \
316 { struct _pthread_cleanup_buffer _buffer; \
317 __pthread_cleanup_push (&_buffer, (routine), (arg));
319 extern void __pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
320 int execute) attribute_hidden;
321 # undef pthread_cleanup_pop
322 # define pthread_cleanup_pop(execute) \
323 __pthread_cleanup_pop (&_buffer, (execute)); }
324 #endif
326 extern void __pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
327 void (*routine) (void *), void *arg);
328 extern void __pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
329 int execute);
331 #endif /* pthreadP.h */