* include/libc-symbols.h: Fix comment typo.
[glibc.git] / nptl / pthreadP.h
blob77d8f5ad247c9861a5abe64d1ac29a43846a4eec
1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006 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_INCONSISTENT 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 = 16,
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 /* Bits used in robust mutex implementation. */
81 #define FUTEX_WAITERS 0x80000000
82 #define FUTEX_OWNER_DIED 0x40000000
83 #define FUTEX_TID_MASK 0x1fffffff
86 /* Internal variables. */
89 /* Default stack size. */
90 extern size_t __default_stacksize attribute_hidden;
92 /* Size and alignment of static TLS block. */
93 extern size_t __static_tls_size attribute_hidden;
94 extern size_t __static_tls_align_m1 attribute_hidden;
96 /* Flag whether the machine is SMP or not. */
97 extern int __is_smp attribute_hidden;
99 /* Thread descriptor handling. */
100 extern list_t __stack_user;
101 hidden_proto (__stack_user)
103 /* Attribute handling. */
104 extern struct pthread_attr *__attr_list attribute_hidden;
105 extern lll_lock_t __attr_list_lock attribute_hidden;
107 /* First available RT signal. */
108 extern int __current_sigrtmin attribute_hidden;
109 /* Last available RT signal. */
110 extern int __current_sigrtmax attribute_hidden;
112 /* Concurrency handling. */
113 extern int __concurrency_level attribute_hidden;
115 /* Thread-local data key handling. */
116 extern struct pthread_key_struct __pthread_keys[PTHREAD_KEYS_MAX];
117 hidden_proto (__pthread_keys)
119 /* Number of threads running. */
120 extern unsigned int __nptl_nthreads attribute_hidden;
122 /* The library can run in debugging mode where it performs a lot more
123 tests. */
124 extern int __pthread_debug attribute_hidden;
125 /** For now disable debugging support. */
126 #if 0
127 # define DEBUGGING_P __builtin_expect (__pthread_debug, 0)
128 # define INVALID_TD_P(pd) (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
129 # define INVALID_NOT_TERMINATED_TD_P(pd) INVALID_TD_P (pd)
130 #else
131 # define DEBUGGING_P 0
132 /* Simplified test. This will not catch all invalid descriptors but
133 is better than nothing. And if the test triggers the thread
134 descriptor is guaranteed to be invalid. */
135 # define INVALID_TD_P(pd) __builtin_expect ((pd)->tid <= 0, 0)
136 # define INVALID_NOT_TERMINATED_TD_P(pd) __builtin_expect ((pd)->tid < 0, 0)
137 #endif
140 /* Cancellation test. */
141 #define CANCELLATION_P(self) \
142 do { \
143 int cancelhandling = THREAD_GETMEM (self, cancelhandling); \
144 if (CANCEL_ENABLED_AND_CANCELED (cancelhandling)) \
146 THREAD_SETMEM (self, result, PTHREAD_CANCELED); \
147 __do_cancel (); \
149 } while (0)
152 extern void __pthread_unwind (__pthread_unwind_buf_t *__buf)
153 __cleanup_fct_attribute __attribute ((__noreturn__))
154 #if !defined SHARED && !defined IS_IN_libpthread
155 weak_function
156 #endif
158 extern void __pthread_unwind_next (__pthread_unwind_buf_t *__buf)
159 __cleanup_fct_attribute __attribute ((__noreturn__))
160 #ifndef SHARED
161 weak_function
162 #endif
164 extern void __pthread_register_cancel (__pthread_unwind_buf_t *__buf)
165 __cleanup_fct_attribute;
166 extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf)
167 __cleanup_fct_attribute;
168 #if defined NOT_IN_libc && defined IS_IN_libpthread
169 hidden_proto (__pthread_unwind)
170 hidden_proto (__pthread_unwind_next)
171 hidden_proto (__pthread_register_cancel)
172 hidden_proto (__pthread_unregister_cancel)
173 # ifdef SHARED
174 extern void attribute_hidden pthread_cancel_init (void);
175 # endif
176 #endif
179 /* Called when a thread reacts on a cancellation request. */
180 static inline void
181 __attribute ((noreturn, always_inline))
182 __do_cancel (void)
184 struct pthread *self = THREAD_SELF;
186 /* Make sure we get no more cancellations. */
187 THREAD_ATOMIC_BIT_SET (self, cancelhandling, EXITING_BIT);
189 __pthread_unwind ((__pthread_unwind_buf_t *)
190 THREAD_GETMEM (self, cleanup_jmp_buf));
194 /* Set cancellation mode to asynchronous. */
195 #define CANCEL_ASYNC() \
196 __pthread_enable_asynccancel ()
197 /* Reset to previous cancellation mode. */
198 #define CANCEL_RESET(oldtype) \
199 __pthread_disable_asynccancel (oldtype)
201 #if !defined NOT_IN_libc
202 /* Same as CANCEL_ASYNC, but for use in libc.so. */
203 # define LIBC_CANCEL_ASYNC() \
204 __libc_enable_asynccancel ()
205 /* Same as CANCEL_RESET, but for use in libc.so. */
206 # define LIBC_CANCEL_RESET(oldtype) \
207 __libc_disable_asynccancel (oldtype)
208 # define LIBC_CANCEL_HANDLED() \
209 __asm (".globl " __SYMBOL_PREFIX "__libc_enable_asynccancel"); \
210 __asm (".globl " __SYMBOL_PREFIX "__libc_disable_asynccancel")
211 #elif defined NOT_IN_libc && defined IS_IN_libpthread
212 # define LIBC_CANCEL_ASYNC() CANCEL_ASYNC ()
213 # define LIBC_CANCEL_RESET(val) CANCEL_RESET (val)
214 # define LIBC_CANCEL_HANDLED() \
215 __asm (".globl " __SYMBOL_PREFIX "__pthread_enable_asynccancel"); \
216 __asm (".globl " __SYMBOL_PREFIX "__pthread_disable_asynccancel")
217 #elif defined NOT_IN_libc && defined IS_IN_librt
218 # define LIBC_CANCEL_ASYNC() \
219 __librt_enable_asynccancel ()
220 # define LIBC_CANCEL_RESET(val) \
221 __librt_disable_asynccancel (val)
222 # define LIBC_CANCEL_HANDLED() \
223 __asm (".globl " __SYMBOL_PREFIX "__librt_enable_asynccancel"); \
224 __asm (".globl " __SYMBOL_PREFIX "__librt_disable_asynccancel")
225 #else
226 # define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */
227 # define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */
228 # define LIBC_CANCEL_HANDLED() /* Nothing. */
229 #endif
231 /* The signal used for asynchronous cancelation. */
232 #define SIGCANCEL __SIGRTMIN
235 /* Signal needed for the kernel-supported POSIX timer implementation.
236 We can reuse the cancellation signal since we can distinguish
237 cancellation from timer expirations. */
238 #define SIGTIMER SIGCANCEL
241 /* Signal used to implement the setuid et.al. functions. */
242 #define SIGSETXID (__SIGRTMIN + 1)
244 /* Used to communicate with signal handler. */
245 extern struct xid_command *__xidcmd attribute_hidden;
248 /* Internal prototypes. */
250 /* Thread list handling. */
251 extern struct pthread *__find_in_stack_list (struct pthread *pd)
252 attribute_hidden internal_function;
254 /* Deallocate a thread's stack after optionally making sure the thread
255 descriptor is still valid. */
256 extern void __free_tcb (struct pthread *pd) attribute_hidden internal_function;
258 /* Free allocated stack. */
259 extern void __deallocate_stack (struct pthread *pd)
260 attribute_hidden internal_function;
262 /* Mark all the stacks except for the current one as available. This
263 function also re-initializes the lock for the stack cache. */
264 extern void __reclaim_stacks (void) attribute_hidden;
266 /* Make all threads's stacks executable. */
267 extern int __make_stacks_executable (void **stack_endp)
268 internal_function attribute_hidden;
270 /* longjmp handling. */
271 extern void __pthread_cleanup_upto (__jmp_buf target, char *targetframe);
272 #if defined NOT_IN_libc && defined IS_IN_libpthread
273 hidden_proto (__pthread_cleanup_upto)
274 #endif
277 /* Functions with versioned interfaces. */
278 extern int __pthread_create_2_1 (pthread_t *newthread,
279 const pthread_attr_t *attr,
280 void *(*start_routine) (void *), void *arg);
281 extern int __pthread_create_2_0 (pthread_t *newthread,
282 const pthread_attr_t *attr,
283 void *(*start_routine) (void *), void *arg);
284 extern int __pthread_attr_init_2_1 (pthread_attr_t *attr);
285 extern int __pthread_attr_init_2_0 (pthread_attr_t *attr);
288 /* Event handlers for libthread_db interface. */
289 extern void __nptl_create_event (void);
290 extern void __nptl_death_event (void);
291 hidden_proto (__nptl_create_event)
292 hidden_proto (__nptl_death_event)
294 /* Register the generation counter in the libpthread with the libc. */
295 #ifdef TLS_MULTIPLE_THREADS_IN_TCB
296 extern void __libc_pthread_init (unsigned long int *ptr,
297 void (*reclaim) (void),
298 const struct pthread_functions *functions)
299 internal_function;
300 #else
301 extern int *__libc_pthread_init (unsigned long int *ptr,
302 void (*reclaim) (void),
303 const struct pthread_functions *functions)
304 internal_function;
306 /* Variable set to a nonzero value if more than one thread runs or ran. */
307 extern int __pthread_multiple_threads attribute_hidden;
308 /* Pointer to the corresponding variable in libc. */
309 extern int *__libc_multiple_threads_ptr attribute_hidden;
310 #endif
312 /* Find a thread given its TID. */
313 extern struct pthread *__find_thread_by_id (pid_t tid) attribute_hidden
314 #ifdef SHARED
316 #else
317 weak_function;
318 #define __find_thread_by_id(tid) \
319 (__find_thread_by_id ? (__find_thread_by_id) (tid) : (struct pthread *) NULL)
320 #endif
322 extern void __pthread_init_static_tls (struct link_map *) attribute_hidden;
325 /* Namespace save aliases. */
326 extern int __pthread_getschedparam (pthread_t thread_id, int *policy,
327 struct sched_param *param);
328 extern int __pthread_setschedparam (pthread_t thread_id, int policy,
329 const struct sched_param *param);
330 extern int __pthread_setcancelstate (int state, int *oldstate);
331 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
332 __const pthread_mutexattr_t *__mutexattr);
333 extern int __pthread_mutex_init_internal (pthread_mutex_t *__mutex,
334 __const pthread_mutexattr_t *__mutexattr)
335 attribute_hidden;
336 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
337 extern int __pthread_mutex_destroy_internal (pthread_mutex_t *__mutex)
338 attribute_hidden;
339 extern int __pthread_mutex_trylock (pthread_mutex_t *_mutex);
340 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
341 extern int __pthread_mutex_lock_internal (pthread_mutex_t *__mutex)
342 attribute_hidden;
343 extern int __pthread_mutex_cond_lock (pthread_mutex_t *__mutex)
344 attribute_hidden internal_function;
345 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
346 extern int __pthread_mutex_unlock_internal (pthread_mutex_t *__mutex)
347 attribute_hidden;
348 extern int __pthread_mutex_unlock_usercnt (pthread_mutex_t *__mutex,
349 int __decr)
350 attribute_hidden internal_function;
351 extern int __pthread_mutexattr_init (pthread_mutexattr_t *attr);
352 extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *attr);
353 extern int __pthread_mutexattr_settype (pthread_mutexattr_t *attr, int kind);
354 extern int __pthread_attr_destroy (pthread_attr_t *attr);
355 extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
356 int *detachstate);
357 extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
358 int detachstate);
359 extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
360 int *inherit);
361 extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
362 extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
363 struct sched_param *param);
364 extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
365 const struct sched_param *param);
366 extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
367 int *policy);
368 extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
369 extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
370 extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
371 extern int __pthread_attr_getstackaddr (__const pthread_attr_t *__restrict
372 __attr, void **__restrict __stackaddr);
373 extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
374 void *__stackaddr);
375 extern int __pthread_attr_getstacksize (__const pthread_attr_t *__restrict
376 __attr,
377 size_t *__restrict __stacksize);
378 extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
379 size_t __stacksize);
380 extern int __pthread_attr_getstack (__const pthread_attr_t *__restrict __attr,
381 void **__restrict __stackaddr,
382 size_t *__restrict __stacksize);
383 extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
384 size_t __stacksize);
385 extern int __pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
386 __const pthread_rwlockattr_t *__restrict
387 __attr);
388 extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
389 extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
390 extern int __pthread_rwlock_rdlock_internal (pthread_rwlock_t *__rwlock);
391 extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
392 extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
393 extern int __pthread_rwlock_wrlock_internal (pthread_rwlock_t *__rwlock);
394 extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
395 extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
396 extern int __pthread_rwlock_unlock_internal (pthread_rwlock_t *__rwlock);
397 extern int __pthread_cond_broadcast (pthread_cond_t *cond);
398 extern int __pthread_cond_destroy (pthread_cond_t *cond);
399 extern int __pthread_cond_init (pthread_cond_t *cond,
400 const pthread_condattr_t *cond_attr);
401 extern int __pthread_cond_signal (pthread_cond_t *cond);
402 extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
403 extern int __pthread_cond_timedwait (pthread_cond_t *cond,
404 pthread_mutex_t *mutex,
405 const struct timespec *abstime);
406 extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
407 extern int __pthread_condattr_init (pthread_condattr_t *attr);
408 extern int __pthread_key_create (pthread_key_t *key, void (*destr) (void *));
409 extern int __pthread_key_create_internal (pthread_key_t *key,
410 void (*destr) (void *));
411 extern void *__pthread_getspecific (pthread_key_t key);
412 extern void *__pthread_getspecific_internal (pthread_key_t key);
413 extern int __pthread_setspecific (pthread_key_t key, const void *value);
414 extern int __pthread_setspecific_internal (pthread_key_t key,
415 const void *value);
416 extern int __pthread_once (pthread_once_t *once_control,
417 void (*init_routine) (void));
418 extern int __pthread_once_internal (pthread_once_t *once_control,
419 void (*init_routine) (void));
420 extern int __pthread_atfork (void (*prepare) (void), void (*parent) (void),
421 void (*child) (void));
422 extern pthread_t __pthread_self (void);
423 extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
424 extern int __pthread_kill (pthread_t threadid, int signo);
425 extern void __pthread_exit (void *value);
426 extern int __pthread_setcanceltype (int type, int *oldtype);
427 extern int __pthread_enable_asynccancel (void) attribute_hidden;
428 extern void __pthread_disable_asynccancel (int oldtype)
429 internal_function attribute_hidden;
431 extern int __pthread_cond_broadcast_2_0 (pthread_cond_2_0_t *cond);
432 extern int __pthread_cond_destroy_2_0 (pthread_cond_2_0_t *cond);
433 extern int __pthread_cond_init_2_0 (pthread_cond_2_0_t *cond,
434 const pthread_condattr_t *cond_attr);
435 extern int __pthread_cond_signal_2_0 (pthread_cond_2_0_t *cond);
436 extern int __pthread_cond_timedwait_2_0 (pthread_cond_2_0_t *cond,
437 pthread_mutex_t *mutex,
438 const struct timespec *abstime);
439 extern int __pthread_cond_wait_2_0 (pthread_cond_2_0_t *cond,
440 pthread_mutex_t *mutex);
442 extern int __pthread_getaffinity_np (pthread_t th, size_t cpusetsize,
443 cpu_set_t *cpuset);
445 /* The two functions are in libc.so and not exported. */
446 extern int __libc_enable_asynccancel (void) attribute_hidden;
447 extern void __libc_disable_asynccancel (int oldtype)
448 internal_function attribute_hidden;
451 /* The two functions are in librt.so and not exported. */
452 extern int __librt_enable_asynccancel (void) attribute_hidden;
453 extern void __librt_disable_asynccancel (int oldtype)
454 internal_function attribute_hidden;
456 #ifdef IS_IN_libpthread
457 /* Special versions which use non-exported functions. */
458 extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
459 void (*routine) (void *), void *arg)
460 attribute_hidden;
461 # undef pthread_cleanup_push
462 # define pthread_cleanup_push(routine,arg) \
463 { struct _pthread_cleanup_buffer _buffer; \
464 __pthread_cleanup_push (&_buffer, (routine), (arg));
466 extern void __pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
467 int execute) attribute_hidden;
468 # undef pthread_cleanup_pop
469 # define pthread_cleanup_pop(execute) \
470 __pthread_cleanup_pop (&_buffer, (execute)); }
471 #endif
473 extern void __pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
474 void (*routine) (void *), void *arg);
475 extern void __pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
476 int execute);
478 /* Old cleanup interfaces, still used in libc.so. */
479 extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
480 void (*routine) (void *), void *arg);
481 extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
482 int execute);
483 extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
484 void (*routine) (void *), void *arg);
485 extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
486 int execute);
488 extern void __nptl_deallocate_tsd (void) attribute_hidden;
490 extern int __nptl_setxid (struct xid_command *cmdp) attribute_hidden;
492 #ifdef SHARED
493 # define PTHREAD_STATIC_FN_REQUIRE(name)
494 #else
495 # define PTHREAD_STATIC_FN_REQUIRE(name) __asm (".globl " #name);
496 #endif
498 #endif /* pthreadP.h */