pthreadP.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _PTHREADP_H
  16. #define _PTHREADP_H 1
  17. #include <pthread.h>
  18. #include <setjmp.h>
  19. #include <stdbool.h>
  20. #include <sys/syscall.h>
  21. #include "descr.h"
  22. #include <tls.h>
  23. #include <lowlevellock.h>
  24. #include <stackinfo.h>
  25. #include <internaltypes.h>
  26. #include <pthread-functions.h>
  27. #include <atomic.h>
  28. #include <kernel-features.h>
  29. #include <errno.h>
  30. #include <internal-signals.h>
  31. #include "pthread_mutex_conf.h"
  32. /* Atomic operations on TLS memory. */
  33. #ifndef THREAD_ATOMIC_CMPXCHG_VAL
  34. # define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, new, old) \
  35. atomic_compare_and_exchange_val_acq (&(descr)->member, new, old)
  36. #endif
  37. #ifndef THREAD_ATOMIC_BIT_SET
  38. # define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
  39. atomic_bit_set (&(descr)->member, bit)
  40. #endif
  41. static inline short max_adaptive_count (void)
  42. {
  43. #if HAVE_TUNABLES
  44. return __mutex_aconf.spin_count;
  45. #else
  46. return DEFAULT_ADAPTIVE_COUNT;
  47. #endif
  48. }
  49. /* Magic cookie representing robust mutex with dead owner. */
  50. #define PTHREAD_MUTEX_INCONSISTENT INT_MAX
  51. /* Magic cookie representing not recoverable robust mutex. */
  52. #define PTHREAD_MUTEX_NOTRECOVERABLE (INT_MAX - 1)
  53. /* Internal mutex type value. */
  54. enum
  55. {
  56. PTHREAD_MUTEX_KIND_MASK_NP = 3,
  57. PTHREAD_MUTEX_ELISION_NP = 256,
  58. PTHREAD_MUTEX_NO_ELISION_NP = 512,
  59. PTHREAD_MUTEX_ROBUST_NORMAL_NP = 16,
  60. PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
  61. = PTHREAD_MUTEX_ROBUST_NORMAL_NP | PTHREAD_MUTEX_RECURSIVE_NP,
  62. PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
  63. = PTHREAD_MUTEX_ROBUST_NORMAL_NP | PTHREAD_MUTEX_ERRORCHECK_NP,
  64. PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP
  65. = PTHREAD_MUTEX_ROBUST_NORMAL_NP | PTHREAD_MUTEX_ADAPTIVE_NP,
  66. PTHREAD_MUTEX_PRIO_INHERIT_NP = 32,
  67. PTHREAD_MUTEX_PI_NORMAL_NP
  68. = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_NORMAL,
  69. PTHREAD_MUTEX_PI_RECURSIVE_NP
  70. = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_RECURSIVE_NP,
  71. PTHREAD_MUTEX_PI_ERRORCHECK_NP
  72. = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ERRORCHECK_NP,
  73. PTHREAD_MUTEX_PI_ADAPTIVE_NP
  74. = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ADAPTIVE_NP,
  75. PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP
  76. = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_NORMAL_NP,
  77. PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP
  78. = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_RECURSIVE_NP,
  79. PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP
  80. = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP,
  81. PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP
  82. = PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP,
  83. PTHREAD_MUTEX_PRIO_PROTECT_NP = 64,
  84. PTHREAD_MUTEX_PP_NORMAL_NP
  85. = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_NORMAL,
  86. PTHREAD_MUTEX_PP_RECURSIVE_NP
  87. = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_RECURSIVE_NP,
  88. PTHREAD_MUTEX_PP_ERRORCHECK_NP
  89. = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_ERRORCHECK_NP,
  90. PTHREAD_MUTEX_PP_ADAPTIVE_NP
  91. = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_ADAPTIVE_NP,
  92. PTHREAD_MUTEX_ELISION_FLAGS_NP
  93. = PTHREAD_MUTEX_ELISION_NP | PTHREAD_MUTEX_NO_ELISION_NP,
  94. PTHREAD_MUTEX_TIMED_ELISION_NP =
  95. PTHREAD_MUTEX_TIMED_NP | PTHREAD_MUTEX_ELISION_NP,
  96. PTHREAD_MUTEX_TIMED_NO_ELISION_NP =
  97. PTHREAD_MUTEX_TIMED_NP | PTHREAD_MUTEX_NO_ELISION_NP,
  98. };
  99. #define PTHREAD_MUTEX_PSHARED_BIT 128
  100. /* See concurrency notes regarding __kind in struct __pthread_mutex_s
  101. in sysdeps/nptl/bits/thread-shared-types.h. */
  102. #define PTHREAD_MUTEX_TYPE(m) \
  103. (atomic_load_relaxed (&((m)->__data.__kind)) & 127)
  104. /* Don't include NO_ELISION, as that type is always the same
  105. as the underlying lock type. */
  106. #define PTHREAD_MUTEX_TYPE_ELISION(m) \
  107. (atomic_load_relaxed (&((m)->__data.__kind)) \
  108. & (127 | PTHREAD_MUTEX_ELISION_NP))
  109. #if LLL_PRIVATE == 0 && LLL_SHARED == 128
  110. # define PTHREAD_MUTEX_PSHARED(m) \
  111. (atomic_load_relaxed (&((m)->__data.__kind)) & 128)
  112. #else
  113. # define PTHREAD_MUTEX_PSHARED(m) \
  114. ((atomic_load_relaxed (&((m)->__data.__kind)) & 128) \
  115. ? LLL_SHARED : LLL_PRIVATE)
  116. #endif
  117. /* The kernel when waking robust mutexes on exit never uses
  118. FUTEX_PRIVATE_FLAG FUTEX_WAKE. */
  119. #define PTHREAD_ROBUST_MUTEX_PSHARED(m) LLL_SHARED
  120. /* Ceiling in __data.__lock. __data.__lock is signed, so don't
  121. use the MSB bit in there, but in the mask also include that bit,
  122. so that the compiler can optimize & PTHREAD_MUTEX_PRIO_CEILING_MASK
  123. masking if the value is then shifted down by
  124. PTHREAD_MUTEX_PRIO_CEILING_SHIFT. */
  125. #define PTHREAD_MUTEX_PRIO_CEILING_SHIFT 19
  126. #define PTHREAD_MUTEX_PRIO_CEILING_MASK 0xfff80000
  127. /* Flags in mutex attr. */
  128. #define PTHREAD_MUTEXATTR_PROTOCOL_SHIFT 28
  129. #define PTHREAD_MUTEXATTR_PROTOCOL_MASK 0x30000000
  130. #define PTHREAD_MUTEXATTR_PRIO_CEILING_SHIFT 12
  131. #define PTHREAD_MUTEXATTR_PRIO_CEILING_MASK 0x00fff000
  132. #define PTHREAD_MUTEXATTR_FLAG_ROBUST 0x40000000
  133. #define PTHREAD_MUTEXATTR_FLAG_PSHARED 0x80000000
  134. #define PTHREAD_MUTEXATTR_FLAG_BITS \
  135. (PTHREAD_MUTEXATTR_FLAG_ROBUST | PTHREAD_MUTEXATTR_FLAG_PSHARED \
  136. | PTHREAD_MUTEXATTR_PROTOCOL_MASK | PTHREAD_MUTEXATTR_PRIO_CEILING_MASK)
  137. /* For the following, see pthread_rwlock_common.c. */
  138. #define PTHREAD_RWLOCK_WRPHASE 1
  139. #define PTHREAD_RWLOCK_WRLOCKED 2
  140. #define PTHREAD_RWLOCK_RWAITING 4
  141. #define PTHREAD_RWLOCK_READER_SHIFT 3
  142. #define PTHREAD_RWLOCK_READER_OVERFLOW ((unsigned int) 1 \
  143. << (sizeof (unsigned int) * 8 - 1))
  144. #define PTHREAD_RWLOCK_WRHANDOVER ((unsigned int) 1 \
  145. << (sizeof (unsigned int) * 8 - 1))
  146. #define PTHREAD_RWLOCK_FUTEX_USED 2
  147. /* Bits used in robust mutex implementation. */
  148. #define FUTEX_WAITERS 0x80000000
  149. #define FUTEX_OWNER_DIED 0x40000000
  150. #define FUTEX_TID_MASK 0x3fffffff
  151. /* pthread_once definitions. See __pthread_once for how these are used. */
  152. #define __PTHREAD_ONCE_INPROGRESS 1
  153. #define __PTHREAD_ONCE_DONE 2
  154. #define __PTHREAD_ONCE_FORK_GEN_INCR 4
  155. /* Attribute to indicate thread creation was issued from C11 thrd_create. */
  156. #define ATTR_C11_THREAD ((void*)(uintptr_t)-1)
  157. /* Condition variable definitions. See __pthread_cond_wait_common.
  158. Need to be defined here so there is one place from which
  159. nptl_lock_constants can grab them. */
  160. #define __PTHREAD_COND_CLOCK_MONOTONIC_MASK 2
  161. #define __PTHREAD_COND_SHARED_MASK 1
  162. /* Internal variables. */
  163. /* Default pthread attributes. */
  164. extern struct pthread_attr __default_pthread_attr attribute_hidden;
  165. extern int __default_pthread_attr_lock attribute_hidden;
  166. /* Size and alignment of static TLS block. */
  167. extern size_t __static_tls_size attribute_hidden;
  168. extern size_t __static_tls_align_m1 attribute_hidden;
  169. /* Flag whether the machine is SMP or not. */
  170. extern int __is_smp attribute_hidden;
  171. /* Thread descriptor handling. */
  172. extern list_t __stack_user;
  173. hidden_proto (__stack_user)
  174. /* Attribute handling. */
  175. extern struct pthread_attr *__attr_list attribute_hidden;
  176. extern int __attr_list_lock attribute_hidden;
  177. /* Concurrency handling. */
  178. extern int __concurrency_level attribute_hidden;
  179. /* Thread-local data key handling. */
  180. extern struct pthread_key_struct __pthread_keys[PTHREAD_KEYS_MAX];
  181. hidden_proto (__pthread_keys)
  182. /* Number of threads running. */
  183. extern unsigned int __nptl_nthreads attribute_hidden;
  184. #ifndef __ASSUME_SET_ROBUST_LIST
  185. /* Negative if we do not have the system call and we can use it. */
  186. extern int __set_robust_list_avail attribute_hidden;
  187. #endif
  188. /* Thread Priority Protection. */
  189. extern int __sched_fifo_min_prio attribute_hidden;
  190. extern int __sched_fifo_max_prio attribute_hidden;
  191. extern void __init_sched_fifo_prio (void) attribute_hidden;
  192. extern int __pthread_tpp_change_priority (int prev_prio, int new_prio)
  193. attribute_hidden;
  194. extern int __pthread_current_priority (void) attribute_hidden;
  195. /* The library can run in debugging mode where it performs a lot more
  196. tests. */
  197. extern int __pthread_debug attribute_hidden;
  198. /** For now disable debugging support. */
  199. #if 0
  200. # define DEBUGGING_P __builtin_expect (__pthread_debug, 0)
  201. # define INVALID_TD_P(pd) (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
  202. # define INVALID_NOT_TERMINATED_TD_P(pd) INVALID_TD_P (pd)
  203. #else
  204. # define DEBUGGING_P 0
  205. /* Simplified test. This will not catch all invalid descriptors but
  206. is better than nothing. And if the test triggers the thread
  207. descriptor is guaranteed to be invalid. */
  208. # define INVALID_TD_P(pd) __builtin_expect ((pd)->tid <= 0, 0)
  209. # define INVALID_NOT_TERMINATED_TD_P(pd) __builtin_expect ((pd)->tid < 0, 0)
  210. #endif
  211. /* Cancellation test. */
  212. #define CANCELLATION_P(self) \
  213. do { \
  214. int cancelhandling = THREAD_GETMEM (self, cancelhandling); \
  215. if (CANCEL_ENABLED_AND_CANCELED (cancelhandling)) \
  216. { \
  217. THREAD_SETMEM (self, result, PTHREAD_CANCELED); \
  218. __do_cancel (); \
  219. } \
  220. } while (0)
  221. extern void __pthread_unwind (__pthread_unwind_buf_t *__buf)
  222. __cleanup_fct_attribute __attribute ((__noreturn__))
  223. #if !defined SHARED && !IS_IN (libpthread)
  224. weak_function
  225. #endif
  226. ;
  227. extern void __pthread_unwind_next (__pthread_unwind_buf_t *__buf)
  228. __cleanup_fct_attribute __attribute ((__noreturn__))
  229. #ifndef SHARED
  230. weak_function
  231. #endif
  232. ;
  233. extern void __pthread_register_cancel (__pthread_unwind_buf_t *__buf)
  234. __cleanup_fct_attribute;
  235. extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf)
  236. __cleanup_fct_attribute;
  237. #if IS_IN (libpthread)
  238. hidden_proto (__pthread_unwind)
  239. hidden_proto (__pthread_unwind_next)
  240. hidden_proto (__pthread_register_cancel)
  241. hidden_proto (__pthread_unregister_cancel)
  242. # ifdef SHARED
  243. extern void attribute_hidden pthread_cancel_init (void);
  244. # endif
  245. extern void __nptl_unwind_freeres (void) attribute_hidden;
  246. #endif
  247. /* Called when a thread reacts on a cancellation request. */
  248. static inline void
  249. __attribute ((noreturn, always_inline))
  250. __do_cancel (void)
  251. {
  252. struct pthread *self = THREAD_SELF;
  253. /* Make sure we get no more cancellations. */
  254. THREAD_ATOMIC_BIT_SET (self, cancelhandling, EXITING_BIT);
  255. __pthread_unwind ((__pthread_unwind_buf_t *)
  256. THREAD_GETMEM (self, cleanup_jmp_buf));
  257. }
  258. /* Set cancellation mode to asynchronous. */
  259. #define CANCEL_ASYNC() \
  260. __pthread_enable_asynccancel ()
  261. /* Reset to previous cancellation mode. */
  262. #define CANCEL_RESET(oldtype) \
  263. __pthread_disable_asynccancel (oldtype)
  264. #if IS_IN (libc)
  265. /* Same as CANCEL_ASYNC, but for use in libc.so. */
  266. # define LIBC_CANCEL_ASYNC() \
  267. __libc_enable_asynccancel ()
  268. /* Same as CANCEL_RESET, but for use in libc.so. */
  269. # define LIBC_CANCEL_RESET(oldtype) \
  270. __libc_disable_asynccancel (oldtype)
  271. #elif IS_IN (libpthread)
  272. # define LIBC_CANCEL_ASYNC() CANCEL_ASYNC ()
  273. # define LIBC_CANCEL_RESET(val) CANCEL_RESET (val)
  274. #elif IS_IN (librt)
  275. # define LIBC_CANCEL_ASYNC() \
  276. __librt_enable_asynccancel ()
  277. # define LIBC_CANCEL_RESET(val) \
  278. __librt_disable_asynccancel (val)
  279. #else
  280. # define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */
  281. # define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */
  282. #endif
  283. /* Internal prototypes. */
  284. /* Thread list handling. */
  285. extern struct pthread *__find_in_stack_list (struct pthread *pd)
  286. attribute_hidden;
  287. /* Deallocate a thread's stack after optionally making sure the thread
  288. descriptor is still valid. */
  289. extern void __free_tcb (struct pthread *pd) attribute_hidden;
  290. /* Free allocated stack. */
  291. extern void __deallocate_stack (struct pthread *pd) attribute_hidden;
  292. /* Mark all the stacks except for the current one as available. This
  293. function also re-initializes the lock for the stack cache. */
  294. extern void __reclaim_stacks (void) attribute_hidden;
  295. /* Make all threads's stacks executable. */
  296. extern int __make_stacks_executable (void **stack_endp) attribute_hidden;
  297. /* longjmp handling. */
  298. extern void __pthread_cleanup_upto (__jmp_buf target, char *targetframe);
  299. #if IS_IN (libpthread)
  300. hidden_proto (__pthread_cleanup_upto)
  301. #endif
  302. /* Functions with versioned interfaces. */
  303. extern int __pthread_create_2_1 (pthread_t *newthread,
  304. const pthread_attr_t *attr,
  305. void *(*start_routine) (void *), void *arg);
  306. extern int __pthread_create_2_0 (pthread_t *newthread,
  307. const pthread_attr_t *attr,
  308. void *(*start_routine) (void *), void *arg);
  309. extern int __pthread_attr_init_2_1 (pthread_attr_t *attr);
  310. extern int __pthread_attr_init_2_0 (pthread_attr_t *attr);
  311. /* Event handlers for libthread_db interface. */
  312. extern void __nptl_create_event (void);
  313. extern void __nptl_death_event (void);
  314. hidden_proto (__nptl_create_event)
  315. hidden_proto (__nptl_death_event)
  316. /* Register the generation counter in the libpthread with the libc. */
  317. #ifdef TLS_MULTIPLE_THREADS_IN_TCB
  318. extern void __libc_pthread_init (unsigned long int *ptr,
  319. void (*reclaim) (void),
  320. const struct pthread_functions *functions);
  321. #else
  322. extern int *__libc_pthread_init (unsigned long int *ptr,
  323. void (*reclaim) (void),
  324. const struct pthread_functions *functions);
  325. /* Variable set to a nonzero value either if more than one thread runs or ran,
  326. or if a single-threaded process is trying to cancel itself. See
  327. nptl/descr.h for more context on the single-threaded process case. */
  328. extern int __pthread_multiple_threads attribute_hidden;
  329. /* Pointer to the corresponding variable in libc. */
  330. extern int *__libc_multiple_threads_ptr attribute_hidden;
  331. #endif
  332. /* Find a thread given its TID. */
  333. extern struct pthread *__find_thread_by_id (pid_t tid) attribute_hidden
  334. #ifdef SHARED
  335. ;
  336. #else
  337. weak_function;
  338. #define __find_thread_by_id(tid) \
  339. (__find_thread_by_id ? (__find_thread_by_id) (tid) : (struct pthread *) NULL)
  340. #endif
  341. extern void __pthread_init_static_tls (struct link_map *) attribute_hidden;
  342. extern size_t __pthread_get_minstack (const pthread_attr_t *attr);
  343. /* Namespace save aliases. */
  344. extern int __pthread_getschedparam (pthread_t thread_id, int *policy,
  345. struct sched_param *param);
  346. extern int __pthread_setschedparam (pthread_t thread_id, int policy,
  347. const struct sched_param *param);
  348. extern int __pthread_setcancelstate (int state, int *oldstate);
  349. extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
  350. const pthread_mutexattr_t *__mutexattr);
  351. extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
  352. extern int __pthread_mutex_trylock (pthread_mutex_t *_mutex);
  353. extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
  354. extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex,
  355. const struct timespec *__abstime);
  356. extern int __pthread_mutex_cond_lock (pthread_mutex_t *__mutex)
  357. attribute_hidden;
  358. extern void __pthread_mutex_cond_lock_adjust (pthread_mutex_t *__mutex)
  359. attribute_hidden;
  360. extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
  361. extern int __pthread_mutex_unlock_usercnt (pthread_mutex_t *__mutex,
  362. int __decr) attribute_hidden;
  363. extern int __pthread_mutexattr_init (pthread_mutexattr_t *attr);
  364. extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *attr);
  365. extern int __pthread_mutexattr_settype (pthread_mutexattr_t *attr, int kind);
  366. extern int __pthread_attr_destroy (pthread_attr_t *attr);
  367. extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
  368. int *detachstate);
  369. extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
  370. int detachstate);
  371. extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
  372. int *inherit);
  373. extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
  374. extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
  375. struct sched_param *param);
  376. extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
  377. const struct sched_param *param);
  378. extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
  379. int *policy);
  380. extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
  381. extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
  382. extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
  383. extern int __pthread_attr_getstackaddr (const pthread_attr_t *__restrict
  384. __attr, void **__restrict __stackaddr);
  385. extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
  386. void *__stackaddr);
  387. extern int __pthread_attr_getstacksize (const pthread_attr_t *__restrict
  388. __attr,
  389. size_t *__restrict __stacksize);
  390. extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
  391. size_t __stacksize);
  392. extern int __pthread_attr_getstack (const pthread_attr_t *__restrict __attr,
  393. void **__restrict __stackaddr,
  394. size_t *__restrict __stacksize);
  395. extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
  396. size_t __stacksize);
  397. extern int __pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
  398. const pthread_rwlockattr_t *__restrict
  399. __attr);
  400. extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
  401. extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
  402. extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
  403. extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
  404. extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
  405. extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
  406. extern int __pthread_cond_broadcast (pthread_cond_t *cond);
  407. extern int __pthread_cond_destroy (pthread_cond_t *cond);
  408. extern int __pthread_cond_init (pthread_cond_t *cond,
  409. const pthread_condattr_t *cond_attr);
  410. extern int __pthread_cond_signal (pthread_cond_t *cond);
  411. extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
  412. extern int __pthread_cond_timedwait (pthread_cond_t *cond,
  413. pthread_mutex_t *mutex,
  414. const struct timespec *abstime);
  415. extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
  416. extern int __pthread_condattr_init (pthread_condattr_t *attr);
  417. extern int __pthread_key_create (pthread_key_t *key, void (*destr) (void *));
  418. extern int __pthread_key_delete (pthread_key_t key);
  419. extern void *__pthread_getspecific (pthread_key_t key);
  420. extern int __pthread_setspecific (pthread_key_t key, const void *value);
  421. extern int __pthread_once (pthread_once_t *once_control,
  422. void (*init_routine) (void));
  423. extern int __pthread_atfork (void (*prepare) (void), void (*parent) (void),
  424. void (*child) (void));
  425. extern pthread_t __pthread_self (void);
  426. extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
  427. extern int __pthread_detach (pthread_t th);
  428. extern int __pthread_cancel (pthread_t th);
  429. extern int __pthread_kill (pthread_t threadid, int signo);
  430. extern void __pthread_exit (void *value) __attribute__ ((__noreturn__));
  431. extern int __pthread_join (pthread_t threadid, void **thread_return);
  432. extern int __pthread_setcanceltype (int type, int *oldtype);
  433. extern int __pthread_enable_asynccancel (void) attribute_hidden;
  434. extern void __pthread_disable_asynccancel (int oldtype) attribute_hidden;
  435. extern void __pthread_testcancel (void);
  436. extern int __pthread_timedjoin_ex (pthread_t, void **, const struct timespec *,
  437. bool);
  438. #if IS_IN (libpthread)
  439. hidden_proto (__pthread_mutex_init)
  440. hidden_proto (__pthread_mutex_destroy)
  441. hidden_proto (__pthread_mutex_lock)
  442. hidden_proto (__pthread_mutex_trylock)
  443. hidden_proto (__pthread_mutex_unlock)
  444. hidden_proto (__pthread_rwlock_rdlock)
  445. hidden_proto (__pthread_rwlock_wrlock)
  446. hidden_proto (__pthread_rwlock_unlock)
  447. hidden_proto (__pthread_key_create)
  448. hidden_proto (__pthread_getspecific)
  449. hidden_proto (__pthread_setspecific)
  450. hidden_proto (__pthread_once)
  451. hidden_proto (__pthread_setcancelstate)
  452. hidden_proto (__pthread_testcancel)
  453. hidden_proto (__pthread_mutexattr_init)
  454. hidden_proto (__pthread_mutexattr_settype)
  455. hidden_proto (__pthread_timedjoin_ex)
  456. #endif
  457. extern int __pthread_cond_broadcast_2_0 (pthread_cond_2_0_t *cond);
  458. extern int __pthread_cond_destroy_2_0 (pthread_cond_2_0_t *cond);
  459. extern int __pthread_cond_init_2_0 (pthread_cond_2_0_t *cond,
  460. const pthread_condattr_t *cond_attr);
  461. extern int __pthread_cond_signal_2_0 (pthread_cond_2_0_t *cond);
  462. extern int __pthread_cond_timedwait_2_0 (pthread_cond_2_0_t *cond,
  463. pthread_mutex_t *mutex,
  464. const struct timespec *abstime);
  465. extern int __pthread_cond_wait_2_0 (pthread_cond_2_0_t *cond,
  466. pthread_mutex_t *mutex);
  467. extern int __pthread_getaffinity_np (pthread_t th, size_t cpusetsize,
  468. cpu_set_t *cpuset);
  469. /* The two functions are in libc.so and not exported. */
  470. extern int __libc_enable_asynccancel (void) attribute_hidden;
  471. extern void __libc_disable_asynccancel (int oldtype) attribute_hidden;
  472. /* The two functions are in librt.so and not exported. */
  473. extern int __librt_enable_asynccancel (void) attribute_hidden;
  474. extern void __librt_disable_asynccancel (int oldtype) attribute_hidden;
  475. #if IS_IN (libpthread)
  476. /* Special versions which use non-exported functions. */
  477. extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
  478. void (*routine) (void *), void *arg)
  479. attribute_hidden;
  480. /* Replace cleanup macros defined in <pthread.h> with internal
  481. versions that don't depend on unwind info and better support
  482. cancellation. */
  483. # undef pthread_cleanup_push
  484. # define pthread_cleanup_push(routine,arg) \
  485. { struct _pthread_cleanup_buffer _buffer; \
  486. __pthread_cleanup_push (&_buffer, (routine), (arg));
  487. extern void __pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
  488. int execute) attribute_hidden;
  489. # undef pthread_cleanup_pop
  490. # define pthread_cleanup_pop(execute) \
  491. __pthread_cleanup_pop (&_buffer, (execute)); }
  492. #endif
  493. extern void __pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
  494. void (*routine) (void *), void *arg);
  495. extern void __pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
  496. int execute);
  497. /* Old cleanup interfaces, still used in libc.so. */
  498. extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
  499. void (*routine) (void *), void *arg);
  500. extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
  501. int execute);
  502. extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
  503. void (*routine) (void *), void *arg);
  504. extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
  505. int execute);
  506. extern void __nptl_deallocate_tsd (void) attribute_hidden;
  507. extern void __nptl_setxid_error (struct xid_command *cmdp, int error)
  508. attribute_hidden;
  509. extern int __nptl_setxid (struct xid_command *cmdp) attribute_hidden;
  510. #ifndef SHARED
  511. extern void __nptl_set_robust (struct pthread *self);
  512. #endif
  513. extern void __nptl_stacks_freeres (void) attribute_hidden;
  514. extern void __shm_directory_freeres (void) attribute_hidden;
  515. extern void __wait_lookup_done (void) attribute_hidden;
  516. #ifdef SHARED
  517. # define PTHREAD_STATIC_FN_REQUIRE(name)
  518. #else
  519. # define PTHREAD_STATIC_FN_REQUIRE(name) __asm (".globl " #name);
  520. #endif
  521. /* Returns 0 if POL is a valid scheduling policy. */
  522. static inline int
  523. check_sched_policy_attr (int pol)
  524. {
  525. if (pol == SCHED_OTHER || pol == SCHED_FIFO || pol == SCHED_RR)
  526. return 0;
  527. return EINVAL;
  528. }
  529. /* Returns 0 if PR is within the accepted range of priority values for
  530. the scheduling policy POL or EINVAL otherwise. */
  531. static inline int
  532. check_sched_priority_attr (int pr, int pol)
  533. {
  534. int min = __sched_get_priority_min (pol);
  535. int max = __sched_get_priority_max (pol);
  536. if (min >= 0 && max >= 0 && pr >= min && pr <= max)
  537. return 0;
  538. return EINVAL;
  539. }
  540. /* Returns 0 if ST is a valid stack size for a thread stack and EINVAL
  541. otherwise. */
  542. static inline int
  543. check_stacksize_attr (size_t st)
  544. {
  545. if (st >= PTHREAD_STACK_MIN)
  546. return 0;
  547. return EINVAL;
  548. }
  549. #define ASSERT_TYPE_SIZE(type, size) \
  550. _Static_assert (sizeof (type) == size, \
  551. "sizeof (" #type ") != " #size)
  552. #define ASSERT_PTHREAD_INTERNAL_SIZE(type, internal) \
  553. _Static_assert (sizeof ((type) { { 0 } }).__size >= sizeof (internal),\
  554. "sizeof (" #type ".__size) < sizeof (" #internal ")")
  555. #define ASSERT_PTHREAD_STRING(x) __STRING (x)
  556. #define ASSERT_PTHREAD_INTERNAL_OFFSET(type, member, offset) \
  557. _Static_assert (offsetof (type, member) == offset, \
  558. "offset of " #member " field of " #type " != " \
  559. ASSERT_PTHREAD_STRING (offset))
  560. #endif /* pthreadP.h */