pthread.h 47 KB

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