(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / nptl / pthreadP.h
blob24168146fa3742d267af0043df219cf6cf13bf19
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
282 #ifdef SHARED
284 #else
285 weak_function;
286 #define __find_thread_by_id(tid) \
287 (__find_thread_by_id ? (__find_thread_by_id) (tid) : (struct pthread *) NULL)
288 #endif
290 extern void __pthread_init_static_tls (struct link_map *) attribute_hidden;
293 /* Namespace save aliases. */
294 extern int __pthread_getschedparam (pthread_t thread_id, int *policy,
295 struct sched_param *param);
296 extern int __pthread_setschedparam (pthread_t thread_id, int policy,
297 const struct sched_param *param);
298 extern int __pthread_setcancelstate (int state, int *oldstate);
299 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
300 __const pthread_mutexattr_t *__mutexattr);
301 extern int __pthread_mutex_init_internal (pthread_mutex_t *__mutex,
302 __const pthread_mutexattr_t *__mutexattr)
303 attribute_hidden;
304 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
305 extern int __pthread_mutex_destroy_internal (pthread_mutex_t *__mutex)
306 attribute_hidden;
307 extern int __pthread_mutex_trylock (pthread_mutex_t *_mutex);
308 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
309 extern int __pthread_mutex_lock_internal (pthread_mutex_t *__mutex)
310 attribute_hidden;
311 extern int __pthread_mutex_cond_lock (pthread_mutex_t *__mutex)
312 attribute_hidden internal_function;
313 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
314 extern int __pthread_mutex_unlock_internal (pthread_mutex_t *__mutex)
315 attribute_hidden;
316 extern int __pthread_mutex_unlock_usercnt (pthread_mutex_t *__mutex,
317 int __decr)
318 attribute_hidden internal_function;
319 extern int __pthread_mutexattr_init (pthread_mutexattr_t *attr);
320 extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *attr);
321 extern int __pthread_mutexattr_settype (pthread_mutexattr_t *attr, int kind);
322 extern int __pthread_attr_destroy (pthread_attr_t *attr);
323 extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
324 int *detachstate);
325 extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
326 int detachstate);
327 extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
328 int *inherit);
329 extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
330 extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
331 struct sched_param *param);
332 extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
333 const struct sched_param *param);
334 extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
335 int *policy);
336 extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
337 extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
338 extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
339 extern int __pthread_attr_getstackaddr (__const pthread_attr_t *__restrict
340 __attr, void **__restrict __stackaddr);
341 extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
342 void *__stackaddr);
343 extern int __pthread_attr_getstacksize (__const pthread_attr_t *__restrict
344 __attr,
345 size_t *__restrict __stacksize);
346 extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
347 size_t __stacksize);
348 extern int __pthread_attr_getstack (__const pthread_attr_t *__restrict __attr,
349 void **__restrict __stackaddr,
350 size_t *__restrict __stacksize);
351 extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
352 size_t __stacksize);
353 extern int __pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
354 __const pthread_rwlockattr_t *__restrict
355 __attr);
356 extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
357 extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
358 extern int __pthread_rwlock_rdlock_internal (pthread_rwlock_t *__rwlock);
359 extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
360 extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
361 extern int __pthread_rwlock_wrlock_internal (pthread_rwlock_t *__rwlock);
362 extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
363 extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
364 extern int __pthread_rwlock_unlock_internal (pthread_rwlock_t *__rwlock);
365 extern int __pthread_cond_broadcast (pthread_cond_t *cond);
366 extern int __pthread_cond_destroy (pthread_cond_t *cond);
367 extern int __pthread_cond_init (pthread_cond_t *cond,
368 const pthread_condattr_t *cond_attr);
369 extern int __pthread_cond_signal (pthread_cond_t *cond);
370 extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
371 extern int __pthread_cond_timedwait (pthread_cond_t *cond,
372 pthread_mutex_t *mutex,
373 const struct timespec *abstime);
374 extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
375 extern int __pthread_condattr_init (pthread_condattr_t *attr);
376 extern int __pthread_key_create (pthread_key_t *key, void (*destr) (void *));
377 extern int __pthread_key_create_internal (pthread_key_t *key,
378 void (*destr) (void *));
379 extern void *__pthread_getspecific (pthread_key_t key);
380 extern void *__pthread_getspecific_internal (pthread_key_t key);
381 extern int __pthread_setspecific (pthread_key_t key, const void *value);
382 extern int __pthread_setspecific_internal (pthread_key_t key,
383 const void *value);
384 extern int __pthread_once (pthread_once_t *once_control,
385 void (*init_routine) (void));
386 extern int __pthread_once_internal (pthread_once_t *once_control,
387 void (*init_routine) (void));
388 extern int __pthread_atfork (void (*prepare) (void), void (*parent) (void),
389 void (*child) (void));
390 extern pthread_t __pthread_self (void);
391 extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
392 extern int __pthread_kill (pthread_t threadid, int signo);
393 extern void __pthread_exit (void *value);
394 extern int __pthread_setcanceltype (int type, int *oldtype);
395 extern int __pthread_enable_asynccancel (void) attribute_hidden;
396 extern void __pthread_disable_asynccancel (int oldtype)
397 internal_function attribute_hidden;
399 extern int __pthread_cond_broadcast_2_0 (pthread_cond_2_0_t *cond);
400 extern int __pthread_cond_destroy_2_0 (pthread_cond_2_0_t *cond);
401 extern int __pthread_cond_init_2_0 (pthread_cond_2_0_t *cond,
402 const pthread_condattr_t *cond_attr);
403 extern int __pthread_cond_signal_2_0 (pthread_cond_2_0_t *cond);
404 extern int __pthread_cond_timedwait_2_0 (pthread_cond_2_0_t *cond,
405 pthread_mutex_t *mutex,
406 const struct timespec *abstime);
407 extern int __pthread_cond_wait_2_0 (pthread_cond_2_0_t *cond,
408 pthread_mutex_t *mutex);
410 extern int __pthread_getaffinity_np (pthread_t th, size_t cpusetsize,
411 cpu_set_t *cpuset);
413 /* The two functions are in libc.so and not exported. */
414 extern int __libc_enable_asynccancel (void) attribute_hidden;
415 extern void __libc_disable_asynccancel (int oldtype)
416 internal_function attribute_hidden;
419 /* The two functions are in librt.so and not exported. */
420 extern int __librt_enable_asynccancel (void) attribute_hidden;
421 extern void __librt_disable_asynccancel (int oldtype)
422 internal_function attribute_hidden;
424 #ifdef IS_IN_libpthread
425 /* Special versions which use non-exported functions. */
426 extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
427 void (*routine) (void *), void *arg)
428 attribute_hidden;
429 # undef pthread_cleanup_push
430 # define pthread_cleanup_push(routine,arg) \
431 { struct _pthread_cleanup_buffer _buffer; \
432 __pthread_cleanup_push (&_buffer, (routine), (arg));
434 extern void __pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
435 int execute) attribute_hidden;
436 # undef pthread_cleanup_pop
437 # define pthread_cleanup_pop(execute) \
438 __pthread_cleanup_pop (&_buffer, (execute)); }
439 #endif
441 extern void __pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
442 void (*routine) (void *), void *arg);
443 extern void __pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
444 int execute);
446 /* Old cleanup interfaces, still used in libc.so. */
447 extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
448 void (*routine) (void *), void *arg);
449 extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
450 int execute);
451 extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
452 void (*routine) (void *), void *arg);
453 extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
454 int execute);
456 extern void __nptl_deallocate_tsd (void) attribute_hidden;
458 extern int __nptl_setxid (struct xid_command *cmdp) attribute_hidden;
460 #ifdef SHARED
461 # define PTHREAD_STATIC_FN_REQUIRE(name)
462 #else
463 # define PTHREAD_STATIC_FN_REQUIRE(name) __asm (".globl " #name);
464 #endif
466 #endif /* pthreadP.h */