elf: Refactor _dl_update_slotinfo to avoid use after free
[glibc.git] / nptl / pthreadP.h
blob2653e0f1b3444d9156039d0aba2c82ae524e24a8
1 /* Copyright (C) 2002-2021 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, see
17 <https://www.gnu.org/licenses/>. */
19 #ifndef _PTHREADP_H
20 #define _PTHREADP_H 1
22 #define __PTHREAD_NPTL
24 #include <pthread.h>
25 #include <setjmp.h>
26 #include <stdbool.h>
27 #include <sys/syscall.h>
28 #include "descr.h"
29 #include <tls.h>
30 #include <lowlevellock.h>
31 #include <stackinfo.h>
32 #include <internaltypes.h>
33 #include <pthread-functions.h>
34 #include <atomic.h>
35 #include <kernel-features.h>
36 #include <errno.h>
37 #include <internal-signals.h>
38 #include "pthread_mutex_conf.h"
41 /* Atomic operations on TLS memory. */
42 #ifndef THREAD_ATOMIC_CMPXCHG_VAL
43 # define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, new, old) \
44 atomic_compare_and_exchange_val_acq (&(descr)->member, new, old)
45 #endif
47 #ifndef THREAD_ATOMIC_BIT_SET
48 # define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
49 atomic_bit_set (&(descr)->member, bit)
50 #endif
53 static inline short max_adaptive_count (void)
55 #if HAVE_TUNABLES
56 return __mutex_aconf.spin_count;
57 #else
58 return DEFAULT_ADAPTIVE_COUNT;
59 #endif
63 /* Magic cookie representing robust mutex with dead owner. */
64 #define PTHREAD_MUTEX_INCONSISTENT INT_MAX
65 /* Magic cookie representing not recoverable robust mutex. */
66 #define PTHREAD_MUTEX_NOTRECOVERABLE (INT_MAX - 1)
69 /* Internal mutex type value. */
70 enum
72 PTHREAD_MUTEX_KIND_MASK_NP = 3,
74 PTHREAD_MUTEX_ELISION_NP = 256,
75 PTHREAD_MUTEX_NO_ELISION_NP = 512,
77 PTHREAD_MUTEX_ROBUST_NORMAL_NP = 16,
78 PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
79 = PTHREAD_MUTEX_ROBUST_NORMAL_NP | PTHREAD_MUTEX_RECURSIVE_NP,
80 PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
81 = PTHREAD_MUTEX_ROBUST_NORMAL_NP | PTHREAD_MUTEX_ERRORCHECK_NP,
82 PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP
83 = PTHREAD_MUTEX_ROBUST_NORMAL_NP | PTHREAD_MUTEX_ADAPTIVE_NP,
84 PTHREAD_MUTEX_PRIO_INHERIT_NP = 32,
85 PTHREAD_MUTEX_PI_NORMAL_NP
86 = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_NORMAL,
87 PTHREAD_MUTEX_PI_RECURSIVE_NP
88 = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_RECURSIVE_NP,
89 PTHREAD_MUTEX_PI_ERRORCHECK_NP
90 = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ERRORCHECK_NP,
91 PTHREAD_MUTEX_PI_ADAPTIVE_NP
92 = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ADAPTIVE_NP,
93 PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP
94 = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_NORMAL_NP,
95 PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP
96 = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_RECURSIVE_NP,
97 PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP
98 = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP,
99 PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP
100 = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP,
101 PTHREAD_MUTEX_PRIO_PROTECT_NP = 64,
102 PTHREAD_MUTEX_PP_NORMAL_NP
103 = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_NORMAL,
104 PTHREAD_MUTEX_PP_RECURSIVE_NP
105 = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_RECURSIVE_NP,
106 PTHREAD_MUTEX_PP_ERRORCHECK_NP
107 = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_ERRORCHECK_NP,
108 PTHREAD_MUTEX_PP_ADAPTIVE_NP
109 = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_ADAPTIVE_NP,
110 PTHREAD_MUTEX_ELISION_FLAGS_NP
111 = PTHREAD_MUTEX_ELISION_NP | PTHREAD_MUTEX_NO_ELISION_NP,
113 PTHREAD_MUTEX_TIMED_ELISION_NP =
114 PTHREAD_MUTEX_TIMED_NP | PTHREAD_MUTEX_ELISION_NP,
115 PTHREAD_MUTEX_TIMED_NO_ELISION_NP =
116 PTHREAD_MUTEX_TIMED_NP | PTHREAD_MUTEX_NO_ELISION_NP,
118 #define PTHREAD_MUTEX_PSHARED_BIT 128
120 /* See concurrency notes regarding __kind in struct __pthread_mutex_s
121 in sysdeps/nptl/bits/thread-shared-types.h. */
122 #define PTHREAD_MUTEX_TYPE(m) \
123 (atomic_load_relaxed (&((m)->__data.__kind)) & 127)
124 /* Don't include NO_ELISION, as that type is always the same
125 as the underlying lock type. */
126 #define PTHREAD_MUTEX_TYPE_ELISION(m) \
127 (atomic_load_relaxed (&((m)->__data.__kind)) \
128 & (127 | PTHREAD_MUTEX_ELISION_NP))
130 #if LLL_PRIVATE == 0 && LLL_SHARED == 128
131 # define PTHREAD_MUTEX_PSHARED(m) \
132 (atomic_load_relaxed (&((m)->__data.__kind)) & 128)
133 #else
134 # define PTHREAD_MUTEX_PSHARED(m) \
135 ((atomic_load_relaxed (&((m)->__data.__kind)) & 128) \
136 ? LLL_SHARED : LLL_PRIVATE)
137 #endif
139 /* The kernel when waking robust mutexes on exit never uses
140 FUTEX_PRIVATE_FLAG FUTEX_WAKE. */
141 #define PTHREAD_ROBUST_MUTEX_PSHARED(m) LLL_SHARED
143 /* Ceiling in __data.__lock. __data.__lock is signed, so don't
144 use the MSB bit in there, but in the mask also include that bit,
145 so that the compiler can optimize & PTHREAD_MUTEX_PRIO_CEILING_MASK
146 masking if the value is then shifted down by
147 PTHREAD_MUTEX_PRIO_CEILING_SHIFT. */
148 #define PTHREAD_MUTEX_PRIO_CEILING_SHIFT 19
149 #define PTHREAD_MUTEX_PRIO_CEILING_MASK 0xfff80000
152 /* Flags in mutex attr. */
153 #define PTHREAD_MUTEXATTR_PROTOCOL_SHIFT 28
154 #define PTHREAD_MUTEXATTR_PROTOCOL_MASK 0x30000000
155 #define PTHREAD_MUTEXATTR_PRIO_CEILING_SHIFT 12
156 #define PTHREAD_MUTEXATTR_PRIO_CEILING_MASK 0x00fff000
157 #define PTHREAD_MUTEXATTR_FLAG_ROBUST 0x40000000
158 #define PTHREAD_MUTEXATTR_FLAG_PSHARED 0x80000000
159 #define PTHREAD_MUTEXATTR_FLAG_BITS \
160 (PTHREAD_MUTEXATTR_FLAG_ROBUST | PTHREAD_MUTEXATTR_FLAG_PSHARED \
161 | PTHREAD_MUTEXATTR_PROTOCOL_MASK | PTHREAD_MUTEXATTR_PRIO_CEILING_MASK)
164 /* For the following, see pthread_rwlock_common.c. */
165 #define PTHREAD_RWLOCK_WRPHASE 1
166 #define PTHREAD_RWLOCK_WRLOCKED 2
167 #define PTHREAD_RWLOCK_RWAITING 4
168 #define PTHREAD_RWLOCK_READER_SHIFT 3
169 #define PTHREAD_RWLOCK_READER_OVERFLOW ((unsigned int) 1 \
170 << (sizeof (unsigned int) * 8 - 1))
171 #define PTHREAD_RWLOCK_WRHANDOVER ((unsigned int) 1 \
172 << (sizeof (unsigned int) * 8 - 1))
173 #define PTHREAD_RWLOCK_FUTEX_USED 2
176 /* Bits used in robust mutex implementation. */
177 #define FUTEX_WAITERS 0x80000000
178 #define FUTEX_OWNER_DIED 0x40000000
179 #define FUTEX_TID_MASK 0x3fffffff
182 /* pthread_once definitions. See __pthread_once for how these are used. */
183 #define __PTHREAD_ONCE_INPROGRESS 1
184 #define __PTHREAD_ONCE_DONE 2
185 #define __PTHREAD_ONCE_FORK_GEN_INCR 4
187 /* Attribute to indicate thread creation was issued from C11 thrd_create. */
188 #define ATTR_C11_THREAD ((void*)(uintptr_t)-1)
191 /* Condition variable definitions. See __pthread_cond_wait_common.
192 Need to be defined here so there is one place from which
193 nptl_lock_constants can grab them. */
194 #define __PTHREAD_COND_CLOCK_MONOTONIC_MASK 2
195 #define __PTHREAD_COND_SHARED_MASK 1
198 /* Internal variables. */
201 /* Default pthread attributes. */
202 extern union pthread_attr_transparent __default_pthread_attr attribute_hidden;
203 extern int __default_pthread_attr_lock attribute_hidden;
204 /* Called from __libpthread_freeres to deallocate the default attribute. */
205 extern void __default_pthread_attr_freeres (void) attribute_hidden;
207 /* Size and alignment of static TLS block. */
208 extern size_t __static_tls_size attribute_hidden;
209 extern size_t __static_tls_align_m1 attribute_hidden;
211 /* Attribute handling. */
212 extern struct pthread_attr *__attr_list attribute_hidden;
213 extern int __attr_list_lock attribute_hidden;
215 /* Concurrency handling. */
216 extern int __concurrency_level attribute_hidden;
218 /* Thread-local data key handling. */
219 extern struct pthread_key_struct __pthread_keys[PTHREAD_KEYS_MAX];
220 hidden_proto (__pthread_keys)
222 /* Number of threads running. */
223 extern unsigned int __nptl_nthreads attribute_hidden;
225 #ifndef __ASSUME_SET_ROBUST_LIST
226 /* Negative if we do not have the system call and we can use it. */
227 extern int __set_robust_list_avail attribute_hidden;
228 #endif
230 /* Thread Priority Protection. */
231 extern int __sched_fifo_min_prio attribute_hidden;
232 extern int __sched_fifo_max_prio attribute_hidden;
233 extern void __init_sched_fifo_prio (void) attribute_hidden;
234 extern int __pthread_tpp_change_priority (int prev_prio, int new_prio)
235 attribute_hidden;
236 extern int __pthread_current_priority (void) attribute_hidden;
238 /* The library can run in debugging mode where it performs a lot more
239 tests. */
240 extern int __pthread_debug attribute_hidden;
241 /** For now disable debugging support. */
242 #if 0
243 # define DEBUGGING_P __builtin_expect (__pthread_debug, 0)
244 # define INVALID_TD_P(pd) (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
245 # define INVALID_NOT_TERMINATED_TD_P(pd) INVALID_TD_P (pd)
246 #else
247 # define DEBUGGING_P 0
248 /* Simplified test. This will not catch all invalid descriptors but
249 is better than nothing. And if the test triggers the thread
250 descriptor is guaranteed to be invalid. */
251 # define INVALID_TD_P(pd) __builtin_expect ((pd)->tid <= 0, 0)
252 # define INVALID_NOT_TERMINATED_TD_P(pd) __builtin_expect ((pd)->tid < 0, 0)
253 #endif
256 /* Cancellation test. */
257 #define CANCELLATION_P(self) \
258 do { \
259 int cancelhandling = THREAD_GETMEM (self, cancelhandling); \
260 if (CANCEL_ENABLED_AND_CANCELED (cancelhandling)) \
262 THREAD_SETMEM (self, result, PTHREAD_CANCELED); \
263 __do_cancel (); \
265 } while (0)
268 extern void __pthread_unwind (__pthread_unwind_buf_t *__buf)
269 __cleanup_fct_attribute __attribute ((__noreturn__))
270 #if !defined SHARED && !IS_IN (libpthread)
271 weak_function
272 #endif
274 extern void __pthread_unwind_next (__pthread_unwind_buf_t *__buf)
275 __cleanup_fct_attribute __attribute ((__noreturn__))
276 #ifndef SHARED
277 weak_function
278 #endif
280 extern void __pthread_register_cancel (__pthread_unwind_buf_t *__buf)
281 __cleanup_fct_attribute;
282 extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf)
283 __cleanup_fct_attribute;
284 #if IS_IN (libpthread)
285 hidden_proto (__pthread_unwind)
286 hidden_proto (__pthread_unwind_next)
287 hidden_proto (__pthread_register_cancel)
288 hidden_proto (__pthread_unregister_cancel)
289 # ifdef SHARED
290 /* The difference from __libc_unwind_link_get is that here, errors
291 terminate the process. */
292 struct unwind_link ;
293 struct unwind_link *__pthread_unwind_link_get (void) attribute_hidden;
294 # endif
295 #endif
298 /* Called when a thread reacts on a cancellation request. */
299 static inline void
300 __attribute ((noreturn, always_inline))
301 __do_cancel (void)
303 struct pthread *self = THREAD_SELF;
305 /* Make sure we get no more cancellations. */
306 THREAD_ATOMIC_BIT_SET (self, cancelhandling, EXITING_BIT);
308 __pthread_unwind ((__pthread_unwind_buf_t *)
309 THREAD_GETMEM (self, cleanup_jmp_buf));
313 /* Internal prototypes. */
315 /* Thread list handling. */
316 extern struct pthread *__find_in_stack_list (struct pthread *pd)
317 attribute_hidden;
319 /* Deallocate a thread's stack after optionally making sure the thread
320 descriptor is still valid. */
321 extern void __free_tcb (struct pthread *pd) attribute_hidden;
323 /* Free allocated stack. */
324 extern void __deallocate_stack (struct pthread *pd) attribute_hidden;
326 /* Mark all the stacks except for the current one as available. This
327 function also re-initializes the lock for the stack cache. */
328 extern void __reclaim_stacks (void) attribute_hidden;
330 /* Make all threads's stacks executable. */
331 extern int __make_stacks_executable (void **stack_endp) attribute_hidden;
333 /* longjmp handling. */
334 extern void __pthread_cleanup_upto (__jmp_buf target, char *targetframe);
335 #if IS_IN (libpthread)
336 hidden_proto (__pthread_cleanup_upto)
337 #endif
340 /* Functions with versioned interfaces. */
341 extern int __pthread_create_2_1 (pthread_t *newthread,
342 const pthread_attr_t *attr,
343 void *(*start_routine) (void *), void *arg);
344 extern int __pthread_create_2_0 (pthread_t *newthread,
345 const pthread_attr_t *attr,
346 void *(*start_routine) (void *), void *arg);
347 extern int __pthread_attr_init (pthread_attr_t *attr);
348 libc_hidden_proto (__pthread_attr_init)
349 extern int __pthread_attr_init_2_0 (pthread_attr_t *attr);
352 /* Event handlers for libthread_db interface. */
353 extern void __nptl_create_event (void);
354 extern void __nptl_death_event (void);
355 hidden_proto (__nptl_create_event)
356 hidden_proto (__nptl_death_event)
358 /* The fork generation counter, defined in libpthread. */
359 extern unsigned long int __fork_generation attribute_hidden;
361 /* Pointer to the fork generation counter in the thread library. */
362 extern unsigned long int *__fork_generation_pointer attribute_hidden;
364 /* Register the generation counter in the libpthread with the libc. */
365 #ifdef TLS_MULTIPLE_THREADS_IN_TCB
366 extern void __libc_pthread_init (unsigned long int *ptr,
367 void (*reclaim) (void),
368 const struct pthread_functions *functions);
369 #else
370 extern int *__libc_pthread_init (unsigned long int *ptr,
371 void (*reclaim) (void),
372 const struct pthread_functions *functions);
374 /* Variable set to a nonzero value either if more than one thread runs or ran,
375 or if a single-threaded process is trying to cancel itself. See
376 nptl/descr.h for more context on the single-threaded process case. */
377 extern int __pthread_multiple_threads attribute_hidden;
378 /* Pointer to the corresponding variable in libc. */
379 extern int *__libc_multiple_threads_ptr attribute_hidden;
380 #endif
382 extern void __pthread_init_static_tls (struct link_map *) attribute_hidden;
384 extern size_t __pthread_get_minstack (const pthread_attr_t *attr);
386 /* Namespace save aliases. */
387 extern int __pthread_getschedparam (pthread_t thread_id, int *policy,
388 struct sched_param *param);
389 extern int __pthread_setschedparam (pthread_t thread_id, int policy,
390 const struct sched_param *param);
391 extern int __pthread_setcancelstate (int state, int *oldstate);
392 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
393 const pthread_mutexattr_t *__mutexattr);
394 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
395 extern int __pthread_mutex_trylock (pthread_mutex_t *_mutex);
396 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
397 extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex,
398 const struct timespec *__abstime);
399 extern int __pthread_mutex_cond_lock (pthread_mutex_t *__mutex)
400 attribute_hidden;
401 extern void __pthread_mutex_cond_lock_adjust (pthread_mutex_t *__mutex)
402 attribute_hidden;
403 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
404 extern int __pthread_mutex_unlock_usercnt (pthread_mutex_t *__mutex,
405 int __decr) attribute_hidden;
406 extern int __pthread_mutexattr_init (pthread_mutexattr_t *attr);
407 extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *attr);
408 extern int __pthread_mutexattr_settype (pthread_mutexattr_t *attr, int kind);
409 extern int __pthread_attr_destroy (pthread_attr_t *attr);
410 libc_hidden_proto (__pthread_attr_destroy)
411 extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
412 int *detachstate);
413 extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
414 int detachstate);
415 extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
416 int *inherit);
417 extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
418 extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
419 struct sched_param *param);
420 extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
421 const struct sched_param *param);
422 extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
423 int *policy);
424 extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
425 extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
426 extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
427 extern int __pthread_attr_getstackaddr (const pthread_attr_t *__restrict
428 __attr, void **__restrict __stackaddr);
429 extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
430 void *__stackaddr);
431 extern int __pthread_attr_getstacksize (const pthread_attr_t *__restrict
432 __attr,
433 size_t *__restrict __stacksize);
434 extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
435 size_t __stacksize);
436 extern int __pthread_attr_getstack (const pthread_attr_t *__restrict __attr,
437 void **__restrict __stackaddr,
438 size_t *__restrict __stacksize);
439 extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
440 size_t __stacksize);
441 int __pthread_attr_setaffinity_np (pthread_attr_t *, size_t, const cpu_set_t *);
442 libc_hidden_proto (__pthread_attr_setaffinity_np)
443 extern __typeof (pthread_getattr_default_np) __pthread_getattr_default_np;
444 libpthread_hidden_proto (__pthread_getattr_default_np)
445 extern int __pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
446 const pthread_rwlockattr_t *__restrict
447 __attr);
448 extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
449 extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
450 extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
451 extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
452 extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
453 extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
454 extern int __pthread_cond_broadcast (pthread_cond_t *cond);
455 extern int __pthread_cond_destroy (pthread_cond_t *cond);
456 libc_hidden_proto (__pthread_cond_destroy)
457 extern int __pthread_cond_init (pthread_cond_t *cond,
458 const pthread_condattr_t *cond_attr);
459 libc_hidden_proto (__pthread_cond_init)
460 extern int __pthread_cond_signal (pthread_cond_t *cond);
461 extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
463 #if __TIMESIZE == 64
464 # define __pthread_clockjoin_np64 __pthread_clockjoin_np
465 # define __pthread_timedjoin_np64 __pthread_timedjoin_np
466 # define __pthread_cond_timedwait64 __pthread_cond_timedwait
467 # define __pthread_cond_clockwait64 __pthread_cond_clockwait
468 # define __pthread_rwlock_clockrdlock64 __pthread_rwlock_clockrdlock
469 # define __pthread_rwlock_clockwrlock64 __pthread_rwlock_clockwrlock
470 # define __pthread_rwlock_timedrdlock64 __pthread_rwlock_timedrdlock
471 # define __pthread_rwlock_timedwrlock64 __pthread_rwlock_timedwrlock
472 # define __pthread_mutex_clocklock64 __pthread_mutex_clocklock
473 # define __pthread_mutex_timedlock64 __pthread_mutex_timedlock
474 #else
475 extern int __pthread_clockjoin_np64 (pthread_t threadid, void **thread_return,
476 clockid_t clockid,
477 const struct __timespec64 *abstime);
478 libpthread_hidden_proto (__pthread_clockjoin_np64)
479 extern int __pthread_timedjoin_np64 (pthread_t threadid, void **thread_return,
480 const struct __timespec64 *abstime);
481 libpthread_hidden_proto (__pthread_timedjoin_np64)
482 extern int __pthread_cond_timedwait64 (pthread_cond_t *cond,
483 pthread_mutex_t *mutex,
484 const struct __timespec64 *abstime);
485 libpthread_hidden_proto (__pthread_cond_timedwait64)
486 extern int __pthread_cond_clockwait64 (pthread_cond_t *cond,
487 pthread_mutex_t *mutex,
488 clockid_t clockid,
489 const struct __timespec64 *abstime);
490 libpthread_hidden_proto (__pthread_cond_clockwait64)
491 extern int __pthread_rwlock_clockrdlock64 (pthread_rwlock_t *rwlock,
492 clockid_t clockid,
493 const struct __timespec64 *abstime);
494 libpthread_hidden_proto (__pthread_rwlock_clockrdlock64)
495 extern int __pthread_rwlock_clockwrlock64 (pthread_rwlock_t *rwlock,
496 clockid_t clockid,
497 const struct __timespec64 *abstime);
498 libpthread_hidden_proto (__pthread_rwlock_clockwrlock64)
499 extern int __pthread_rwlock_timedrdlock64 (pthread_rwlock_t *rwlock,
500 const struct __timespec64 *abstime);
501 libpthread_hidden_proto (__pthread_rwlock_timedrdlock64)
502 extern int __pthread_rwlock_timedwrlock64 (pthread_rwlock_t *rwlock,
503 const struct __timespec64 *abstime);
504 libpthread_hidden_proto (__pthread_rwlock_timedwrlock64)
505 extern int __pthread_mutex_clocklock64 (pthread_mutex_t *mutex,
506 clockid_t clockid,
507 const struct __timespec64 *abstime);
508 libpthread_hidden_proto (__pthread_mutex_clocklock64)
509 extern int __pthread_mutex_timedlock64 (pthread_mutex_t *mutex,
510 const struct __timespec64 *abstime);
511 libpthread_hidden_proto (__pthread_mutex_timedlock64)
512 #endif
514 extern int __pthread_cond_timedwait (pthread_cond_t *cond,
515 pthread_mutex_t *mutex,
516 const struct timespec *abstime);
517 extern int __pthread_cond_clockwait (pthread_cond_t *cond,
518 pthread_mutex_t *mutex,
519 clockid_t clockid,
520 const struct timespec *abstime)
521 __nonnull ((1, 2, 4));
522 extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
523 extern int __pthread_condattr_init (pthread_condattr_t *attr);
524 extern int __pthread_key_create (pthread_key_t *key, void (*destr) (void *));
525 extern int __pthread_key_delete (pthread_key_t key);
526 extern void *__pthread_getspecific (pthread_key_t key);
527 extern int __pthread_setspecific (pthread_key_t key, const void *value);
528 extern int __pthread_once (pthread_once_t *once_control,
529 void (*init_routine) (void));
530 extern int __pthread_atfork (void (*prepare) (void), void (*parent) (void),
531 void (*child) (void));
532 extern pthread_t __pthread_self (void);
533 extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
534 extern int __pthread_detach (pthread_t th);
535 extern int __pthread_cancel (pthread_t th);
536 extern int __pthread_kill (pthread_t threadid, int signo);
537 extern void __pthread_exit (void *value) __attribute__ ((__noreturn__));
538 extern int __pthread_join (pthread_t threadid, void **thread_return);
539 extern int __pthread_setcanceltype (int type, int *oldtype);
540 extern int __pthread_enable_asynccancel (void) attribute_hidden;
541 extern void __pthread_disable_asynccancel (int oldtype) attribute_hidden;
542 extern void __pthread_testcancel (void);
543 extern int __pthread_clockjoin_ex (pthread_t, void **, clockid_t,
544 const struct __timespec64 *, bool)
545 attribute_hidden;
546 extern int __pthread_sigmask (int, const sigset_t *, sigset_t *);
547 libc_hidden_proto (__pthread_sigmask);
550 #if IS_IN (libpthread)
551 hidden_proto (__pthread_mutex_init)
552 hidden_proto (__pthread_mutex_destroy)
553 hidden_proto (__pthread_mutex_lock)
554 hidden_proto (__pthread_mutex_trylock)
555 hidden_proto (__pthread_mutex_unlock)
556 hidden_proto (__pthread_rwlock_rdlock)
557 hidden_proto (__pthread_rwlock_wrlock)
558 hidden_proto (__pthread_rwlock_unlock)
559 hidden_proto (__pthread_key_create)
560 hidden_proto (__pthread_getspecific)
561 hidden_proto (__pthread_setspecific)
562 hidden_proto (__pthread_once)
563 hidden_proto (__pthread_setcancelstate)
564 hidden_proto (__pthread_testcancel)
565 hidden_proto (__pthread_mutexattr_init)
566 hidden_proto (__pthread_mutexattr_settype)
567 #endif
569 extern int __pthread_cond_broadcast_2_0 (pthread_cond_2_0_t *cond);
570 extern int __pthread_cond_destroy_2_0 (pthread_cond_2_0_t *cond);
571 extern int __pthread_cond_init_2_0 (pthread_cond_2_0_t *cond,
572 const pthread_condattr_t *cond_attr);
573 extern int __pthread_cond_signal_2_0 (pthread_cond_2_0_t *cond);
574 extern int __pthread_cond_timedwait_2_0 (pthread_cond_2_0_t *cond,
575 pthread_mutex_t *mutex,
576 const struct timespec *abstime);
577 extern int __pthread_cond_wait_2_0 (pthread_cond_2_0_t *cond,
578 pthread_mutex_t *mutex);
580 extern int __pthread_getaffinity_np (pthread_t th, size_t cpusetsize,
581 cpu_set_t *cpuset);
582 libc_hidden_proto (__pthread_getaffinity_np)
584 /* Special internal version of pthread_attr_setsigmask_np which does
585 not filter out internal signals from *SIGMASK. This can be used to
586 launch threads with internal signals blocked. */
587 extern int __pthread_attr_setsigmask_internal (pthread_attr_t *attr,
588 const sigset_t *sigmask);
589 libc_hidden_proto (__pthread_attr_setsigmask_internal)
591 extern __typeof (pthread_attr_getsigmask_np) __pthread_attr_getsigmask_np;
592 libc_hidden_proto (__pthread_attr_getsigmask_np)
594 #if IS_IN (libpthread)
595 /* Special versions which use non-exported functions. */
596 extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
597 void (*routine) (void *), void *arg)
598 attribute_hidden;
600 /* Replace cleanup macros defined in <pthread.h> with internal
601 versions that don't depend on unwind info and better support
602 cancellation. */
603 # undef pthread_cleanup_push
604 # define pthread_cleanup_push(routine,arg) \
605 { struct _pthread_cleanup_buffer _buffer; \
606 __pthread_cleanup_push (&_buffer, (routine), (arg));
608 extern void __pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
609 int execute) attribute_hidden;
610 # undef pthread_cleanup_pop
611 # define pthread_cleanup_pop(execute) \
612 __pthread_cleanup_pop (&_buffer, (execute)); }
614 # if defined __EXCEPTIONS && !defined __cplusplus
615 /* Structure to hold the cleanup handler information. */
616 struct __pthread_cleanup_combined_frame
618 void (*__cancel_routine) (void *);
619 void *__cancel_arg;
620 int __do_it;
621 struct _pthread_cleanup_buffer __buffer;
624 /* Special cleanup macros which register cleanup both using
625 __pthread_cleanup_{push,pop} and using cleanup attribute. This is needed
626 for pthread_once, so that it supports both throwing exceptions from the
627 pthread_once callback (only cleanup attribute works there) and cancellation
628 of the thread running the callback if the callback or some routines it
629 calls don't have unwind information. */
631 static __always_inline void
632 __pthread_cleanup_combined_routine (struct __pthread_cleanup_combined_frame
633 *__frame)
635 if (__frame->__do_it)
637 __frame->__cancel_routine (__frame->__cancel_arg);
638 __frame->__do_it = 0;
639 __pthread_cleanup_pop (&__frame->__buffer, 0);
643 static inline void
644 __pthread_cleanup_combined_routine_voidptr (void *__arg)
646 struct __pthread_cleanup_combined_frame *__frame
647 = (struct __pthread_cleanup_combined_frame *) __arg;
648 if (__frame->__do_it)
650 __frame->__cancel_routine (__frame->__cancel_arg);
651 __frame->__do_it = 0;
655 # define pthread_cleanup_combined_push(routine, arg) \
656 do { \
657 void (*__cancel_routine) (void *) = (routine); \
658 struct __pthread_cleanup_combined_frame __clframe \
659 __attribute__ ((__cleanup__ (__pthread_cleanup_combined_routine))) \
660 = { .__cancel_routine = __cancel_routine, .__cancel_arg = (arg), \
661 .__do_it = 1 }; \
662 __pthread_cleanup_push (&__clframe.__buffer, \
663 __pthread_cleanup_combined_routine_voidptr, \
664 &__clframe);
666 # define pthread_cleanup_combined_pop(execute) \
667 __pthread_cleanup_pop (&__clframe.__buffer, 0); \
668 __clframe.__do_it = 0; \
669 if (execute) \
670 __cancel_routine (__clframe.__cancel_arg); \
671 } while (0)
673 # endif
674 #endif
676 extern void __pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
677 void (*routine) (void *), void *arg);
678 extern void __pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
679 int execute);
681 /* Old cleanup interfaces, still used in libc.so. */
682 extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
683 void (*routine) (void *), void *arg);
684 extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
685 int execute);
686 extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
687 void (*routine) (void *), void *arg);
688 extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
689 int execute);
691 extern void __nptl_deallocate_tsd (void) attribute_hidden;
693 extern void __nptl_setxid_error (struct xid_command *cmdp, int error)
694 attribute_hidden;
695 extern int __nptl_setxid (struct xid_command *cmdp) attribute_hidden;
696 #ifndef SHARED
697 extern void __nptl_set_robust (struct pthread *self);
698 #endif
700 extern void __nptl_stacks_freeres (void) attribute_hidden;
702 extern void __wait_lookup_done (void) attribute_hidden;
704 /* Allocates the extension space for ATTR. Returns an error code on
705 memory allocation failure, zero on success. If ATTR already has an
706 extension space, this function does nothing. */
707 int __pthread_attr_extension (struct pthread_attr *attr) attribute_hidden
708 __attribute_warn_unused_result__;
710 #ifdef SHARED
711 # define PTHREAD_STATIC_FN_REQUIRE(name)
712 #else
713 # define PTHREAD_STATIC_FN_REQUIRE(name) __asm (".globl " #name);
714 #endif
716 /* Make a deep copy of the attribute *SOURCE in *TARGET. *TARGET is
717 not assumed to have been initialized. Returns 0 on success, or a
718 positive error code otherwise. */
719 int __pthread_attr_copy (pthread_attr_t *target, const pthread_attr_t *source);
720 libc_hidden_proto (__pthread_attr_copy)
722 /* Returns 0 if POL is a valid scheduling policy. */
723 static inline int
724 check_sched_policy_attr (int pol)
726 if (pol == SCHED_OTHER || pol == SCHED_FIFO || pol == SCHED_RR)
727 return 0;
729 return EINVAL;
732 /* Returns 0 if PR is within the accepted range of priority values for
733 the scheduling policy POL or EINVAL otherwise. */
734 static inline int
735 check_sched_priority_attr (int pr, int pol)
737 int min = __sched_get_priority_min (pol);
738 int max = __sched_get_priority_max (pol);
740 if (min >= 0 && max >= 0 && pr >= min && pr <= max)
741 return 0;
743 return EINVAL;
746 /* Returns 0 if ST is a valid stack size for a thread stack and EINVAL
747 otherwise. */
748 static inline int
749 check_stacksize_attr (size_t st)
751 if (st >= PTHREAD_STACK_MIN)
752 return 0;
754 return EINVAL;
757 #define ASSERT_TYPE_SIZE(type, size) \
758 _Static_assert (sizeof (type) == size, \
759 "sizeof (" #type ") != " #size)
761 #define ASSERT_PTHREAD_INTERNAL_SIZE(type, internal) \
762 _Static_assert (sizeof ((type) { { 0 } }).__size >= sizeof (internal),\
763 "sizeof (" #type ".__size) < sizeof (" #internal ")")
765 #define ASSERT_PTHREAD_STRING(x) __STRING (x)
766 #define ASSERT_PTHREAD_INTERNAL_OFFSET(type, member, offset) \
767 _Static_assert (offsetof (type, member) == offset, \
768 "offset of " #member " field of " #type " != " \
769 ASSERT_PTHREAD_STRING (offset))
770 #define ASSERT_PTHREAD_INTERNAL_MEMBER_SIZE(type, member, mtype) \
771 _Static_assert (sizeof (((type) { 0 }).member) != 8, \
772 "sizeof (" #type "." #member ") != sizeof (" #mtype "))")
774 #endif /* pthreadP.h */