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