2005-12-29 Roland McGrath <roland@frob.com>
[glibc.git] / nptl / pthreadP.h
blob61b7176159e25869949b9ad85e4658a79e5495be
1 /* Copyright (C) 2002, 2003, 2004, 2005 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 <stdbool.h>
26 #include <sys/syscall.h>
27 #include "descr.h"
28 #include <tls.h>
29 #include <lowlevellock.h>
30 #include <stackinfo.h>
31 #include <internaltypes.h>
32 #include <pthread-functions.h>
33 #include <atomic.h>
36 /* Atomic operations on TLS memory. */
37 #ifndef THREAD_ATOMIC_CMPXCHG_VAL
38 # define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, new, old) \
39 atomic_compare_and_exchange_val_acq (&(descr)->member, new, old)
40 #endif
42 #ifndef THREAD_ATOMIC_BIT_SET
43 # define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
44 atomic_bit_set (&(descr)->member, bit)
45 #endif
48 /* Adaptive mutex definitions. */
49 #ifndef MAX_ADAPTIVE_COUNT
50 # define MAX_ADAPTIVE_COUNT 100
51 #endif
54 /* Magic cookie representing robust mutex with dead owner. */
55 #define PTHREAD_MUTEX_OWNERDEAD INT_MAX
56 /* Magic cookie representing not recoverable robust mutex. */
57 #define PTHREAD_MUTEX_NOTRECOVERABLE (INT_MAX - 1)
60 /* Internal mutex type value. */
61 enum
63 PTHREAD_MUTEX_ROBUST_PRIVATE_NP = 256,
64 PTHREAD_MUTEX_ROBUST_PRIVATE_RECURSIVE_NP
65 = PTHREAD_MUTEX_ROBUST_PRIVATE_NP | PTHREAD_MUTEX_RECURSIVE_NP,
66 PTHREAD_MUTEX_ROBUST_PRIVATE_ERRORCHECK_NP
67 = PTHREAD_MUTEX_ROBUST_PRIVATE_NP | PTHREAD_MUTEX_ERRORCHECK_NP,
68 PTHREAD_MUTEX_ROBUST_PRIVATE_ADAPTIVE_NP
69 = PTHREAD_MUTEX_ROBUST_PRIVATE_NP | PTHREAD_MUTEX_ADAPTIVE_NP
73 /* Flags in mutex attr. */
74 #define PTHREAD_MUTEXATTR_FLAG_ROBUST 0x40000000
75 #define PTHREAD_MUTEXATTR_FLAG_PSHARED 0x80000000
76 #define PTHREAD_MUTEXATTR_FLAG_BITS \
77 (PTHREAD_MUTEXATTR_FLAG_ROBUST | PTHREAD_MUTEXATTR_FLAG_PSHARED)
80 /* Internal variables. */
83 /* Default stack size. */
84 extern size_t __default_stacksize attribute_hidden;
86 /* Size and alignment of static TLS block. */
87 extern size_t __static_tls_size attribute_hidden;
88 extern size_t __static_tls_align_m1 attribute_hidden;
90 /* Flag whether the machine is SMP or not. */
91 extern int __is_smp attribute_hidden;
93 /* Thread descriptor handling. */
94 extern list_t __stack_user;
95 hidden_proto (__stack_user)
97 /* Attribute handling. */
98 extern struct pthread_attr *__attr_list attribute_hidden;
99 extern lll_lock_t __attr_list_lock attribute_hidden;
101 /* First available RT signal. */
102 extern int __current_sigrtmin attribute_hidden;
103 /* Last available RT signal. */
104 extern int __current_sigrtmax attribute_hidden;
106 /* Concurrency handling. */
107 extern int __concurrency_level attribute_hidden;
109 /* Thread-local data key handling. */
110 extern struct pthread_key_struct __pthread_keys[PTHREAD_KEYS_MAX];
111 hidden_proto (__pthread_keys)
113 /* Number of threads running. */
114 extern unsigned int __nptl_nthreads attribute_hidden;
116 /* The library can run in debugging mode where it performs a lot more
117 tests. */
118 extern int __pthread_debug attribute_hidden;
119 /** For now disable debugging support. */
120 #if 0
121 # define DEBUGGING_P __builtin_expect (__pthread_debug, 0)
122 # define INVALID_TD_P(pd) (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
123 # define INVALID_NOT_TERMINATED_TD_P(pd) INVALID_TD_P (pd)
124 #else
125 # define DEBUGGING_P 0
126 /* Simplified test. This will not catch all invalid descriptors but
127 is better than nothing. And if the test triggers the thread
128 descriptor is guaranteed to be invalid. */
129 # define INVALID_TD_P(pd) __builtin_expect ((pd)->tid <= 0, 0)
130 # define INVALID_NOT_TERMINATED_TD_P(pd) __builtin_expect ((pd)->tid < 0, 0)
131 #endif
134 /* Cancellation test. */
135 #define CANCELLATION_P(self) \
136 do { \
137 int cancelhandling = THREAD_GETMEM (self, cancelhandling); \
138 if (CANCEL_ENABLED_AND_CANCELED (cancelhandling)) \
140 THREAD_SETMEM (self, result, PTHREAD_CANCELED); \
141 __do_cancel (); \
143 } while (0)
146 extern void __pthread_unwind (__pthread_unwind_buf_t *__buf)
147 __cleanup_fct_attribute __attribute ((__noreturn__))
148 #if !defined SHARED && !defined IS_IN_libpthread
149 weak_function
150 #endif
152 extern void __pthread_unwind_next (__pthread_unwind_buf_t *__buf)
153 __cleanup_fct_attribute __attribute ((__noreturn__))
154 #ifndef SHARED
155 weak_function
156 #endif
158 extern void __pthread_register_cancel (__pthread_unwind_buf_t *__buf)
159 __cleanup_fct_attribute;
160 extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf)
161 __cleanup_fct_attribute;
162 #if defined NOT_IN_libc && defined IS_IN_libpthread
163 hidden_proto (__pthread_unwind)
164 hidden_proto (__pthread_unwind_next)
165 hidden_proto (__pthread_register_cancel)
166 hidden_proto (__pthread_unregister_cancel)
167 # ifdef SHARED
168 extern void attribute_hidden pthread_cancel_init (void);
169 # endif
170 #endif
173 /* Called when a thread reacts on a cancellation request. */
174 static inline void
175 __attribute ((noreturn, always_inline))
176 __do_cancel (void)
178 struct pthread *self = THREAD_SELF;
180 /* Make sure we get no more cancellations. */
181 THREAD_ATOMIC_BIT_SET (self, cancelhandling, EXITING_BIT);
183 __pthread_unwind ((__pthread_unwind_buf_t *)
184 THREAD_GETMEM (self, cleanup_jmp_buf));
188 /* Set cancellation mode to asynchronous. */
189 #define CANCEL_ASYNC() \
190 __pthread_enable_asynccancel ()
191 /* Reset to previous cancellation mode. */
192 #define CANCEL_RESET(oldtype) \
193 __pthread_disable_asynccancel (oldtype)
195 #if !defined NOT_IN_libc
196 /* Same as CANCEL_ASYNC, but for use in libc.so. */
197 # define LIBC_CANCEL_ASYNC() \
198 __libc_enable_asynccancel ()
199 /* Same as CANCEL_RESET, but for use in libc.so. */
200 # define LIBC_CANCEL_RESET(oldtype) \
201 __libc_disable_asynccancel (oldtype)
202 # define LIBC_CANCEL_HANDLED() \
203 __asm (".globl " __SYMBOL_PREFIX "__libc_enable_asynccancel"); \
204 __asm (".globl " __SYMBOL_PREFIX "__libc_disable_asynccancel")
205 #elif defined NOT_IN_libc && defined IS_IN_libpthread
206 # define LIBC_CANCEL_ASYNC() CANCEL_ASYNC ()
207 # define LIBC_CANCEL_RESET(val) CANCEL_RESET (val)
208 # define LIBC_CANCEL_HANDLED() \
209 __asm (".globl " __SYMBOL_PREFIX "__pthread_enable_asynccancel"); \
210 __asm (".globl " __SYMBOL_PREFIX "__pthread_disable_asynccancel")
211 #elif defined NOT_IN_libc && defined IS_IN_librt
212 # define LIBC_CANCEL_ASYNC() \
213 __librt_enable_asynccancel ()
214 # define LIBC_CANCEL_RESET(val) \
215 __librt_disable_asynccancel (val)
216 # define LIBC_CANCEL_HANDLED() \
217 __asm (".globl " __SYMBOL_PREFIX "__librt_enable_asynccancel"); \
218 __asm (".globl " __SYMBOL_PREFIX "__librt_disable_asynccancel")
219 #else
220 # define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */
221 # define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */
222 # define LIBC_CANCEL_HANDLED() /* Nothing. */
223 #endif
225 /* The signal used for asynchronous cancelation. */
226 #define SIGCANCEL __SIGRTMIN
229 /* Signal needed for the kernel-supported POSIX timer implementation.
230 We can reuse the cancellation signal since we can distinguish
231 cancellation from timer expirations. */
232 #define SIGTIMER SIGCANCEL
235 /* Signal used to implement the setuid et.al. functions. */
236 #define SIGSETXID (__SIGRTMIN + 1)
238 /* Used to communicate with signal handler. */
239 extern struct xid_command *__xidcmd attribute_hidden;
242 /* Internal prototypes. */
244 /* Thread list handling. */
245 extern struct pthread *__find_in_stack_list (struct pthread *pd)
246 attribute_hidden internal_function;
248 /* Deallocate a thread's stack after optionally making sure the thread
249 descriptor is still valid. */
250 extern void __free_tcb (struct pthread *pd) attribute_hidden internal_function;
252 /* Free allocated stack. */
253 extern void __deallocate_stack (struct pthread *pd)
254 attribute_hidden internal_function;
256 /* Mark all the stacks except for the current one as available. This
257 function also re-initializes the lock for the stack cache. */
258 extern void __reclaim_stacks (void) attribute_hidden;
260 /* Make all threads's stacks executable. */
261 extern int __make_stacks_executable (void **stack_endp)
262 internal_function attribute_hidden;
264 /* longjmp handling. */
265 extern void __pthread_cleanup_upto (__jmp_buf target, char *targetframe);
266 #if defined NOT_IN_libc && defined IS_IN_libpthread
267 hidden_proto (__pthread_cleanup_upto)
268 #endif
271 /* Functions with versioned interfaces. */
272 extern int __pthread_create_2_1 (pthread_t *newthread,
273 const pthread_attr_t *attr,
274 void *(*start_routine) (void *), void *arg);
275 extern int __pthread_create_2_0 (pthread_t *newthread,
276 const pthread_attr_t *attr,
277 void *(*start_routine) (void *), void *arg);
278 extern int __pthread_attr_init_2_1 (pthread_attr_t *attr);
279 extern int __pthread_attr_init_2_0 (pthread_attr_t *attr);
282 /* Event handlers for libthread_db interface. */
283 extern void __nptl_create_event (void);
284 extern void __nptl_death_event (void);
285 hidden_proto (__nptl_create_event)
286 hidden_proto (__nptl_death_event)
288 /* Register the generation counter in the libpthread with the libc. */
289 #ifdef TLS_MULTIPLE_THREADS_IN_TCB
290 extern void __libc_pthread_init (unsigned long int *ptr,
291 void (*reclaim) (void),
292 const struct pthread_functions *functions)
293 internal_function;
294 #else
295 extern int *__libc_pthread_init (unsigned long int *ptr,
296 void (*reclaim) (void),
297 const struct pthread_functions *functions)
298 internal_function;
300 /* Variable set to a nonzero value if more than one thread runs or ran. */
301 extern int __pthread_multiple_threads attribute_hidden;
302 /* Pointer to the corresponding variable in libc. */
303 extern int *__libc_multiple_threads_ptr attribute_hidden;
304 #endif
306 /* Find a thread given its TID. */
307 extern struct pthread *__find_thread_by_id (pid_t tid) attribute_hidden
308 #ifdef SHARED
310 #else
311 weak_function;
312 #define __find_thread_by_id(tid) \
313 (__find_thread_by_id ? (__find_thread_by_id) (tid) : (struct pthread *) NULL)
314 #endif
316 extern void __pthread_init_static_tls (struct link_map *) attribute_hidden;
319 /* Namespace save aliases. */
320 extern int __pthread_getschedparam (pthread_t thread_id, int *policy,
321 struct sched_param *param);
322 extern int __pthread_setschedparam (pthread_t thread_id, int policy,
323 const struct sched_param *param);
324 extern int __pthread_setcancelstate (int state, int *oldstate);
325 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
326 __const pthread_mutexattr_t *__mutexattr);
327 extern int __pthread_mutex_init_internal (pthread_mutex_t *__mutex,
328 __const pthread_mutexattr_t *__mutexattr)
329 attribute_hidden;
330 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
331 extern int __pthread_mutex_destroy_internal (pthread_mutex_t *__mutex)
332 attribute_hidden;
333 extern int __pthread_mutex_trylock (pthread_mutex_t *_mutex);
334 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
335 extern int __pthread_mutex_lock_internal (pthread_mutex_t *__mutex)
336 attribute_hidden;
337 extern int __pthread_mutex_cond_lock (pthread_mutex_t *__mutex)
338 attribute_hidden internal_function;
339 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
340 extern int __pthread_mutex_unlock_internal (pthread_mutex_t *__mutex)
341 attribute_hidden;
342 extern int __pthread_mutex_unlock_usercnt (pthread_mutex_t *__mutex,
343 int __decr)
344 attribute_hidden internal_function;
345 extern int __pthread_mutexattr_init (pthread_mutexattr_t *attr);
346 extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *attr);
347 extern int __pthread_mutexattr_settype (pthread_mutexattr_t *attr, int kind);
348 extern int __pthread_attr_destroy (pthread_attr_t *attr);
349 extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
350 int *detachstate);
351 extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
352 int detachstate);
353 extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
354 int *inherit);
355 extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
356 extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
357 struct sched_param *param);
358 extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
359 const struct sched_param *param);
360 extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
361 int *policy);
362 extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
363 extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
364 extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
365 extern int __pthread_attr_getstackaddr (__const pthread_attr_t *__restrict
366 __attr, void **__restrict __stackaddr);
367 extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
368 void *__stackaddr);
369 extern int __pthread_attr_getstacksize (__const pthread_attr_t *__restrict
370 __attr,
371 size_t *__restrict __stacksize);
372 extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
373 size_t __stacksize);
374 extern int __pthread_attr_getstack (__const pthread_attr_t *__restrict __attr,
375 void **__restrict __stackaddr,
376 size_t *__restrict __stacksize);
377 extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
378 size_t __stacksize);
379 extern int __pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
380 __const pthread_rwlockattr_t *__restrict
381 __attr);
382 extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
383 extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
384 extern int __pthread_rwlock_rdlock_internal (pthread_rwlock_t *__rwlock);
385 extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
386 extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
387 extern int __pthread_rwlock_wrlock_internal (pthread_rwlock_t *__rwlock);
388 extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
389 extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
390 extern int __pthread_rwlock_unlock_internal (pthread_rwlock_t *__rwlock);
391 extern int __pthread_cond_broadcast (pthread_cond_t *cond);
392 extern int __pthread_cond_destroy (pthread_cond_t *cond);
393 extern int __pthread_cond_init (pthread_cond_t *cond,
394 const pthread_condattr_t *cond_attr);
395 extern int __pthread_cond_signal (pthread_cond_t *cond);
396 extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
397 extern int __pthread_cond_timedwait (pthread_cond_t *cond,
398 pthread_mutex_t *mutex,
399 const struct timespec *abstime);
400 extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
401 extern int __pthread_condattr_init (pthread_condattr_t *attr);
402 extern int __pthread_key_create (pthread_key_t *key, void (*destr) (void *));
403 extern int __pthread_key_create_internal (pthread_key_t *key,
404 void (*destr) (void *));
405 extern void *__pthread_getspecific (pthread_key_t key);
406 extern void *__pthread_getspecific_internal (pthread_key_t key);
407 extern int __pthread_setspecific (pthread_key_t key, const void *value);
408 extern int __pthread_setspecific_internal (pthread_key_t key,
409 const void *value);
410 extern int __pthread_once (pthread_once_t *once_control,
411 void (*init_routine) (void));
412 extern int __pthread_once_internal (pthread_once_t *once_control,
413 void (*init_routine) (void));
414 extern int __pthread_atfork (void (*prepare) (void), void (*parent) (void),
415 void (*child) (void));
416 extern pthread_t __pthread_self (void);
417 extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
418 extern int __pthread_kill (pthread_t threadid, int signo);
419 extern void __pthread_exit (void *value);
420 extern int __pthread_setcanceltype (int type, int *oldtype);
421 extern int __pthread_enable_asynccancel (void) attribute_hidden;
422 extern void __pthread_disable_asynccancel (int oldtype)
423 internal_function attribute_hidden;
425 extern int __pthread_cond_broadcast_2_0 (pthread_cond_2_0_t *cond);
426 extern int __pthread_cond_destroy_2_0 (pthread_cond_2_0_t *cond);
427 extern int __pthread_cond_init_2_0 (pthread_cond_2_0_t *cond,
428 const pthread_condattr_t *cond_attr);
429 extern int __pthread_cond_signal_2_0 (pthread_cond_2_0_t *cond);
430 extern int __pthread_cond_timedwait_2_0 (pthread_cond_2_0_t *cond,
431 pthread_mutex_t *mutex,
432 const struct timespec *abstime);
433 extern int __pthread_cond_wait_2_0 (pthread_cond_2_0_t *cond,
434 pthread_mutex_t *mutex);
436 extern int __pthread_getaffinity_np (pthread_t th, size_t cpusetsize,
437 cpu_set_t *cpuset);
439 /* The two functions are in libc.so and not exported. */
440 extern int __libc_enable_asynccancel (void) attribute_hidden;
441 extern void __libc_disable_asynccancel (int oldtype)
442 internal_function attribute_hidden;
445 /* The two functions are in librt.so and not exported. */
446 extern int __librt_enable_asynccancel (void) attribute_hidden;
447 extern void __librt_disable_asynccancel (int oldtype)
448 internal_function attribute_hidden;
450 #ifdef IS_IN_libpthread
451 /* Special versions which use non-exported functions. */
452 extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
453 void (*routine) (void *), void *arg)
454 attribute_hidden;
455 # undef pthread_cleanup_push
456 # define pthread_cleanup_push(routine,arg) \
457 { struct _pthread_cleanup_buffer _buffer; \
458 __pthread_cleanup_push (&_buffer, (routine), (arg));
460 extern void __pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
461 int execute) attribute_hidden;
462 # undef pthread_cleanup_pop
463 # define pthread_cleanup_pop(execute) \
464 __pthread_cleanup_pop (&_buffer, (execute)); }
465 #endif
467 extern void __pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
468 void (*routine) (void *), void *arg);
469 extern void __pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
470 int execute);
472 /* Old cleanup interfaces, still used in libc.so. */
473 extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
474 void (*routine) (void *), void *arg);
475 extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
476 int execute);
477 extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
478 void (*routine) (void *), void *arg);
479 extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
480 int execute);
482 extern void __nptl_deallocate_tsd (void) attribute_hidden;
484 extern int __nptl_setxid (struct xid_command *cmdp) attribute_hidden;
486 #ifdef SHARED
487 # define PTHREAD_STATIC_FN_REQUIRE(name)
488 #else
489 # define PTHREAD_STATIC_FN_REQUIRE(name) __asm (".globl " #name);
490 #endif
492 #endif /* pthreadP.h */