1/* Copyright (C) 2002-2016 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 <nptl-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#define PTHREAD_MUTEX_TYPE(m) \
114 ((m)->__data.__kind & 127)
115/* Don't include NO_ELISION, as that type is always the same
116 as the underlying lock type. */
117#define PTHREAD_MUTEX_TYPE_ELISION(m) \
118 ((m)->__data.__kind & (127|PTHREAD_MUTEX_ELISION_NP))
119
120#if LLL_PRIVATE == 0 && LLL_SHARED == 128
121# define PTHREAD_MUTEX_PSHARED(m) \
122 ((m)->__data.__kind & 128)
123#else
124# define PTHREAD_MUTEX_PSHARED(m) \
125 (((m)->__data.__kind & 128) ? LLL_SHARED : LLL_PRIVATE)
126#endif
127
128/* The kernel when waking robust mutexes on exit never uses
129 FUTEX_PRIVATE_FLAG FUTEX_WAKE. */
130#define PTHREAD_ROBUST_MUTEX_PSHARED(m) LLL_SHARED
131
132/* Ceiling in __data.__lock. __data.__lock is signed, so don't
133 use the MSB bit in there, but in the mask also include that bit,
134 so that the compiler can optimize & PTHREAD_MUTEX_PRIO_CEILING_MASK
135 masking if the value is then shifted down by
136 PTHREAD_MUTEX_PRIO_CEILING_SHIFT. */
137#define PTHREAD_MUTEX_PRIO_CEILING_SHIFT 19
138#define PTHREAD_MUTEX_PRIO_CEILING_MASK 0xfff80000
139
140
141/* Flags in mutex attr. */
142#define PTHREAD_MUTEXATTR_PROTOCOL_SHIFT 28
143#define PTHREAD_MUTEXATTR_PROTOCOL_MASK 0x30000000
144#define PTHREAD_MUTEXATTR_PRIO_CEILING_SHIFT 12
145#define PTHREAD_MUTEXATTR_PRIO_CEILING_MASK 0x00fff000
146#define PTHREAD_MUTEXATTR_FLAG_ROBUST 0x40000000
147#define PTHREAD_MUTEXATTR_FLAG_PSHARED 0x80000000
148#define PTHREAD_MUTEXATTR_FLAG_BITS \
149 (PTHREAD_MUTEXATTR_FLAG_ROBUST | PTHREAD_MUTEXATTR_FLAG_PSHARED \
150 | PTHREAD_MUTEXATTR_PROTOCOL_MASK | PTHREAD_MUTEXATTR_PRIO_CEILING_MASK)
151
152
153/* Check whether rwlock prefers readers. */
154#define PTHREAD_RWLOCK_PREFER_READER_P(rwlock) \
155 ((rwlock)->__data.__flags == 0)
156
157
158/* Bits used in robust mutex implementation. */
159#define FUTEX_WAITERS 0x80000000
160#define FUTEX_OWNER_DIED 0x40000000
161#define FUTEX_TID_MASK 0x3fffffff
162
163
164/* pthread_once definitions. See __pthread_once for how these are used. */
165#define __PTHREAD_ONCE_INPROGRESS 1
166#define __PTHREAD_ONCE_DONE 2
167#define __PTHREAD_ONCE_FORK_GEN_INCR 4
168
169
170/* Internal variables. */
171
172
173/* Default pthread attributes. */
174extern struct pthread_attr __default_pthread_attr attribute_hidden;
175extern int __default_pthread_attr_lock attribute_hidden;
176
177/* Size and alignment of static TLS block. */
178extern size_t __static_tls_size attribute_hidden;
179extern size_t __static_tls_align_m1 attribute_hidden;
180
181/* Flag whether the machine is SMP or not. */
182extern int __is_smp attribute_hidden;
183
184/* Thread descriptor handling. */
185extern list_t __stack_user;
186hidden_proto (__stack_user)
187
188/* Attribute handling. */
189extern struct pthread_attr *__attr_list attribute_hidden;
190extern int __attr_list_lock attribute_hidden;
191
192/* Concurrency handling. */
193extern int __concurrency_level attribute_hidden;
194
195/* Thread-local data key handling. */
196extern struct pthread_key_struct __pthread_keys[PTHREAD_KEYS_MAX];
197hidden_proto (__pthread_keys)
198
199/* Number of threads running. */
200extern unsigned int __nptl_nthreads attribute_hidden;
201
202#ifndef __ASSUME_SET_ROBUST_LIST
203/* Negative if we do not have the system call and we can use it. */
204extern int __set_robust_list_avail attribute_hidden;
205#endif
206
207/* Thread Priority Protection. */
208extern int __sched_fifo_min_prio attribute_hidden;
209extern int __sched_fifo_max_prio attribute_hidden;
210extern void __init_sched_fifo_prio (void) attribute_hidden;
211extern int __pthread_tpp_change_priority (int prev_prio, int new_prio)
212 attribute_hidden;
213extern int __pthread_current_priority (void) attribute_hidden;
214
215/* The library can run in debugging mode where it performs a lot more
216 tests. */
217extern int __pthread_debug attribute_hidden;
218/** For now disable debugging support. */
219#if 0
220# define DEBUGGING_P __builtin_expect (__pthread_debug, 0)
221# define INVALID_TD_P(pd) (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
222# define INVALID_NOT_TERMINATED_TD_P(pd) INVALID_TD_P (pd)
223#else
224# define DEBUGGING_P 0
225/* Simplified test. This will not catch all invalid descriptors but
226 is better than nothing. And if the test triggers the thread
227 descriptor is guaranteed to be invalid. */
228# define INVALID_TD_P(pd) __builtin_expect ((pd)->tid <= 0, 0)
229# define INVALID_NOT_TERMINATED_TD_P(pd) __builtin_expect ((pd)->tid < 0, 0)
230#endif
231
232
233/* Cancellation test. */
234#define CANCELLATION_P(self) \
235 do { \
236 int cancelhandling = THREAD_GETMEM (self, cancelhandling); \
237 if (CANCEL_ENABLED_AND_CANCELED (cancelhandling)) \
238 { \
239 THREAD_SETMEM (self, result, PTHREAD_CANCELED); \
240 __do_cancel (); \
241 } \
242 } while (0)
243
244
245extern void __pthread_unwind (__pthread_unwind_buf_t *__buf)
246 __cleanup_fct_attribute __attribute ((__noreturn__))
247#if !defined SHARED && !IS_IN (libpthread)
248 weak_function
249#endif
250 ;
251extern void __pthread_unwind_next (__pthread_unwind_buf_t *__buf)
252 __cleanup_fct_attribute __attribute ((__noreturn__))
253#ifndef SHARED
254 weak_function
255#endif
256 ;
257extern void __pthread_register_cancel (__pthread_unwind_buf_t *__buf)
258 __cleanup_fct_attribute;
259extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf)
260 __cleanup_fct_attribute;
261#if IS_IN (libpthread)
262hidden_proto (__pthread_unwind)
263hidden_proto (__pthread_unwind_next)
264hidden_proto (__pthread_register_cancel)
265hidden_proto (__pthread_unregister_cancel)
266# ifdef SHARED
267extern void attribute_hidden pthread_cancel_init (void);
268extern void __unwind_freeres (void);
269# endif
270#endif
271
272
273/* Called when a thread reacts on a cancellation request. */
274static inline void
275__attribute ((noreturn, always_inline))
276__do_cancel (void)
277{
278 struct pthread *self = THREAD_SELF;
279
280 /* Make sure we get no more cancellations. */
281 THREAD_ATOMIC_BIT_SET (self, cancelhandling, EXITING_BIT);
282
283 __pthread_unwind ((__pthread_unwind_buf_t *)
284 THREAD_GETMEM (self, cleanup_jmp_buf));
285}
286
287
288/* Set cancellation mode to asynchronous. */
289#define CANCEL_ASYNC() \
290 __pthread_enable_asynccancel ()
291/* Reset to previous cancellation mode. */
292#define CANCEL_RESET(oldtype) \
293 __pthread_disable_asynccancel (oldtype)
294
295#if IS_IN (libc)
296/* Same as CANCEL_ASYNC, but for use in libc.so. */
297# define LIBC_CANCEL_ASYNC() \
298 __libc_enable_asynccancel ()
299/* Same as CANCEL_RESET, but for use in libc.so. */
300# define LIBC_CANCEL_RESET(oldtype) \
301 __libc_disable_asynccancel (oldtype)
302# define LIBC_CANCEL_HANDLED() \
303 __asm (".globl " __SYMBOL_PREFIX "__libc_enable_asynccancel"); \
304 __asm (".globl " __SYMBOL_PREFIX "__libc_disable_asynccancel")
305#elif IS_IN (libpthread)
306# define LIBC_CANCEL_ASYNC() CANCEL_ASYNC ()
307# define LIBC_CANCEL_RESET(val) CANCEL_RESET (val)
308# define LIBC_CANCEL_HANDLED() \
309 __asm (".globl " __SYMBOL_PREFIX "__pthread_enable_asynccancel"); \
310 __asm (".globl " __SYMBOL_PREFIX "__pthread_disable_asynccancel")
311#elif IS_IN (librt)
312# define LIBC_CANCEL_ASYNC() \
313 __librt_enable_asynccancel ()
314# define LIBC_CANCEL_RESET(val) \
315 __librt_disable_asynccancel (val)
316# define LIBC_CANCEL_HANDLED() \
317 __asm (".globl " __SYMBOL_PREFIX "__librt_enable_asynccancel"); \
318 __asm (".globl " __SYMBOL_PREFIX "__librt_disable_asynccancel")
319#else
320# define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */
321# define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */
322# define LIBC_CANCEL_HANDLED() /* Nothing. */
323#endif
324
325
326/* Internal prototypes. */
327
328/* Thread list handling. */
329extern struct pthread *__find_in_stack_list (struct pthread *pd)
330 attribute_hidden internal_function;
331
332/* Deallocate a thread's stack after optionally making sure the thread
333 descriptor is still valid. */
334extern void __free_tcb (struct pthread *pd) attribute_hidden internal_function;
335
336/* Free allocated stack. */
337extern void __deallocate_stack (struct pthread *pd)
338 attribute_hidden internal_function;
339
340/* Mark all the stacks except for the current one as available. This
341 function also re-initializes the lock for the stack cache. */
342extern void __reclaim_stacks (void) attribute_hidden;
343
344/* Make all threads's stacks executable. */
345extern int __make_stacks_executable (void **stack_endp)
346 internal_function attribute_hidden;
347
348/* longjmp handling. */
349extern void __pthread_cleanup_upto (__jmp_buf target, char *targetframe);
350#if IS_IN (libpthread)
351hidden_proto (__pthread_cleanup_upto)
352#endif
353
354
355/* Functions with versioned interfaces. */
356extern int __pthread_create_2_1 (pthread_t *newthread,
357 const pthread_attr_t *attr,
358 void *(*start_routine) (void *), void *arg);
359extern int __pthread_create_2_0 (pthread_t *newthread,
360 const pthread_attr_t *attr,
361 void *(*start_routine) (void *), void *arg);
362extern int __pthread_attr_init_2_1 (pthread_attr_t *attr);
363extern int __pthread_attr_init_2_0 (pthread_attr_t *attr);
364
365
366/* Event handlers for libthread_db interface. */
367extern void __nptl_create_event (void);
368extern void __nptl_death_event (void);
369hidden_proto (__nptl_create_event)
370hidden_proto (__nptl_death_event)
371
372/* Register the generation counter in the libpthread with the libc. */
373#ifdef TLS_MULTIPLE_THREADS_IN_TCB
374extern void __libc_pthread_init (unsigned long int *ptr,
375 void (*reclaim) (void),
376 const struct pthread_functions *functions)
377 internal_function;
378#else
379extern int *__libc_pthread_init (unsigned long int *ptr,
380 void (*reclaim) (void),
381 const struct pthread_functions *functions)
382 internal_function;
383
384/* Variable set to a nonzero value either if more than one thread runs or ran,
385 or if a single-threaded process is trying to cancel itself. See
386 nptl/descr.h for more context on the single-threaded process case. */
387extern int __pthread_multiple_threads attribute_hidden;
388/* Pointer to the corresponding variable in libc. */
389extern int *__libc_multiple_threads_ptr attribute_hidden;
390#endif
391
392/* Find a thread given its TID. */
393extern struct pthread *__find_thread_by_id (pid_t tid) attribute_hidden
394#ifdef SHARED
395;
396#else
397weak_function;
398#define __find_thread_by_id(tid) \
399 (__find_thread_by_id ? (__find_thread_by_id) (tid) : (struct pthread *) NULL)
400#endif
401
402extern void __pthread_init_static_tls (struct link_map *) attribute_hidden;
403
404extern size_t __pthread_get_minstack (const pthread_attr_t *attr);
405
406/* Namespace save aliases. */
407extern int __pthread_getschedparam (pthread_t thread_id, int *policy,
408 struct sched_param *param);
409extern int __pthread_setschedparam (pthread_t thread_id, int policy,
410 const struct sched_param *param);
411extern int __pthread_setcancelstate (int state, int *oldstate);
412extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
413 const pthread_mutexattr_t *__mutexattr);
414extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
415extern int __pthread_mutex_trylock (pthread_mutex_t *_mutex);
416extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
417extern int __pthread_mutex_cond_lock (pthread_mutex_t *__mutex)
418 attribute_hidden internal_function;
419extern void __pthread_mutex_cond_lock_adjust (pthread_mutex_t *__mutex)
420 attribute_hidden internal_function;
421extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
422extern int __pthread_mutex_unlock_usercnt (pthread_mutex_t *__mutex,
423 int __decr)
424 attribute_hidden internal_function;
425extern int __pthread_mutexattr_init (pthread_mutexattr_t *attr);
426extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *attr);
427extern int __pthread_mutexattr_settype (pthread_mutexattr_t *attr, int kind);
428extern int __pthread_attr_destroy (pthread_attr_t *attr);
429extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
430 int *detachstate);
431extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
432 int detachstate);
433extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
434 int *inherit);
435extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
436extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
437 struct sched_param *param);
438extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
439 const struct sched_param *param);
440extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
441 int *policy);
442extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
443extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
444extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
445extern int __pthread_attr_getstackaddr (const pthread_attr_t *__restrict
446 __attr, void **__restrict __stackaddr);
447extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
448 void *__stackaddr);
449extern int __pthread_attr_getstacksize (const pthread_attr_t *__restrict
450 __attr,
451 size_t *__restrict __stacksize);
452extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
453 size_t __stacksize);
454extern int __pthread_attr_getstack (const pthread_attr_t *__restrict __attr,
455 void **__restrict __stackaddr,
456 size_t *__restrict __stacksize);
457extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
458 size_t __stacksize);
459extern int __pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
460 const pthread_rwlockattr_t *__restrict
461 __attr);
462extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
463extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
464extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
465extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
466extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
467extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
468extern int __pthread_cond_broadcast (pthread_cond_t *cond);
469extern int __pthread_cond_destroy (pthread_cond_t *cond);
470extern int __pthread_cond_init (pthread_cond_t *cond,
471 const pthread_condattr_t *cond_attr);
472extern int __pthread_cond_signal (pthread_cond_t *cond);
473extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
474extern int __pthread_cond_timedwait (pthread_cond_t *cond,
475 pthread_mutex_t *mutex,
476 const struct timespec *abstime);
477extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
478extern int __pthread_condattr_init (pthread_condattr_t *attr);
479extern int __pthread_key_create (pthread_key_t *key, void (*destr) (void *));
480extern void *__pthread_getspecific (pthread_key_t key);
481extern int __pthread_setspecific (pthread_key_t key, const void *value);
482extern int __pthread_once (pthread_once_t *once_control,
483 void (*init_routine) (void));
484extern int __pthread_atfork (void (*prepare) (void), void (*parent) (void),
485 void (*child) (void));
486extern pthread_t __pthread_self (void);
487extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
488extern int __pthread_kill (pthread_t threadid, int signo);
489extern void __pthread_exit (void *value) __attribute__ ((__noreturn__));
490extern int __pthread_setcanceltype (int type, int *oldtype);
491extern int __pthread_enable_asynccancel (void) attribute_hidden;
492extern void __pthread_disable_asynccancel (int oldtype)
493 internal_function attribute_hidden;
494
495#if IS_IN (libpthread)
496hidden_proto (__pthread_mutex_init)
497hidden_proto (__pthread_mutex_destroy)
498hidden_proto (__pthread_mutex_lock)
499hidden_proto (__pthread_mutex_unlock)
500hidden_proto (__pthread_rwlock_rdlock)
501hidden_proto (__pthread_rwlock_wrlock)
502hidden_proto (__pthread_rwlock_unlock)
503hidden_proto (__pthread_key_create)
504hidden_proto (__pthread_getspecific)
505hidden_proto (__pthread_setspecific)
506hidden_proto (__pthread_once)
507hidden_proto (__pthread_setcancelstate)
508#endif
509
510extern int __pthread_cond_broadcast_2_0 (pthread_cond_2_0_t *cond);
511extern int __pthread_cond_destroy_2_0 (pthread_cond_2_0_t *cond);
512extern int __pthread_cond_init_2_0 (pthread_cond_2_0_t *cond,
513 const pthread_condattr_t *cond_attr);
514extern int __pthread_cond_signal_2_0 (pthread_cond_2_0_t *cond);
515extern int __pthread_cond_timedwait_2_0 (pthread_cond_2_0_t *cond,
516 pthread_mutex_t *mutex,
517 const struct timespec *abstime);
518extern int __pthread_cond_wait_2_0 (pthread_cond_2_0_t *cond,
519 pthread_mutex_t *mutex);
520
521extern int __pthread_getaffinity_np (pthread_t th, size_t cpusetsize,
522 cpu_set_t *cpuset);
523
524/* The two functions are in libc.so and not exported. */
525extern int __libc_enable_asynccancel (void) attribute_hidden;
526extern void __libc_disable_asynccancel (int oldtype)
527 internal_function attribute_hidden;
528
529
530/* The two functions are in librt.so and not exported. */
531extern int __librt_enable_asynccancel (void) attribute_hidden;
532extern void __librt_disable_asynccancel (int oldtype)
533 internal_function attribute_hidden;
534
535#if IS_IN (libpthread)
536/* Special versions which use non-exported functions. */
537extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
538 void (*routine) (void *), void *arg)
539 attribute_hidden;
540
541/* Replace cleanup macros defined in <pthread.h> with internal
542 versions that don't depend on unwind info and better support
543 cancellation. */
544# undef pthread_cleanup_push
545# define pthread_cleanup_push(routine,arg) \
546 { struct _pthread_cleanup_buffer _buffer; \
547 __pthread_cleanup_push (&_buffer, (routine), (arg));
548
549extern void __pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
550 int execute) attribute_hidden;
551# undef pthread_cleanup_pop
552# define pthread_cleanup_pop(execute) \
553 __pthread_cleanup_pop (&_buffer, (execute)); }
554#endif
555
556extern void __pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
557 void (*routine) (void *), void *arg);
558extern void __pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
559 int execute);
560
561/* Old cleanup interfaces, still used in libc.so. */
562extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
563 void (*routine) (void *), void *arg);
564extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
565 int execute);
566extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
567 void (*routine) (void *), void *arg);
568extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
569 int execute);
570
571extern void __nptl_deallocate_tsd (void) attribute_hidden;
572
573extern void __nptl_setxid_error (struct xid_command *cmdp, int error)
574 attribute_hidden;
575extern int __nptl_setxid (struct xid_command *cmdp) attribute_hidden;
576#ifndef SHARED
577extern void __nptl_set_robust (struct pthread *self);
578#endif
579
580extern void __free_stacks (size_t limit) attribute_hidden;
581
582extern void __wait_lookup_done (void) attribute_hidden;
583
584#ifdef SHARED
585# define PTHREAD_STATIC_FN_REQUIRE(name)
586#else
587# define PTHREAD_STATIC_FN_REQUIRE(name) __asm (".globl " #name);
588#endif
589
590/* Test if the mutex is suitable for the FUTEX_WAIT_REQUEUE_PI operation. */
591#if (defined lll_futex_wait_requeue_pi \
592 && defined __ASSUME_REQUEUE_PI)
593# define USE_REQUEUE_PI(mut) \
594 ((mut) && (mut) != (void *) ~0l \
595 && (((mut)->__data.__kind \
596 & (PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_NORMAL_NP)) \
597 == PTHREAD_MUTEX_PRIO_INHERIT_NP))
598#else
599# define USE_REQUEUE_PI(mut) 0
600#endif
601
602/* Returns 0 if POL is a valid scheduling policy. */
603static inline int
604check_sched_policy_attr (int pol)
605{
606 if (pol == SCHED_OTHER || pol == SCHED_FIFO || pol == SCHED_RR)
607 return 0;
608
609 return EINVAL;
610}
611
612/* Returns 0 if PR is within the accepted range of priority values for
613 the scheduling policy POL or EINVAL otherwise. */
614static inline int
615check_sched_priority_attr (int pr, int pol)
616{
617 int min = __sched_get_priority_min (pol);
618 int max = __sched_get_priority_max (pol);
619
620 if (min >= 0 && max >= 0 && pr >= min && pr <= max)
621 return 0;
622
623 return EINVAL;
624}
625
626/* Returns 0 if ST is a valid stack size for a thread stack and EINVAL
627 otherwise. */
628static inline int
629check_stacksize_attr (size_t st)
630{
631 if (st >= PTHREAD_STACK_MIN)
632 return 0;
633
634 return EINVAL;
635}
636
637#endif /* pthreadP.h */
638