pthread.h 48 KB

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