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