1 | /* Copyright (C) 2002-2018 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, see |
17 | <http://www.gnu.org/licenses/>. */ |
18 | |
19 | #ifndef _PTHREADP_H |
20 | #define _PTHREADP_H 1 |
21 | |
22 | #include <pthread.h> |
23 | #include <setjmp.h> |
24 | #include <stdbool.h> |
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> |
31 | #include <pthread-functions.h> |
32 | #include <atomic.h> |
33 | #include <kernel-features.h> |
34 | #include <errno.h> |
35 | #include <internal-signals.h> |
36 | |
37 | |
38 | /* Atomic operations on TLS memory. */ |
39 | #ifndef THREAD_ATOMIC_CMPXCHG_VAL |
40 | # define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, new, old) \ |
41 | atomic_compare_and_exchange_val_acq (&(descr)->member, new, old) |
42 | #endif |
43 | |
44 | #ifndef THREAD_ATOMIC_BIT_SET |
45 | # define THREAD_ATOMIC_BIT_SET(descr, member, bit) \ |
46 | atomic_bit_set (&(descr)->member, bit) |
47 | #endif |
48 | |
49 | |
50 | /* Adaptive mutex definitions. */ |
51 | #ifndef MAX_ADAPTIVE_COUNT |
52 | # define MAX_ADAPTIVE_COUNT 100 |
53 | #endif |
54 | |
55 | |
56 | /* Magic cookie representing robust mutex with dead owner. */ |
57 | #define PTHREAD_MUTEX_INCONSISTENT INT_MAX |
58 | /* Magic cookie representing not recoverable robust mutex. */ |
59 | #define PTHREAD_MUTEX_NOTRECOVERABLE (INT_MAX - 1) |
60 | |
61 | |
62 | /* Internal mutex type value. */ |
63 | enum |
64 | { |
65 | PTHREAD_MUTEX_KIND_MASK_NP = 3, |
66 | |
67 | PTHREAD_MUTEX_ELISION_NP = 256, |
68 | PTHREAD_MUTEX_NO_ELISION_NP = 512, |
69 | |
70 | PTHREAD_MUTEX_ROBUST_NORMAL_NP = 16, |
71 | PTHREAD_MUTEX_ROBUST_RECURSIVE_NP |
72 | = PTHREAD_MUTEX_ROBUST_NORMAL_NP | PTHREAD_MUTEX_RECURSIVE_NP, |
73 | PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP |
74 | = PTHREAD_MUTEX_ROBUST_NORMAL_NP | PTHREAD_MUTEX_ERRORCHECK_NP, |
75 | PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP |
76 | = PTHREAD_MUTEX_ROBUST_NORMAL_NP | PTHREAD_MUTEX_ADAPTIVE_NP, |
77 | PTHREAD_MUTEX_PRIO_INHERIT_NP = 32, |
78 | PTHREAD_MUTEX_PI_NORMAL_NP |
79 | = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_NORMAL, |
80 | PTHREAD_MUTEX_PI_RECURSIVE_NP |
81 | = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_RECURSIVE_NP, |
82 | PTHREAD_MUTEX_PI_ERRORCHECK_NP |
83 | = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ERRORCHECK_NP, |
84 | PTHREAD_MUTEX_PI_ADAPTIVE_NP |
85 | = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ADAPTIVE_NP, |
86 | PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP |
87 | = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_NORMAL_NP, |
88 | PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP |
89 | = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_RECURSIVE_NP, |
90 | PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP |
91 | = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP, |
92 | PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP |
93 | = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP, |
94 | PTHREAD_MUTEX_PRIO_PROTECT_NP = 64, |
95 | PTHREAD_MUTEX_PP_NORMAL_NP |
96 | = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_NORMAL, |
97 | PTHREAD_MUTEX_PP_RECURSIVE_NP |
98 | = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_RECURSIVE_NP, |
99 | PTHREAD_MUTEX_PP_ERRORCHECK_NP |
100 | = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_ERRORCHECK_NP, |
101 | PTHREAD_MUTEX_PP_ADAPTIVE_NP |
102 | = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_ADAPTIVE_NP, |
103 | PTHREAD_MUTEX_ELISION_FLAGS_NP |
104 | = PTHREAD_MUTEX_ELISION_NP | PTHREAD_MUTEX_NO_ELISION_NP, |
105 | |
106 | PTHREAD_MUTEX_TIMED_ELISION_NP = |
107 | PTHREAD_MUTEX_TIMED_NP | PTHREAD_MUTEX_ELISION_NP, |
108 | PTHREAD_MUTEX_TIMED_NO_ELISION_NP = |
109 | PTHREAD_MUTEX_TIMED_NP | PTHREAD_MUTEX_NO_ELISION_NP, |
110 | }; |
111 | #define PTHREAD_MUTEX_PSHARED_BIT 128 |
112 | |
113 | #define PTHREAD_MUTEX_TYPE(m) \ |
114 | ((m)->__data.__kind & 127) |
115 | /* Don't include NO_ELISION, as that type is always the same |
116 | as the underlying lock type. */ |
117 | #define PTHREAD_MUTEX_TYPE_ELISION(m) \ |
118 | ((m)->__data.__kind & (127|PTHREAD_MUTEX_ELISION_NP)) |
119 | |
120 | #if LLL_PRIVATE == 0 && LLL_SHARED == 128 |
121 | # define PTHREAD_MUTEX_PSHARED(m) \ |
122 | ((m)->__data.__kind & 128) |
123 | #else |
124 | # define PTHREAD_MUTEX_PSHARED(m) \ |
125 | (((m)->__data.__kind & 128) ? LLL_SHARED : LLL_PRIVATE) |
126 | #endif |
127 | |
128 | /* The kernel when waking robust mutexes on exit never uses |
129 | FUTEX_PRIVATE_FLAG FUTEX_WAKE. */ |
130 | #define PTHREAD_ROBUST_MUTEX_PSHARED(m) LLL_SHARED |
131 | |
132 | /* Ceiling in __data.__lock. __data.__lock is signed, so don't |
133 | use the MSB bit in there, but in the mask also include that bit, |
134 | so that the compiler can optimize & PTHREAD_MUTEX_PRIO_CEILING_MASK |
135 | masking if the value is then shifted down by |
136 | PTHREAD_MUTEX_PRIO_CEILING_SHIFT. */ |
137 | #define PTHREAD_MUTEX_PRIO_CEILING_SHIFT 19 |
138 | #define PTHREAD_MUTEX_PRIO_CEILING_MASK 0xfff80000 |
139 | |
140 | |
141 | /* Flags in mutex attr. */ |
142 | #define PTHREAD_MUTEXATTR_PROTOCOL_SHIFT 28 |
143 | #define PTHREAD_MUTEXATTR_PROTOCOL_MASK 0x30000000 |
144 | #define PTHREAD_MUTEXATTR_PRIO_CEILING_SHIFT 12 |
145 | #define PTHREAD_MUTEXATTR_PRIO_CEILING_MASK 0x00fff000 |
146 | #define PTHREAD_MUTEXATTR_FLAG_ROBUST 0x40000000 |
147 | #define PTHREAD_MUTEXATTR_FLAG_PSHARED 0x80000000 |
148 | #define PTHREAD_MUTEXATTR_FLAG_BITS \ |
149 | (PTHREAD_MUTEXATTR_FLAG_ROBUST | PTHREAD_MUTEXATTR_FLAG_PSHARED \ |
150 | | PTHREAD_MUTEXATTR_PROTOCOL_MASK | PTHREAD_MUTEXATTR_PRIO_CEILING_MASK) |
151 | |
152 | |
153 | /* For the following, see pthread_rwlock_common.c. */ |
154 | #define PTHREAD_RWLOCK_WRPHASE 1 |
155 | #define PTHREAD_RWLOCK_WRLOCKED 2 |
156 | #define PTHREAD_RWLOCK_RWAITING 4 |
157 | #define PTHREAD_RWLOCK_READER_SHIFT 3 |
158 | #define PTHREAD_RWLOCK_READER_OVERFLOW ((unsigned int) 1 \ |
159 | << (sizeof (unsigned int) * 8 - 1)) |
160 | #define PTHREAD_RWLOCK_WRHANDOVER ((unsigned int) 1 \ |
161 | << (sizeof (unsigned int) * 8 - 1)) |
162 | #define PTHREAD_RWLOCK_FUTEX_USED 2 |
163 | |
164 | |
165 | /* Bits used in robust mutex implementation. */ |
166 | #define FUTEX_WAITERS 0x80000000 |
167 | #define FUTEX_OWNER_DIED 0x40000000 |
168 | #define FUTEX_TID_MASK 0x3fffffff |
169 | |
170 | |
171 | /* pthread_once definitions. See __pthread_once for how these are used. */ |
172 | #define __PTHREAD_ONCE_INPROGRESS 1 |
173 | #define __PTHREAD_ONCE_DONE 2 |
174 | #define __PTHREAD_ONCE_FORK_GEN_INCR 4 |
175 | |
176 | /* Attribute to indicate thread creation was issued from C11 thrd_create. */ |
177 | #define ATTR_C11_THREAD ((void*)(uintptr_t)-1) |
178 | |
179 | |
180 | /* Condition variable definitions. See __pthread_cond_wait_common. |
181 | Need to be defined here so there is one place from which |
182 | nptl_lock_constants can grab them. */ |
183 | #define __PTHREAD_COND_CLOCK_MONOTONIC_MASK 2 |
184 | #define __PTHREAD_COND_SHARED_MASK 1 |
185 | |
186 | |
187 | /* Internal variables. */ |
188 | |
189 | |
190 | /* Default pthread attributes. */ |
191 | extern struct pthread_attr __default_pthread_attr attribute_hidden; |
192 | extern int __default_pthread_attr_lock attribute_hidden; |
193 | |
194 | /* Size and alignment of static TLS block. */ |
195 | extern size_t __static_tls_size attribute_hidden; |
196 | extern size_t __static_tls_align_m1 attribute_hidden; |
197 | |
198 | /* Flag whether the machine is SMP or not. */ |
199 | extern int __is_smp attribute_hidden; |
200 | |
201 | /* Thread descriptor handling. */ |
202 | extern list_t __stack_user; |
203 | hidden_proto (__stack_user) |
204 | |
205 | /* Attribute handling. */ |
206 | extern struct pthread_attr *__attr_list attribute_hidden; |
207 | extern int __attr_list_lock attribute_hidden; |
208 | |
209 | /* Concurrency handling. */ |
210 | extern int __concurrency_level attribute_hidden; |
211 | |
212 | /* Thread-local data key handling. */ |
213 | extern struct pthread_key_struct __pthread_keys[PTHREAD_KEYS_MAX]; |
214 | hidden_proto (__pthread_keys) |
215 | |
216 | /* Number of threads running. */ |
217 | extern unsigned int __nptl_nthreads attribute_hidden; |
218 | |
219 | #ifndef __ASSUME_SET_ROBUST_LIST |
220 | /* Negative if we do not have the system call and we can use it. */ |
221 | extern int __set_robust_list_avail attribute_hidden; |
222 | #endif |
223 | |
224 | /* Thread Priority Protection. */ |
225 | extern int __sched_fifo_min_prio attribute_hidden; |
226 | extern int __sched_fifo_max_prio attribute_hidden; |
227 | extern void __init_sched_fifo_prio (void) attribute_hidden; |
228 | extern int __pthread_tpp_change_priority (int prev_prio, int new_prio) |
229 | attribute_hidden; |
230 | extern int __pthread_current_priority (void) attribute_hidden; |
231 | |
232 | /* The library can run in debugging mode where it performs a lot more |
233 | tests. */ |
234 | extern int __pthread_debug attribute_hidden; |
235 | /** For now disable debugging support. */ |
236 | #if 0 |
237 | # define DEBUGGING_P __builtin_expect (__pthread_debug, 0) |
238 | # define INVALID_TD_P(pd) (DEBUGGING_P && __find_in_stack_list (pd) == NULL) |
239 | # define INVALID_NOT_TERMINATED_TD_P(pd) INVALID_TD_P (pd) |
240 | #else |
241 | # define DEBUGGING_P 0 |
242 | /* Simplified test. This will not catch all invalid descriptors but |
243 | is better than nothing. And if the test triggers the thread |
244 | descriptor is guaranteed to be invalid. */ |
245 | # define INVALID_TD_P(pd) __builtin_expect ((pd)->tid <= 0, 0) |
246 | # define INVALID_NOT_TERMINATED_TD_P(pd) __builtin_expect ((pd)->tid < 0, 0) |
247 | #endif |
248 | |
249 | |
250 | /* Cancellation test. */ |
251 | #define CANCELLATION_P(self) \ |
252 | do { \ |
253 | int cancelhandling = THREAD_GETMEM (self, cancelhandling); \ |
254 | if (CANCEL_ENABLED_AND_CANCELED (cancelhandling)) \ |
255 | { \ |
256 | THREAD_SETMEM (self, result, PTHREAD_CANCELED); \ |
257 | __do_cancel (); \ |
258 | } \ |
259 | } while (0) |
260 | |
261 | |
262 | extern void __pthread_unwind (__pthread_unwind_buf_t *__buf) |
263 | __cleanup_fct_attribute __attribute ((__noreturn__)) |
264 | #if !defined SHARED && !IS_IN (libpthread) |
265 | weak_function |
266 | #endif |
267 | ; |
268 | extern void __pthread_unwind_next (__pthread_unwind_buf_t *__buf) |
269 | __cleanup_fct_attribute __attribute ((__noreturn__)) |
270 | #ifndef SHARED |
271 | weak_function |
272 | #endif |
273 | ; |
274 | extern void __pthread_register_cancel (__pthread_unwind_buf_t *__buf) |
275 | __cleanup_fct_attribute; |
276 | extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf) |
277 | __cleanup_fct_attribute; |
278 | #if IS_IN (libpthread) |
279 | hidden_proto (__pthread_unwind) |
280 | hidden_proto (__pthread_unwind_next) |
281 | hidden_proto (__pthread_register_cancel) |
282 | hidden_proto (__pthread_unregister_cancel) |
283 | # ifdef SHARED |
284 | extern void attribute_hidden pthread_cancel_init (void); |
285 | # endif |
286 | extern void __nptl_unwind_freeres (void) attribute_hidden; |
287 | #endif |
288 | |
289 | |
290 | /* Called when a thread reacts on a cancellation request. */ |
291 | static inline void |
292 | __attribute ((noreturn, always_inline)) |
293 | __do_cancel (void) |
294 | { |
295 | struct pthread *self = THREAD_SELF; |
296 | |
297 | /* Make sure we get no more cancellations. */ |
298 | THREAD_ATOMIC_BIT_SET (self, cancelhandling, EXITING_BIT); |
299 | |
300 | __pthread_unwind ((__pthread_unwind_buf_t *) |
301 | THREAD_GETMEM (self, cleanup_jmp_buf)); |
302 | } |
303 | |
304 | |
305 | /* Set cancellation mode to asynchronous. */ |
306 | #define CANCEL_ASYNC() \ |
307 | __pthread_enable_asynccancel () |
308 | /* Reset to previous cancellation mode. */ |
309 | #define CANCEL_RESET(oldtype) \ |
310 | __pthread_disable_asynccancel (oldtype) |
311 | |
312 | #if IS_IN (libc) |
313 | /* Same as CANCEL_ASYNC, but for use in libc.so. */ |
314 | # define LIBC_CANCEL_ASYNC() \ |
315 | __libc_enable_asynccancel () |
316 | /* Same as CANCEL_RESET, but for use in libc.so. */ |
317 | # define LIBC_CANCEL_RESET(oldtype) \ |
318 | __libc_disable_asynccancel (oldtype) |
319 | # define LIBC_CANCEL_HANDLED() \ |
320 | __asm (".globl " __SYMBOL_PREFIX "__libc_enable_asynccancel"); \ |
321 | __asm (".globl " __SYMBOL_PREFIX "__libc_disable_asynccancel") |
322 | #elif IS_IN (libpthread) |
323 | # define LIBC_CANCEL_ASYNC() CANCEL_ASYNC () |
324 | # define LIBC_CANCEL_RESET(val) CANCEL_RESET (val) |
325 | # define LIBC_CANCEL_HANDLED() \ |
326 | __asm (".globl " __SYMBOL_PREFIX "__pthread_enable_asynccancel"); \ |
327 | __asm (".globl " __SYMBOL_PREFIX "__pthread_disable_asynccancel") |
328 | #elif IS_IN (librt) |
329 | # define LIBC_CANCEL_ASYNC() \ |
330 | __librt_enable_asynccancel () |
331 | # define LIBC_CANCEL_RESET(val) \ |
332 | __librt_disable_asynccancel (val) |
333 | # define LIBC_CANCEL_HANDLED() \ |
334 | __asm (".globl " __SYMBOL_PREFIX "__librt_enable_asynccancel"); \ |
335 | __asm (".globl " __SYMBOL_PREFIX "__librt_disable_asynccancel") |
336 | #else |
337 | # define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */ |
338 | # define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */ |
339 | # define LIBC_CANCEL_HANDLED() /* Nothing. */ |
340 | #endif |
341 | |
342 | |
343 | /* Internal prototypes. */ |
344 | |
345 | /* Thread list handling. */ |
346 | extern struct pthread *__find_in_stack_list (struct pthread *pd) |
347 | attribute_hidden; |
348 | |
349 | /* Deallocate a thread's stack after optionally making sure the thread |
350 | descriptor is still valid. */ |
351 | extern void __free_tcb (struct pthread *pd) attribute_hidden; |
352 | |
353 | /* Free allocated stack. */ |
354 | extern void __deallocate_stack (struct pthread *pd) attribute_hidden; |
355 | |
356 | /* Mark all the stacks except for the current one as available. This |
357 | function also re-initializes the lock for the stack cache. */ |
358 | extern void __reclaim_stacks (void) attribute_hidden; |
359 | |
360 | /* Make all threads's stacks executable. */ |
361 | extern int __make_stacks_executable (void **stack_endp) attribute_hidden; |
362 | |
363 | /* longjmp handling. */ |
364 | extern void __pthread_cleanup_upto (__jmp_buf target, char *targetframe); |
365 | #if IS_IN (libpthread) |
366 | hidden_proto (__pthread_cleanup_upto) |
367 | #endif |
368 | |
369 | |
370 | /* Functions with versioned interfaces. */ |
371 | extern int __pthread_create_2_1 (pthread_t *newthread, |
372 | const pthread_attr_t *attr, |
373 | void *(*start_routine) (void *), void *arg); |
374 | extern int __pthread_create_2_0 (pthread_t *newthread, |
375 | const pthread_attr_t *attr, |
376 | void *(*start_routine) (void *), void *arg); |
377 | extern int __pthread_attr_init_2_1 (pthread_attr_t *attr); |
378 | extern int __pthread_attr_init_2_0 (pthread_attr_t *attr); |
379 | |
380 | |
381 | /* Event handlers for libthread_db interface. */ |
382 | extern void __nptl_create_event (void); |
383 | extern void __nptl_death_event (void); |
384 | hidden_proto (__nptl_create_event) |
385 | hidden_proto (__nptl_death_event) |
386 | |
387 | /* Register the generation counter in the libpthread with the libc. */ |
388 | #ifdef TLS_MULTIPLE_THREADS_IN_TCB |
389 | extern void __libc_pthread_init (unsigned long int *ptr, |
390 | void (*reclaim) (void), |
391 | const struct pthread_functions *functions); |
392 | #else |
393 | extern int *__libc_pthread_init (unsigned long int *ptr, |
394 | void (*reclaim) (void), |
395 | const struct pthread_functions *functions); |
396 | |
397 | /* Variable set to a nonzero value either if more than one thread runs or ran, |
398 | or if a single-threaded process is trying to cancel itself. See |
399 | nptl/descr.h for more context on the single-threaded process case. */ |
400 | extern int __pthread_multiple_threads attribute_hidden; |
401 | /* Pointer to the corresponding variable in libc. */ |
402 | extern int *__libc_multiple_threads_ptr attribute_hidden; |
403 | #endif |
404 | |
405 | /* Find a thread given its TID. */ |
406 | extern struct pthread *__find_thread_by_id (pid_t tid) attribute_hidden |
407 | #ifdef SHARED |
408 | ; |
409 | #else |
410 | weak_function; |
411 | #define __find_thread_by_id(tid) \ |
412 | (__find_thread_by_id ? (__find_thread_by_id) (tid) : (struct pthread *) NULL) |
413 | #endif |
414 | |
415 | extern void __pthread_init_static_tls (struct link_map *) attribute_hidden; |
416 | |
417 | extern size_t __pthread_get_minstack (const pthread_attr_t *attr); |
418 | |
419 | /* Namespace save aliases. */ |
420 | extern int __pthread_getschedparam (pthread_t thread_id, int *policy, |
421 | struct sched_param *param); |
422 | extern int __pthread_setschedparam (pthread_t thread_id, int policy, |
423 | const struct sched_param *param); |
424 | extern int __pthread_setcancelstate (int state, int *oldstate); |
425 | extern int __pthread_mutex_init (pthread_mutex_t *__mutex, |
426 | const pthread_mutexattr_t *__mutexattr); |
427 | extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex); |
428 | extern int __pthread_mutex_trylock (pthread_mutex_t *_mutex); |
429 | extern int __pthread_mutex_lock (pthread_mutex_t *__mutex); |
430 | extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex, |
431 | const struct timespec *__abstime); |
432 | extern int __pthread_mutex_cond_lock (pthread_mutex_t *__mutex) |
433 | attribute_hidden; |
434 | extern void __pthread_mutex_cond_lock_adjust (pthread_mutex_t *__mutex) |
435 | attribute_hidden; |
436 | extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex); |
437 | extern int __pthread_mutex_unlock_usercnt (pthread_mutex_t *__mutex, |
438 | int __decr) attribute_hidden; |
439 | extern int __pthread_mutexattr_init (pthread_mutexattr_t *attr); |
440 | extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *attr); |
441 | extern int __pthread_mutexattr_settype (pthread_mutexattr_t *attr, int kind); |
442 | extern int __pthread_attr_destroy (pthread_attr_t *attr); |
443 | extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr, |
444 | int *detachstate); |
445 | extern int __pthread_attr_setdetachstate (pthread_attr_t *attr, |
446 | int detachstate); |
447 | extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr, |
448 | int *inherit); |
449 | extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit); |
450 | extern int __pthread_attr_getschedparam (const pthread_attr_t *attr, |
451 | struct sched_param *param); |
452 | extern int __pthread_attr_setschedparam (pthread_attr_t *attr, |
453 | const struct sched_param *param); |
454 | extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr, |
455 | int *policy); |
456 | extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy); |
457 | extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope); |
458 | extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope); |
459 | extern int __pthread_attr_getstackaddr (const pthread_attr_t *__restrict |
460 | __attr, void **__restrict __stackaddr); |
461 | extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr, |
462 | void *__stackaddr); |
463 | extern int __pthread_attr_getstacksize (const pthread_attr_t *__restrict |
464 | __attr, |
465 | size_t *__restrict __stacksize); |
466 | extern int __pthread_attr_setstacksize (pthread_attr_t *__attr, |
467 | size_t __stacksize); |
468 | extern int __pthread_attr_getstack (const pthread_attr_t *__restrict __attr, |
469 | void **__restrict __stackaddr, |
470 | size_t *__restrict __stacksize); |
471 | extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr, |
472 | size_t __stacksize); |
473 | extern int __pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock, |
474 | const pthread_rwlockattr_t *__restrict |
475 | __attr); |
476 | extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock); |
477 | extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock); |
478 | extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock); |
479 | extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock); |
480 | extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock); |
481 | extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock); |
482 | extern int __pthread_cond_broadcast (pthread_cond_t *cond); |
483 | extern int __pthread_cond_destroy (pthread_cond_t *cond); |
484 | extern int __pthread_cond_init (pthread_cond_t *cond, |
485 | const pthread_condattr_t *cond_attr); |
486 | extern int __pthread_cond_signal (pthread_cond_t *cond); |
487 | extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex); |
488 | extern int __pthread_cond_timedwait (pthread_cond_t *cond, |
489 | pthread_mutex_t *mutex, |
490 | const struct timespec *abstime); |
491 | extern int __pthread_condattr_destroy (pthread_condattr_t *attr); |
492 | extern int __pthread_condattr_init (pthread_condattr_t *attr); |
493 | extern int __pthread_key_create (pthread_key_t *key, void (*destr) (void *)); |
494 | extern int __pthread_key_delete (pthread_key_t key); |
495 | extern void *__pthread_getspecific (pthread_key_t key); |
496 | extern int __pthread_setspecific (pthread_key_t key, const void *value); |
497 | extern int __pthread_once (pthread_once_t *once_control, |
498 | void (*init_routine) (void)); |
499 | extern int __pthread_atfork (void (*prepare) (void), void (*parent) (void), |
500 | void (*child) (void)); |
501 | extern pthread_t __pthread_self (void); |
502 | extern int __pthread_equal (pthread_t thread1, pthread_t thread2); |
503 | extern int __pthread_detach (pthread_t th); |
504 | extern int __pthread_cancel (pthread_t th); |
505 | extern int __pthread_kill (pthread_t threadid, int signo); |
506 | extern void __pthread_exit (void *value) __attribute__ ((__noreturn__)); |
507 | extern int __pthread_join (pthread_t threadid, void **thread_return); |
508 | extern int __pthread_setcanceltype (int type, int *oldtype); |
509 | extern int __pthread_enable_asynccancel (void) attribute_hidden; |
510 | extern void __pthread_disable_asynccancel (int oldtype) attribute_hidden; |
511 | extern void __pthread_testcancel (void); |
512 | extern int __pthread_timedjoin_ex (pthread_t, void **, const struct timespec *, |
513 | bool); |
514 | |
515 | #if IS_IN (libpthread) |
516 | hidden_proto (__pthread_mutex_init) |
517 | hidden_proto (__pthread_mutex_destroy) |
518 | hidden_proto (__pthread_mutex_lock) |
519 | hidden_proto (__pthread_mutex_trylock) |
520 | hidden_proto (__pthread_mutex_unlock) |
521 | hidden_proto (__pthread_rwlock_rdlock) |
522 | hidden_proto (__pthread_rwlock_wrlock) |
523 | hidden_proto (__pthread_rwlock_unlock) |
524 | hidden_proto (__pthread_key_create) |
525 | hidden_proto (__pthread_getspecific) |
526 | hidden_proto (__pthread_setspecific) |
527 | hidden_proto (__pthread_once) |
528 | hidden_proto (__pthread_setcancelstate) |
529 | hidden_proto (__pthread_testcancel) |
530 | hidden_proto (__pthread_mutexattr_init) |
531 | hidden_proto (__pthread_mutexattr_settype) |
532 | hidden_proto (__pthread_timedjoin_ex) |
533 | #endif |
534 | |
535 | extern int __pthread_cond_broadcast_2_0 (pthread_cond_2_0_t *cond); |
536 | extern int __pthread_cond_destroy_2_0 (pthread_cond_2_0_t *cond); |
537 | extern int __pthread_cond_init_2_0 (pthread_cond_2_0_t *cond, |
538 | const pthread_condattr_t *cond_attr); |
539 | extern int __pthread_cond_signal_2_0 (pthread_cond_2_0_t *cond); |
540 | extern int __pthread_cond_timedwait_2_0 (pthread_cond_2_0_t *cond, |
541 | pthread_mutex_t *mutex, |
542 | const struct timespec *abstime); |
543 | extern int __pthread_cond_wait_2_0 (pthread_cond_2_0_t *cond, |
544 | pthread_mutex_t *mutex); |
545 | |
546 | extern int __pthread_getaffinity_np (pthread_t th, size_t cpusetsize, |
547 | cpu_set_t *cpuset); |
548 | |
549 | /* The two functions are in libc.so and not exported. */ |
550 | extern int __libc_enable_asynccancel (void) attribute_hidden; |
551 | extern void __libc_disable_asynccancel (int oldtype) attribute_hidden; |
552 | |
553 | |
554 | /* The two functions are in librt.so and not exported. */ |
555 | extern int __librt_enable_asynccancel (void) attribute_hidden; |
556 | extern void __librt_disable_asynccancel (int oldtype) attribute_hidden; |
557 | |
558 | #if IS_IN (libpthread) |
559 | /* Special versions which use non-exported functions. */ |
560 | extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer, |
561 | void (*routine) (void *), void *arg) |
562 | attribute_hidden; |
563 | |
564 | /* Replace cleanup macros defined in <pthread.h> with internal |
565 | versions that don't depend on unwind info and better support |
566 | cancellation. */ |
567 | # undef pthread_cleanup_push |
568 | # define pthread_cleanup_push(routine,arg) \ |
569 | { struct _pthread_cleanup_buffer _buffer; \ |
570 | __pthread_cleanup_push (&_buffer, (routine), (arg)); |
571 | |
572 | extern void __pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer, |
573 | int execute) attribute_hidden; |
574 | # undef pthread_cleanup_pop |
575 | # define pthread_cleanup_pop(execute) \ |
576 | __pthread_cleanup_pop (&_buffer, (execute)); } |
577 | #endif |
578 | |
579 | extern void __pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer, |
580 | void (*routine) (void *), void *arg); |
581 | extern void __pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer, |
582 | int execute); |
583 | |
584 | /* Old cleanup interfaces, still used in libc.so. */ |
585 | extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer, |
586 | void (*routine) (void *), void *arg); |
587 | extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer, |
588 | int execute); |
589 | extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer, |
590 | void (*routine) (void *), void *arg); |
591 | extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer, |
592 | int execute); |
593 | |
594 | extern void __nptl_deallocate_tsd (void) attribute_hidden; |
595 | |
596 | extern void __nptl_setxid_error (struct xid_command *cmdp, int error) |
597 | attribute_hidden; |
598 | extern int __nptl_setxid (struct xid_command *cmdp) attribute_hidden; |
599 | #ifndef SHARED |
600 | extern void __nptl_set_robust (struct pthread *self); |
601 | #endif |
602 | |
603 | extern void __nptl_stacks_freeres (void) attribute_hidden; |
604 | extern void __shm_directory_freeres (void) attribute_hidden; |
605 | |
606 | extern void __wait_lookup_done (void) attribute_hidden; |
607 | |
608 | #ifdef SHARED |
609 | # define PTHREAD_STATIC_FN_REQUIRE(name) |
610 | #else |
611 | # define PTHREAD_STATIC_FN_REQUIRE(name) __asm (".globl " #name); |
612 | #endif |
613 | |
614 | /* Returns 0 if POL is a valid scheduling policy. */ |
615 | static inline int |
616 | check_sched_policy_attr (int pol) |
617 | { |
618 | if (pol == SCHED_OTHER || pol == SCHED_FIFO || pol == SCHED_RR) |
619 | return 0; |
620 | |
621 | return EINVAL; |
622 | } |
623 | |
624 | /* Returns 0 if PR is within the accepted range of priority values for |
625 | the scheduling policy POL or EINVAL otherwise. */ |
626 | static inline int |
627 | check_sched_priority_attr (int pr, int pol) |
628 | { |
629 | int min = __sched_get_priority_min (pol); |
630 | int max = __sched_get_priority_max (pol); |
631 | |
632 | if (min >= 0 && max >= 0 && pr >= min && pr <= max) |
633 | return 0; |
634 | |
635 | return EINVAL; |
636 | } |
637 | |
638 | /* Returns 0 if ST is a valid stack size for a thread stack and EINVAL |
639 | otherwise. */ |
640 | static inline int |
641 | check_stacksize_attr (size_t st) |
642 | { |
643 | if (st >= PTHREAD_STACK_MIN) |
644 | return 0; |
645 | |
646 | return EINVAL; |
647 | } |
648 | |
649 | #define ASSERT_TYPE_SIZE(type, size) \ |
650 | _Static_assert (sizeof (type) == size, \ |
651 | "sizeof (" #type ") != " #size) |
652 | |
653 | #define ASSERT_PTHREAD_INTERNAL_SIZE(type, internal) \ |
654 | _Static_assert (sizeof ((type) { { 0 } }).__size >= sizeof (internal),\ |
655 | "sizeof (" #type ".__size) < sizeof (" #internal ")") |
656 | |
657 | #define ASSERT_PTHREAD_STRING(x) __STRING (x) |
658 | #define ASSERT_PTHREAD_INTERNAL_OFFSET(type, member, offset) \ |
659 | _Static_assert (offsetof (type, member) == offset, \ |
660 | "offset of " #member " field of " #type " != " \ |
661 | ASSERT_PTHREAD_STRING (offset)) |
662 | |
663 | #endif /* pthreadP.h */ |
664 | |