pthread.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. /* Copyright (C) 2002-2021 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #ifndef _PTHREAD_H
  15. #define _PTHREAD_H 1
  16. #include <features.h>
  17. #include <sched.h>
  18. #include <time.h>
  19. #include <bits/endian.h>
  20. #include <bits/pthreadtypes.h>
  21. #include <bits/setjmp.h>
  22. #include <bits/wordsize.h>
  23. #include <bits/types/struct_timespec.h>
  24. #include <bits/types/__sigset_t.h>
  25. #include <bits/types/struct___jmp_buf_tag.h>
  26. /* Detach state. */
  27. enum
  28. {
  29. PTHREAD_CREATE_JOINABLE,
  30. #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
  31. PTHREAD_CREATE_DETACHED
  32. #define PTHREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
  33. };
  34. /* Mutex types. */
  35. enum
  36. {
  37. PTHREAD_MUTEX_TIMED_NP,
  38. PTHREAD_MUTEX_RECURSIVE_NP,
  39. PTHREAD_MUTEX_ERRORCHECK_NP,
  40. PTHREAD_MUTEX_ADAPTIVE_NP
  41. #if defined __USE_UNIX98 || defined __USE_XOPEN2K8
  42. ,
  43. PTHREAD_MUTEX_NORMAL = PTHREAD_MUTEX_TIMED_NP,
  44. PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP,
  45. PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP,
  46. PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
  47. #endif
  48. #ifdef __USE_GNU
  49. /* For compatibility. */
  50. , PTHREAD_MUTEX_FAST_NP = PTHREAD_MUTEX_TIMED_NP
  51. #endif
  52. };
  53. #ifdef __USE_XOPEN2K
  54. /* Robust mutex or not flags. */
  55. enum
  56. {
  57. PTHREAD_MUTEX_STALLED,
  58. PTHREAD_MUTEX_STALLED_NP = PTHREAD_MUTEX_STALLED,
  59. PTHREAD_MUTEX_ROBUST,
  60. PTHREAD_MUTEX_ROBUST_NP = PTHREAD_MUTEX_ROBUST
  61. };
  62. #endif
  63. #if defined __USE_POSIX199506 || defined __USE_UNIX98
  64. /* Mutex protocols. */
  65. enum
  66. {
  67. PTHREAD_PRIO_NONE,
  68. PTHREAD_PRIO_INHERIT,
  69. PTHREAD_PRIO_PROTECT
  70. };
  71. #endif
  72. #define PTHREAD_MUTEX_INITIALIZER \
  73. { { __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_TIMED_NP) } }
  74. #ifdef __USE_GNU
  75. # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
  76. { { __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_RECURSIVE_NP) } }
  77. # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
  78. { { __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_ERRORCHECK_NP) } }
  79. # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
  80. { { __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_ADAPTIVE_NP) } }
  81. #endif
  82. /* Read-write lock types. */
  83. #if defined __USE_UNIX98 || defined __USE_XOPEN2K
  84. enum
  85. {
  86. PTHREAD_RWLOCK_PREFER_READER_NP,
  87. PTHREAD_RWLOCK_PREFER_WRITER_NP,
  88. PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,
  89. PTHREAD_RWLOCK_DEFAULT_NP = PTHREAD_RWLOCK_PREFER_READER_NP
  90. };
  91. /* Read-write lock initializers. */
  92. # define PTHREAD_RWLOCK_INITIALIZER \
  93. { { __PTHREAD_RWLOCK_INITIALIZER (PTHREAD_RWLOCK_DEFAULT_NP) } }
  94. # ifdef __USE_GNU
  95. # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
  96. { { __PTHREAD_RWLOCK_INITIALIZER (PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP) } }
  97. # endif
  98. #endif /* Unix98 or XOpen2K */
  99. /* Scheduler inheritance. */
  100. enum
  101. {
  102. PTHREAD_INHERIT_SCHED,
  103. #define PTHREAD_INHERIT_SCHED PTHREAD_INHERIT_SCHED
  104. PTHREAD_EXPLICIT_SCHED
  105. #define PTHREAD_EXPLICIT_SCHED PTHREAD_EXPLICIT_SCHED
  106. };
  107. /* Scope handling. */
  108. enum
  109. {
  110. PTHREAD_SCOPE_SYSTEM,
  111. #define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_SYSTEM
  112. PTHREAD_SCOPE_PROCESS
  113. #define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_PROCESS
  114. };
  115. /* Process shared or private flag. */
  116. enum
  117. {
  118. PTHREAD_PROCESS_PRIVATE,
  119. #define PTHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
  120. PTHREAD_PROCESS_SHARED
  121. #define PTHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
  122. };
  123. /* Conditional variable handling. */
  124. #define PTHREAD_COND_INITIALIZER { { {0}, {0}, {0, 0}, {0, 0}, 0, 0, {0, 0} } }
  125. /* Cleanup buffers */
  126. struct _pthread_cleanup_buffer
  127. {
  128. void (*__routine) (void *); /* Function to call. */
  129. void *__arg; /* Its argument. */
  130. int __canceltype; /* Saved cancellation type. */
  131. struct _pthread_cleanup_buffer *__prev; /* Chaining of cleanup functions. */
  132. };
  133. /* Cancellation */
  134. enum
  135. {
  136. PTHREAD_CANCEL_ENABLE,
  137. #define PTHREAD_CANCEL_ENABLE PTHREAD_CANCEL_ENABLE
  138. PTHREAD_CANCEL_DISABLE
  139. #define PTHREAD_CANCEL_DISABLE PTHREAD_CANCEL_DISABLE
  140. };
  141. enum
  142. {
  143. PTHREAD_CANCEL_DEFERRED,
  144. #define PTHREAD_CANCEL_DEFERRED PTHREAD_CANCEL_DEFERRED
  145. PTHREAD_CANCEL_ASYNCHRONOUS
  146. #define PTHREAD_CANCEL_ASYNCHRONOUS PTHREAD_CANCEL_ASYNCHRONOUS
  147. };
  148. #define PTHREAD_CANCELED ((void *) -1)
  149. /* Single execution handling. */
  150. #define PTHREAD_ONCE_INIT 0
  151. #ifdef __USE_XOPEN2K
  152. /* Value returned by 'pthread_barrier_wait' for one of the threads after
  153. the required number of threads have called this function.
  154. -1 is distinct from 0 and all errno constants */
  155. # define PTHREAD_BARRIER_SERIAL_THREAD -1
  156. #endif
  157. __BEGIN_DECLS
  158. /* Create a new thread, starting with execution of START-ROUTINE
  159. getting passed ARG. Creation attributed come from ATTR. The new
  160. handle is stored in *NEWTHREAD. */
  161. extern int pthread_create (pthread_t *__restrict __newthread,
  162. const pthread_attr_t *__restrict __attr,
  163. void *(*__start_routine) (void *),
  164. void *__restrict __arg) __THROWNL __nonnull ((1, 3));
  165. /* Terminate calling thread.
  166. The registered cleanup handlers are called via exception handling
  167. so we cannot mark this function with __THROW.*/
  168. extern void pthread_exit (void *__retval) __attribute__ ((__noreturn__));
  169. /* Make calling thread wait for termination of the thread TH. The
  170. exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN
  171. is not NULL.
  172. This function is a cancellation point and therefore not marked with
  173. __THROW. */
  174. extern int pthread_join (pthread_t __th, void **__thread_return);
  175. #ifdef __USE_GNU
  176. /* Check whether thread TH has terminated. If yes return the status of
  177. the thread in *THREAD_RETURN, if THREAD_RETURN is not NULL. */
  178. extern int pthread_tryjoin_np (pthread_t __th, void **__thread_return) __THROW;
  179. /* Make calling thread wait for termination of the thread TH, but only
  180. until TIMEOUT. The exit status of the thread is stored in
  181. *THREAD_RETURN, if THREAD_RETURN is not NULL.
  182. This function is a cancellation point and therefore not marked with
  183. __THROW. */
  184. extern int pthread_timedjoin_np (pthread_t __th, void **__thread_return,
  185. const struct timespec *__abstime);
  186. /* Make calling thread wait for termination of the thread TH, but only
  187. until TIMEOUT measured against the clock specified by CLOCKID. The
  188. exit status of the thread is stored in *THREAD_RETURN, if
  189. THREAD_RETURN is not NULL.
  190. This function is a cancellation point and therefore not marked with
  191. __THROW. */
  192. extern int pthread_clockjoin_np (pthread_t __th, void **__thread_return,
  193. clockid_t __clockid,
  194. const struct timespec *__abstime);
  195. #endif
  196. /* Indicate that the thread TH is never to be joined with PTHREAD_JOIN.
  197. The resources of TH will therefore be freed immediately when it
  198. terminates, instead of waiting for another thread to perform PTHREAD_JOIN
  199. on it. */
  200. extern int pthread_detach (pthread_t __th) __THROW;
  201. /* Obtain the identifier of the current thread. */
  202. extern pthread_t pthread_self (void) __THROW __attribute__ ((__const__));
  203. /* Compare two thread identifiers. */
  204. extern int pthread_equal (pthread_t __thread1, pthread_t __thread2)
  205. __THROW __attribute__ ((__const__));
  206. /* Thread attribute handling. */
  207. /* Initialize thread attribute *ATTR with default attributes
  208. (detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER,
  209. no user-provided stack). */
  210. extern int pthread_attr_init (pthread_attr_t *__attr) __THROW __nonnull ((1));
  211. /* Destroy thread attribute *ATTR. */
  212. extern int pthread_attr_destroy (pthread_attr_t *__attr)
  213. __THROW __nonnull ((1));
  214. /* Get detach state attribute. */
  215. extern int pthread_attr_getdetachstate (const pthread_attr_t *__attr,
  216. int *__detachstate)
  217. __THROW __nonnull ((1, 2));
  218. /* Set detach state attribute. */
  219. extern int pthread_attr_setdetachstate (pthread_attr_t *__attr,
  220. int __detachstate)
  221. __THROW __nonnull ((1));
  222. /* Get the size of the guard area created for stack overflow protection. */
  223. extern int pthread_attr_getguardsize (const pthread_attr_t *__attr,
  224. size_t *__guardsize)
  225. __THROW __nonnull ((1, 2));
  226. /* Set the size of the guard area created for stack overflow protection. */
  227. extern int pthread_attr_setguardsize (pthread_attr_t *__attr,
  228. size_t __guardsize)
  229. __THROW __nonnull ((1));
  230. /* Return in *PARAM the scheduling parameters of *ATTR. */
  231. extern int pthread_attr_getschedparam (const pthread_attr_t *__restrict __attr,
  232. struct sched_param *__restrict __param)
  233. __THROW __nonnull ((1, 2));
  234. /* Set scheduling parameters (priority, etc) in *ATTR according to PARAM. */
  235. extern int pthread_attr_setschedparam (pthread_attr_t *__restrict __attr,
  236. const struct sched_param *__restrict
  237. __param) __THROW __nonnull ((1, 2));
  238. /* Return in *POLICY the scheduling policy of *ATTR. */
  239. extern int pthread_attr_getschedpolicy (const pthread_attr_t *__restrict
  240. __attr, int *__restrict __policy)
  241. __THROW __nonnull ((1, 2));
  242. /* Set scheduling policy in *ATTR according to POLICY. */
  243. extern int pthread_attr_setschedpolicy (pthread_attr_t *__attr, int __policy)
  244. __THROW __nonnull ((1));
  245. /* Return in *INHERIT the scheduling inheritance mode of *ATTR. */
  246. extern int pthread_attr_getinheritsched (const pthread_attr_t *__restrict
  247. __attr, int *__restrict __inherit)
  248. __THROW __nonnull ((1, 2));
  249. /* Set scheduling inheritance mode in *ATTR according to INHERIT. */
  250. extern int pthread_attr_setinheritsched (pthread_attr_t *__attr,
  251. int __inherit)
  252. __THROW __nonnull ((1));
  253. /* Return in *SCOPE the scheduling contention scope of *ATTR. */
  254. extern int pthread_attr_getscope (const pthread_attr_t *__restrict __attr,
  255. int *__restrict __scope)
  256. __THROW __nonnull ((1, 2));
  257. /* Set scheduling contention scope in *ATTR according to SCOPE. */
  258. extern int pthread_attr_setscope (pthread_attr_t *__attr, int __scope)
  259. __THROW __nonnull ((1));
  260. /* Return the previously set address for the stack. */
  261. extern int pthread_attr_getstackaddr (const pthread_attr_t *__restrict
  262. __attr, void **__restrict __stackaddr)
  263. __THROW __nonnull ((1, 2)) __attribute_deprecated__;
  264. /* Set the starting address of the stack of the thread to be created.
  265. Depending on whether the stack grows up or down the value must either
  266. be higher or lower than all the address in the memory block. The
  267. minimal size of the block must be PTHREAD_STACK_MIN. */
  268. extern int pthread_attr_setstackaddr (pthread_attr_t *__attr,
  269. void *__stackaddr)
  270. __THROW __nonnull ((1)) __attribute_deprecated__;
  271. /* Return the currently used minimal stack size. */
  272. extern int pthread_attr_getstacksize (const pthread_attr_t *__restrict
  273. __attr, size_t *__restrict __stacksize)
  274. __THROW __nonnull ((1, 2));
  275. /* Add information about the minimum stack size needed for the thread
  276. to be started. This size must never be less than PTHREAD_STACK_MIN
  277. and must also not exceed the system limits. */
  278. extern int pthread_attr_setstacksize (pthread_attr_t *__attr,
  279. size_t __stacksize)
  280. __THROW __nonnull ((1));
  281. #ifdef __USE_XOPEN2K
  282. /* Return the previously set address for the stack. */
  283. extern int pthread_attr_getstack (const pthread_attr_t *__restrict __attr,
  284. void **__restrict __stackaddr,
  285. size_t *__restrict __stacksize)
  286. __THROW __nonnull ((1, 2, 3));
  287. /* The following two interfaces are intended to replace the last two. They
  288. require setting the address as well as the size since only setting the
  289. address will make the implementation on some architectures impossible. */
  290. extern int pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
  291. size_t __stacksize) __THROW __nonnull ((1));
  292. #endif
  293. #ifdef __USE_GNU
  294. /* Thread created with attribute ATTR will be limited to run only on
  295. the processors represented in CPUSET. */
  296. extern int pthread_attr_setaffinity_np (pthread_attr_t *__attr,
  297. size_t __cpusetsize,
  298. const cpu_set_t *__cpuset)
  299. __THROW __nonnull ((1, 3));
  300. /* Get bit set in CPUSET representing the processors threads created with
  301. ATTR can run on. */
  302. extern int pthread_attr_getaffinity_np (const pthread_attr_t *__attr,
  303. size_t __cpusetsize,
  304. cpu_set_t *__cpuset)
  305. __THROW __nonnull ((1, 3));
  306. /* Get the default attributes used by pthread_create in this process. */
  307. extern int pthread_getattr_default_np (pthread_attr_t *__attr)
  308. __THROW __nonnull ((1));
  309. /* Store *SIGMASK as the signal mask for the new thread in *ATTR. */
  310. extern int pthread_attr_setsigmask_np (pthread_attr_t *__attr,
  311. const __sigset_t *sigmask);
  312. /* Store the signal mask of *ATTR in *SIGMASK. If there is no signal
  313. mask stored, return PTHREAD_ATTR_NOSIGMASK_NP. Return zero on
  314. success. */
  315. extern int pthread_attr_getsigmask_np (const pthread_attr_t *__attr,
  316. __sigset_t *sigmask);
  317. /* Special return value from pthread_attr_getsigmask_np if the signal
  318. mask has not been set. */
  319. #define PTHREAD_ATTR_NO_SIGMASK_NP (-1)
  320. /* Set the default attributes to be used by pthread_create in this
  321. process. */
  322. extern int pthread_setattr_default_np (const pthread_attr_t *__attr)
  323. __THROW __nonnull ((1));
  324. /* Initialize thread attribute *ATTR with attributes corresponding to the
  325. already running thread TH. It shall be called on uninitialized ATTR
  326. and destroyed with pthread_attr_destroy when no longer needed. */
  327. extern int pthread_getattr_np (pthread_t __th, pthread_attr_t *__attr)
  328. __THROW __nonnull ((2));
  329. #endif
  330. /* Functions for scheduling control. */
  331. /* Set the scheduling parameters for TARGET_THREAD according to POLICY
  332. and *PARAM. */
  333. extern int pthread_setschedparam (pthread_t __target_thread, int __policy,
  334. const struct sched_param *__param)
  335. __THROW __nonnull ((3));
  336. /* Return in *POLICY and *PARAM the scheduling parameters for TARGET_THREAD. */
  337. extern int pthread_getschedparam (pthread_t __target_thread,
  338. int *__restrict __policy,
  339. struct sched_param *__restrict __param)
  340. __THROW __nonnull ((2, 3));
  341. /* Set the scheduling priority for TARGET_THREAD. */
  342. extern int pthread_setschedprio (pthread_t __target_thread, int __prio)
  343. __THROW;
  344. #ifdef __USE_GNU
  345. /* Get thread name visible in the kernel and its interfaces. */
  346. extern int pthread_getname_np (pthread_t __target_thread, char *__buf,
  347. size_t __buflen)
  348. __THROW __nonnull ((2));
  349. /* Set thread name visible in the kernel and its interfaces. */
  350. extern int pthread_setname_np (pthread_t __target_thread, const char *__name)
  351. __THROW __nonnull ((2));
  352. #endif
  353. #ifdef __USE_UNIX98
  354. /* Determine level of concurrency. */
  355. extern int pthread_getconcurrency (void) __THROW;
  356. /* Set new concurrency level to LEVEL. */
  357. extern int pthread_setconcurrency (int __level) __THROW;
  358. #endif
  359. #ifdef __USE_GNU
  360. /* Yield the processor to another thread or process.
  361. This function is similar to the POSIX `sched_yield' function but
  362. might be differently implemented in the case of a m-on-n thread
  363. implementation. */
  364. extern int pthread_yield (void) __THROW;
  365. /* Limit specified thread TH to run only on the processors represented
  366. in CPUSET. */
  367. extern int pthread_setaffinity_np (pthread_t __th, size_t __cpusetsize,
  368. const cpu_set_t *__cpuset)
  369. __THROW __nonnull ((3));
  370. /* Get bit set in CPUSET representing the processors TH can run on. */
  371. extern int pthread_getaffinity_np (pthread_t __th, size_t __cpusetsize,
  372. cpu_set_t *__cpuset)
  373. __THROW __nonnull ((3));
  374. #endif
  375. /* Functions for handling initialization. */
  376. /* Guarantee that the initialization function INIT_ROUTINE will be called
  377. only once, even if pthread_once is executed several times with the
  378. same ONCE_CONTROL argument. ONCE_CONTROL must point to a static or
  379. extern variable initialized to PTHREAD_ONCE_INIT.
  380. The initialization functions might throw exception which is why
  381. this function is not marked with __THROW. */
  382. extern int pthread_once (pthread_once_t *__once_control,
  383. void (*__init_routine) (void)) __nonnull ((1, 2));
  384. /* Functions for handling cancellation.
  385. Note that these functions are explicitly not marked to not throw an
  386. exception in C++ code. If cancellation is implemented by unwinding
  387. this is necessary to have the compiler generate the unwind information. */
  388. /* Set cancelability state of current thread to STATE, returning old
  389. state in *OLDSTATE if OLDSTATE is not NULL. */
  390. extern int pthread_setcancelstate (int __state, int *__oldstate);
  391. /* Set cancellation state of current thread to TYPE, returning the old
  392. type in *OLDTYPE if OLDTYPE is not NULL. */
  393. extern int pthread_setcanceltype (int __type, int *__oldtype);
  394. /* Cancel THREAD immediately or at the next possibility. */
  395. extern int pthread_cancel (pthread_t __th);
  396. /* Test for pending cancellation for the current thread and terminate
  397. the thread as per pthread_exit(PTHREAD_CANCELED) if it has been
  398. cancelled. */
  399. extern void pthread_testcancel (void);
  400. /* Cancellation handling with integration into exception handling. */
  401. struct __cancel_jmp_buf_tag
  402. {
  403. __jmp_buf __cancel_jmp_buf;
  404. int __mask_was_saved;
  405. };
  406. typedef struct
  407. {
  408. struct __cancel_jmp_buf_tag __cancel_jmp_buf[1];
  409. void *__pad[4];
  410. } __pthread_unwind_buf_t __attribute__ ((__aligned__));
  411. /* No special attributes by default. */
  412. #ifndef __cleanup_fct_attribute
  413. # define __cleanup_fct_attribute
  414. #endif
  415. /* Structure to hold the cleanup handler information. */
  416. struct __pthread_cleanup_frame
  417. {
  418. void (*__cancel_routine) (void *);
  419. void *__cancel_arg;
  420. int __do_it;
  421. int __cancel_type;
  422. };
  423. #if defined __GNUC__ && defined __EXCEPTIONS
  424. # ifdef __cplusplus
  425. /* Class to handle cancellation handler invocation. */
  426. class __pthread_cleanup_class
  427. {
  428. void (*__cancel_routine) (void *);
  429. void *__cancel_arg;
  430. int __do_it;
  431. int __cancel_type;
  432. public:
  433. __pthread_cleanup_class (void (*__fct) (void *), void *__arg)
  434. : __cancel_routine (__fct), __cancel_arg (__arg), __do_it (1) { }
  435. ~__pthread_cleanup_class () { if (__do_it) __cancel_routine (__cancel_arg); }
  436. void __setdoit (int __newval) { __do_it = __newval; }
  437. void __defer () { pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED,
  438. &__cancel_type); }
  439. void __restore () const { pthread_setcanceltype (__cancel_type, 0); }
  440. };
  441. /* Install a cleanup handler: ROUTINE will be called with arguments ARG
  442. when the thread is canceled or calls pthread_exit. ROUTINE will also
  443. be called with arguments ARG when the matching pthread_cleanup_pop
  444. is executed with non-zero EXECUTE argument.
  445. pthread_cleanup_push and pthread_cleanup_pop are macros and must always
  446. be used in matching pairs at the same nesting level of braces. */
  447. # define pthread_cleanup_push(routine, arg) \
  448. do { \
  449. __pthread_cleanup_class __clframe (routine, arg)
  450. /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
  451. If EXECUTE is non-zero, the handler function is called. */
  452. # define pthread_cleanup_pop(execute) \
  453. __clframe.__setdoit (execute); \
  454. } while (0)
  455. # ifdef __USE_GNU
  456. /* Install a cleanup handler as pthread_cleanup_push does, but also
  457. saves the current cancellation type and sets it to deferred
  458. cancellation. */
  459. # define pthread_cleanup_push_defer_np(routine, arg) \
  460. do { \
  461. __pthread_cleanup_class __clframe (routine, arg); \
  462. __clframe.__defer ()
  463. /* Remove a cleanup handler as pthread_cleanup_pop does, but also
  464. restores the cancellation type that was in effect when the matching
  465. pthread_cleanup_push_defer was called. */
  466. # define pthread_cleanup_pop_restore_np(execute) \
  467. __clframe.__restore (); \
  468. __clframe.__setdoit (execute); \
  469. } while (0)
  470. # endif
  471. # else
  472. /* Function called to call the cleanup handler. As an extern inline
  473. function the compiler is free to decide inlining the change when
  474. needed or fall back on the copy which must exist somewhere
  475. else. */
  476. __extern_inline void
  477. __pthread_cleanup_routine (struct __pthread_cleanup_frame *__frame)
  478. {
  479. if (__frame->__do_it)
  480. __frame->__cancel_routine (__frame->__cancel_arg);
  481. }
  482. /* Install a cleanup handler: ROUTINE will be called with arguments ARG
  483. when the thread is canceled or calls pthread_exit. ROUTINE will also
  484. be called with arguments ARG when the matching pthread_cleanup_pop
  485. is executed with non-zero EXECUTE argument.
  486. pthread_cleanup_push and pthread_cleanup_pop are macros and must always
  487. be used in matching pairs at the same nesting level of braces. */
  488. # define pthread_cleanup_push(routine, arg) \
  489. do { \
  490. struct __pthread_cleanup_frame __clframe \
  491. __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
  492. = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
  493. .__do_it = 1 };
  494. /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
  495. If EXECUTE is non-zero, the handler function is called. */
  496. # define pthread_cleanup_pop(execute) \
  497. __clframe.__do_it = (execute); \
  498. } while (0)
  499. # ifdef __USE_GNU
  500. /* Install a cleanup handler as pthread_cleanup_push does, but also
  501. saves the current cancellation type and sets it to deferred
  502. cancellation. */
  503. # define pthread_cleanup_push_defer_np(routine, arg) \
  504. do { \
  505. struct __pthread_cleanup_frame __clframe \
  506. __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
  507. = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
  508. .__do_it = 1 }; \
  509. (void) pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, \
  510. &__clframe.__cancel_type)
  511. /* Remove a cleanup handler as pthread_cleanup_pop does, but also
  512. restores the cancellation type that was in effect when the matching
  513. pthread_cleanup_push_defer was called. */
  514. # define pthread_cleanup_pop_restore_np(execute) \
  515. (void) pthread_setcanceltype (__clframe.__cancel_type, NULL); \
  516. __clframe.__do_it = (execute); \
  517. } while (0)
  518. # endif
  519. # endif
  520. #else
  521. /* Install a cleanup handler: ROUTINE will be called with arguments ARG
  522. when the thread is canceled or calls pthread_exit. ROUTINE will also
  523. be called with arguments ARG when the matching pthread_cleanup_pop
  524. is executed with non-zero EXECUTE argument.
  525. pthread_cleanup_push and pthread_cleanup_pop are macros and must always
  526. be used in matching pairs at the same nesting level of braces. */
  527. # define pthread_cleanup_push(routine, arg) \
  528. do { \
  529. __pthread_unwind_buf_t __cancel_buf; \
  530. void (*__cancel_routine) (void *) = (routine); \
  531. void *__cancel_arg = (arg); \
  532. int __not_first_call = __sigsetjmp_cancel (__cancel_buf.__cancel_jmp_buf, \
  533. 0); \
  534. if (__glibc_unlikely (__not_first_call)) \
  535. { \
  536. __cancel_routine (__cancel_arg); \
  537. __pthread_unwind_next (&__cancel_buf); \
  538. /* NOTREACHED */ \
  539. } \
  540. \
  541. __pthread_register_cancel (&__cancel_buf); \
  542. do {
  543. extern void __pthread_register_cancel (__pthread_unwind_buf_t *__buf)
  544. __cleanup_fct_attribute;
  545. /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
  546. If EXECUTE is non-zero, the handler function is called. */
  547. # define pthread_cleanup_pop(execute) \
  548. do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
  549. } while (0); \
  550. __pthread_unregister_cancel (&__cancel_buf); \
  551. if (execute) \
  552. __cancel_routine (__cancel_arg); \
  553. } while (0)
  554. extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf)
  555. __cleanup_fct_attribute;
  556. # ifdef __USE_GNU
  557. /* Install a cleanup handler as pthread_cleanup_push does, but also
  558. saves the current cancellation type and sets it to deferred
  559. cancellation. */
  560. # define pthread_cleanup_push_defer_np(routine, arg) \
  561. do { \
  562. __pthread_unwind_buf_t __cancel_buf; \
  563. void (*__cancel_routine) (void *) = (routine); \
  564. void *__cancel_arg = (arg); \
  565. int __not_first_call = __sigsetjmp_cancel (__cancel_buf.__cancel_jmp_buf, \
  566. 0); \
  567. if (__glibc_unlikely (__not_first_call)) \
  568. { \
  569. __cancel_routine (__cancel_arg); \
  570. __pthread_unwind_next (&__cancel_buf); \
  571. /* NOTREACHED */ \
  572. } \
  573. \
  574. __pthread_register_cancel_defer (&__cancel_buf); \
  575. do {
  576. extern void __pthread_register_cancel_defer (__pthread_unwind_buf_t *__buf)
  577. __cleanup_fct_attribute;
  578. /* Remove a cleanup handler as pthread_cleanup_pop does, but also
  579. restores the cancellation type that was in effect when the matching
  580. pthread_cleanup_push_defer was called. */
  581. # define pthread_cleanup_pop_restore_np(execute) \
  582. do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
  583. } while (0); \
  584. __pthread_unregister_cancel_restore (&__cancel_buf); \
  585. if (execute) \
  586. __cancel_routine (__cancel_arg); \
  587. } while (0)
  588. extern void __pthread_unregister_cancel_restore (__pthread_unwind_buf_t *__buf)
  589. __cleanup_fct_attribute;
  590. # endif
  591. /* Internal interface to initiate cleanup. */
  592. extern void __pthread_unwind_next (__pthread_unwind_buf_t *__buf)
  593. __cleanup_fct_attribute __attribute__ ((__noreturn__))
  594. # ifndef SHARED
  595. __attribute__ ((__weak__))
  596. # endif
  597. ;
  598. #endif
  599. /* Function used in the macros. Calling __sigsetjmp, with its first
  600. argument declared as an array, results in a -Wstringop-overflow
  601. warning from GCC 11 because struct pthread_unwind_buf is smaller
  602. than jmp_buf. The calls from the macros have __SAVEMASK set to 0,
  603. so nothing beyond the common prefix is used and this warning is a
  604. false positive. Use an alias with its first argument declared to
  605. use the type in the macros if possible to avoid this warning. */
  606. #if __GNUC_PREREQ (11, 0)
  607. extern int __REDIRECT_NTHNL (__sigsetjmp_cancel,
  608. (struct __cancel_jmp_buf_tag __env[1],
  609. int __savemask),
  610. __sigsetjmp) __attribute_returns_twice__;
  611. #else
  612. # define __sigsetjmp_cancel(env, savemask) \
  613. __sigsetjmp ((struct __jmp_buf_tag *) (void *) (env), (savemask))
  614. extern int __sigsetjmp (struct __jmp_buf_tag __env[1],
  615. int __savemask) __THROWNL;
  616. #endif
  617. /* Mutex handling. */
  618. /* Initialize a mutex. */
  619. extern int pthread_mutex_init (pthread_mutex_t *__mutex,
  620. const pthread_mutexattr_t *__mutexattr)
  621. __THROW __nonnull ((1));
  622. /* Destroy a mutex. */
  623. extern int pthread_mutex_destroy (pthread_mutex_t *__mutex)
  624. __THROW __nonnull ((1));
  625. /* Try locking a mutex. */
  626. extern int pthread_mutex_trylock (pthread_mutex_t *__mutex)
  627. __THROWNL __nonnull ((1));
  628. /* Lock a mutex. */
  629. extern int pthread_mutex_lock (pthread_mutex_t *__mutex)
  630. __THROWNL __nonnull ((1));
  631. #ifdef __USE_XOPEN2K
  632. /* Wait until lock becomes available, or specified time passes. */
  633. extern int pthread_mutex_timedlock (pthread_mutex_t *__restrict __mutex,
  634. const struct timespec *__restrict
  635. __abstime) __THROWNL __nonnull ((1, 2));
  636. #endif
  637. #ifdef __USE_GNU
  638. extern int pthread_mutex_clocklock (pthread_mutex_t *__restrict __mutex,
  639. clockid_t __clockid,
  640. const struct timespec *__restrict
  641. __abstime) __THROWNL __nonnull ((1, 3));
  642. #endif
  643. /* Unlock a mutex. */
  644. extern int pthread_mutex_unlock (pthread_mutex_t *__mutex)
  645. __THROWNL __nonnull ((1));
  646. /* Get the priority ceiling of MUTEX. */
  647. extern int pthread_mutex_getprioceiling (const pthread_mutex_t *
  648. __restrict __mutex,
  649. int *__restrict __prioceiling)
  650. __THROW __nonnull ((1, 2));
  651. /* Set the priority ceiling of MUTEX to PRIOCEILING, return old
  652. priority ceiling value in *OLD_CEILING. */
  653. extern int pthread_mutex_setprioceiling (pthread_mutex_t *__restrict __mutex,
  654. int __prioceiling,
  655. int *__restrict __old_ceiling)
  656. __THROW __nonnull ((1, 3));
  657. #ifdef __USE_XOPEN2K8
  658. /* Declare the state protected by MUTEX as consistent. */
  659. extern int pthread_mutex_consistent (pthread_mutex_t *__mutex)
  660. __THROW __nonnull ((1));
  661. # ifdef __USE_GNU
  662. extern int pthread_mutex_consistent_np (pthread_mutex_t *__mutex)
  663. __THROW __nonnull ((1));
  664. # endif
  665. #endif
  666. /* Functions for handling mutex attributes. */
  667. /* Initialize mutex attribute object ATTR with default attributes
  668. (kind is PTHREAD_MUTEX_TIMED_NP). */
  669. extern int pthread_mutexattr_init (pthread_mutexattr_t *__attr)
  670. __THROW __nonnull ((1));
  671. /* Destroy mutex attribute object ATTR. */
  672. extern int pthread_mutexattr_destroy (pthread_mutexattr_t *__attr)
  673. __THROW __nonnull ((1));
  674. /* Get the process-shared flag of the mutex attribute ATTR. */
  675. extern int pthread_mutexattr_getpshared (const pthread_mutexattr_t *
  676. __restrict __attr,
  677. int *__restrict __pshared)
  678. __THROW __nonnull ((1, 2));
  679. /* Set the process-shared flag of the mutex attribute ATTR. */
  680. extern int pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
  681. int __pshared)
  682. __THROW __nonnull ((1));
  683. #if defined __USE_UNIX98 || defined __USE_XOPEN2K8
  684. /* Return in *KIND the mutex kind attribute in *ATTR. */
  685. extern int pthread_mutexattr_gettype (const pthread_mutexattr_t *__restrict
  686. __attr, int *__restrict __kind)
  687. __THROW __nonnull ((1, 2));
  688. /* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_NORMAL,
  689. PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK, or
  690. PTHREAD_MUTEX_DEFAULT). */
  691. extern int pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind)
  692. __THROW __nonnull ((1));
  693. #endif
  694. /* Return in *PROTOCOL the mutex protocol attribute in *ATTR. */
  695. extern int pthread_mutexattr_getprotocol (const pthread_mutexattr_t *
  696. __restrict __attr,
  697. int *__restrict __protocol)
  698. __THROW __nonnull ((1, 2));
  699. /* Set the mutex protocol attribute in *ATTR to PROTOCOL (either
  700. PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT, or PTHREAD_PRIO_PROTECT). */
  701. extern int pthread_mutexattr_setprotocol (pthread_mutexattr_t *__attr,
  702. int __protocol)
  703. __THROW __nonnull ((1));
  704. /* Return in *PRIOCEILING the mutex prioceiling attribute in *ATTR. */
  705. extern int pthread_mutexattr_getprioceiling (const pthread_mutexattr_t *
  706. __restrict __attr,
  707. int *__restrict __prioceiling)
  708. __THROW __nonnull ((1, 2));
  709. /* Set the mutex prioceiling attribute in *ATTR to PRIOCEILING. */
  710. extern int pthread_mutexattr_setprioceiling (pthread_mutexattr_t *__attr,
  711. int __prioceiling)
  712. __THROW __nonnull ((1));
  713. #ifdef __USE_XOPEN2K
  714. /* Get the robustness flag of the mutex attribute ATTR. */
  715. extern int pthread_mutexattr_getrobust (const pthread_mutexattr_t *__attr,
  716. int *__robustness)
  717. __THROW __nonnull ((1, 2));
  718. # ifdef __USE_GNU
  719. extern int pthread_mutexattr_getrobust_np (const pthread_mutexattr_t *__attr,
  720. int *__robustness)
  721. __THROW __nonnull ((1, 2));
  722. # endif
  723. /* Set the robustness flag of the mutex attribute ATTR. */
  724. extern int pthread_mutexattr_setrobust (pthread_mutexattr_t *__attr,
  725. int __robustness)
  726. __THROW __nonnull ((1));
  727. # ifdef __USE_GNU
  728. extern int pthread_mutexattr_setrobust_np (pthread_mutexattr_t *__attr,
  729. int __robustness)
  730. __THROW __nonnull ((1));
  731. # endif
  732. #endif
  733. #if defined __USE_UNIX98 || defined __USE_XOPEN2K
  734. /* Functions for handling read-write locks. */
  735. /* Initialize read-write lock RWLOCK using attributes ATTR, or use
  736. the default values if later is NULL. */
  737. extern int pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
  738. const pthread_rwlockattr_t *__restrict
  739. __attr) __THROW __nonnull ((1));
  740. /* Destroy read-write lock RWLOCK. */
  741. extern int pthread_rwlock_destroy (pthread_rwlock_t *__rwlock)
  742. __THROW __nonnull ((1));
  743. /* Acquire read lock for RWLOCK. */
  744. extern int pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock)
  745. __THROWNL __nonnull ((1));
  746. /* Try to acquire read lock for RWLOCK. */
  747. extern int pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock)
  748. __THROWNL __nonnull ((1));
  749. # ifdef __USE_XOPEN2K
  750. /* Try to acquire read lock for RWLOCK or return after specfied time. */
  751. extern int pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock,
  752. const struct timespec *__restrict
  753. __abstime) __THROWNL __nonnull ((1, 2));
  754. # endif
  755. # ifdef __USE_GNU
  756. extern int pthread_rwlock_clockrdlock (pthread_rwlock_t *__restrict __rwlock,
  757. clockid_t __clockid,
  758. const struct timespec *__restrict
  759. __abstime) __THROWNL __nonnull ((1, 3));
  760. # endif
  761. /* Acquire write lock for RWLOCK. */
  762. extern int pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock)
  763. __THROWNL __nonnull ((1));
  764. /* Try to acquire write lock for RWLOCK. */
  765. extern int pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock)
  766. __THROWNL __nonnull ((1));
  767. # ifdef __USE_XOPEN2K
  768. /* Try to acquire write lock for RWLOCK or return after specfied time. */
  769. extern int pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock,
  770. const struct timespec *__restrict
  771. __abstime) __THROWNL __nonnull ((1, 2));
  772. # endif
  773. # ifdef __USE_GNU
  774. extern int pthread_rwlock_clockwrlock (pthread_rwlock_t *__restrict __rwlock,
  775. clockid_t __clockid,
  776. const struct timespec *__restrict
  777. __abstime) __THROWNL __nonnull ((1, 3));
  778. # endif
  779. /* Unlock RWLOCK. */
  780. extern int pthread_rwlock_unlock (pthread_rwlock_t *__rwlock)
  781. __THROWNL __nonnull ((1));
  782. /* Functions for handling read-write lock attributes. */
  783. /* Initialize attribute object ATTR with default values. */
  784. extern int pthread_rwlockattr_init (pthread_rwlockattr_t *__attr)
  785. __THROW __nonnull ((1));
  786. /* Destroy attribute object ATTR. */
  787. extern int pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr)
  788. __THROW __nonnull ((1));
  789. /* Return current setting of process-shared attribute of ATTR in PSHARED. */
  790. extern int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *
  791. __restrict __attr,
  792. int *__restrict __pshared)
  793. __THROW __nonnull ((1, 2));
  794. /* Set process-shared attribute of ATTR to PSHARED. */
  795. extern int pthread_rwlockattr_setpshared (pthread_rwlockattr_t *__attr,
  796. int __pshared)
  797. __THROW __nonnull ((1));
  798. /* Return current setting of reader/writer preference. */
  799. extern int pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t *
  800. __restrict __attr,
  801. int *__restrict __pref)
  802. __THROW __nonnull ((1, 2));
  803. /* Set reader/write preference. */
  804. extern int pthread_rwlockattr_setkind_np (pthread_rwlockattr_t *__attr,
  805. int __pref) __THROW __nonnull ((1));
  806. #endif
  807. /* Functions for handling conditional variables. */
  808. /* Initialize condition variable COND using attributes ATTR, or use
  809. the default values if later is NULL. */
  810. extern int pthread_cond_init (pthread_cond_t *__restrict __cond,
  811. const pthread_condattr_t *__restrict __cond_attr)
  812. __THROW __nonnull ((1));
  813. /* Destroy condition variable COND. */
  814. extern int pthread_cond_destroy (pthread_cond_t *__cond)
  815. __THROW __nonnull ((1));
  816. /* Wake up one thread waiting for condition variable COND. */
  817. extern int pthread_cond_signal (pthread_cond_t *__cond)
  818. __THROWNL __nonnull ((1));
  819. /* Wake up all threads waiting for condition variables COND. */
  820. extern int pthread_cond_broadcast (pthread_cond_t *__cond)
  821. __THROWNL __nonnull ((1));
  822. /* Wait for condition variable COND to be signaled or broadcast.
  823. MUTEX is assumed to be locked before.
  824. This function is a cancellation point and therefore not marked with
  825. __THROW. */
  826. extern int pthread_cond_wait (pthread_cond_t *__restrict __cond,
  827. pthread_mutex_t *__restrict __mutex)
  828. __nonnull ((1, 2));
  829. /* Wait for condition variable COND to be signaled or broadcast until
  830. ABSTIME. MUTEX is assumed to be locked before. ABSTIME is an
  831. absolute time specification; zero is the beginning of the epoch
  832. (00:00:00 GMT, January 1, 1970).
  833. This function is a cancellation point and therefore not marked with
  834. __THROW. */
  835. extern int pthread_cond_timedwait (pthread_cond_t *__restrict __cond,
  836. pthread_mutex_t *__restrict __mutex,
  837. const struct timespec *__restrict __abstime)
  838. __nonnull ((1, 2, 3));
  839. # ifdef __USE_GNU
  840. /* Wait for condition variable COND to be signaled or broadcast until
  841. ABSTIME measured by the specified clock. MUTEX is assumed to be
  842. locked before. CLOCK is the clock to use. ABSTIME is an absolute
  843. time specification against CLOCK's epoch.
  844. This function is a cancellation point and therefore not marked with
  845. __THROW. */
  846. extern int pthread_cond_clockwait (pthread_cond_t *__restrict __cond,
  847. pthread_mutex_t *__restrict __mutex,
  848. __clockid_t __clock_id,
  849. const struct timespec *__restrict __abstime)
  850. __nonnull ((1, 2, 4));
  851. # endif
  852. /* Functions for handling condition variable attributes. */
  853. /* Initialize condition variable attribute ATTR. */
  854. extern int pthread_condattr_init (pthread_condattr_t *__attr)
  855. __THROW __nonnull ((1));
  856. /* Destroy condition variable attribute ATTR. */
  857. extern int pthread_condattr_destroy (pthread_condattr_t *__attr)
  858. __THROW __nonnull ((1));
  859. /* Get the process-shared flag of the condition variable attribute ATTR. */
  860. extern int pthread_condattr_getpshared (const pthread_condattr_t *
  861. __restrict __attr,
  862. int *__restrict __pshared)
  863. __THROW __nonnull ((1, 2));
  864. /* Set the process-shared flag of the condition variable attribute ATTR. */
  865. extern int pthread_condattr_setpshared (pthread_condattr_t *__attr,
  866. int __pshared) __THROW __nonnull ((1));
  867. #ifdef __USE_XOPEN2K
  868. /* Get the clock selected for the condition variable attribute ATTR. */
  869. extern int pthread_condattr_getclock (const pthread_condattr_t *
  870. __restrict __attr,
  871. __clockid_t *__restrict __clock_id)
  872. __THROW __nonnull ((1, 2));
  873. /* Set the clock selected for the condition variable attribute ATTR. */
  874. extern int pthread_condattr_setclock (pthread_condattr_t *__attr,
  875. __clockid_t __clock_id)
  876. __THROW __nonnull ((1));
  877. #endif
  878. #ifdef __USE_XOPEN2K
  879. /* Functions to handle spinlocks. */
  880. /* Initialize the spinlock LOCK. If PSHARED is nonzero the spinlock can
  881. be shared between different processes. */
  882. extern int pthread_spin_init (pthread_spinlock_t *__lock, int __pshared)
  883. __THROW __nonnull ((1));
  884. /* Destroy the spinlock LOCK. */
  885. extern int pthread_spin_destroy (pthread_spinlock_t *__lock)
  886. __THROW __nonnull ((1));
  887. /* Wait until spinlock LOCK is retrieved. */
  888. extern int pthread_spin_lock (pthread_spinlock_t *__lock)
  889. __THROWNL __nonnull ((1));
  890. /* Try to lock spinlock LOCK. */
  891. extern int pthread_spin_trylock (pthread_spinlock_t *__lock)
  892. __THROWNL __nonnull ((1));
  893. /* Release spinlock LOCK. */
  894. extern int pthread_spin_unlock (pthread_spinlock_t *__lock)
  895. __THROWNL __nonnull ((1));
  896. /* Functions to handle barriers. */
  897. /* Initialize BARRIER with the attributes in ATTR. The barrier is
  898. opened when COUNT waiters arrived. */
  899. extern int pthread_barrier_init (pthread_barrier_t *__restrict __barrier,
  900. const pthread_barrierattr_t *__restrict
  901. __attr, unsigned int __count)
  902. __THROW __nonnull ((1));
  903. /* Destroy a previously dynamically initialized barrier BARRIER. */
  904. extern int pthread_barrier_destroy (pthread_barrier_t *__barrier)
  905. __THROW __nonnull ((1));
  906. /* Wait on barrier BARRIER. */
  907. extern int pthread_barrier_wait (pthread_barrier_t *__barrier)
  908. __THROWNL __nonnull ((1));
  909. /* Initialize barrier attribute ATTR. */
  910. extern int pthread_barrierattr_init (pthread_barrierattr_t *__attr)
  911. __THROW __nonnull ((1));
  912. /* Destroy previously dynamically initialized barrier attribute ATTR. */
  913. extern int pthread_barrierattr_destroy (pthread_barrierattr_t *__attr)
  914. __THROW __nonnull ((1));
  915. /* Get the process-shared flag of the barrier attribute ATTR. */
  916. extern int pthread_barrierattr_getpshared (const pthread_barrierattr_t *
  917. __restrict __attr,
  918. int *__restrict __pshared)
  919. __THROW __nonnull ((1, 2));
  920. /* Set the process-shared flag of the barrier attribute ATTR. */
  921. extern int pthread_barrierattr_setpshared (pthread_barrierattr_t *__attr,
  922. int __pshared)
  923. __THROW __nonnull ((1));
  924. #endif
  925. /* Functions for handling thread-specific data. */
  926. /* Create a key value identifying a location in the thread-specific
  927. data area. Each thread maintains a distinct thread-specific data
  928. area. DESTR_FUNCTION, if non-NULL, is called with the value
  929. associated to that key when the key is destroyed.
  930. DESTR_FUNCTION is not called if the value associated is NULL when
  931. the key is destroyed. */
  932. extern int pthread_key_create (pthread_key_t *__key,
  933. void (*__destr_function) (void *))
  934. __THROW __nonnull ((1));
  935. /* Destroy KEY. */
  936. extern int pthread_key_delete (pthread_key_t __key) __THROW;
  937. /* Return current value of the thread-specific data slot identified by KEY. */
  938. extern void *pthread_getspecific (pthread_key_t __key) __THROW;
  939. /* Store POINTER in the thread-specific data slot identified by KEY. */
  940. extern int pthread_setspecific (pthread_key_t __key,
  941. const void *__pointer) __THROW ;
  942. #ifdef __USE_XOPEN2K
  943. /* Get ID of CPU-time clock for thread THREAD_ID. */
  944. extern int pthread_getcpuclockid (pthread_t __thread_id,
  945. __clockid_t *__clock_id)
  946. __THROW __nonnull ((2));
  947. #endif
  948. /* Install handlers to be called when a new process is created with FORK.
  949. The PREPARE handler is called in the parent process just before performing
  950. FORK. The PARENT handler is called in the parent process just after FORK.
  951. The CHILD handler is called in the child process. Each of the three
  952. handlers can be NULL, meaning that no handler needs to be called at that
  953. point.
  954. PTHREAD_ATFORK can be called several times, in which case the PREPARE
  955. handlers are called in LIFO order (last added with PTHREAD_ATFORK,
  956. first called before FORK), and the PARENT and CHILD handlers are called
  957. in FIFO (first added, first called). */
  958. extern int pthread_atfork (void (*__prepare) (void),
  959. void (*__parent) (void),
  960. void (*__child) (void)) __THROW;
  961. #ifdef __USE_EXTERN_INLINES
  962. /* Optimizations. */
  963. __extern_inline int
  964. __NTH (pthread_equal (pthread_t __thread1, pthread_t __thread2))
  965. {
  966. return __thread1 == __thread2;
  967. }
  968. #endif
  969. __END_DECLS
  970. #endif /* pthread.h */