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