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