1/* Copyright (C) 2002-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#ifndef _PTHREADP_H
20#define _PTHREADP_H 1
21
22#include <pthread.h>
23#include <setjmp.h>
24#include <stdbool.h>
25#include <sys/syscall.h>
26#include "descr.h"
27#include <tls.h>
28#include <lowlevellock.h>
29#include <stackinfo.h>
30#include <internaltypes.h>
31#include <pthread-functions.h>
32#include <atomic.h>
33#include <kernel-features.h>
34#include <errno.h>
35#include <internal-signals.h>
36
37
38/* Atomic operations on TLS memory. */
39#ifndef THREAD_ATOMIC_CMPXCHG_VAL
40# define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, new, old) \
41 atomic_compare_and_exchange_val_acq (&(descr)->member, new, old)
42#endif
43
44#ifndef THREAD_ATOMIC_BIT_SET
45# define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
46 atomic_bit_set (&(descr)->member, bit)
47#endif
48
49
50/* Adaptive mutex definitions. */
51#ifndef MAX_ADAPTIVE_COUNT
52# define MAX_ADAPTIVE_COUNT 100
53#endif
54
55
56/* Magic cookie representing robust mutex with dead owner. */
57#define PTHREAD_MUTEX_INCONSISTENT INT_MAX
58/* Magic cookie representing not recoverable robust mutex. */
59#define PTHREAD_MUTEX_NOTRECOVERABLE (INT_MAX - 1)
60
61
62/* Internal mutex type value. */
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 struct pthread_attr __default_pthread_attr attribute_hidden;
196extern int __default_pthread_attr_lock attribute_hidden;
197
198/* Size and alignment of static TLS block. */
199extern size_t __static_tls_size attribute_hidden;
200extern size_t __static_tls_align_m1 attribute_hidden;
201
202/* Flag whether the machine is SMP or not. */
203extern int __is_smp attribute_hidden;
204
205/* Thread descriptor handling. */
206extern list_t __stack_user;
207hidden_proto (__stack_user)
208
209/* Attribute handling. */
210extern struct pthread_attr *__attr_list attribute_hidden;
211extern int __attr_list_lock attribute_hidden;
212
213/* Concurrency handling. */
214extern int __concurrency_level attribute_hidden;
215
216/* Thread-local data key handling. */
217extern struct pthread_key_struct __pthread_keys[PTHREAD_KEYS_MAX];
218hidden_proto (__pthread_keys)
219
220/* Number of threads running. */
221extern unsigned int __nptl_nthreads attribute_hidden;
222
223#ifndef __ASSUME_SET_ROBUST_LIST
224/* Negative if we do not have the system call and we can use it. */
225extern int __set_robust_list_avail attribute_hidden;
226#endif
227
228/* Thread Priority Protection. */
229extern int __sched_fifo_min_prio attribute_hidden;
230extern int __sched_fifo_max_prio attribute_hidden;
231extern void __init_sched_fifo_prio (void) attribute_hidden;
232extern int __pthread_tpp_change_priority (int prev_prio, int new_prio)
233 attribute_hidden;
234extern int __pthread_current_priority (void) attribute_hidden;
235
236/* The library can run in debugging mode where it performs a lot more
237 tests. */
238extern int __pthread_debug attribute_hidden;
239/** For now disable debugging support. */
240#if 0
241# define DEBUGGING_P __builtin_expect (__pthread_debug, 0)
242# define INVALID_TD_P(pd) (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
243# define INVALID_NOT_TERMINATED_TD_P(pd) INVALID_TD_P (pd)
244#else
245# define DEBUGGING_P 0
246/* Simplified test. This will not catch all invalid descriptors but
247 is better than nothing. And if the test triggers the thread
248 descriptor is guaranteed to be invalid. */
249# define INVALID_TD_P(pd) __builtin_expect ((pd)->tid <= 0, 0)
250# define INVALID_NOT_TERMINATED_TD_P(pd) __builtin_expect ((pd)->tid < 0, 0)
251#endif
252
253
254/* Cancellation test. */
255#define CANCELLATION_P(self) \
256 do { \
257 int cancelhandling = THREAD_GETMEM (self, cancelhandling); \
258 if (CANCEL_ENABLED_AND_CANCELED (cancelhandling)) \
259 { \
260 THREAD_SETMEM (self, result, PTHREAD_CANCELED); \
261 __do_cancel (); \
262 } \
263 } while (0)
264
265
266extern void __pthread_unwind (__pthread_unwind_buf_t *__buf)
267 __cleanup_fct_attribute __attribute ((__noreturn__))
268#if !defined SHARED && !IS_IN (libpthread)
269 weak_function
270#endif
271 ;
272extern void __pthread_unwind_next (__pthread_unwind_buf_t *__buf)
273 __cleanup_fct_attribute __attribute ((__noreturn__))
274#ifndef SHARED
275 weak_function
276#endif
277 ;
278extern void __pthread_register_cancel (__pthread_unwind_buf_t *__buf)
279 __cleanup_fct_attribute;
280extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf)
281 __cleanup_fct_attribute;
282#if IS_IN (libpthread)
283hidden_proto (__pthread_unwind)
284hidden_proto (__pthread_unwind_next)
285hidden_proto (__pthread_register_cancel)
286hidden_proto (__pthread_unregister_cancel)
287# ifdef SHARED
288extern void attribute_hidden pthread_cancel_init (void);
289# endif
290extern void __nptl_unwind_freeres (void) attribute_hidden;
291#endif
292
293
294/* Called when a thread reacts on a cancellation request. */
295static inline void
296__attribute ((noreturn, always_inline))
297__do_cancel (void)
298{
299 struct pthread *self = THREAD_SELF;
300
301 /* Make sure we get no more cancellations. */
302 THREAD_ATOMIC_BIT_SET (self, cancelhandling, EXITING_BIT);
303
304 __pthread_unwind ((__pthread_unwind_buf_t *)
305 THREAD_GETMEM (self, cleanup_jmp_buf));
306}
307
308
309/* Set cancellation mode to asynchronous. */
310#define CANCEL_ASYNC() \
311 __pthread_enable_asynccancel ()
312/* Reset to previous cancellation mode. */
313#define CANCEL_RESET(oldtype) \
314 __pthread_disable_asynccancel (oldtype)
315
316#if IS_IN (libc)
317/* Same as CANCEL_ASYNC, but for use in libc.so. */
318# define LIBC_CANCEL_ASYNC() \
319 __libc_enable_asynccancel ()
320/* Same as CANCEL_RESET, but for use in libc.so. */
321# define LIBC_CANCEL_RESET(oldtype) \
322 __libc_disable_asynccancel (oldtype)
323# define LIBC_CANCEL_HANDLED() \
324 __asm (".globl " __SYMBOL_PREFIX "__libc_enable_asynccancel"); \
325 __asm (".globl " __SYMBOL_PREFIX "__libc_disable_asynccancel")
326#elif IS_IN (libpthread)
327# define LIBC_CANCEL_ASYNC() CANCEL_ASYNC ()
328# define LIBC_CANCEL_RESET(val) CANCEL_RESET (val)
329# define LIBC_CANCEL_HANDLED() \
330 __asm (".globl " __SYMBOL_PREFIX "__pthread_enable_asynccancel"); \
331 __asm (".globl " __SYMBOL_PREFIX "__pthread_disable_asynccancel")
332#elif IS_IN (librt)
333# define LIBC_CANCEL_ASYNC() \
334 __librt_enable_asynccancel ()
335# define LIBC_CANCEL_RESET(val) \
336 __librt_disable_asynccancel (val)
337# define LIBC_CANCEL_HANDLED() \
338 __asm (".globl " __SYMBOL_PREFIX "__librt_enable_asynccancel"); \
339 __asm (".globl " __SYMBOL_PREFIX "__librt_disable_asynccancel")
340#else
341# define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */
342# define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */
343# define LIBC_CANCEL_HANDLED() /* Nothing. */
344#endif
345
346
347/* Internal prototypes. */
348
349/* Thread list handling. */
350extern struct pthread *__find_in_stack_list (struct pthread *pd)
351 attribute_hidden;
352
353/* Deallocate a thread's stack after optionally making sure the thread
354 descriptor is still valid. */
355extern void __free_tcb (struct pthread *pd) attribute_hidden;
356
357/* Free allocated stack. */
358extern void __deallocate_stack (struct pthread *pd) attribute_hidden;
359
360/* Mark all the stacks except for the current one as available. This
361 function also re-initializes the lock for the stack cache. */
362extern void __reclaim_stacks (void) attribute_hidden;
363
364/* Make all threads's stacks executable. */
365extern int __make_stacks_executable (void **stack_endp) attribute_hidden;
366
367/* longjmp handling. */
368extern void __pthread_cleanup_upto (__jmp_buf target, char *targetframe);
369#if IS_IN (libpthread)
370hidden_proto (__pthread_cleanup_upto)
371#endif
372
373
374/* Functions with versioned interfaces. */
375extern int __pthread_create_2_1 (pthread_t *newthread,
376 const pthread_attr_t *attr,
377 void *(*start_routine) (void *), void *arg);
378extern int __pthread_create_2_0 (pthread_t *newthread,
379 const pthread_attr_t *attr,
380 void *(*start_routine) (void *), void *arg);
381extern int __pthread_attr_init_2_1 (pthread_attr_t *attr);
382extern int __pthread_attr_init_2_0 (pthread_attr_t *attr);
383
384
385/* Event handlers for libthread_db interface. */
386extern void __nptl_create_event (void);
387extern void __nptl_death_event (void);
388hidden_proto (__nptl_create_event)
389hidden_proto (__nptl_death_event)
390
391/* Register the generation counter in the libpthread with the libc. */
392#ifdef TLS_MULTIPLE_THREADS_IN_TCB
393extern void __libc_pthread_init (unsigned long int *ptr,
394 void (*reclaim) (void),
395 const struct pthread_functions *functions);
396#else
397extern int *__libc_pthread_init (unsigned long int *ptr,
398 void (*reclaim) (void),
399 const struct pthread_functions *functions);
400
401/* Variable set to a nonzero value either if more than one thread runs or ran,
402 or if a single-threaded process is trying to cancel itself. See
403 nptl/descr.h for more context on the single-threaded process case. */
404extern int __pthread_multiple_threads attribute_hidden;
405/* Pointer to the corresponding variable in libc. */
406extern int *__libc_multiple_threads_ptr attribute_hidden;
407#endif
408
409/* Find a thread given its TID. */
410extern struct pthread *__find_thread_by_id (pid_t tid) attribute_hidden
411#ifdef SHARED
412;
413#else
414weak_function;
415#define __find_thread_by_id(tid) \
416 (__find_thread_by_id ? (__find_thread_by_id) (tid) : (struct pthread *) NULL)
417#endif
418
419extern void __pthread_init_static_tls (struct link_map *) attribute_hidden;
420
421extern size_t __pthread_get_minstack (const pthread_attr_t *attr);
422
423/* Namespace save aliases. */
424extern int __pthread_getschedparam (pthread_t thread_id, int *policy,
425 struct sched_param *param);
426extern int __pthread_setschedparam (pthread_t thread_id, int policy,
427 const struct sched_param *param);
428extern int __pthread_setcancelstate (int state, int *oldstate);
429extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
430 const pthread_mutexattr_t *__mutexattr);
431extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
432extern int __pthread_mutex_trylock (pthread_mutex_t *_mutex);
433extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
434extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex,
435 const struct timespec *__abstime);
436extern int __pthread_mutex_cond_lock (pthread_mutex_t *__mutex)
437 attribute_hidden;
438extern void __pthread_mutex_cond_lock_adjust (pthread_mutex_t *__mutex)
439 attribute_hidden;
440extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
441extern int __pthread_mutex_unlock_usercnt (pthread_mutex_t *__mutex,
442 int __decr) attribute_hidden;
443extern int __pthread_mutexattr_init (pthread_mutexattr_t *attr);
444extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *attr);
445extern int __pthread_mutexattr_settype (pthread_mutexattr_t *attr, int kind);
446extern int __pthread_attr_destroy (pthread_attr_t *attr);
447extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
448 int *detachstate);
449extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
450 int detachstate);
451extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
452 int *inherit);
453extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
454extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
455 struct sched_param *param);
456extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
457 const struct sched_param *param);
458extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
459 int *policy);
460extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
461extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
462extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
463extern int __pthread_attr_getstackaddr (const pthread_attr_t *__restrict
464 __attr, void **__restrict __stackaddr);
465extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
466 void *__stackaddr);
467extern int __pthread_attr_getstacksize (const pthread_attr_t *__restrict
468 __attr,
469 size_t *__restrict __stacksize);
470extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
471 size_t __stacksize);
472extern int __pthread_attr_getstack (const pthread_attr_t *__restrict __attr,
473 void **__restrict __stackaddr,
474 size_t *__restrict __stacksize);
475extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
476 size_t __stacksize);
477extern int __pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
478 const pthread_rwlockattr_t *__restrict
479 __attr);
480extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
481extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
482extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
483extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
484extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
485extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
486extern int __pthread_cond_broadcast (pthread_cond_t *cond);
487extern int __pthread_cond_destroy (pthread_cond_t *cond);
488extern int __pthread_cond_init (pthread_cond_t *cond,
489 const pthread_condattr_t *cond_attr);
490extern int __pthread_cond_signal (pthread_cond_t *cond);
491extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
492extern int __pthread_cond_timedwait (pthread_cond_t *cond,
493 pthread_mutex_t *mutex,
494 const struct timespec *abstime);
495extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
496extern int __pthread_condattr_init (pthread_condattr_t *attr);
497extern int __pthread_key_create (pthread_key_t *key, void (*destr) (void *));
498extern int __pthread_key_delete (pthread_key_t key);
499extern void *__pthread_getspecific (pthread_key_t key);
500extern int __pthread_setspecific (pthread_key_t key, const void *value);
501extern int __pthread_once (pthread_once_t *once_control,
502 void (*init_routine) (void));
503extern int __pthread_atfork (void (*prepare) (void), void (*parent) (void),
504 void (*child) (void));
505extern pthread_t __pthread_self (void);
506extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
507extern int __pthread_detach (pthread_t th);
508extern int __pthread_cancel (pthread_t th);
509extern int __pthread_kill (pthread_t threadid, int signo);
510extern void __pthread_exit (void *value) __attribute__ ((__noreturn__));
511extern int __pthread_join (pthread_t threadid, void **thread_return);
512extern int __pthread_setcanceltype (int type, int *oldtype);
513extern int __pthread_enable_asynccancel (void) attribute_hidden;
514extern void __pthread_disable_asynccancel (int oldtype) attribute_hidden;
515extern void __pthread_testcancel (void);
516extern int __pthread_timedjoin_ex (pthread_t, void **, const struct timespec *,
517 bool);
518
519#if IS_IN (libpthread)
520hidden_proto (__pthread_mutex_init)
521hidden_proto (__pthread_mutex_destroy)
522hidden_proto (__pthread_mutex_lock)
523hidden_proto (__pthread_mutex_trylock)
524hidden_proto (__pthread_mutex_unlock)
525hidden_proto (__pthread_rwlock_rdlock)
526hidden_proto (__pthread_rwlock_wrlock)
527hidden_proto (__pthread_rwlock_unlock)
528hidden_proto (__pthread_key_create)
529hidden_proto (__pthread_getspecific)
530hidden_proto (__pthread_setspecific)
531hidden_proto (__pthread_once)
532hidden_proto (__pthread_setcancelstate)
533hidden_proto (__pthread_testcancel)
534hidden_proto (__pthread_mutexattr_init)
535hidden_proto (__pthread_mutexattr_settype)
536hidden_proto (__pthread_timedjoin_ex)
537#endif
538
539extern int __pthread_cond_broadcast_2_0 (pthread_cond_2_0_t *cond);
540extern int __pthread_cond_destroy_2_0 (pthread_cond_2_0_t *cond);
541extern int __pthread_cond_init_2_0 (pthread_cond_2_0_t *cond,
542 const pthread_condattr_t *cond_attr);
543extern int __pthread_cond_signal_2_0 (pthread_cond_2_0_t *cond);
544extern int __pthread_cond_timedwait_2_0 (pthread_cond_2_0_t *cond,
545 pthread_mutex_t *mutex,
546 const struct timespec *abstime);
547extern int __pthread_cond_wait_2_0 (pthread_cond_2_0_t *cond,
548 pthread_mutex_t *mutex);
549
550extern int __pthread_getaffinity_np (pthread_t th, size_t cpusetsize,
551 cpu_set_t *cpuset);
552
553/* The two functions are in libc.so and not exported. */
554extern int __libc_enable_asynccancel (void) attribute_hidden;
555extern void __libc_disable_asynccancel (int oldtype) attribute_hidden;
556
557
558/* The two functions are in librt.so and not exported. */
559extern int __librt_enable_asynccancel (void) attribute_hidden;
560extern void __librt_disable_asynccancel (int oldtype) attribute_hidden;
561
562#if IS_IN (libpthread)
563/* Special versions which use non-exported functions. */
564extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
565 void (*routine) (void *), void *arg)
566 attribute_hidden;
567
568/* Replace cleanup macros defined in <pthread.h> with internal
569 versions that don't depend on unwind info and better support
570 cancellation. */
571# undef pthread_cleanup_push
572# define pthread_cleanup_push(routine,arg) \
573 { struct _pthread_cleanup_buffer _buffer; \
574 __pthread_cleanup_push (&_buffer, (routine), (arg));
575
576extern void __pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
577 int execute) attribute_hidden;
578# undef pthread_cleanup_pop
579# define pthread_cleanup_pop(execute) \
580 __pthread_cleanup_pop (&_buffer, (execute)); }
581#endif
582
583extern void __pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
584 void (*routine) (void *), void *arg);
585extern void __pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
586 int execute);
587
588/* Old cleanup interfaces, still used in libc.so. */
589extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
590 void (*routine) (void *), void *arg);
591extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
592 int execute);
593extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
594 void (*routine) (void *), void *arg);
595extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
596 int execute);
597
598extern void __nptl_deallocate_tsd (void) attribute_hidden;
599
600extern void __nptl_setxid_error (struct xid_command *cmdp, int error)
601 attribute_hidden;
602extern int __nptl_setxid (struct xid_command *cmdp) attribute_hidden;
603#ifndef SHARED
604extern void __nptl_set_robust (struct pthread *self);
605#endif
606
607extern void __nptl_stacks_freeres (void) attribute_hidden;
608extern void __shm_directory_freeres (void) attribute_hidden;
609
610extern void __wait_lookup_done (void) attribute_hidden;
611
612#ifdef SHARED
613# define PTHREAD_STATIC_FN_REQUIRE(name)
614#else
615# define PTHREAD_STATIC_FN_REQUIRE(name) __asm (".globl " #name);
616#endif
617
618/* Returns 0 if POL is a valid scheduling policy. */
619static inline int
620check_sched_policy_attr (int pol)
621{
622 if (pol == SCHED_OTHER || pol == SCHED_FIFO || pol == SCHED_RR)
623 return 0;
624
625 return EINVAL;
626}
627
628/* Returns 0 if PR is within the accepted range of priority values for
629 the scheduling policy POL or EINVAL otherwise. */
630static inline int
631check_sched_priority_attr (int pr, int pol)
632{
633 int min = __sched_get_priority_min (pol);
634 int max = __sched_get_priority_max (pol);
635
636 if (min >= 0 && max >= 0 && pr >= min && pr <= max)
637 return 0;
638
639 return EINVAL;
640}
641
642/* Returns 0 if ST is a valid stack size for a thread stack and EINVAL
643 otherwise. */
644static inline int
645check_stacksize_attr (size_t st)
646{
647 if (st >= PTHREAD_STACK_MIN)
648 return 0;
649
650 return EINVAL;
651}
652
653#define ASSERT_TYPE_SIZE(type, size) \
654 _Static_assert (sizeof (type) == size, \
655 "sizeof (" #type ") != " #size)
656
657#define ASSERT_PTHREAD_INTERNAL_SIZE(type, internal) \
658 _Static_assert (sizeof ((type) { { 0 } }).__size >= sizeof (internal),\
659 "sizeof (" #type ".__size) < sizeof (" #internal ")")
660
661#define ASSERT_PTHREAD_STRING(x) __STRING (x)
662#define ASSERT_PTHREAD_INTERNAL_OFFSET(type, member, offset) \
663 _Static_assert (offsetof (type, member) == offset, \
664 "offset of " #member " field of " #type " != " \
665 ASSERT_PTHREAD_STRING (offset))
666
667#endif /* pthreadP.h */
668