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
26 #include <sys/syscall.h>
29 #include <lowlevellock.h>
30 #include <stackinfo.h>
31 #include <internaltypes.h>
32 #include <pthread-functions.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)
42 #ifndef THREAD_ATOMIC_BIT_SET
43 # define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
44 atomic_bit_set (&(descr)->member, bit)
48 /* Adaptive mutex definitions. */
49 #ifndef MAX_ADAPTIVE_COUNT
50 # define MAX_ADAPTIVE_COUNT 100
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. */
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
,
70 PTHREAD_MUTEX_PRIO_INHERIT_PRIVATE_NP
= 32,
71 PTHREAD_MUTEX_PRIO_PROTECT_PRIVATE_NP
= 64
73 #define PTHREAD_MUTEX_PRIO_CEILING_SHIFT 16
74 #define PTHREAD_MUTEX_PRIO_CEILING_MASK 0x00ff0000
77 /* Flags in mutex attr. */
78 #define PTHREAD_MUTEXATTR_PROTOCOL_SHIFT 28
79 #define PTHREAD_MUTEXATTR_PROTOCOL_MASK 0x30000000
80 #define PTHREAD_MUTEXATTR_PRIO_CEILING_SHIFT 16
81 #define PTHREAD_MUTEXATTR_PRIO_CEILING_MASK 0x00ff0000
82 #define PTHREAD_MUTEXATTR_FLAG_ROBUST 0x40000000
83 #define PTHREAD_MUTEXATTR_FLAG_PSHARED 0x80000000
84 #define PTHREAD_MUTEXATTR_FLAG_BITS \
85 (PTHREAD_MUTEXATTR_FLAG_ROBUST | PTHREAD_MUTEXATTR_FLAG_PSHARED \
86 | PTHREAD_MUTEXATTR_PROTOCOL_MASK | PTHREAD_MUTEXATTR_PRIO_CEILING_MASK)
89 /* Bits used in robust mutex implementation. */
90 #define FUTEX_WAITERS 0x80000000
91 #define FUTEX_OWNER_DIED 0x40000000
92 #define FUTEX_TID_MASK 0x1fffffff
95 /* Internal variables. */
98 /* Default stack size. */
99 extern size_t __default_stacksize attribute_hidden
;
101 /* Size and alignment of static TLS block. */
102 extern size_t __static_tls_size attribute_hidden
;
103 extern size_t __static_tls_align_m1 attribute_hidden
;
105 /* Flag whether the machine is SMP or not. */
106 extern int __is_smp attribute_hidden
;
108 /* Thread descriptor handling. */
109 extern list_t __stack_user
;
110 hidden_proto (__stack_user
)
112 /* Attribute handling. */
113 extern struct pthread_attr
*__attr_list attribute_hidden
;
114 extern lll_lock_t __attr_list_lock attribute_hidden
;
116 /* First available RT signal. */
117 extern int __current_sigrtmin attribute_hidden
;
118 /* Last available RT signal. */
119 extern int __current_sigrtmax attribute_hidden
;
121 /* Concurrency handling. */
122 extern int __concurrency_level attribute_hidden
;
124 /* Thread-local data key handling. */
125 extern struct pthread_key_struct __pthread_keys
[PTHREAD_KEYS_MAX
];
126 hidden_proto (__pthread_keys
)
128 /* Number of threads running. */
129 extern unsigned int __nptl_nthreads attribute_hidden
;
131 /* The library can run in debugging mode where it performs a lot more
133 extern int __pthread_debug attribute_hidden
;
134 /** For now disable debugging support. */
136 # define DEBUGGING_P __builtin_expect (__pthread_debug, 0)
137 # define INVALID_TD_P(pd) (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
138 # define INVALID_NOT_TERMINATED_TD_P(pd) INVALID_TD_P (pd)
140 # define DEBUGGING_P 0
141 /* Simplified test. This will not catch all invalid descriptors but
142 is better than nothing. And if the test triggers the thread
143 descriptor is guaranteed to be invalid. */
144 # define INVALID_TD_P(pd) __builtin_expect ((pd)->tid <= 0, 0)
145 # define INVALID_NOT_TERMINATED_TD_P(pd) __builtin_expect ((pd)->tid < 0, 0)
149 /* Cancellation test. */
150 #define CANCELLATION_P(self) \
152 int cancelhandling = THREAD_GETMEM (self, cancelhandling); \
153 if (CANCEL_ENABLED_AND_CANCELED (cancelhandling)) \
155 THREAD_SETMEM (self, result, PTHREAD_CANCELED); \
161 extern void __pthread_unwind (__pthread_unwind_buf_t
*__buf
)
162 __cleanup_fct_attribute
__attribute ((__noreturn__
))
163 #if !defined SHARED && !defined IS_IN_libpthread
167 extern void __pthread_unwind_next (__pthread_unwind_buf_t
*__buf
)
168 __cleanup_fct_attribute
__attribute ((__noreturn__
))
173 extern void __pthread_register_cancel (__pthread_unwind_buf_t
*__buf
)
174 __cleanup_fct_attribute
;
175 extern void __pthread_unregister_cancel (__pthread_unwind_buf_t
*__buf
)
176 __cleanup_fct_attribute
;
177 #if defined NOT_IN_libc && defined IS_IN_libpthread
178 hidden_proto (__pthread_unwind
)
179 hidden_proto (__pthread_unwind_next
)
180 hidden_proto (__pthread_register_cancel
)
181 hidden_proto (__pthread_unregister_cancel
)
183 extern void attribute_hidden
pthread_cancel_init (void);
188 /* Called when a thread reacts on a cancellation request. */
190 __attribute ((noreturn
, always_inline
))
193 struct pthread
*self
= THREAD_SELF
;
195 /* Make sure we get no more cancellations. */
196 THREAD_ATOMIC_BIT_SET (self
, cancelhandling
, EXITING_BIT
);
198 __pthread_unwind ((__pthread_unwind_buf_t
*)
199 THREAD_GETMEM (self
, cleanup_jmp_buf
));
203 /* Set cancellation mode to asynchronous. */
204 #define CANCEL_ASYNC() \
205 __pthread_enable_asynccancel ()
206 /* Reset to previous cancellation mode. */
207 #define CANCEL_RESET(oldtype) \
208 __pthread_disable_asynccancel (oldtype)
210 #if !defined NOT_IN_libc
211 /* Same as CANCEL_ASYNC, but for use in libc.so. */
212 # define LIBC_CANCEL_ASYNC() \
213 __libc_enable_asynccancel ()
214 /* Same as CANCEL_RESET, but for use in libc.so. */
215 # define LIBC_CANCEL_RESET(oldtype) \
216 __libc_disable_asynccancel (oldtype)
217 # define LIBC_CANCEL_HANDLED() \
218 __asm (".globl " __SYMBOL_PREFIX "__libc_enable_asynccancel"); \
219 __asm (".globl " __SYMBOL_PREFIX "__libc_disable_asynccancel")
220 #elif defined NOT_IN_libc && defined IS_IN_libpthread
221 # define LIBC_CANCEL_ASYNC() CANCEL_ASYNC ()
222 # define LIBC_CANCEL_RESET(val) CANCEL_RESET (val)
223 # define LIBC_CANCEL_HANDLED() \
224 __asm (".globl " __SYMBOL_PREFIX "__pthread_enable_asynccancel"); \
225 __asm (".globl " __SYMBOL_PREFIX "__pthread_disable_asynccancel")
226 #elif defined NOT_IN_libc && defined IS_IN_librt
227 # define LIBC_CANCEL_ASYNC() \
228 __librt_enable_asynccancel ()
229 # define LIBC_CANCEL_RESET(val) \
230 __librt_disable_asynccancel (val)
231 # define LIBC_CANCEL_HANDLED() \
232 __asm (".globl " __SYMBOL_PREFIX "__librt_enable_asynccancel"); \
233 __asm (".globl " __SYMBOL_PREFIX "__librt_disable_asynccancel")
235 # define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */
236 # define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */
237 # define LIBC_CANCEL_HANDLED() /* Nothing. */
240 /* The signal used for asynchronous cancelation. */
241 #define SIGCANCEL __SIGRTMIN
244 /* Signal needed for the kernel-supported POSIX timer implementation.
245 We can reuse the cancellation signal since we can distinguish
246 cancellation from timer expirations. */
247 #define SIGTIMER SIGCANCEL
250 /* Signal used to implement the setuid et.al. functions. */
251 #define SIGSETXID (__SIGRTMIN + 1)
253 /* Used to communicate with signal handler. */
254 extern struct xid_command
*__xidcmd attribute_hidden
;
257 /* Internal prototypes. */
259 /* Thread list handling. */
260 extern struct pthread
*__find_in_stack_list (struct pthread
*pd
)
261 attribute_hidden internal_function
;
263 /* Deallocate a thread's stack after optionally making sure the thread
264 descriptor is still valid. */
265 extern void __free_tcb (struct pthread
*pd
) attribute_hidden internal_function
;
267 /* Free allocated stack. */
268 extern void __deallocate_stack (struct pthread
*pd
)
269 attribute_hidden internal_function
;
271 /* Mark all the stacks except for the current one as available. This
272 function also re-initializes the lock for the stack cache. */
273 extern void __reclaim_stacks (void) attribute_hidden
;
275 /* Make all threads's stacks executable. */
276 extern int __make_stacks_executable (void **stack_endp
)
277 internal_function attribute_hidden
;
279 /* longjmp handling. */
280 extern void __pthread_cleanup_upto (__jmp_buf target
, char *targetframe
);
281 #if defined NOT_IN_libc && defined IS_IN_libpthread
282 hidden_proto (__pthread_cleanup_upto
)
286 /* Functions with versioned interfaces. */
287 extern int __pthread_create_2_1 (pthread_t
*newthread
,
288 const pthread_attr_t
*attr
,
289 void *(*start_routine
) (void *), void *arg
);
290 extern int __pthread_create_2_0 (pthread_t
*newthread
,
291 const pthread_attr_t
*attr
,
292 void *(*start_routine
) (void *), void *arg
);
293 extern int __pthread_attr_init_2_1 (pthread_attr_t
*attr
);
294 extern int __pthread_attr_init_2_0 (pthread_attr_t
*attr
);
297 /* Event handlers for libthread_db interface. */
298 extern void __nptl_create_event (void);
299 extern void __nptl_death_event (void);
300 hidden_proto (__nptl_create_event
)
301 hidden_proto (__nptl_death_event
)
303 /* Register the generation counter in the libpthread with the libc. */
304 #ifdef TLS_MULTIPLE_THREADS_IN_TCB
305 extern void __libc_pthread_init (unsigned long int *ptr
,
306 void (*reclaim
) (void),
307 const struct pthread_functions
*functions
)
310 extern int *__libc_pthread_init (unsigned long int *ptr
,
311 void (*reclaim
) (void),
312 const struct pthread_functions
*functions
)
315 /* Variable set to a nonzero value if more than one thread runs or ran. */
316 extern int __pthread_multiple_threads attribute_hidden
;
317 /* Pointer to the corresponding variable in libc. */
318 extern int *__libc_multiple_threads_ptr attribute_hidden
;
321 /* Find a thread given its TID. */
322 extern struct pthread
*__find_thread_by_id (pid_t tid
) attribute_hidden
327 #define __find_thread_by_id(tid) \
328 (__find_thread_by_id ? (__find_thread_by_id) (tid) : (struct pthread *) NULL)
331 extern void __pthread_init_static_tls (struct link_map
*) attribute_hidden
;
334 /* Namespace save aliases. */
335 extern int __pthread_getschedparam (pthread_t thread_id
, int *policy
,
336 struct sched_param
*param
);
337 extern int __pthread_setschedparam (pthread_t thread_id
, int policy
,
338 const struct sched_param
*param
);
339 extern int __pthread_setcancelstate (int state
, int *oldstate
);
340 extern int __pthread_mutex_init (pthread_mutex_t
*__mutex
,
341 __const pthread_mutexattr_t
*__mutexattr
);
342 extern int __pthread_mutex_init_internal (pthread_mutex_t
*__mutex
,
343 __const pthread_mutexattr_t
*__mutexattr
)
345 extern int __pthread_mutex_destroy (pthread_mutex_t
*__mutex
);
346 extern int __pthread_mutex_destroy_internal (pthread_mutex_t
*__mutex
)
348 extern int __pthread_mutex_trylock (pthread_mutex_t
*_mutex
);
349 extern int __pthread_mutex_lock (pthread_mutex_t
*__mutex
);
350 extern int __pthread_mutex_lock_internal (pthread_mutex_t
*__mutex
)
352 extern int __pthread_mutex_cond_lock (pthread_mutex_t
*__mutex
)
353 attribute_hidden internal_function
;
354 extern int __pthread_mutex_unlock (pthread_mutex_t
*__mutex
);
355 extern int __pthread_mutex_unlock_internal (pthread_mutex_t
*__mutex
)
357 extern int __pthread_mutex_unlock_usercnt (pthread_mutex_t
*__mutex
,
359 attribute_hidden internal_function
;
360 extern int __pthread_mutexattr_init (pthread_mutexattr_t
*attr
);
361 extern int __pthread_mutexattr_destroy (pthread_mutexattr_t
*attr
);
362 extern int __pthread_mutexattr_settype (pthread_mutexattr_t
*attr
, int kind
);
363 extern int __pthread_attr_destroy (pthread_attr_t
*attr
);
364 extern int __pthread_attr_getdetachstate (const pthread_attr_t
*attr
,
366 extern int __pthread_attr_setdetachstate (pthread_attr_t
*attr
,
368 extern int __pthread_attr_getinheritsched (const pthread_attr_t
*attr
,
370 extern int __pthread_attr_setinheritsched (pthread_attr_t
*attr
, int inherit
);
371 extern int __pthread_attr_getschedparam (const pthread_attr_t
*attr
,
372 struct sched_param
*param
);
373 extern int __pthread_attr_setschedparam (pthread_attr_t
*attr
,
374 const struct sched_param
*param
);
375 extern int __pthread_attr_getschedpolicy (const pthread_attr_t
*attr
,
377 extern int __pthread_attr_setschedpolicy (pthread_attr_t
*attr
, int policy
);
378 extern int __pthread_attr_getscope (const pthread_attr_t
*attr
, int *scope
);
379 extern int __pthread_attr_setscope (pthread_attr_t
*attr
, int scope
);
380 extern int __pthread_attr_getstackaddr (__const pthread_attr_t
*__restrict
381 __attr
, void **__restrict __stackaddr
);
382 extern int __pthread_attr_setstackaddr (pthread_attr_t
*__attr
,
384 extern int __pthread_attr_getstacksize (__const pthread_attr_t
*__restrict
386 size_t *__restrict __stacksize
);
387 extern int __pthread_attr_setstacksize (pthread_attr_t
*__attr
,
389 extern int __pthread_attr_getstack (__const pthread_attr_t
*__restrict __attr
,
390 void **__restrict __stackaddr
,
391 size_t *__restrict __stacksize
);
392 extern int __pthread_attr_setstack (pthread_attr_t
*__attr
, void *__stackaddr
,
394 extern int __pthread_rwlock_init (pthread_rwlock_t
*__restrict __rwlock
,
395 __const pthread_rwlockattr_t
*__restrict
397 extern int __pthread_rwlock_destroy (pthread_rwlock_t
*__rwlock
);
398 extern int __pthread_rwlock_rdlock (pthread_rwlock_t
*__rwlock
);
399 extern int __pthread_rwlock_rdlock_internal (pthread_rwlock_t
*__rwlock
);
400 extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t
*__rwlock
);
401 extern int __pthread_rwlock_wrlock (pthread_rwlock_t
*__rwlock
);
402 extern int __pthread_rwlock_wrlock_internal (pthread_rwlock_t
*__rwlock
);
403 extern int __pthread_rwlock_trywrlock (pthread_rwlock_t
*__rwlock
);
404 extern int __pthread_rwlock_unlock (pthread_rwlock_t
*__rwlock
);
405 extern int __pthread_rwlock_unlock_internal (pthread_rwlock_t
*__rwlock
);
406 extern int __pthread_cond_broadcast (pthread_cond_t
*cond
);
407 extern int __pthread_cond_destroy (pthread_cond_t
*cond
);
408 extern int __pthread_cond_init (pthread_cond_t
*cond
,
409 const pthread_condattr_t
*cond_attr
);
410 extern int __pthread_cond_signal (pthread_cond_t
*cond
);
411 extern int __pthread_cond_wait (pthread_cond_t
*cond
, pthread_mutex_t
*mutex
);
412 extern int __pthread_cond_timedwait (pthread_cond_t
*cond
,
413 pthread_mutex_t
*mutex
,
414 const struct timespec
*abstime
);
415 extern int __pthread_condattr_destroy (pthread_condattr_t
*attr
);
416 extern int __pthread_condattr_init (pthread_condattr_t
*attr
);
417 extern int __pthread_key_create (pthread_key_t
*key
, void (*destr
) (void *));
418 extern int __pthread_key_create_internal (pthread_key_t
*key
,
419 void (*destr
) (void *));
420 extern void *__pthread_getspecific (pthread_key_t key
);
421 extern void *__pthread_getspecific_internal (pthread_key_t key
);
422 extern int __pthread_setspecific (pthread_key_t key
, const void *value
);
423 extern int __pthread_setspecific_internal (pthread_key_t key
,
425 extern int __pthread_once (pthread_once_t
*once_control
,
426 void (*init_routine
) (void));
427 extern int __pthread_once_internal (pthread_once_t
*once_control
,
428 void (*init_routine
) (void));
429 extern int __pthread_atfork (void (*prepare
) (void), void (*parent
) (void),
430 void (*child
) (void));
431 extern pthread_t
__pthread_self (void);
432 extern int __pthread_equal (pthread_t thread1
, pthread_t thread2
);
433 extern int __pthread_kill (pthread_t threadid
, int signo
);
434 extern void __pthread_exit (void *value
);
435 extern int __pthread_setcanceltype (int type
, int *oldtype
);
436 extern int __pthread_enable_asynccancel (void) attribute_hidden
;
437 extern void __pthread_disable_asynccancel (int oldtype
)
438 internal_function attribute_hidden
;
440 extern int __pthread_cond_broadcast_2_0 (pthread_cond_2_0_t
*cond
);
441 extern int __pthread_cond_destroy_2_0 (pthread_cond_2_0_t
*cond
);
442 extern int __pthread_cond_init_2_0 (pthread_cond_2_0_t
*cond
,
443 const pthread_condattr_t
*cond_attr
);
444 extern int __pthread_cond_signal_2_0 (pthread_cond_2_0_t
*cond
);
445 extern int __pthread_cond_timedwait_2_0 (pthread_cond_2_0_t
*cond
,
446 pthread_mutex_t
*mutex
,
447 const struct timespec
*abstime
);
448 extern int __pthread_cond_wait_2_0 (pthread_cond_2_0_t
*cond
,
449 pthread_mutex_t
*mutex
);
451 extern int __pthread_getaffinity_np (pthread_t th
, size_t cpusetsize
,
454 /* The two functions are in libc.so and not exported. */
455 extern int __libc_enable_asynccancel (void) attribute_hidden
;
456 extern void __libc_disable_asynccancel (int oldtype
)
457 internal_function attribute_hidden
;
460 /* The two functions are in librt.so and not exported. */
461 extern int __librt_enable_asynccancel (void) attribute_hidden
;
462 extern void __librt_disable_asynccancel (int oldtype
)
463 internal_function attribute_hidden
;
465 #ifdef IS_IN_libpthread
466 /* Special versions which use non-exported functions. */
467 extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer
*buffer
,
468 void (*routine
) (void *), void *arg
)
470 # undef pthread_cleanup_push
471 # define pthread_cleanup_push(routine,arg) \
472 { struct _pthread_cleanup_buffer _buffer; \
473 __pthread_cleanup_push (&_buffer, (routine), (arg));
475 extern void __pthread_cleanup_pop (struct _pthread_cleanup_buffer
*buffer
,
476 int execute
) attribute_hidden
;
477 # undef pthread_cleanup_pop
478 # define pthread_cleanup_pop(execute) \
479 __pthread_cleanup_pop (&_buffer, (execute)); }
482 extern void __pthread_cleanup_push_defer (struct _pthread_cleanup_buffer
*buffer
,
483 void (*routine
) (void *), void *arg
);
484 extern void __pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer
*buffer
,
487 /* Old cleanup interfaces, still used in libc.so. */
488 extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer
*buffer
,
489 void (*routine
) (void *), void *arg
);
490 extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer
*buffer
,
492 extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer
*buffer
,
493 void (*routine
) (void *), void *arg
);
494 extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer
*buffer
,
497 extern void __nptl_deallocate_tsd (void) attribute_hidden
;
499 extern int __nptl_setxid (struct xid_command
*cmdp
) attribute_hidden
;
502 # define PTHREAD_STATIC_FN_REQUIRE(name)
504 # define PTHREAD_STATIC_FN_REQUIRE(name) __asm (".globl " #name);
507 #endif /* pthreadP.h */