pthread.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /* Copyright (C) 2002, 2003, 2004, 2005 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, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. #ifndef _PTHREAD_H
  16. #define _PTHREAD_H 1
  17. #include <features.h>
  18. #include <sched.h>
  19. #include <time.h>
  20. #define __need_sigset_t
  21. #include <signal.h>
  22. #include <bits/pthreadtypes.h>
  23. #include <bits/setjmp.h>
  24. #include <bits/wordsize.h>
  25. #if defined _LIBC && ( defined IS_IN_libc || defined NOT_IN_libc )
  26. #include <bits/uClibc_pthread.h>
  27. #endif
  28. /* Detach state. */
  29. enum
  30. {
  31. PTHREAD_CREATE_JOINABLE,
  32. #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
  33. PTHREAD_CREATE_DETACHED
  34. #define PTHREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
  35. };
  36. /* Mutex types. */
  37. enum
  38. {
  39. PTHREAD_MUTEX_TIMED_NP,
  40. PTHREAD_MUTEX_RECURSIVE_NP,
  41. PTHREAD_MUTEX_ERRORCHECK_NP,
  42. PTHREAD_MUTEX_ADAPTIVE_NP
  43. #ifdef __USE_UNIX98
  44. ,
  45. PTHREAD_MUTEX_NORMAL = PTHREAD_MUTEX_TIMED_NP,
  46. PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP,
  47. PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP,
  48. PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
  49. #endif
  50. #ifdef __USE_GNU
  51. /* For compatibility. */
  52. , PTHREAD_MUTEX_FAST_NP = PTHREAD_MUTEX_TIMED_NP
  53. #endif
  54. };
  55. /* Mutex initializers. */
  56. #if __WORDSIZE == 64
  57. # define PTHREAD_MUTEX_INITIALIZER \
  58. { { 0, 0, 0, 0, 0, 0, { 0, 0 } } }
  59. # ifdef __USE_GNU
  60. # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
  61. { { 0, 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, 0, { 0, 0 } } }
  62. # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
  63. { { 0, 0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, 0, { 0, 0 } } }
  64. # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
  65. { { 0, 0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP, 0, { 0, 0 } } }
  66. # endif
  67. #else
  68. # define PTHREAD_MUTEX_INITIALIZER \
  69. { { 0, 0, 0, 0, 0, { 0 } } }
  70. # ifdef __USE_GNU
  71. # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
  72. { { 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, 0, { 0 } } }
  73. # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
  74. { { 0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, 0, { 0 } } }
  75. # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
  76. { { 0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP, 0, { 0 } } }
  77. # endif
  78. #endif
  79. /* Read-write lock types. */
  80. #if defined __USE_UNIX98 || defined __USE_XOPEN2K
  81. enum
  82. {
  83. PTHREAD_RWLOCK_PREFER_READER_NP,
  84. PTHREAD_RWLOCK_PREFER_WRITER_NP,
  85. PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,
  86. PTHREAD_RWLOCK_DEFAULT_NP = PTHREAD_RWLOCK_PREFER_READER_NP
  87. };
  88. /* Read-write lock initializers. */
  89. # if __WORDSIZE == 64
  90. # define PTHREAD_RWLOCK_INITIALIZER \
  91. { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
  92. # else
  93. # define PTHREAD_RWLOCK_INITIALIZER \
  94. { { 0, 0, 0, 0, 0, 0, 0, 0 } }
  95. # endif
  96. # ifdef __USE_GNU
  97. # if __WORDSIZE == 64
  98. # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
  99. { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
  100. PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP } }
  101. # else
  102. # define PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP \
  103. { { 0, 0, 0, 0, 0, 0, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP, 0 } }
  104. # endif
  105. # endif
  106. #endif /* Unix98 or XOpen2K */
  107. /* Scheduler inheritance. */
  108. enum
  109. {
  110. PTHREAD_INHERIT_SCHED,
  111. #define PTHREAD_INHERIT_SCHED PTHREAD_INHERIT_SCHED
  112. PTHREAD_EXPLICIT_SCHED
  113. #define PTHREAD_EXPLICIT_SCHED PTHREAD_EXPLICIT_SCHED
  114. };
  115. /* Scope handling. */
  116. enum
  117. {
  118. PTHREAD_SCOPE_SYSTEM,
  119. #define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_SYSTEM
  120. PTHREAD_SCOPE_PROCESS
  121. #define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_PROCESS
  122. };
  123. /* Process shared or private flag. */
  124. enum
  125. {
  126. PTHREAD_PROCESS_PRIVATE,
  127. #define PTHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
  128. PTHREAD_PROCESS_SHARED
  129. #define PTHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
  130. };
  131. /* Conditional variable handling. */
  132. #define PTHREAD_COND_INITIALIZER { { 0, 0, 0, 0, 0, (void *) 0, 0, 0 } }
  133. /* Cleanup buffers */
  134. struct _pthread_cleanup_buffer
  135. {
  136. void (*__routine) (void *); /* Function to call. */
  137. void *__arg; /* Its argument. */
  138. int __canceltype; /* Saved cancellation type. */
  139. struct _pthread_cleanup_buffer *__prev; /* Chaining of cleanup functions. */
  140. };
  141. /* Cancellation */
  142. enum
  143. {
  144. PTHREAD_CANCEL_ENABLE,
  145. #define PTHREAD_CANCEL_ENABLE PTHREAD_CANCEL_ENABLE
  146. PTHREAD_CANCEL_DISABLE
  147. #define PTHREAD_CANCEL_DISABLE PTHREAD_CANCEL_DISABLE
  148. };
  149. enum
  150. {
  151. PTHREAD_CANCEL_DEFERRED,
  152. #define PTHREAD_CANCEL_DEFERRED PTHREAD_CANCEL_DEFERRED
  153. PTHREAD_CANCEL_ASYNCHRONOUS
  154. #define PTHREAD_CANCEL_ASYNCHRONOUS PTHREAD_CANCEL_ASYNCHRONOUS
  155. };
  156. #define PTHREAD_CANCELED ((void *) -1)
  157. /* Single execution handling. */
  158. #define PTHREAD_ONCE_INIT 0
  159. #ifdef __USE_XOPEN2K
  160. /* Value returned by 'pthread_barrier_wait' for one of the threads after
  161. the required number of threads have called this function.
  162. -1 is distinct from 0 and all errno constants */
  163. # define PTHREAD_BARRIER_SERIAL_THREAD -1
  164. #endif
  165. __BEGIN_DECLS
  166. /* Create a new thread, starting with execution of START-ROUTINE
  167. getting passed ARG. Creation attributed come from ATTR. The new
  168. handle is stored in *NEWTHREAD. */
  169. extern int pthread_create (pthread_t *__restrict __newthread,
  170. __const pthread_attr_t *__restrict __attr,
  171. void *(*__start_routine) (void *),
  172. void *__restrict __arg) __THROW;
  173. /* Terminate calling thread.
  174. The registered cleanup handlers are called via exception handling
  175. so we cannot mark this function with __THROW.*/
  176. extern void pthread_exit (void *__retval) __attribute__ ((__noreturn__));
  177. /* Make calling thread wait for termination of the thread TH. The
  178. exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN
  179. is not NULL.
  180. This function is a cancellation point and therefore not marked with
  181. __THROW. */
  182. extern int pthread_join (pthread_t __th, void **__thread_return);
  183. #ifdef __USE_GNU
  184. /* Check whether thread TH has terminated. If yes return the status of
  185. the thread in *THREAD_RETURN, if THREAD_RETURN is not NULL. */
  186. extern int pthread_tryjoin_np (pthread_t __th, void **__thread_return) __THROW;
  187. /* Make calling thread wait for termination of the thread TH, but only
  188. until TIMEOUT. The exit status of the thread is stored in
  189. *THREAD_RETURN, if THREAD_RETURN is not NULL.
  190. This function is a cancellation point and therefore not marked with
  191. __THROW. */
  192. extern int pthread_timedjoin_np (pthread_t __th, void **__thread_return,
  193. __const struct timespec *__abstime);
  194. #endif
  195. /* Indicate that the thread TH is never to be joined with PTHREAD_JOIN.
  196. The resources of TH will therefore be freed immediately when it
  197. terminates, instead of waiting for another thread to perform PTHREAD_JOIN
  198. on it. */
  199. extern int pthread_detach (pthread_t __th) __THROW;
  200. /* Obtain the identifier of the current thread. */
  201. extern pthread_t pthread_self (void) __THROW __attribute__ ((__const__));
  202. /* Compare two thread identifiers. */
  203. extern int pthread_equal (pthread_t __thread1, pthread_t __thread2) __THROW;
  204. /* Thread attribute handling. */
  205. /* Initialize thread attribute *ATTR with default attributes
  206. (detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER,
  207. no user-provided stack). */
  208. extern int pthread_attr_init (pthread_attr_t *__attr) __THROW;
  209. /* Destroy thread attribute *ATTR. */
  210. extern int pthread_attr_destroy (pthread_attr_t *__attr) __THROW;
  211. /* Get detach state attribute. */
  212. extern int pthread_attr_getdetachstate (__const pthread_attr_t *__attr,
  213. int *__detachstate) __THROW;
  214. /* Set detach state attribute. */
  215. extern int pthread_attr_setdetachstate (pthread_attr_t *__attr,
  216. int __detachstate) __THROW;
  217. /* Get the size of the guard area created for stack overflow protection. */
  218. extern int pthread_attr_getguardsize (__const pthread_attr_t *__attr,
  219. size_t *__guardsize) __THROW;
  220. /* Set the size of the guard area created for stack overflow protection. */
  221. extern int pthread_attr_setguardsize (pthread_attr_t *__attr,
  222. size_t __guardsize) __THROW;
  223. /* Return in *PARAM the scheduling parameters of *ATTR. */
  224. extern int pthread_attr_getschedparam (__const pthread_attr_t *__restrict
  225. __attr,
  226. struct sched_param *__restrict __param)
  227. __THROW;
  228. /* Set scheduling parameters (priority, etc) in *ATTR according to PARAM. */
  229. extern int pthread_attr_setschedparam (pthread_attr_t *__restrict __attr,
  230. __const struct sched_param *__restrict
  231. __param) __THROW;
  232. /* Return in *POLICY the scheduling policy of *ATTR. */
  233. extern int pthread_attr_getschedpolicy (__const pthread_attr_t *__restrict
  234. __attr, int *__restrict __policy)
  235. __THROW;
  236. /* Set scheduling policy in *ATTR according to POLICY. */
  237. extern int pthread_attr_setschedpolicy (pthread_attr_t *__attr, int __policy)
  238. __THROW;
  239. /* Return in *INHERIT the scheduling inheritance mode of *ATTR. */
  240. extern int pthread_attr_getinheritsched (__const pthread_attr_t *__restrict
  241. __attr, int *__restrict __inherit)
  242. __THROW;
  243. /* Set scheduling inheritance mode in *ATTR according to INHERIT. */
  244. extern int pthread_attr_setinheritsched (pthread_attr_t *__attr,
  245. int __inherit) __THROW;
  246. /* Return in *SCOPE the scheduling contention scope of *ATTR. */
  247. extern int pthread_attr_getscope (__const pthread_attr_t *__restrict __attr,
  248. int *__restrict __scope) __THROW;
  249. /* Set scheduling contention scope in *ATTR according to SCOPE. */
  250. extern int pthread_attr_setscope (pthread_attr_t *__attr, int __scope)
  251. __THROW;
  252. /* Return the previously set address for the stack. */
  253. extern int pthread_attr_getstackaddr (__const pthread_attr_t *__restrict
  254. __attr, void **__restrict __stackaddr)
  255. __THROW __attribute_deprecated__;
  256. /* Set the starting address of the stack of the thread to be created.
  257. Depending on whether the stack grows up or down the value must either
  258. be higher or lower than all the address in the memory block. The
  259. minimal size of the block must be PTHREAD_STACK_MIN. */
  260. extern int pthread_attr_setstackaddr (pthread_attr_t *__attr,
  261. void *__stackaddr)
  262. __THROW __attribute_deprecated__;
  263. /* Return the currently used minimal stack size. */
  264. extern int pthread_attr_getstacksize (__const pthread_attr_t *__restrict
  265. __attr, size_t *__restrict __stacksize)
  266. __THROW;
  267. /* Add information about the minimum stack size needed for the thread
  268. to be started. This size must never be less than PTHREAD_STACK_MIN
  269. and must also not exceed the system limits. */
  270. extern int pthread_attr_setstacksize (pthread_attr_t *__attr,
  271. size_t __stacksize) __THROW;
  272. #ifdef __USE_XOPEN2K
  273. /* Return the previously set address for the stack. */
  274. extern int pthread_attr_getstack (__const pthread_attr_t *__restrict __attr,
  275. void **__restrict __stackaddr,
  276. size_t *__restrict __stacksize) __THROW;
  277. /* The following two interfaces are intended to replace the last two. They
  278. require setting the address as well as the size since only setting the
  279. address will make the implementation on some architectures impossible. */
  280. extern int pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
  281. size_t __stacksize) __THROW;
  282. #endif
  283. #ifdef __USE_GNU
  284. /* Thread created with attribute ATTR will be limited to run only on
  285. the processors represented in CPUSET. */
  286. extern int pthread_attr_setaffinity_np (pthread_attr_t *__attr,
  287. size_t __cpusetsize,
  288. __const cpu_set_t *__cpuset) __THROW;
  289. /* Get bit set in CPUSET representing the processors threads created with
  290. ATTR can run on. */
  291. extern int pthread_attr_getaffinity_np (__const pthread_attr_t *__attr,
  292. size_t __cpusetsize,
  293. cpu_set_t *__cpuset) __THROW;
  294. /* Initialize thread attribute *ATTR with attributes corresponding to the
  295. already running thread TH. It shall be called on unitialized ATTR
  296. and destroyed with pthread_attr_destroy when no longer needed. */
  297. extern int pthread_getattr_np (pthread_t __th, pthread_attr_t *__attr) __THROW;
  298. #endif
  299. /* Functions for scheduling control. */
  300. /* Set the scheduling parameters for TARGET_THREAD according to POLICY
  301. and *PARAM. */
  302. extern int pthread_setschedparam (pthread_t __target_thread, int __policy,
  303. __const struct sched_param *__param)
  304. __THROW;
  305. /* Return in *POLICY and *PARAM the scheduling parameters for TARGET_THREAD. */
  306. extern int pthread_getschedparam (pthread_t __target_thread,
  307. int *__restrict __policy,
  308. struct sched_param *__restrict __param)
  309. __THROW;
  310. /* Set the scheduling priority for TARGET_THREAD. */
  311. extern int pthread_setschedprio (pthread_t __target_thread, int __prio)
  312. __THROW;
  313. #ifdef __USE_UNIX98
  314. /* Determine level of concurrency. */
  315. extern int pthread_getconcurrency (void) __THROW;
  316. /* Set new concurrency level to LEVEL. */
  317. extern int pthread_setconcurrency (int __level) __THROW;
  318. #endif
  319. #ifdef __USE_GNU
  320. /* Yield the processor to another thread or process.
  321. This function is similar to the POSIX `sched_yield' function but
  322. might be differently implemented in the case of a m-on-n thread
  323. implementation. */
  324. extern int pthread_yield (void) __THROW;
  325. /* Limit specified thread TH to run only on the processors represented
  326. in CPUSET. */
  327. extern int pthread_setaffinity_np (pthread_t __th, size_t __cpusetsize,
  328. __const cpu_set_t *__cpuset) __THROW;
  329. /* Get bit set in CPUSET representing the processors TH can run on. */
  330. extern int pthread_getaffinity_np (pthread_t __th, size_t __cpusetsize,
  331. cpu_set_t *__cpuset) __THROW;
  332. #endif
  333. /* Functions for handling initialization. */
  334. /* Guarantee that the initialization function INIT_ROUTINE will be called
  335. only once, even if pthread_once is executed several times with the
  336. same ONCE_CONTROL argument. ONCE_CONTROL must point to a static or
  337. extern variable initialized to PTHREAD_ONCE_INIT.
  338. The initialization functions might throw exception which is why
  339. this function is not marked with __THROW. */
  340. extern int pthread_once (pthread_once_t *__once_control,
  341. void (*__init_routine) (void));
  342. /* Functions for handling cancellation.
  343. Note that these functions are explicitly not marked to not throw an
  344. exception in C++ code. If cancellation is implemented by unwinding
  345. this is necessary to have the compiler generate the unwind information. */
  346. /* Set cancelability state of current thread to STATE, returning old
  347. state in *OLDSTATE if OLDSTATE is not NULL. */
  348. extern int pthread_setcancelstate (int __state, int *__oldstate);
  349. /* Set cancellation state of current thread to TYPE, returning the old
  350. type in *OLDTYPE if OLDTYPE is not NULL. */
  351. extern int pthread_setcanceltype (int __type, int *__oldtype);
  352. /* Cancel THREAD immediately or at the next possibility. */
  353. extern int pthread_cancel (pthread_t __th);
  354. /* Test for pending cancellation for the current thread and terminate
  355. the thread as per pthread_exit(PTHREAD_CANCELED) if it has been
  356. cancelled. */
  357. extern void pthread_testcancel (void);
  358. /* Cancellation handling with integration into exception handling. */
  359. typedef struct
  360. {
  361. struct
  362. {
  363. __jmp_buf __cancel_jmp_buf;
  364. int __mask_was_saved;
  365. } __cancel_jmp_buf[1];
  366. void *__pad[4];
  367. } __pthread_unwind_buf_t __attribute__ ((__aligned__));
  368. /* No special attributes by default. */
  369. #ifndef __cleanup_fct_attribute
  370. # define __cleanup_fct_attribute
  371. #endif
  372. /* Structure to hold the cleanup handler information. */
  373. struct __pthread_cleanup_frame
  374. {
  375. void (*__cancel_routine) (void *);
  376. void *__cancel_arg;
  377. int __do_it;
  378. int __cancel_type;
  379. };
  380. #if defined __GNUC__ && defined __EXCEPTIONS
  381. # ifdef __cplusplus
  382. /* Class to handle cancellation handler invocation. */
  383. class __pthread_cleanup_class
  384. {
  385. void (*__cancel_routine) (void *);
  386. void *__cancel_arg;
  387. int __do_it;
  388. int __cancel_type;
  389. public:
  390. __pthread_cleanup_class (void (*__fct) (void *), void *__arg)
  391. : __cancel_routine (__fct), __cancel_arg (__arg), __do_it (1) { }
  392. ~__pthread_cleanup_class () { if (__do_it) __cancel_routine (__cancel_arg); }
  393. void __setdoit (int __newval) { __do_it = __newval; }
  394. void __defer () { pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED,
  395. &__cancel_type); }
  396. void __restore () const { pthread_setcanceltype (__cancel_type, 0); }
  397. };
  398. /* Install a cleanup handler: ROUTINE will be called with arguments ARG
  399. when the thread is canceled or calls pthread_exit. ROUTINE will also
  400. be called with arguments ARG when the matching pthread_cleanup_pop
  401. is executed with non-zero EXECUTE argument.
  402. pthread_cleanup_push and pthread_cleanup_pop are macros and must always
  403. be used in matching pairs at the same nesting level of braces. */
  404. # define pthread_cleanup_push(routine, arg) \
  405. do { \
  406. __pthread_cleanup_class __clframe (routine, arg)
  407. /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
  408. If EXECUTE is non-zero, the handler function is called. */
  409. # define pthread_cleanup_pop(execute) \
  410. __clframe.__setdoit (execute); \
  411. } while (0)
  412. # ifdef __USE_GNU
  413. /* Install a cleanup handler as pthread_cleanup_push does, but also
  414. saves the current cancellation type and sets it to deferred
  415. cancellation. */
  416. # define pthread_cleanup_push_defer_np(routine, arg) \
  417. do { \
  418. __pthread_cleanup_class __clframe (routine, arg); \
  419. __clframe.__defer ()
  420. /* Remove a cleanup handler as pthread_cleanup_pop does, but also
  421. restores the cancellation type that was in effect when the matching
  422. pthread_cleanup_push_defer was called. */
  423. # define pthread_cleanup_pop_restore_np(execute) \
  424. __clframe.__restore (); \
  425. __clframe.__setdoit (execute); \
  426. } while (0)
  427. # endif
  428. # else
  429. /* Function called to call the cleanup handler. As an extern inline
  430. function the compiler is free to decide inlining the change when
  431. needed or fall back on the copy which must exist somewhere
  432. else. */
  433. __extern_inline void
  434. __pthread_cleanup_routine (struct __pthread_cleanup_frame *__frame);
  435. __extern_inline void
  436. __pthread_cleanup_routine (struct __pthread_cleanup_frame *__frame)
  437. {
  438. if (__frame->__do_it)
  439. __frame->__cancel_routine (__frame->__cancel_arg);
  440. }
  441. /* Install a cleanup handler: ROUTINE will be called with arguments ARG
  442. when the thread is canceled or calls pthread_exit. ROUTINE will also
  443. be called with arguments ARG when the matching pthread_cleanup_pop
  444. is executed with non-zero EXECUTE argument.
  445. pthread_cleanup_push and pthread_cleanup_pop are macros and must always
  446. be used in matching pairs at the same nesting level of braces. */
  447. # define pthread_cleanup_push(routine, arg) \
  448. do { \
  449. struct __pthread_cleanup_frame __clframe \
  450. __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
  451. = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
  452. .__do_it = 1 };
  453. /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
  454. If EXECUTE is non-zero, the handler function is called. */
  455. # define pthread_cleanup_pop(execute) \
  456. __clframe.__do_it = (execute); \
  457. } while (0)
  458. # ifdef __USE_GNU
  459. /* Install a cleanup handler as pthread_cleanup_push does, but also
  460. saves the current cancellation type and sets it to deferred
  461. cancellation. */
  462. # define pthread_cleanup_push_defer_np(routine, arg) \
  463. do { \
  464. struct __pthread_cleanup_frame __clframe \
  465. __attribute__ ((__cleanup__ (__pthread_cleanup_routine))) \
  466. = { .__cancel_routine = (routine), .__cancel_arg = (arg), \
  467. .__do_it = 1 }; \
  468. (void) pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, \
  469. &__clframe.__cancel_type)
  470. /* Remove a cleanup handler as pthread_cleanup_pop does, but also
  471. restores the cancellation type that was in effect when the matching
  472. pthread_cleanup_push_defer was called. */
  473. # define pthread_cleanup_pop_restore_np(execute) \
  474. (void) pthread_setcanceltype (__clframe.__cancel_type, NULL); \
  475. __clframe.__do_it = (execute); \
  476. } while (0)
  477. # endif
  478. # endif
  479. #else
  480. /* Install a cleanup handler: ROUTINE will be called with arguments ARG
  481. when the thread is canceled or calls pthread_exit. ROUTINE will also
  482. be called with arguments ARG when the matching pthread_cleanup_pop
  483. is executed with non-zero EXECUTE argument.
  484. pthread_cleanup_push and pthread_cleanup_pop are macros and must always
  485. be used in matching pairs at the same nesting level of braces. */
  486. # define pthread_cleanup_push(routine, arg) \
  487. do { \
  488. __pthread_unwind_buf_t __cancel_buf; \
  489. void (*__cancel_routine) (void *) = (routine); \
  490. void *__cancel_arg = (arg); \
  491. int not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) \
  492. __cancel_buf.__cancel_jmp_buf, 0); \
  493. if (__builtin_expect (not_first_call, 0)) \
  494. { \
  495. __cancel_routine (__cancel_arg); \
  496. __pthread_unwind_next (&__cancel_buf); \
  497. /* NOTREACHED */ \
  498. } \
  499. \
  500. __pthread_register_cancel (&__cancel_buf); \
  501. do {
  502. extern void __pthread_register_cancel (__pthread_unwind_buf_t *__buf)
  503. __cleanup_fct_attribute;
  504. /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
  505. If EXECUTE is non-zero, the handler function is called. */
  506. # define pthread_cleanup_pop(execute) \
  507. } while (0); \
  508. __pthread_unregister_cancel (&__cancel_buf); \
  509. if (execute) \
  510. __cancel_routine (__cancel_arg); \
  511. } while (0)
  512. extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf)
  513. __cleanup_fct_attribute;
  514. # ifdef __USE_GNU
  515. /* Install a cleanup handler as pthread_cleanup_push does, but also
  516. saves the current cancellation type and sets it to deferred
  517. cancellation. */
  518. # define pthread_cleanup_push_defer_np(routine, arg) \
  519. do { \
  520. __pthread_unwind_buf_t __cancel_buf; \
  521. void (*__cancel_routine) (void *) = (routine); \
  522. void *__cancel_arg = (arg); \
  523. int not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) \
  524. __cancel_buf.__cancel_jmp_buf, 0); \
  525. if (__builtin_expect (not_first_call, 0)) \
  526. { \
  527. __cancel_routine (__cancel_arg); \
  528. __pthread_unwind_next (&__cancel_buf); \
  529. /* NOTREACHED */ \
  530. } \
  531. \
  532. __pthread_register_cancel_defer (&__cancel_buf); \
  533. do {
  534. extern void __pthread_register_cancel_defer (__pthread_unwind_buf_t *__buf)
  535. __cleanup_fct_attribute;
  536. /* Remove a cleanup handler as pthread_cleanup_pop does, but also
  537. restores the cancellation type that was in effect when the matching
  538. pthread_cleanup_push_defer was called. */
  539. # define pthread_cleanup_pop_restore_np(execute) \
  540. } while (0); \
  541. __pthread_unregister_cancel_restore (&__cancel_buf); \
  542. if (execute) \
  543. __cancel_routine (__cancel_arg); \
  544. } while (0)
  545. extern void __pthread_unregister_cancel_restore (__pthread_unwind_buf_t *__buf)
  546. __cleanup_fct_attribute;
  547. # endif
  548. /* Internal interface to initiate cleanup. */
  549. extern void __pthread_unwind_next (__pthread_unwind_buf_t *__buf)
  550. __cleanup_fct_attribute __attribute ((__noreturn__))
  551. # ifndef SHARED
  552. __attribute ((__weak__))
  553. # endif
  554. ;
  555. #endif
  556. /* Function used in the macros. */
  557. struct __jmp_buf_tag;
  558. extern int __sigsetjmp (struct __jmp_buf_tag *__env, int __savemask) __THROW;
  559. /* Mutex handling. */
  560. /* Initialize a mutex. */
  561. extern int pthread_mutex_init (pthread_mutex_t *__mutex,
  562. __const pthread_mutexattr_t *__mutexattr)
  563. __THROW;
  564. /* Destroy a mutex. */
  565. extern int pthread_mutex_destroy (pthread_mutex_t *__mutex) __THROW;
  566. /* Try locking a mutex. */
  567. extern int pthread_mutex_trylock (pthread_mutex_t *_mutex) __THROW;
  568. /* Lock a mutex. */
  569. extern int pthread_mutex_lock (pthread_mutex_t *__mutex) __THROW;
  570. #ifdef __USE_XOPEN2K
  571. /* Wait until lock becomes available, or specified time passes. */
  572. extern int pthread_mutex_timedlock (pthread_mutex_t *__restrict __mutex,
  573. __const struct timespec *__restrict
  574. __abstime) __THROW;
  575. #endif
  576. /* Unlock a mutex. */
  577. extern int pthread_mutex_unlock (pthread_mutex_t *__mutex) __THROW;
  578. /* Functions for handling mutex attributes. */
  579. /* Initialize mutex attribute object ATTR with default attributes
  580. (kind is PTHREAD_MUTEX_TIMED_NP). */
  581. extern int pthread_mutexattr_init (pthread_mutexattr_t *__attr) __THROW;
  582. /* Destroy mutex attribute object ATTR. */
  583. extern int pthread_mutexattr_destroy (pthread_mutexattr_t *__attr) __THROW;
  584. /* Get the process-shared flag of the mutex attribute ATTR. */
  585. extern int pthread_mutexattr_getpshared (__const pthread_mutexattr_t *
  586. __restrict __attr,
  587. int *__restrict __pshared) __THROW;
  588. /* Set the process-shared flag of the mutex attribute ATTR. */
  589. extern int pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
  590. int __pshared) __THROW;
  591. #ifdef __USE_UNIX98
  592. /* Return in *KIND the mutex kind attribute in *ATTR. */
  593. extern int pthread_mutexattr_gettype (__const pthread_mutexattr_t *__restrict
  594. __attr, int *__restrict __kind) __THROW;
  595. /* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_NORMAL,
  596. PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK, or
  597. PTHREAD_MUTEX_DEFAULT). */
  598. extern int pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind)
  599. __THROW;
  600. #endif
  601. #if defined __USE_UNIX98 || defined __USE_XOPEN2K
  602. /* Functions for handling read-write locks. */
  603. /* Initialize read-write lock RWLOCK using attributes ATTR, or use
  604. the default values if later is NULL. */
  605. extern int pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
  606. __const pthread_rwlockattr_t *__restrict
  607. __attr) __THROW;
  608. /* Destroy read-write lock RWLOCK. */
  609. extern int pthread_rwlock_destroy (pthread_rwlock_t *__rwlock) __THROW;
  610. /* Acquire read lock for RWLOCK. */
  611. extern int pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock) __THROW;
  612. /* Try to acquire read lock for RWLOCK. */
  613. extern int pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock) __THROW;
  614. # ifdef __USE_XOPEN2K
  615. /* Try to acquire read lock for RWLOCK or return after specfied time. */
  616. extern int pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock,
  617. __const struct timespec *__restrict
  618. __abstime) __THROW;
  619. # endif
  620. /* Acquire write lock for RWLOCK. */
  621. extern int pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock) __THROW;
  622. /* Try to acquire write lock for RWLOCK. */
  623. extern int pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock) __THROW;
  624. # ifdef __USE_XOPEN2K
  625. /* Try to acquire write lock for RWLOCK or return after specfied time. */
  626. extern int pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock,
  627. __const struct timespec *__restrict
  628. __abstime) __THROW;
  629. # endif
  630. /* Unlock RWLOCK. */
  631. extern int pthread_rwlock_unlock (pthread_rwlock_t *__rwlock) __THROW;
  632. /* Functions for handling read-write lock attributes. */
  633. /* Initialize attribute object ATTR with default values. */
  634. extern int pthread_rwlockattr_init (pthread_rwlockattr_t *__attr) __THROW;
  635. /* Destroy attribute object ATTR. */
  636. extern int pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr) __THROW;
  637. /* Return current setting of process-shared attribute of ATTR in PSHARED. */
  638. extern int pthread_rwlockattr_getpshared (__const pthread_rwlockattr_t *
  639. __restrict __attr,
  640. int *__restrict __pshared) __THROW;
  641. /* Set process-shared attribute of ATTR to PSHARED. */
  642. extern int pthread_rwlockattr_setpshared (pthread_rwlockattr_t *__attr,
  643. int __pshared) __THROW;
  644. /* Return current setting of reader/writer preference. */
  645. extern int pthread_rwlockattr_getkind_np (__const pthread_rwlockattr_t *
  646. __restrict __attr,
  647. int *__restrict __pref) __THROW;
  648. /* Set reader/write preference. */
  649. extern int pthread_rwlockattr_setkind_np (pthread_rwlockattr_t *__attr,
  650. int __pref) __THROW;
  651. #endif
  652. /* Functions for handling conditional variables. */
  653. /* Initialize condition variable COND using attributes ATTR, or use
  654. the default values if later is NULL. */
  655. extern int pthread_cond_init (pthread_cond_t *__restrict __cond,
  656. __const pthread_condattr_t *__restrict
  657. __cond_attr) __THROW;
  658. /* Destroy condition variable COND. */
  659. extern int pthread_cond_destroy (pthread_cond_t *__cond) __THROW;
  660. /* Wake up one thread waiting for condition variable COND. */
  661. extern int pthread_cond_signal (pthread_cond_t *__cond) __THROW;
  662. /* Wake up all threads waiting for condition variables COND. */
  663. extern int pthread_cond_broadcast (pthread_cond_t *__cond) __THROW;
  664. /* Wait for condition variable COND to be signaled or broadcast.
  665. MUTEX is assumed to be locked before.
  666. This function is a cancellation point and therefore not marked with
  667. __THROW. */
  668. extern int pthread_cond_wait (pthread_cond_t *__restrict __cond,
  669. pthread_mutex_t *__restrict __mutex);
  670. /* Wait for condition variable COND to be signaled or broadcast until
  671. ABSTIME. MUTEX is assumed to be locked before. ABSTIME is an
  672. absolute time specification; zero is the beginning of the epoch
  673. (00:00:00 GMT, January 1, 1970).
  674. This function is a cancellation point and therefore not marked with
  675. __THROW. */
  676. extern int pthread_cond_timedwait (pthread_cond_t *__restrict __cond,
  677. pthread_mutex_t *__restrict __mutex,
  678. __const struct timespec *__restrict
  679. __abstime);
  680. /* Functions for handling condition variable attributes. */
  681. /* Initialize condition variable attribute ATTR. */
  682. extern int pthread_condattr_init (pthread_condattr_t *__attr) __THROW;
  683. /* Destroy condition variable attribute ATTR. */
  684. extern int pthread_condattr_destroy (pthread_condattr_t *__attr) __THROW;
  685. /* Get the process-shared flag of the condition variable attribute ATTR. */
  686. extern int pthread_condattr_getpshared (__const pthread_condattr_t *
  687. __restrict __attr,
  688. int *__restrict __pshared) __THROW;
  689. /* Set the process-shared flag of the condition variable attribute ATTR. */
  690. extern int pthread_condattr_setpshared (pthread_condattr_t *__attr,
  691. int __pshared) __THROW;
  692. #ifdef __USE_XOPEN2K
  693. /* Get the clock selected for the conditon variable attribute ATTR. */
  694. extern int pthread_condattr_getclock (__const pthread_condattr_t *
  695. __restrict __attr,
  696. __clockid_t *__restrict __clock_id)
  697. __THROW;
  698. /* Set the clock selected for the conditon variable attribute ATTR. */
  699. extern int pthread_condattr_setclock (pthread_condattr_t *__attr,
  700. __clockid_t __clock_id) __THROW;
  701. #endif
  702. #ifdef __USE_XOPEN2K
  703. /* Functions to handle spinlocks. */
  704. /* Initialize the spinlock LOCK. If PSHARED is nonzero the spinlock can
  705. be shared between different processes. */
  706. extern int pthread_spin_init (pthread_spinlock_t *__lock, int __pshared)
  707. __THROW;
  708. /* Destroy the spinlock LOCK. */
  709. extern int pthread_spin_destroy (pthread_spinlock_t *__lock) __THROW;
  710. /* Wait until spinlock LOCK is retrieved. */
  711. extern int pthread_spin_lock (pthread_spinlock_t *__lock) __THROW;
  712. /* Try to lock spinlock LOCK. */
  713. extern int pthread_spin_trylock (pthread_spinlock_t *__lock) __THROW;
  714. /* Release spinlock LOCK. */
  715. extern int pthread_spin_unlock (pthread_spinlock_t *__lock) __THROW;
  716. /* Functions to handle barriers. */
  717. /* Initialize BARRIER with the attributes in ATTR. The barrier is
  718. opened when COUNT waiters arrived. */
  719. extern int pthread_barrier_init (pthread_barrier_t *__restrict __barrier,
  720. __const pthread_barrierattr_t *__restrict
  721. __attr, unsigned int __count) __THROW;
  722. /* Destroy a previously dynamically initialized barrier BARRIER. */
  723. extern int pthread_barrier_destroy (pthread_barrier_t *__barrier) __THROW;
  724. /* Wait on barrier BARRIER. */
  725. extern int pthread_barrier_wait (pthread_barrier_t *__barrier) __THROW;
  726. /* Initialize barrier attribute ATTR. */
  727. extern int pthread_barrierattr_init (pthread_barrierattr_t *__attr) __THROW;
  728. /* Destroy previously dynamically initialized barrier attribute ATTR. */
  729. extern int pthread_barrierattr_destroy (pthread_barrierattr_t *__attr) __THROW;
  730. /* Get the process-shared flag of the barrier attribute ATTR. */
  731. extern int pthread_barrierattr_getpshared (__const pthread_barrierattr_t *
  732. __restrict __attr,
  733. int *__restrict __pshared) __THROW;
  734. /* Set the process-shared flag of the barrier attribute ATTR. */
  735. extern int pthread_barrierattr_setpshared (pthread_barrierattr_t *__attr,
  736. int __pshared) __THROW;
  737. #endif
  738. /* Functions for handling thread-specific data. */
  739. /* Create a key value identifying a location in the thread-specific
  740. data area. Each thread maintains a distinct thread-specific data
  741. area. DESTR_FUNCTION, if non-NULL, is called with the value
  742. associated to that key when the key is destroyed.
  743. DESTR_FUNCTION is not called if the value associated is NULL when
  744. the key is destroyed. */
  745. extern int pthread_key_create (pthread_key_t *__key,
  746. void (*__destr_function) (void *)) __THROW;
  747. /* Destroy KEY. */
  748. extern int pthread_key_delete (pthread_key_t __key) __THROW;
  749. /* Return current value of the thread-specific data slot identified by KEY. */
  750. extern void *pthread_getspecific (pthread_key_t __key) __THROW;
  751. /* Store POINTER in the thread-specific data slot identified by KEY. */
  752. extern int pthread_setspecific (pthread_key_t __key,
  753. __const void *__pointer) __THROW;
  754. #ifdef __USE_XOPEN2K
  755. /* Get ID of CPU-time clock for thread THREAD_ID. */
  756. extern int pthread_getcpuclockid (pthread_t __thread_id,
  757. __clockid_t *__clock_id) __THROW;
  758. #endif
  759. /* Install handlers to be called when a new process is created with FORK.
  760. The PREPARE handler is called in the parent process just before performing
  761. FORK. The PARENT handler is called in the parent process just after FORK.
  762. The CHILD handler is called in the child process. Each of the three
  763. handlers can be NULL, meaning that no handler needs to be called at that
  764. point.
  765. PTHREAD_ATFORK can be called several times, in which case the PREPARE
  766. handlers are called in LIFO order (last added with PTHREAD_ATFORK,
  767. first called before FORK), and the PARENT and CHILD handlers are called
  768. in FIFO (first added, first called). */
  769. extern int pthread_atfork (void (*__prepare) (void),
  770. void (*__parent) (void),
  771. void (*__child) (void)) __THROW;
  772. __END_DECLS
  773. #endif /* pthread.h */