]>
Commit | Line | Data |
---|---|---|
bf293afe | 1 | /* Copyright (C) 2002, 2003 Free Software Foundation, Inc. |
76a50749 UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. | |
4 | ||
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. | |
9 | ||
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. | |
14 | ||
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. */ | |
19 | ||
20 | #ifndef _PTHREADP_H | |
21 | #define _PTHREADP_H 1 | |
22 | ||
23 | #include <pthread.h> | |
24 | #include <setjmp.h> | |
76a50749 UD |
25 | #include <sys/syscall.h> |
26 | #include "descr.h" | |
27 | #include <tls.h> | |
28 | #include <lowlevellock.h> | |
29 | #include <stackinfo.h> | |
30 | #include <internaltypes.h> | |
73e9ae88 | 31 | #include <pthread-functions.h> |
86246935 | 32 | #include <atomic.h> |
76a50749 UD |
33 | |
34 | ||
a1b5424f UD |
35 | /* Atomic operations on TLS memory. */ |
36 | #ifndef THREAD_ATOMIC_CMPXCHG_VAL | |
37 | # define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, new, old) \ | |
38 | atomic_compare_and_exchange_val_acq (&(descr)->member, new, old) | |
39 | #endif | |
40 | ||
41 | #ifndef THREAD_ATOMIC_BIT_SET | |
42 | # define THREAD_ATOMIC_BIT_SET(descr, member, bit) \ | |
43 | atomic_bit_set (&(descr)->member, bit) | |
44 | #endif | |
45 | ||
46 | ||
76a50749 UD |
47 | /* Internal variables. */ |
48 | ||
49 | ||
50 | /* Default stack size. */ | |
51 | extern size_t __default_stacksize attribute_hidden; | |
52 | ||
53 | /* Size and alignment of static TLS block. */ | |
54 | extern size_t __static_tls_size attribute_hidden; | |
bc6389ad | 55 | extern size_t __static_tls_align_m1 attribute_hidden; |
76a50749 UD |
56 | |
57 | /* Thread descriptor handling. */ | |
fa9a4ff0 RM |
58 | extern list_t __stack_user; |
59 | hidden_proto (__stack_user) | |
76a50749 UD |
60 | |
61 | /* Attribute handling. */ | |
62 | extern struct pthread_attr *__attr_list attribute_hidden; | |
63 | extern lll_lock_t __attr_list_lock attribute_hidden; | |
64 | ||
65 | /* First available RT signal. */ | |
66 | extern int __current_sigrtmin attribute_hidden; | |
67 | /* Last available RT signal. */ | |
68 | extern int __current_sigrtmax attribute_hidden; | |
69 | ||
70 | /* Concurrency handling. */ | |
71 | extern int __concurrency_level attribute_hidden; | |
72 | ||
73 | /* Thread-local data key handling. */ | |
fa9a4ff0 RM |
74 | extern struct pthread_key_struct __pthread_keys[PTHREAD_KEYS_MAX]; |
75 | hidden_proto (__pthread_keys) | |
76a50749 | 76 | |
47202270 UD |
77 | /* Number of threads running. */ |
78 | extern unsigned int __nptl_nthreads attribute_hidden; | |
79 | ||
76a50749 UD |
80 | /* The library can run in debugging mode where it performs a lot more |
81 | tests. */ | |
82 | extern int __pthread_debug attribute_hidden; | |
729924a0 UD |
83 | /** For now disable debugging support. */ |
84 | #if 0 | |
85 | # define DEBUGGING_P __builtin_expect (__pthread_debug, 0) | |
8c2e9a29 UD |
86 | # define INVALID_TD_P(pd) (DEBUGGING_P && __find_in_stack_list (pd) == NULL) |
87 | # define INVALID_NOT_TERMINATED_TD_P(pd) INVALID_TD_P (pd) | |
729924a0 UD |
88 | #else |
89 | # define DEBUGGING_P 0 | |
8c2e9a29 UD |
90 | /* Simplified test. This will not catch all invalid descriptors but |
91 | is better than nothing. And if the test triggers the thread | |
92 | descriptor is guaranteed to be invalid. */ | |
93 | # define INVALID_TD_P(pd) __builtin_expect ((pd)->tid <= 0, 0) | |
94 | # define INVALID_NOT_TERMINATED_TD_P(pd) __builtin_expect ((pd)->tid < 0, 0) | |
729924a0 | 95 | #endif |
76a50749 UD |
96 | |
97 | ||
98 | /* Cancellation test. */ | |
99 | #define CANCELLATION_P(self) \ | |
100 | do { \ | |
101 | int cancelhandling = THREAD_GETMEM (self, cancelhandling); \ | |
102 | if (CANCEL_ENABLED_AND_CANCELED (cancelhandling)) \ | |
103 | { \ | |
104 | THREAD_SETMEM (self, result, PTHREAD_CANCELED); \ | |
9ae0909b | 105 | __do_cancel (); \ |
76a50749 UD |
106 | } \ |
107 | } while (0) | |
108 | ||
6efd4814 UD |
109 | |
110 | extern void __pthread_unwind (__pthread_unwind_buf_t *__buf) | |
111 | __cleanup_fct_attribute __attribute ((__noreturn__)) | |
112 | #ifndef SHARED | |
113 | weak_function | |
114 | #endif | |
115 | ; | |
116 | ||
117 | /* Called when a thread reacts on a cancellation request. */ | |
118 | static inline void | |
dd9423a6 | 119 | __attribute ((noreturn, always_inline)) |
6efd4814 UD |
120 | __do_cancel (void) |
121 | { | |
122 | struct pthread *self = THREAD_SELF; | |
123 | ||
124 | /* Make sure we get no more cancellations. */ | |
125 | THREAD_ATOMIC_BIT_SET (self, cancelhandling, EXITING_BIT); | |
126 | ||
127 | __pthread_unwind ((__pthread_unwind_buf_t *) | |
128 | THREAD_GETMEM (self, cleanup_jmp_buf)); | |
129 | } | |
130 | ||
131 | ||
76a50749 | 132 | /* Set cancellation mode to asynchronous. */ |
bdb04f92 UD |
133 | #define CANCEL_ASYNC() \ |
134 | __pthread_enable_asynccancel () | |
76a50749 UD |
135 | /* Reset to previous cancellation mode. */ |
136 | #define CANCEL_RESET(oldtype) \ | |
bdb04f92 | 137 | __pthread_disable_asynccancel (oldtype) |
76a50749 | 138 | |
9634cf9d | 139 | #if !defined NOT_IN_libc |
9ae0909b | 140 | /* Same as CANCEL_ASYNC, but for use in libc.so. */ |
9634cf9d | 141 | # define LIBC_CANCEL_ASYNC() \ |
9ae0909b UD |
142 | __libc_enable_asynccancel () |
143 | /* Same as CANCEL_RESET, but for use in libc.so. */ | |
9634cf9d | 144 | # define LIBC_CANCEL_RESET(oldtype) \ |
9ae0909b | 145 | __libc_disable_asynccancel (oldtype) |
52f3d213 UD |
146 | # define LIBC_CANCEL_HANDLED() \ |
147 | __asm (".globl " __SYMBOL_PREFIX "__libc_enable_asynccancel"); \ | |
148 | __asm (".globl " __SYMBOL_PREFIX "__libc_disable_asynccancel") | |
9634cf9d UD |
149 | #elif defined NOT_IN_libc && defined IS_IN_libpthread |
150 | # define LIBC_CANCEL_ASYNC() CANCEL_ASYNC () | |
151 | # define LIBC_CANCEL_RESET(val) CANCEL_RESET (val) | |
52f3d213 UD |
152 | # define LIBC_CANCEL_HANDLED() \ |
153 | __asm (".globl " __SYMBOL_PREFIX "__pthread_enable_asynccancel"); \ | |
154 | __asm (".globl " __SYMBOL_PREFIX "__pthread_disable_asynccancel") | |
9634cf9d UD |
155 | #else |
156 | # define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */ | |
157 | # define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */ | |
52f3d213 | 158 | # define LIBC_CANCEL_HANDLED() /* Nothing. */ |
9634cf9d | 159 | #endif |
9ae0909b | 160 | |
2a9ae45c UD |
161 | /* The signal used for asynchronous cancelation. */ |
162 | #define SIGCANCEL __SIGRTMIN | |
163 | ||
164 | ||
e7608d77 UD |
165 | /* Signal needed for the kernel-supported POSIX timer implementation. |
166 | We can reuse the cancellation signal since we can distinguish | |
167 | cancellation from timer expirations. */ | |
168 | #define SIGTIMER SIGCANCEL | |
2a9ae45c | 169 | |
9ae0909b | 170 | |
76a50749 UD |
171 | /* Internal prototypes. */ |
172 | ||
173 | /* Thread list handling. */ | |
174 | extern struct pthread *__find_in_stack_list (struct pthread *pd) | |
90491dc4 | 175 | attribute_hidden internal_function; |
76a50749 UD |
176 | |
177 | /* Deallocate a thread's stack after optionally making sure the thread | |
178 | descriptor is still valid. */ | |
90491dc4 | 179 | extern void __free_tcb (struct pthread *pd) attribute_hidden internal_function; |
76a50749 UD |
180 | |
181 | /* Free allocated stack. */ | |
90491dc4 UD |
182 | extern void __deallocate_stack (struct pthread *pd) |
183 | attribute_hidden internal_function; | |
76a50749 UD |
184 | |
185 | /* Mark all the stacks except for the current one as available. This | |
186 | function also re-initializes the lock for the stack cache. */ | |
187 | extern void __reclaim_stacks (void) attribute_hidden; | |
188 | ||
189 | /* longjmp handling. */ | |
190 | extern void __pthread_cleanup_upto (__jmp_buf target, char *targetframe); | |
191 | ||
192 | ||
193 | /* Functions with versioned interfaces. */ | |
194 | extern int __pthread_create_2_1 (pthread_t *newthread, | |
195 | const pthread_attr_t *attr, | |
196 | void *(*start_routine) (void *), void *arg); | |
197 | extern int __pthread_create_2_0 (pthread_t *newthread, | |
198 | const pthread_attr_t *attr, | |
199 | void *(*start_routine) (void *), void *arg); | |
200 | extern int __pthread_attr_init_2_1 (pthread_attr_t *attr); | |
201 | extern int __pthread_attr_init_2_0 (pthread_attr_t *attr); | |
202 | ||
203 | ||
204 | /* Event handlers for libthread_db interface. */ | |
fa9a4ff0 RM |
205 | extern void __nptl_create_event (void); |
206 | extern void __nptl_death_event (void); | |
207 | hidden_proto (__nptl_create_event) | |
208 | hidden_proto (__nptl_death_event) | |
76a50749 | 209 | |
8454830b | 210 | /* Register the generation counter in the libpthread with the libc. */ |
5a03acfe | 211 | #ifdef TLS_MULTIPLE_THREADS_IN_TCB |
8454830b UD |
212 | extern void __libc_pthread_init (unsigned long int *ptr, |
213 | void (*reclaim) (void), | |
5a03acfe UD |
214 | const struct pthread_functions *functions) |
215 | internal_function; | |
216 | #else | |
217 | extern int *__libc_pthread_init (unsigned long int *ptr, | |
218 | void (*reclaim) (void), | |
219 | const struct pthread_functions *functions) | |
220 | internal_function; | |
221 | ||
222 | /* Variable set to a nonzero value if more than one thread runs or ran. */ | |
223 | extern int __pthread_multiple_threads attribute_hidden; | |
224 | /* Pointer to the corresponding variable in libc. */ | |
225 | extern int *__libc_multiple_threads_ptr attribute_hidden; | |
226 | #endif | |
8454830b | 227 | |
76a50749 UD |
228 | |
229 | /* Namespace save aliases. */ | |
8454830b UD |
230 | extern int __pthread_getschedparam (pthread_t thread_id, int *policy, |
231 | struct sched_param *param); | |
232 | extern int __pthread_setschedparam (pthread_t thread_id, int policy, | |
233 | const struct sched_param *param); | |
234 | extern int __pthread_setcancelstate (int state, int *oldstate); | |
76a50749 UD |
235 | extern int __pthread_mutex_init (pthread_mutex_t *__mutex, |
236 | __const pthread_mutexattr_t *__mutexattr); | |
8454830b UD |
237 | extern int __pthread_mutex_init_internal (pthread_mutex_t *__mutex, |
238 | __const pthread_mutexattr_t *__mutexattr); | |
76a50749 | 239 | extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex); |
8454830b | 240 | extern int __pthread_mutex_destroy_internal (pthread_mutex_t *__mutex); |
76a50749 UD |
241 | extern int __pthread_mutex_trylock (pthread_mutex_t *_mutex); |
242 | extern int __pthread_mutex_lock (pthread_mutex_t *__mutex); | |
09efc3ba | 243 | extern int __pthread_mutex_lock_internal (pthread_mutex_t *__mutex); |
69431c9a UD |
244 | extern int __pthread_mutex_cond_lock (pthread_mutex_t *__mutex) |
245 | attribute_hidden; | |
76a50749 | 246 | extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex); |
09efc3ba | 247 | extern int __pthread_mutex_unlock_internal (pthread_mutex_t *__mutex); |
76a50749 UD |
248 | extern int __pthread_mutexattr_init (pthread_mutexattr_t *attr); |
249 | extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *attr); | |
250 | extern int __pthread_mutexattr_settype (pthread_mutexattr_t *attr, int kind); | |
8454830b UD |
251 | extern int __pthread_attr_destroy (pthread_attr_t *attr); |
252 | extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr, | |
253 | int *detachstate); | |
254 | extern int __pthread_attr_setdetachstate (pthread_attr_t *attr, | |
255 | int detachstate); | |
256 | extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr, | |
257 | int *inherit); | |
258 | extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit); | |
259 | extern int __pthread_attr_getschedparam (const pthread_attr_t *attr, | |
260 | struct sched_param *param); | |
261 | extern int __pthread_attr_setschedparam (pthread_attr_t *attr, | |
262 | const struct sched_param *param); | |
263 | extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr, | |
264 | int *policy); | |
265 | extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy); | |
266 | extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope); | |
267 | extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope); | |
76a50749 UD |
268 | extern int __pthread_attr_getstackaddr (__const pthread_attr_t *__restrict |
269 | __attr, void **__restrict __stackaddr); | |
270 | extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr, | |
271 | void *__stackaddr); | |
272 | extern int __pthread_attr_getstacksize (__const pthread_attr_t *__restrict | |
273 | __attr, | |
274 | size_t *__restrict __stacksize); | |
275 | extern int __pthread_attr_setstacksize (pthread_attr_t *__attr, | |
276 | size_t __stacksize); | |
277 | extern int __pthread_attr_getstack (__const pthread_attr_t *__restrict __attr, | |
278 | void **__restrict __stackaddr, | |
279 | size_t *__restrict __stacksize); | |
280 | extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr, | |
281 | size_t __stacksize); | |
282 | extern int __pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock, | |
283 | __const pthread_rwlockattr_t *__restrict | |
284 | __attr); | |
285 | extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock); | |
286 | extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock); | |
73e9ae88 | 287 | extern int __pthread_rwlock_rdlock_internal (pthread_rwlock_t *__rwlock); |
76a50749 UD |
288 | extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock); |
289 | extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock); | |
73e9ae88 | 290 | extern int __pthread_rwlock_wrlock_internal (pthread_rwlock_t *__rwlock); |
76a50749 UD |
291 | extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock); |
292 | extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock); | |
73e9ae88 | 293 | extern int __pthread_rwlock_unlock_internal (pthread_rwlock_t *__rwlock); |
8454830b UD |
294 | extern int __pthread_cond_broadcast (pthread_cond_t *cond); |
295 | extern int __pthread_cond_destroy (pthread_cond_t *cond); | |
296 | extern int __pthread_cond_init (pthread_cond_t *cond, | |
297 | const pthread_condattr_t *cond_attr); | |
298 | extern int __pthread_cond_signal (pthread_cond_t *cond); | |
299 | extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex); | |
b5facfda UD |
300 | extern int __pthread_cond_timedwait (pthread_cond_t *cond, |
301 | pthread_mutex_t *mutex, | |
302 | const struct timespec *abstime); | |
8454830b UD |
303 | extern int __pthread_condattr_destroy (pthread_condattr_t *attr); |
304 | extern int __pthread_condattr_init (pthread_condattr_t *attr); | |
76a50749 | 305 | extern int __pthread_key_create (pthread_key_t *key, void (*destr) (void *)); |
73e9ae88 UD |
306 | extern int __pthread_key_create_internal (pthread_key_t *key, |
307 | void (*destr) (void *)); | |
76a50749 | 308 | extern void *__pthread_getspecific (pthread_key_t key); |
73e9ae88 | 309 | extern void *__pthread_getspecific_internal (pthread_key_t key); |
76a50749 | 310 | extern int __pthread_setspecific (pthread_key_t key, const void *value); |
73e9ae88 UD |
311 | extern int __pthread_setspecific_internal (pthread_key_t key, |
312 | const void *value); | |
76a50749 UD |
313 | extern int __pthread_once (pthread_once_t *once_control, |
314 | void (*init_routine) (void)); | |
09efc3ba UD |
315 | extern int __pthread_once_internal (pthread_once_t *once_control, |
316 | void (*init_routine) (void)); | |
76a50749 UD |
317 | extern int __pthread_atfork (void (*prepare) (void), void (*parent) (void), |
318 | void (*child) (void)); | |
8454830b UD |
319 | extern pthread_t __pthread_self (void); |
320 | extern int __pthread_equal (pthread_t thread1, pthread_t thread2); | |
09efc3ba | 321 | extern int __pthread_kill (pthread_t threadid, int signo); |
8454830b | 322 | extern void __pthread_exit (void *value); |
09efc3ba | 323 | extern int __pthread_setcanceltype (int type, int *oldtype); |
bdb04f92 | 324 | extern int __pthread_enable_asynccancel (void) attribute_hidden; |
9ae0909b UD |
325 | extern void __pthread_disable_asynccancel (int oldtype) |
326 | internal_function attribute_hidden; | |
327 | ||
997256dd UD |
328 | extern int __pthread_cond_broadcast_2_0 (pthread_cond_2_0_t *cond); |
329 | extern int __pthread_cond_destroy_2_0 (pthread_cond_2_0_t *cond); | |
330 | extern int __pthread_cond_init_2_0 (pthread_cond_2_0_t *cond, | |
05df18c3 | 331 | const pthread_condattr_t *cond_attr); |
997256dd UD |
332 | extern int __pthread_cond_signal_2_0 (pthread_cond_2_0_t *cond); |
333 | extern int __pthread_cond_timedwait_2_0 (pthread_cond_2_0_t *cond, | |
05df18c3 UD |
334 | pthread_mutex_t *mutex, |
335 | const struct timespec *abstime); | |
997256dd | 336 | extern int __pthread_cond_wait_2_0 (pthread_cond_2_0_t *cond, |
05df18c3 UD |
337 | pthread_mutex_t *mutex); |
338 | ||
73e9ae88 | 339 | |
9ae0909b UD |
340 | /* The two functions are in libc.so and not exported. */ |
341 | extern int __libc_enable_asynccancel (void) attribute_hidden; | |
342 | extern void __libc_disable_asynccancel (int oldtype) | |
343 | internal_function attribute_hidden; | |
09efc3ba | 344 | |
1e506629 | 345 | #ifdef IS_IN_libpthread |
09efc3ba | 346 | /* Special versions which use non-exported functions. */ |
73e9ae88 UD |
347 | extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer, |
348 | void (*routine) (void *), void *arg) | |
09efc3ba | 349 | attribute_hidden; |
1e506629 UD |
350 | # undef pthread_cleanup_push |
351 | # define pthread_cleanup_push(routine,arg) \ | |
09efc3ba | 352 | { struct _pthread_cleanup_buffer _buffer; \ |
73e9ae88 | 353 | __pthread_cleanup_push (&_buffer, (routine), (arg)); |
09efc3ba | 354 | |
73e9ae88 UD |
355 | extern void __pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer, |
356 | int execute) attribute_hidden; | |
1e506629 UD |
357 | # undef pthread_cleanup_pop |
358 | # define pthread_cleanup_pop(execute) \ | |
73e9ae88 | 359 | __pthread_cleanup_pop (&_buffer, (execute)); } |
1e506629 | 360 | #endif |
76a50749 | 361 | |
73e9ae88 UD |
362 | extern void __pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer, |
363 | void (*routine) (void *), void *arg); | |
364 | extern void __pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer, | |
365 | int execute); | |
366 | ||
6efd4814 UD |
367 | /* Old cleanup interfaces, still used in libc.so. */ |
368 | extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer, | |
369 | void (*routine) (void *), void *arg); | |
370 | extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer, | |
371 | int execute); | |
372 | extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer, | |
373 | void (*routine) (void *), void *arg); | |
374 | extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer, | |
375 | int execute); | |
376 | ||
76a50749 | 377 | #endif /* pthreadP.h */ |