1/* Copyright (C) 2002-2020 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 <https://www.gnu.org/licenses/>. */
18
19#ifndef _PTHREADP_H
20#define _PTHREADP_H 1
21
22#define __PTHREAD_NPTL
23
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"
39
40
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
46
47#ifndef THREAD_ATOMIC_BIT_SET
48# define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
49 atomic_bit_set (&(descr)->member, bit)
50#endif
51
52
53static inline short max_adaptive_count (void)
54{
55#if HAVE_TUNABLES
56 return __mutex_aconf.spin_count;
57#else
58 return DEFAULT_ADAPTIVE_COUNT;
59#endif
60}
61
62
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)
67
68
69/* Internal mutex type value. */
70enum
71{
72 PTHREAD_MUTEX_KIND_MASK_NP = 3,
73
74 PTHREAD_MUTEX_ELISION_NP = 256,
75 PTHREAD_MUTEX_NO_ELISION_NP = 512,
76
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,
112
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,
117};
118#define PTHREAD_MUTEX_PSHARED_BIT 128
119
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))
129
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
138
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
142
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
150
151
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)
162
163
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
174
175
176/* Bits used in robust mutex implementation. */
177#define FUTEX_WAITERS 0x80000000
178#define FUTEX_OWNER_DIED 0x40000000
179#define FUTEX_TID_MASK 0x3fffffff
180
181
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
186
187/* Attribute to indicate thread creation was issued from C11 thrd_create. */
188#define ATTR_C11_THREAD ((void*)(uintptr_t)-1)
189
190
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
196
197
198/* Internal variables. */
199
200
201/* Default pthread attributes. */
202extern union pthread_attr_transparent __default_pthread_attr attribute_hidden;
203extern int __default_pthread_attr_lock attribute_hidden;
204/* Called from __libpthread_freeres to deallocate the default attribute. */
205extern void __default_pthread_attr_freeres (void) attribute_hidden;
206
207/* Size and alignment of static TLS block. */
208extern size_t __static_tls_size attribute_hidden;
209extern size_t __static_tls_align_m1 attribute_hidden;
210
211/* Flag whether the machine is SMP or not. */
212extern int __is_smp attribute_hidden;
213
214/* Thread descriptor handling. */
215extern list_t __stack_user;
216hidden_proto (__stack_user)
217
218/* Attribute handling. */
219extern struct pthread_attr *__attr_list attribute_hidden;
220extern int __attr_list_lock attribute_hidden;
221
222/* Concurrency handling. */
223extern int __concurrency_level attribute_hidden;
224
225/* Thread-local data key handling. */
226extern struct pthread_key_struct __pthread_keys[PTHREAD_KEYS_MAX];
227hidden_proto (__pthread_keys)
228
229/* Number of threads running. */
230extern unsigned int __nptl_nthreads attribute_hidden;
231
232#ifndef __ASSUME_SET_ROBUST_LIST
233/* Negative if we do not have the system call and we can use it. */
234extern int __set_robust_list_avail attribute_hidden;
235#endif
236
237/* Thread Priority Protection. */
238extern int __sched_fifo_min_prio attribute_hidden;
239extern int __sched_fifo_max_prio attribute_hidden;
240extern void __init_sched_fifo_prio (void) attribute_hidden;
241extern int __pthread_tpp_change_priority (int prev_prio, int new_prio)
242 attribute_hidden;
243extern int __pthread_current_priority (void) attribute_hidden;
244
245/* The library can run in debugging mode where it performs a lot more
246 tests. */
247extern int __pthread_debug attribute_hidden;
248/** For now disable debugging support. */
249#if 0
250# define DEBUGGING_P __builtin_expect (__pthread_debug, 0)
251# define INVALID_TD_P(pd) (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
252# define INVALID_NOT_TERMINATED_TD_P(pd) INVALID_TD_P (pd)
253#else
254# define DEBUGGING_P 0
255/* Simplified test. This will not catch all invalid descriptors but
256 is better than nothing. And if the test triggers the thread
257 descriptor is guaranteed to be invalid. */
258# define INVALID_TD_P(pd) __builtin_expect ((pd)->tid <= 0, 0)
259# define INVALID_NOT_TERMINATED_TD_P(pd) __builtin_expect ((pd)->tid < 0, 0)
260#endif
261
262
263/* Cancellation test. */
264#define CANCELLATION_P(self) \
265 do { \
266 int cancelhandling = THREAD_GETMEM (self, cancelhandling); \
267 if (CANCEL_ENABLED_AND_CANCELED (cancelhandling)) \
268 { \
269 THREAD_SETMEM (self, result, PTHREAD_CANCELED); \
270 __do_cancel (); \
271 } \
272 } while (0)
273
274
275extern void __pthread_unwind (__pthread_unwind_buf_t *__buf)
276 __cleanup_fct_attribute __attribute ((__noreturn__))
277#if !defined SHARED && !IS_IN (libpthread)
278 weak_function
279#endif
280 ;
281extern void __pthread_unwind_next (__pthread_unwind_buf_t *__buf)
282 __cleanup_fct_attribute __attribute ((__noreturn__))
283#ifndef SHARED
284 weak_function
285#endif
286 ;
287extern void __pthread_register_cancel (__pthread_unwind_buf_t *__buf)
288 __cleanup_fct_attribute;
289extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf)
290 __cleanup_fct_attribute;
291#if IS_IN (libpthread)
292hidden_proto (__pthread_unwind)
293hidden_proto (__pthread_unwind_next)
294hidden_proto (__pthread_register_cancel)
295hidden_proto (__pthread_unregister_cancel)
296# ifdef SHARED
297extern void attribute_hidden pthread_cancel_init (void);
298# endif
299extern void __nptl_unwind_freeres (void) attribute_hidden;
300#endif
301
302
303/* Called when a thread reacts on a cancellation request. */
304static inline void
305__attribute ((noreturn, always_inline))
306__do_cancel (void)
307{
308 struct pthread *self = THREAD_SELF;
309
310 /* Make sure we get no more cancellations. */
311 THREAD_ATOMIC_BIT_SET (self, cancelhandling, EXITING_BIT);
312
313 __pthread_unwind ((__pthread_unwind_buf_t *)
314 THREAD_GETMEM (self, cleanup_jmp_buf));
315}
316
317
318/* Internal prototypes. */
319
320/* Thread list handling. */
321extern struct pthread *__find_in_stack_list (struct pthread *pd)
322 attribute_hidden;
323
324/* Deallocate a thread's stack after optionally making sure the thread
325 descriptor is still valid. */
326extern void __free_tcb (struct pthread *pd) attribute_hidden;
327
328/* Free allocated stack. */
329extern void __deallocate_stack (struct pthread *pd) attribute_hidden;
330
331/* Mark all the stacks except for the current one as available. This
332 function also re-initializes the lock for the stack cache. */
333extern void __reclaim_stacks (void) attribute_hidden;
334
335/* Make all threads's stacks executable. */
336extern int __make_stacks_executable (void **stack_endp) attribute_hidden;
337
338/* longjmp handling. */
339extern void __pthread_cleanup_upto (__jmp_buf target, char *targetframe);
340#if IS_IN (libpthread)
341hidden_proto (__pthread_cleanup_upto)
342#endif
343
344
345/* Functions with versioned interfaces. */
346extern int __pthread_create_2_1 (pthread_t *newthread,
347 const pthread_attr_t *attr,
348 void *(*start_routine) (void *), void *arg);
349extern int __pthread_create_2_0 (pthread_t *newthread,
350 const pthread_attr_t *attr,
351 void *(*start_routine) (void *), void *arg);
352extern int __pthread_attr_init (pthread_attr_t *attr);
353libc_hidden_proto (__pthread_attr_init)
354extern int __pthread_attr_init_2_0 (pthread_attr_t *attr);
355
356
357/* Event handlers for libthread_db interface. */
358extern void __nptl_create_event (void);
359extern void __nptl_death_event (void);
360hidden_proto (__nptl_create_event)
361hidden_proto (__nptl_death_event)
362
363/* Register the generation counter in the libpthread with the libc. */
364#ifdef TLS_MULTIPLE_THREADS_IN_TCB
365extern void __libc_pthread_init (unsigned long int *ptr,
366 void (*reclaim) (void),
367 const struct pthread_functions *functions);
368#else
369extern int *__libc_pthread_init (unsigned long int *ptr,
370 void (*reclaim) (void),
371 const struct pthread_functions *functions);
372
373/* Variable set to a nonzero value either if more than one thread runs or ran,
374 or if a single-threaded process is trying to cancel itself. See
375 nptl/descr.h for more context on the single-threaded process case. */
376extern int __pthread_multiple_threads attribute_hidden;
377/* Pointer to the corresponding variable in libc. */
378extern int *__libc_multiple_threads_ptr attribute_hidden;
379#endif
380
381extern void __pthread_init_static_tls (struct link_map *) attribute_hidden;
382
383extern size_t __pthread_get_minstack (const pthread_attr_t *attr);
384
385/* Namespace save aliases. */
386extern int __pthread_getschedparam (pthread_t thread_id, int *policy,
387 struct sched_param *param);
388extern int __pthread_setschedparam (pthread_t thread_id, int policy,
389 const struct sched_param *param);
390extern int __pthread_setcancelstate (int state, int *oldstate);
391extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
392 const pthread_mutexattr_t *__mutexattr);
393extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
394extern int __pthread_mutex_trylock (pthread_mutex_t *_mutex);
395extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
396extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex,
397 const struct timespec *__abstime);
398extern int __pthread_mutex_cond_lock (pthread_mutex_t *__mutex)
399 attribute_hidden;
400extern void __pthread_mutex_cond_lock_adjust (pthread_mutex_t *__mutex)
401 attribute_hidden;
402extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
403extern int __pthread_mutex_unlock_usercnt (pthread_mutex_t *__mutex,
404 int __decr) attribute_hidden;
405extern int __pthread_mutexattr_init (pthread_mutexattr_t *attr);
406extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *attr);
407extern int __pthread_mutexattr_settype (pthread_mutexattr_t *attr, int kind);
408extern int __pthread_attr_destroy (pthread_attr_t *attr);
409libc_hidden_proto (__pthread_attr_destroy)
410extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
411 int *detachstate);
412extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
413 int detachstate);
414extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
415 int *inherit);
416extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
417extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
418 struct sched_param *param);
419extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
420 const struct sched_param *param);
421extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
422 int *policy);
423extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
424extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
425extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
426extern int __pthread_attr_getstackaddr (const pthread_attr_t *__restrict
427 __attr, void **__restrict __stackaddr);
428extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
429 void *__stackaddr);
430extern int __pthread_attr_getstacksize (const pthread_attr_t *__restrict
431 __attr,
432 size_t *__restrict __stacksize);
433extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
434 size_t __stacksize);
435extern int __pthread_attr_getstack (const pthread_attr_t *__restrict __attr,
436 void **__restrict __stackaddr,
437 size_t *__restrict __stacksize);
438extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
439 size_t __stacksize);
440int __pthread_attr_setaffinity_np (pthread_attr_t *, size_t, const cpu_set_t *);
441libc_hidden_proto (__pthread_attr_setaffinity_np)
442extern __typeof (pthread_getattr_default_np) __pthread_getattr_default_np;
443libpthread_hidden_proto (__pthread_getattr_default_np)
444extern int __pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
445 const pthread_rwlockattr_t *__restrict
446 __attr);
447extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
448extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
449extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
450extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
451extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
452extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
453extern int __pthread_cond_broadcast (pthread_cond_t *cond);
454extern int __pthread_cond_destroy (pthread_cond_t *cond);
455libc_hidden_proto (__pthread_cond_destroy)
456extern int __pthread_cond_init (pthread_cond_t *cond,
457 const pthread_condattr_t *cond_attr);
458libc_hidden_proto (__pthread_cond_init)
459extern int __pthread_cond_signal (pthread_cond_t *cond);
460extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
461extern int __pthread_cond_timedwait (pthread_cond_t *cond,
462 pthread_mutex_t *mutex,
463 const struct timespec *abstime);
464extern int __pthread_cond_clockwait (pthread_cond_t *cond,
465 pthread_mutex_t *mutex,
466 clockid_t clockid,
467 const struct timespec *abstime)
468 __nonnull ((1, 2, 4));
469extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
470extern int __pthread_condattr_init (pthread_condattr_t *attr);
471extern int __pthread_key_create (pthread_key_t *key, void (*destr) (void *));
472extern int __pthread_key_delete (pthread_key_t key);
473extern void *__pthread_getspecific (pthread_key_t key);
474extern int __pthread_setspecific (pthread_key_t key, const void *value);
475extern int __pthread_once (pthread_once_t *once_control,
476 void (*init_routine) (void));
477extern int __pthread_atfork (void (*prepare) (void), void (*parent) (void),
478 void (*child) (void));
479extern pthread_t __pthread_self (void);
480extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
481extern int __pthread_detach (pthread_t th);
482extern int __pthread_cancel (pthread_t th);
483extern int __pthread_kill (pthread_t threadid, int signo);
484extern void __pthread_exit (void *value) __attribute__ ((__noreturn__));
485extern int __pthread_join (pthread_t threadid, void **thread_return);
486extern int __pthread_setcanceltype (int type, int *oldtype);
487extern int __pthread_enable_asynccancel (void) attribute_hidden;
488extern void __pthread_disable_asynccancel (int oldtype) attribute_hidden;
489extern void __pthread_testcancel (void);
490extern int __pthread_clockjoin_ex (pthread_t, void **, clockid_t,
491 const struct timespec *, bool)
492 attribute_hidden;
493extern int __pthread_sigmask (int, const sigset_t *, sigset_t *);
494libc_hidden_proto (__pthread_sigmask);
495
496
497#if IS_IN (libpthread)
498hidden_proto (__pthread_mutex_init)
499hidden_proto (__pthread_mutex_destroy)
500hidden_proto (__pthread_mutex_lock)
501hidden_proto (__pthread_mutex_trylock)
502hidden_proto (__pthread_mutex_unlock)
503hidden_proto (__pthread_rwlock_rdlock)
504hidden_proto (__pthread_rwlock_wrlock)
505hidden_proto (__pthread_rwlock_unlock)
506hidden_proto (__pthread_key_create)
507hidden_proto (__pthread_getspecific)
508hidden_proto (__pthread_setspecific)
509hidden_proto (__pthread_once)
510hidden_proto (__pthread_setcancelstate)
511hidden_proto (__pthread_testcancel)
512hidden_proto (__pthread_mutexattr_init)
513hidden_proto (__pthread_mutexattr_settype)
514#endif
515
516extern int __pthread_cond_broadcast_2_0 (pthread_cond_2_0_t *cond);
517extern int __pthread_cond_destroy_2_0 (pthread_cond_2_0_t *cond);
518extern int __pthread_cond_init_2_0 (pthread_cond_2_0_t *cond,
519 const pthread_condattr_t *cond_attr);
520extern int __pthread_cond_signal_2_0 (pthread_cond_2_0_t *cond);
521extern int __pthread_cond_timedwait_2_0 (pthread_cond_2_0_t *cond,
522 pthread_mutex_t *mutex,
523 const struct timespec *abstime);
524extern int __pthread_cond_wait_2_0 (pthread_cond_2_0_t *cond,
525 pthread_mutex_t *mutex);
526
527extern int __pthread_getaffinity_np (pthread_t th, size_t cpusetsize,
528 cpu_set_t *cpuset);
529libc_hidden_proto (__pthread_getaffinity_np)
530
531/* Special internal version of pthread_attr_setsigmask_np which does
532 not filter out internal signals from *SIGMASK. This can be used to
533 launch threads with internal signals blocked. */
534 extern int __pthread_attr_setsigmask_internal (pthread_attr_t *attr,
535 const sigset_t *sigmask);
536libc_hidden_proto (__pthread_attr_setsigmask_internal)
537
538extern __typeof (pthread_attr_getsigmask_np) __pthread_attr_getsigmask_np;
539libc_hidden_proto (__pthread_attr_getsigmask_np)
540
541#if IS_IN (libpthread)
542/* Special versions which use non-exported functions. */
543extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
544 void (*routine) (void *), void *arg)
545 attribute_hidden;
546
547/* Replace cleanup macros defined in <pthread.h> with internal
548 versions that don't depend on unwind info and better support
549 cancellation. */
550# undef pthread_cleanup_push
551# define pthread_cleanup_push(routine,arg) \
552 { struct _pthread_cleanup_buffer _buffer; \
553 __pthread_cleanup_push (&_buffer, (routine), (arg));
554
555extern void __pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
556 int execute) attribute_hidden;
557# undef pthread_cleanup_pop
558# define pthread_cleanup_pop(execute) \
559 __pthread_cleanup_pop (&_buffer, (execute)); }
560#endif
561
562extern void __pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
563 void (*routine) (void *), void *arg);
564extern void __pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
565 int execute);
566
567/* Old cleanup interfaces, still used in libc.so. */
568extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
569 void (*routine) (void *), void *arg);
570extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
571 int execute);
572extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
573 void (*routine) (void *), void *arg);
574extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
575 int execute);
576
577extern void __nptl_deallocate_tsd (void) attribute_hidden;
578
579extern void __nptl_setxid_error (struct xid_command *cmdp, int error)
580 attribute_hidden;
581extern int __nptl_setxid (struct xid_command *cmdp) attribute_hidden;
582#ifndef SHARED
583extern void __nptl_set_robust (struct pthread *self);
584#endif
585
586extern void __nptl_stacks_freeres (void) attribute_hidden;
587extern void __shm_directory_freeres (void) attribute_hidden;
588
589extern void __wait_lookup_done (void) attribute_hidden;
590
591/* Allocates the extension space for ATTR. Returns an error code on
592 memory allocation failure, zero on success. If ATTR already has an
593 extension space, this function does nothing. */
594int __pthread_attr_extension (struct pthread_attr *attr) attribute_hidden
595 __attribute_warn_unused_result__;
596
597#ifdef SHARED
598# define PTHREAD_STATIC_FN_REQUIRE(name)
599#else
600# define PTHREAD_STATIC_FN_REQUIRE(name) __asm (".globl " #name);
601#endif
602
603/* Make a deep copy of the attribute *SOURCE in *TARGET. *TARGET is
604 not assumed to have been initialized. Returns 0 on success, or a
605 positive error code otherwise. */
606int __pthread_attr_copy (pthread_attr_t *target, const pthread_attr_t *source);
607libc_hidden_proto (__pthread_attr_copy)
608
609/* Returns 0 if POL is a valid scheduling policy. */
610static inline int
611check_sched_policy_attr (int pol)
612{
613 if (pol == SCHED_OTHER || pol == SCHED_FIFO || pol == SCHED_RR)
614 return 0;
615
616 return EINVAL;
617}
618
619/* Returns 0 if PR is within the accepted range of priority values for
620 the scheduling policy POL or EINVAL otherwise. */
621static inline int
622check_sched_priority_attr (int pr, int pol)
623{
624 int min = __sched_get_priority_min (pol);
625 int max = __sched_get_priority_max (pol);
626
627 if (min >= 0 && max >= 0 && pr >= min && pr <= max)
628 return 0;
629
630 return EINVAL;
631}
632
633/* Returns 0 if ST is a valid stack size for a thread stack and EINVAL
634 otherwise. */
635static inline int
636check_stacksize_attr (size_t st)
637{
638 if (st >= PTHREAD_STACK_MIN)
639 return 0;
640
641 return EINVAL;
642}
643
644#define ASSERT_TYPE_SIZE(type, size) \
645 _Static_assert (sizeof (type) == size, \
646 "sizeof (" #type ") != " #size)
647
648#define ASSERT_PTHREAD_INTERNAL_SIZE(type, internal) \
649 _Static_assert (sizeof ((type) { { 0 } }).__size >= sizeof (internal),\
650 "sizeof (" #type ".__size) < sizeof (" #internal ")")
651
652#define ASSERT_PTHREAD_STRING(x) __STRING (x)
653#define ASSERT_PTHREAD_INTERNAL_OFFSET(type, member, offset) \
654 _Static_assert (offsetof (type, member) == offset, \
655 "offset of " #member " field of " #type " != " \
656 ASSERT_PTHREAD_STRING (offset))
657#define ASSERT_PTHREAD_INTERNAL_MEMBER_SIZE(type, member, mtype) \
658 _Static_assert (sizeof (((type) { 0 }).member) != 8, \
659 "sizeof (" #type "." #member ") != sizeof (" #mtype "))")
660
661#endif /* pthreadP.h */
662