Update copyright dates not handled by scripts/update-copyrights.
[glibc.git] / sysdeps / nptl / pthreadP.h
blob708bd924695a31e15c8efcc761b5164530efc8fe
1 /* Copyright (C) 2002-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 #ifndef _PTHREADP_H
19 #define _PTHREADP_H 1
21 #define __PTHREAD_NPTL
23 #include <pthread.h>
24 #include <setjmp.h>
25 #include <stdbool.h>
26 #include <sys/syscall.h>
27 #include <nptl/descr.h>
28 #include <tls.h>
29 #include <lowlevellock.h>
30 #include <stackinfo.h>
31 #include <internaltypes.h>
32 #include <atomic.h>
33 #include <kernel-features.h>
34 #include <errno.h>
35 #include <internal-signals.h>
36 #include "pthread_mutex_conf.h"
39 /* Atomic operations on TLS memory. */
40 #ifndef THREAD_ATOMIC_CMPXCHG_VAL
41 # define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, new, old) \
42 atomic_compare_and_exchange_val_acq (&(descr)->member, new, old)
43 #endif
45 #ifndef THREAD_ATOMIC_BIT_SET
46 # define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
47 atomic_bit_set (&(descr)->member, bit)
48 #endif
51 static inline short max_adaptive_count (void)
53 #if HAVE_TUNABLES
54 return __mutex_aconf.spin_count;
55 #else
56 return DEFAULT_ADAPTIVE_COUNT;
57 #endif
61 /* Magic cookie representing robust mutex with dead owner. */
62 #define PTHREAD_MUTEX_INCONSISTENT INT_MAX
63 /* Magic cookie representing not recoverable robust mutex. */
64 #define PTHREAD_MUTEX_NOTRECOVERABLE (INT_MAX - 1)
67 /* Internal mutex type value. */
68 enum
70 PTHREAD_MUTEX_KIND_MASK_NP = 3,
72 PTHREAD_MUTEX_ELISION_NP = 256,
73 PTHREAD_MUTEX_NO_ELISION_NP = 512,
75 PTHREAD_MUTEX_ROBUST_NORMAL_NP = 16,
76 PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
77 = PTHREAD_MUTEX_ROBUST_NORMAL_NP | PTHREAD_MUTEX_RECURSIVE_NP,
78 PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
79 = PTHREAD_MUTEX_ROBUST_NORMAL_NP | PTHREAD_MUTEX_ERRORCHECK_NP,
80 PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP
81 = PTHREAD_MUTEX_ROBUST_NORMAL_NP | PTHREAD_MUTEX_ADAPTIVE_NP,
82 PTHREAD_MUTEX_PRIO_INHERIT_NP = 32,
83 PTHREAD_MUTEX_PI_NORMAL_NP
84 = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_NORMAL,
85 PTHREAD_MUTEX_PI_RECURSIVE_NP
86 = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_RECURSIVE_NP,
87 PTHREAD_MUTEX_PI_ERRORCHECK_NP
88 = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ERRORCHECK_NP,
89 PTHREAD_MUTEX_PI_ADAPTIVE_NP
90 = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ADAPTIVE_NP,
91 PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP
92 = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_NORMAL_NP,
93 PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP
94 = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_RECURSIVE_NP,
95 PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP
96 = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP,
97 PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP
98 = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP,
99 PTHREAD_MUTEX_PRIO_PROTECT_NP = 64,
100 PTHREAD_MUTEX_PP_NORMAL_NP
101 = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_NORMAL,
102 PTHREAD_MUTEX_PP_RECURSIVE_NP
103 = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_RECURSIVE_NP,
104 PTHREAD_MUTEX_PP_ERRORCHECK_NP
105 = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_ERRORCHECK_NP,
106 PTHREAD_MUTEX_PP_ADAPTIVE_NP
107 = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_ADAPTIVE_NP,
108 PTHREAD_MUTEX_ELISION_FLAGS_NP
109 = PTHREAD_MUTEX_ELISION_NP | PTHREAD_MUTEX_NO_ELISION_NP,
111 PTHREAD_MUTEX_TIMED_ELISION_NP =
112 PTHREAD_MUTEX_TIMED_NP | PTHREAD_MUTEX_ELISION_NP,
113 PTHREAD_MUTEX_TIMED_NO_ELISION_NP =
114 PTHREAD_MUTEX_TIMED_NP | PTHREAD_MUTEX_NO_ELISION_NP,
116 #define PTHREAD_MUTEX_PSHARED_BIT 128
118 /* See concurrency notes regarding __kind in struct __pthread_mutex_s
119 in sysdeps/nptl/bits/thread-shared-types.h. */
120 #define PTHREAD_MUTEX_TYPE(m) \
121 (atomic_load_relaxed (&((m)->__data.__kind)) & 127)
122 /* Don't include NO_ELISION, as that type is always the same
123 as the underlying lock type. */
124 #define PTHREAD_MUTEX_TYPE_ELISION(m) \
125 (atomic_load_relaxed (&((m)->__data.__kind)) \
126 & (127 | PTHREAD_MUTEX_ELISION_NP))
128 #if LLL_PRIVATE == 0 && LLL_SHARED == 128
129 # define PTHREAD_MUTEX_PSHARED(m) \
130 (atomic_load_relaxed (&((m)->__data.__kind)) & 128)
131 #else
132 # define PTHREAD_MUTEX_PSHARED(m) \
133 ((atomic_load_relaxed (&((m)->__data.__kind)) & 128) \
134 ? LLL_SHARED : LLL_PRIVATE)
135 #endif
137 /* The kernel when waking robust mutexes on exit never uses
138 FUTEX_PRIVATE_FLAG FUTEX_WAKE. */
139 #define PTHREAD_ROBUST_MUTEX_PSHARED(m) LLL_SHARED
141 /* Ceiling in __data.__lock. __data.__lock is signed, so don't
142 use the MSB bit in there, but in the mask also include that bit,
143 so that the compiler can optimize & PTHREAD_MUTEX_PRIO_CEILING_MASK
144 masking if the value is then shifted down by
145 PTHREAD_MUTEX_PRIO_CEILING_SHIFT. */
146 #define PTHREAD_MUTEX_PRIO_CEILING_SHIFT 19
147 #define PTHREAD_MUTEX_PRIO_CEILING_MASK 0xfff80000
150 /* Flags in mutex attr. */
151 #define PTHREAD_MUTEXATTR_PROTOCOL_SHIFT 28
152 #define PTHREAD_MUTEXATTR_PROTOCOL_MASK 0x30000000
153 #define PTHREAD_MUTEXATTR_PRIO_CEILING_SHIFT 12
154 #define PTHREAD_MUTEXATTR_PRIO_CEILING_MASK 0x00fff000
155 #define PTHREAD_MUTEXATTR_FLAG_ROBUST 0x40000000
156 #define PTHREAD_MUTEXATTR_FLAG_PSHARED 0x80000000
157 #define PTHREAD_MUTEXATTR_FLAG_BITS \
158 (PTHREAD_MUTEXATTR_FLAG_ROBUST | PTHREAD_MUTEXATTR_FLAG_PSHARED \
159 | PTHREAD_MUTEXATTR_PROTOCOL_MASK | PTHREAD_MUTEXATTR_PRIO_CEILING_MASK)
162 /* For the following, see pthread_rwlock_common.c. */
163 #define PTHREAD_RWLOCK_WRPHASE 1
164 #define PTHREAD_RWLOCK_WRLOCKED 2
165 #define PTHREAD_RWLOCK_RWAITING 4
166 #define PTHREAD_RWLOCK_READER_SHIFT 3
167 #define PTHREAD_RWLOCK_READER_OVERFLOW ((unsigned int) 1 \
168 << (sizeof (unsigned int) * 8 - 1))
169 #define PTHREAD_RWLOCK_WRHANDOVER ((unsigned int) 1 \
170 << (sizeof (unsigned int) * 8 - 1))
171 #define PTHREAD_RWLOCK_FUTEX_USED 2
174 /* Bits used in robust mutex implementation. */
175 #define FUTEX_WAITERS 0x80000000
176 #define FUTEX_OWNER_DIED 0x40000000
177 #define FUTEX_TID_MASK 0x3fffffff
180 /* pthread_once definitions. See __pthread_once for how these are used. */
181 #define __PTHREAD_ONCE_INPROGRESS 1
182 #define __PTHREAD_ONCE_DONE 2
183 #define __PTHREAD_ONCE_FORK_GEN_INCR 4
185 /* Attribute to indicate thread creation was issued from C11 thrd_create. */
186 #define ATTR_C11_THREAD ((void*)(uintptr_t)-1)
189 /* Condition variable definitions. See __pthread_cond_wait_common.
190 Need to be defined here so there is one place from which
191 nptl_lock_constants can grab them. */
192 #define __PTHREAD_COND_CLOCK_MONOTONIC_MASK 2
193 #define __PTHREAD_COND_SHARED_MASK 1
196 /* Internal variables. */
199 /* Default pthread attributes. */
200 extern union pthread_attr_transparent __default_pthread_attr;
201 libc_hidden_proto (__default_pthread_attr)
202 extern int __default_pthread_attr_lock;
203 libc_hidden_proto (__default_pthread_attr_lock)
204 /* Called from __libc_freeres to deallocate the default attribute. */
205 extern void __default_pthread_attr_freeres (void) attribute_hidden;
207 /* Attribute handling. */
208 extern struct pthread_attr *__attr_list attribute_hidden;
209 extern int __attr_list_lock attribute_hidden;
211 /* Concurrency handling. */
212 extern int __concurrency_level attribute_hidden;
214 /* Thread-local data key handling. */
215 extern struct pthread_key_struct __pthread_keys[PTHREAD_KEYS_MAX];
216 libc_hidden_proto (__pthread_keys)
218 /* Number of threads running. */
219 extern unsigned int __nptl_nthreads;
220 libc_hidden_proto (__nptl_nthreads)
222 #ifndef __ASSUME_SET_ROBUST_LIST
223 /* True if the set_robust_list system call works. Initialized in
224 __tls_init_tp. */
225 extern bool __nptl_set_robust_list_avail;
226 rtld_hidden_proto (__nptl_set_robust_list_avail)
227 #endif
229 /* Thread Priority Protection. */
230 extern int __sched_fifo_min_prio;
231 libc_hidden_proto (__sched_fifo_min_prio)
232 extern int __sched_fifo_max_prio;
233 libc_hidden_proto (__sched_fifo_max_prio)
234 extern void __init_sched_fifo_prio (void);
235 libc_hidden_proto (__init_sched_fifo_prio)
236 extern int __pthread_tpp_change_priority (int prev_prio, int new_prio);
237 libc_hidden_proto (__pthread_tpp_change_priority)
238 extern int __pthread_current_priority (void);
239 libc_hidden_proto (__pthread_current_priority)
241 /* This will not catch all invalid descriptors but is better than
242 nothing. And if the test triggers the thread descriptor is
243 guaranteed to be invalid. */
244 #define INVALID_TD_P(pd) __builtin_expect ((pd)->tid <= 0, 0)
245 #define INVALID_NOT_TERMINATED_TD_P(pd) __builtin_expect ((pd)->tid < 0, 0)
247 extern void __pthread_unwind (__pthread_unwind_buf_t *__buf)
248 __cleanup_fct_attribute __attribute ((__noreturn__))
249 #if !defined SHARED && !IS_IN (libpthread)
250 weak_function
251 #endif
253 libc_hidden_proto (__pthread_unwind)
254 extern void __pthread_unwind_next (__pthread_unwind_buf_t *__buf)
255 __cleanup_fct_attribute __attribute ((__noreturn__))
256 #ifndef SHARED
257 weak_function
258 #endif
260 /* NB: No hidden proto for __pthread_unwind_next: inside glibc, the
261 legacy unwinding mechanism is used. */
263 extern void __pthread_register_cancel (__pthread_unwind_buf_t *__buf)
264 __cleanup_fct_attribute;
265 libc_hidden_proto (__pthread_register_cancel)
266 extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf)
267 __cleanup_fct_attribute;
268 libc_hidden_proto (__pthread_unregister_cancel)
270 /* Called when a thread reacts on a cancellation request. */
271 static inline void
272 __attribute ((noreturn, always_inline))
273 __do_cancel (void)
275 struct pthread *self = THREAD_SELF;
277 /* Make sure we get no more cancellations. */
278 THREAD_ATOMIC_BIT_SET (self, cancelhandling, EXITING_BIT);
280 __pthread_unwind ((__pthread_unwind_buf_t *)
281 THREAD_GETMEM (self, cleanup_jmp_buf));
285 /* Internal prototypes. */
287 /* Deallocate a thread's stack after optionally making sure the thread
288 descriptor is still valid. */
289 extern void __nptl_free_tcb (struct pthread *pd);
290 libc_hidden_proto (__nptl_free_tcb)
292 /* Change the permissions of a thread stack. Called from
293 _dl_make_stacks_executable and pthread_create. */
295 __nptl_change_stack_perm (struct pthread *pd);
296 rtld_hidden_proto (__nptl_change_stack_perm)
298 /* longjmp handling. */
299 extern void __pthread_cleanup_upto (__jmp_buf target, char *targetframe);
300 libc_hidden_proto (__pthread_cleanup_upto)
303 /* Functions with versioned interfaces. */
304 extern int __pthread_create (pthread_t *newthread,
305 const pthread_attr_t *attr,
306 void *(*start_routine) (void *), void *arg);
307 libc_hidden_proto (__pthread_create)
308 extern int __pthread_create_2_0 (pthread_t *newthread,
309 const pthread_attr_t *attr,
310 void *(*start_routine) (void *), void *arg);
311 extern int __pthread_attr_init (pthread_attr_t *attr);
312 libc_hidden_proto (__pthread_attr_init)
313 extern int __pthread_attr_init_2_0 (pthread_attr_t *attr);
315 /* Part of the legacy thread events interface (which has been
316 superseded by PTRACE_O_TRACECLONE). This can be set by the
317 debugger before initialization is complete. */
318 extern bool __nptl_initial_report_events;
319 rtld_hidden_proto (__nptl_initial_report_events)
321 /* Event handlers for libthread_db interface. */
322 extern void __nptl_create_event (void);
323 extern void __nptl_death_event (void);
324 libc_hidden_proto (__nptl_create_event)
325 libc_hidden_proto (__nptl_death_event)
327 /* The fork generation counter, defined in libpthread. */
328 extern unsigned long int __fork_generation attribute_hidden;
330 /* Pointer to the fork generation counter in the thread library. */
331 extern unsigned long int *__fork_generation_pointer attribute_hidden;
333 extern size_t __pthread_get_minstack (const pthread_attr_t *attr);
334 libc_hidden_proto (__pthread_get_minstack)
336 /* Namespace save aliases. */
337 extern int __pthread_getschedparam (pthread_t thread_id, int *policy,
338 struct sched_param *param);
339 libc_hidden_proto (__pthread_getschedparam)
340 extern int __pthread_setschedparam (pthread_t thread_id, int policy,
341 const struct sched_param *param);
342 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
343 const pthread_mutexattr_t *__mutexattr);
344 libc_hidden_proto (__pthread_mutex_init)
345 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
346 libc_hidden_proto (__pthread_mutex_destroy)
347 extern int __pthread_mutex_trylock (pthread_mutex_t *_mutex);
348 libc_hidden_proto (__pthread_mutex_trylock)
349 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
350 libc_hidden_proto (__pthread_mutex_lock)
351 extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex,
352 const struct timespec *__abstime);
353 extern int __pthread_mutex_cond_lock (pthread_mutex_t *__mutex)
354 attribute_hidden;
355 extern void __pthread_mutex_cond_lock_adjust (pthread_mutex_t *__mutex)
356 attribute_hidden;
357 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
358 libc_hidden_proto (__pthread_mutex_unlock)
359 extern int __pthread_mutex_unlock_usercnt (pthread_mutex_t *__mutex,
360 int __decr);
361 libc_hidden_proto (__pthread_mutex_unlock_usercnt)
362 extern int __pthread_mutexattr_init (pthread_mutexattr_t *attr);
363 libc_hidden_proto (__pthread_mutexattr_init)
364 extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *attr);
365 extern int __pthread_mutexattr_settype (pthread_mutexattr_t *attr, int kind);
366 libc_hidden_proto (__pthread_mutexattr_settype)
367 extern int __pthread_attr_destroy (pthread_attr_t *attr);
368 libc_hidden_proto (__pthread_attr_destroy)
369 extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
370 int *detachstate);
371 extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
372 int detachstate);
373 extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
374 int *inherit);
375 extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
376 extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
377 struct sched_param *param);
378 extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
379 const struct sched_param *param);
380 extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
381 int *policy);
382 extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
383 extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
384 extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
385 extern int __pthread_attr_getstackaddr (const pthread_attr_t *__restrict
386 __attr, void **__restrict __stackaddr);
387 extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
388 void *__stackaddr);
389 extern int __pthread_attr_getstacksize (const pthread_attr_t *__restrict
390 __attr,
391 size_t *__restrict __stacksize);
392 extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
393 size_t __stacksize);
394 extern int __pthread_attr_getstack (const pthread_attr_t *__restrict __attr,
395 void **__restrict __stackaddr,
396 size_t *__restrict __stacksize);
397 extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
398 size_t __stacksize);
399 int __pthread_attr_setaffinity_np (pthread_attr_t *, size_t, const cpu_set_t *);
400 libc_hidden_proto (__pthread_attr_setaffinity_np)
401 extern __typeof (pthread_getattr_default_np) __pthread_getattr_default_np;
402 libc_hidden_proto (__pthread_getattr_default_np)
403 extern int __pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
404 const pthread_rwlockattr_t *__restrict
405 __attr);
406 extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
407 extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
408 libc_hidden_proto (__pthread_rwlock_rdlock)
409 extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
410 extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
411 libc_hidden_proto (__pthread_rwlock_wrlock)
412 extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
413 extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
414 extern int __pthread_cond_broadcast (pthread_cond_t *cond);
415 libc_hidden_proto (__pthread_cond_broadcast)
416 extern int __pthread_cond_destroy (pthread_cond_t *cond);
417 libc_hidden_proto (__pthread_cond_destroy)
418 extern int __pthread_cond_init (pthread_cond_t *cond,
419 const pthread_condattr_t *cond_attr);
420 libc_hidden_proto (__pthread_cond_init)
421 extern int __pthread_cond_signal (pthread_cond_t *cond);
422 libc_hidden_proto (__pthread_cond_signal)
423 extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
424 libc_hidden_proto (__pthread_cond_wait)
426 #if __TIMESIZE == 64
427 # define __pthread_clockjoin_np64 __pthread_clockjoin_np
428 # define __pthread_timedjoin_np64 __pthread_timedjoin_np
429 # define __pthread_cond_timedwait64 __pthread_cond_timedwait
430 # define __pthread_cond_clockwait64 __pthread_cond_clockwait
431 # define __pthread_rwlock_clockrdlock64 __pthread_rwlock_clockrdlock
432 # define __pthread_rwlock_clockwrlock64 __pthread_rwlock_clockwrlock
433 # define __pthread_rwlock_timedrdlock64 __pthread_rwlock_timedrdlock
434 # define __pthread_rwlock_timedwrlock64 __pthread_rwlock_timedwrlock
435 # define __pthread_mutex_clocklock64 __pthread_mutex_clocklock
436 # define __pthread_mutex_timedlock64 __pthread_mutex_timedlock
437 #else
438 extern int __pthread_clockjoin_np64 (pthread_t threadid, void **thread_return,
439 clockid_t clockid,
440 const struct __timespec64 *abstime);
441 libc_hidden_proto (__pthread_clockjoin_np64)
442 extern int __pthread_timedjoin_np64 (pthread_t threadid, void **thread_return,
443 const struct __timespec64 *abstime);
444 libc_hidden_proto (__pthread_timedjoin_np64)
445 extern int __pthread_cond_timedwait64 (pthread_cond_t *cond,
446 pthread_mutex_t *mutex,
447 const struct __timespec64 *abstime);
448 libc_hidden_proto (__pthread_cond_timedwait64)
449 extern int __pthread_cond_clockwait64 (pthread_cond_t *cond,
450 pthread_mutex_t *mutex,
451 clockid_t clockid,
452 const struct __timespec64 *abstime);
453 libc_hidden_proto (__pthread_cond_clockwait64)
454 extern int __pthread_rwlock_clockrdlock64 (pthread_rwlock_t *rwlock,
455 clockid_t clockid,
456 const struct __timespec64 *abstime);
457 libc_hidden_proto (__pthread_rwlock_clockrdlock64)
458 extern int __pthread_rwlock_clockwrlock64 (pthread_rwlock_t *rwlock,
459 clockid_t clockid,
460 const struct __timespec64 *abstime);
461 libc_hidden_proto (__pthread_rwlock_clockwrlock64)
462 extern int __pthread_rwlock_timedrdlock64 (pthread_rwlock_t *rwlock,
463 const struct __timespec64 *abstime);
464 libc_hidden_proto (__pthread_rwlock_timedrdlock64)
465 extern int __pthread_rwlock_timedwrlock64 (pthread_rwlock_t *rwlock,
466 const struct __timespec64 *abstime);
467 libc_hidden_proto (__pthread_rwlock_timedwrlock64)
468 extern int __pthread_mutex_clocklock64 (pthread_mutex_t *mutex,
469 clockid_t clockid,
470 const struct __timespec64 *abstime);
471 libc_hidden_proto (__pthread_mutex_clocklock64)
472 extern int __pthread_mutex_timedlock64 (pthread_mutex_t *mutex,
473 const struct __timespec64 *abstime);
474 libc_hidden_proto (__pthread_mutex_timedlock64)
475 #endif
477 extern int __pthread_cond_timedwait (pthread_cond_t *cond,
478 pthread_mutex_t *mutex,
479 const struct timespec *abstime);
480 libc_hidden_proto (__pthread_cond_timedwait)
481 extern int __pthread_cond_clockwait (pthread_cond_t *cond,
482 pthread_mutex_t *mutex,
483 clockid_t clockid,
484 const struct timespec *abstime)
485 __nonnull ((1, 2, 4));
486 libc_hidden_proto (__pthread_cond_clockwait)
488 extern int __pthread_mutex_clocklock (pthread_mutex_t *mutex,
489 clockid_t clockid,
490 const struct timespec *abstime);
491 libc_hidden_proto (__pthread_mutex_clocklock)
492 extern int __pthread_mutex_timedlock (pthread_mutex_t *mutex,
493 const struct timespec *abstime);
494 libc_hidden_proto (__pthread_mutex_timedlock)
496 extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
497 extern int __pthread_condattr_init (pthread_condattr_t *attr);
498 extern int __pthread_key_create (pthread_key_t *key, void (*destr) (void *));
499 libc_hidden_proto (__pthread_key_create)
500 extern int __pthread_key_delete (pthread_key_t key);
501 libc_hidden_proto (__pthread_key_delete)
502 extern void *__pthread_getspecific (pthread_key_t key);
503 libc_hidden_proto (__pthread_getspecific)
504 extern int __pthread_setspecific (pthread_key_t key, const void *value);
505 libc_hidden_proto (__pthread_setspecific)
506 extern int __pthread_once (pthread_once_t *once_control,
507 void (*init_routine) (void));
508 libc_hidden_proto (__pthread_once)
509 extern int __pthread_atfork (void (*prepare) (void), void (*parent) (void),
510 void (*child) (void));
511 libc_hidden_proto (__pthread_self)
512 extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
513 extern int __pthread_detach (pthread_t th);
514 libc_hidden_proto (__pthread_detach)
515 extern int __pthread_kill (pthread_t threadid, int signo);
516 libc_hidden_proto (__pthread_kill)
517 extern int __pthread_cancel (pthread_t th);
518 extern int __pthread_kill_internal (pthread_t threadid, int signo)
519 attribute_hidden;
520 extern void __pthread_exit (void *value) __attribute__ ((__noreturn__));
521 libc_hidden_proto (__pthread_exit)
522 extern int __pthread_join (pthread_t threadid, void **thread_return);
523 libc_hidden_proto (__pthread_join)
524 extern int __pthread_setcanceltype (int type, int *oldtype);
525 libc_hidden_proto (__pthread_setcanceltype)
526 extern void __pthread_testcancel (void);
527 libc_hidden_proto (__pthread_testcancel)
528 extern int __pthread_clockjoin_ex (pthread_t, void **, clockid_t,
529 const struct __timespec64 *, bool)
530 attribute_hidden;
531 extern int __pthread_sigmask (int, const sigset_t *, sigset_t *);
532 libc_hidden_proto (__pthread_sigmask);
535 #if IS_IN (libpthread)
536 hidden_proto (__pthread_rwlock_unlock)
537 #endif
539 extern int __pthread_cond_broadcast_2_0 (pthread_cond_2_0_t *cond);
540 extern int __pthread_cond_destroy_2_0 (pthread_cond_2_0_t *cond);
541 extern int __pthread_cond_init_2_0 (pthread_cond_2_0_t *cond,
542 const pthread_condattr_t *cond_attr);
543 extern int __pthread_cond_signal_2_0 (pthread_cond_2_0_t *cond);
544 extern int __pthread_cond_timedwait_2_0 (pthread_cond_2_0_t *cond,
545 pthread_mutex_t *mutex,
546 const struct timespec *abstime);
547 extern int __pthread_cond_wait_2_0 (pthread_cond_2_0_t *cond,
548 pthread_mutex_t *mutex);
550 extern int __pthread_getaffinity_np (pthread_t th, size_t cpusetsize,
551 cpu_set_t *cpuset);
552 libc_hidden_proto (__pthread_getaffinity_np)
554 /* Special internal version of pthread_attr_setsigmask_np which does
555 not filter out internal signals from *SIGMASK. This can be used to
556 launch threads with internal signals blocked. */
557 extern int __pthread_attr_setsigmask_internal (pthread_attr_t *attr,
558 const sigset_t *sigmask);
559 libc_hidden_proto (__pthread_attr_setsigmask_internal)
561 extern __typeof (pthread_attr_getsigmask_np) __pthread_attr_getsigmask_np;
562 libc_hidden_proto (__pthread_attr_getsigmask_np)
564 /* Special versions which use non-exported functions. */
565 extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
566 void (*routine) (void *), void *arg);
567 libc_hidden_proto (__pthread_cleanup_push)
569 /* Replace cleanup macros defined in <pthread.h> with internal
570 versions that don't depend on unwind info and better support
571 cancellation. */
572 # undef pthread_cleanup_push
573 # define pthread_cleanup_push(routine,arg) \
574 { struct _pthread_cleanup_buffer _buffer; \
575 __pthread_cleanup_push (&_buffer, (routine), (arg));
577 extern void __pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
578 int execute);
579 libc_hidden_proto (__pthread_cleanup_pop)
580 # undef pthread_cleanup_pop
581 # define pthread_cleanup_pop(execute) \
582 __pthread_cleanup_pop (&_buffer, (execute)); }
584 #if defined __EXCEPTIONS && !defined __cplusplus
585 /* Structure to hold the cleanup handler information. */
586 struct __pthread_cleanup_combined_frame
588 void (*__cancel_routine) (void *);
589 void *__cancel_arg;
590 int __do_it;
591 struct _pthread_cleanup_buffer __buffer;
594 /* Special cleanup macros which register cleanup both using
595 __pthread_cleanup_{push,pop} and using cleanup attribute. This is needed
596 for pthread_once, so that it supports both throwing exceptions from the
597 pthread_once callback (only cleanup attribute works there) and cancellation
598 of the thread running the callback if the callback or some routines it
599 calls don't have unwind information. */
601 static __always_inline void
602 __pthread_cleanup_combined_routine (struct __pthread_cleanup_combined_frame
603 *__frame)
605 if (__frame->__do_it)
607 __frame->__cancel_routine (__frame->__cancel_arg);
608 __frame->__do_it = 0;
609 __pthread_cleanup_pop (&__frame->__buffer, 0);
613 static inline void
614 __pthread_cleanup_combined_routine_voidptr (void *__arg)
616 struct __pthread_cleanup_combined_frame *__frame
617 = (struct __pthread_cleanup_combined_frame *) __arg;
618 if (__frame->__do_it)
620 __frame->__cancel_routine (__frame->__cancel_arg);
621 __frame->__do_it = 0;
625 # define pthread_cleanup_combined_push(routine, arg) \
626 do { \
627 void (*__cancel_routine) (void *) = (routine); \
628 struct __pthread_cleanup_combined_frame __clframe \
629 __attribute__ ((__cleanup__ (__pthread_cleanup_combined_routine))) \
630 = { .__cancel_routine = __cancel_routine, .__cancel_arg = (arg), \
631 .__do_it = 1 }; \
632 __pthread_cleanup_push (&__clframe.__buffer, \
633 __pthread_cleanup_combined_routine_voidptr, \
634 &__clframe);
636 # define pthread_cleanup_combined_pop(execute) \
637 __pthread_cleanup_pop (&__clframe.__buffer, 0); \
638 __clframe.__do_it = 0; \
639 if (execute) \
640 __cancel_routine (__clframe.__cancel_arg); \
641 } while (0)
643 #endif /* __EXCEPTIONS && !defined __cplusplus */
645 extern void __pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
646 void (*routine) (void *), void *arg);
647 extern void __pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
648 int execute);
650 /* Old cleanup interfaces, still used in libc.so. */
651 extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
652 void (*routine) (void *), void *arg);
653 extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
654 int execute);
655 extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
656 void (*routine) (void *), void *arg);
657 extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
658 int execute);
660 extern void __nptl_deallocate_tsd (void);
661 libc_hidden_proto (__nptl_deallocate_tsd)
663 void __nptl_setxid_sighandler (int sig, siginfo_t *si, void *ctx);
664 libc_hidden_proto (__nptl_setxid_sighandler)
665 extern int __nptl_setxid (struct xid_command *cmdp) attribute_hidden;
667 extern void __wait_lookup_done (void) attribute_hidden;
669 /* Allocates the extension space for ATTR. Returns an error code on
670 memory allocation failure, zero on success. If ATTR already has an
671 extension space, this function does nothing. */
672 int __pthread_attr_extension (struct pthread_attr *attr) attribute_hidden
673 __attribute_warn_unused_result__;
675 #ifdef SHARED
676 # define PTHREAD_STATIC_FN_REQUIRE(name)
677 #else
678 # define PTHREAD_STATIC_FN_REQUIRE(name) __asm (".globl " #name);
679 #endif
681 /* Make a deep copy of the attribute *SOURCE in *TARGET. *TARGET is
682 not assumed to have been initialized. Returns 0 on success, or a
683 positive error code otherwise. */
684 int __pthread_attr_copy (pthread_attr_t *target, const pthread_attr_t *source);
685 libc_hidden_proto (__pthread_attr_copy)
687 /* Returns 0 if POL is a valid scheduling policy. */
688 static inline int
689 check_sched_policy_attr (int pol)
691 if (pol == SCHED_OTHER || pol == SCHED_FIFO || pol == SCHED_RR)
692 return 0;
694 return EINVAL;
697 /* Returns 0 if PR is within the accepted range of priority values for
698 the scheduling policy POL or EINVAL otherwise. */
699 static inline int
700 check_sched_priority_attr (int pr, int pol)
702 int min = __sched_get_priority_min (pol);
703 int max = __sched_get_priority_max (pol);
705 if (min >= 0 && max >= 0 && pr >= min && pr <= max)
706 return 0;
708 return EINVAL;
711 /* Returns 0 if ST is a valid stack size for a thread stack and EINVAL
712 otherwise. */
713 static inline int
714 check_stacksize_attr (size_t st)
716 if (st >= PTHREAD_STACK_MIN)
717 return 0;
719 return EINVAL;
722 #define ASSERT_TYPE_SIZE(type, size) \
723 _Static_assert (sizeof (type) == size, \
724 "sizeof (" #type ") != " #size)
726 #define ASSERT_PTHREAD_INTERNAL_SIZE(type, internal) \
727 _Static_assert (sizeof ((type) { { 0 } }).__size >= sizeof (internal),\
728 "sizeof (" #type ".__size) < sizeof (" #internal ")")
730 #define ASSERT_PTHREAD_STRING(x) __STRING (x)
731 #define ASSERT_PTHREAD_INTERNAL_OFFSET(type, member, offset) \
732 _Static_assert (offsetof (type, member) == offset, \
733 "offset of " #member " field of " #type " != " \
734 ASSERT_PTHREAD_STRING (offset))
735 #define ASSERT_PTHREAD_INTERNAL_MEMBER_SIZE(type, member, mtype) \
736 _Static_assert (sizeof (((type) { 0 }).member) != 8, \
737 "sizeof (" #type "." #member ") != sizeof (" #mtype "))")
739 #endif /* pthreadP.h */