pthread.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Written by Joel Sherrill <joel.sherrill@OARcorp.com>.
  3. *
  4. * COPYRIGHT (c) 1989-2013, 2015.
  5. * On-Line Applications Research Corporation (OAR).
  6. *
  7. * Permission to use, copy, modify, and distribute this software for any
  8. * purpose without fee is hereby granted, provided that this entire notice
  9. * is included in all copies of any software which is or includes a copy
  10. * or modification of this software.
  11. *
  12. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
  13. * WARRANTY. IN PARTICULAR, THE AUTHOR MAKES NO REPRESENTATION
  14. * OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS
  15. * SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  16. */
  17. #ifndef __PTHREAD_h
  18. #define __PTHREAD_h
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. #include <unistd.h>
  23. #if defined(_POSIX_THREADS)
  24. #include <sys/types.h>
  25. #include <time.h>
  26. #include <sched.h>
  27. #include <sys/cdefs.h>
  28. struct _pthread_cleanup_context {
  29. void (*_routine)(void *);
  30. void *_arg;
  31. int _canceltype;
  32. struct _pthread_cleanup_context *_previous;
  33. };
  34. /* Register Fork Handlers */
  35. int pthread_atfork (void (*prepare)(void), void (*parent)(void),
  36. void (*child)(void));
  37. /* Mutex Initialization Attributes, P1003.1c/Draft 10, p. 81 */
  38. int pthread_mutexattr_init (pthread_mutexattr_t *__attr);
  39. int pthread_mutexattr_destroy (pthread_mutexattr_t *__attr);
  40. int pthread_mutexattr_getpshared (const pthread_mutexattr_t *__attr,
  41. int *__pshared);
  42. int pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
  43. int __pshared);
  44. #if defined(_UNIX98_THREAD_MUTEX_ATTRIBUTES)
  45. /* Single UNIX Specification 2 Mutex Attributes types */
  46. int pthread_mutexattr_gettype (const pthread_mutexattr_t *__attr, int *__kind);
  47. int pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind);
  48. #endif
  49. /* Initializing and Destroying a Mutex, P1003.1c/Draft 10, p. 87 */
  50. int pthread_mutex_init (pthread_mutex_t *__mutex,
  51. const pthread_mutexattr_t *__attr);
  52. int pthread_mutex_destroy (pthread_mutex_t *__mutex);
  53. /* This is used to statically initialize a pthread_mutex_t. Example:
  54. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  55. */
  56. #define PTHREAD_MUTEX_INITIALIZER _PTHREAD_MUTEX_INITIALIZER
  57. /* Locking and Unlocking a Mutex, P1003.1c/Draft 10, p. 93
  58. NOTE: P1003.4b/D8 adds pthread_mutex_timedlock(), p. 29 */
  59. int pthread_mutex_lock (pthread_mutex_t *__mutex);
  60. int pthread_mutex_trylock (pthread_mutex_t *__mutex);
  61. int pthread_mutex_unlock (pthread_mutex_t *__mutex);
  62. #if defined(_POSIX_TIMEOUTS)
  63. int pthread_mutex_timedlock (pthread_mutex_t *__mutex,
  64. const struct timespec *__timeout);
  65. #endif /* _POSIX_TIMEOUTS */
  66. #if __GNU_VISIBLE
  67. /* The Issue 8 standard adds pthread_mutex_clocklock() */
  68. int pthread_mutex_clocklock(pthread_mutex_t *__restrict, clockid_t,
  69. const struct timespec *__restrict);
  70. #endif /* __GNU_VISIBLE */
  71. /* Condition Variable Initialization Attributes, P1003.1c/Draft 10, p. 96 */
  72. int pthread_condattr_init (pthread_condattr_t *__attr);
  73. int pthread_condattr_destroy (pthread_condattr_t *__attr);
  74. int pthread_condattr_getclock (const pthread_condattr_t *__restrict __attr,
  75. clockid_t *__restrict __clock_id);
  76. int pthread_condattr_setclock (pthread_condattr_t *__attr,
  77. clockid_t __clock_id);
  78. int pthread_condattr_getpshared (const pthread_condattr_t *__attr,
  79. int *__pshared);
  80. int pthread_condattr_setpshared (pthread_condattr_t *__attr, int __pshared);
  81. /* Initializing and Destroying a Condition Variable, P1003.1c/Draft 10, p. 87 */
  82. int pthread_cond_init (pthread_cond_t *__cond,
  83. const pthread_condattr_t *__attr);
  84. int pthread_cond_destroy (pthread_cond_t *__mutex);
  85. /* This is used to statically initialize a pthread_cond_t. Example:
  86. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  87. */
  88. #define PTHREAD_COND_INITIALIZER _PTHREAD_COND_INITIALIZER
  89. /* Broadcasting and Signaling a Condition, P1003.1c/Draft 10, p. 101 */
  90. int pthread_cond_signal (pthread_cond_t *__cond);
  91. int pthread_cond_broadcast (pthread_cond_t *__cond);
  92. /* Waiting on a Condition, P1003.1c/Draft 10, p. 105 */
  93. int pthread_cond_wait (pthread_cond_t *__cond, pthread_mutex_t *__mutex);
  94. int pthread_cond_timedwait (pthread_cond_t *__cond,
  95. pthread_mutex_t *__mutex,
  96. const struct timespec *__abstime);
  97. #if __GNU_VISIBLE
  98. /* The Issue 8 standard adds pthread_cond_clockwait() */
  99. int pthread_cond_clockwait(pthread_cond_t *__restrict,
  100. pthread_mutex_t *__restrict, clockid_t,
  101. const struct timespec *__restrict);
  102. #endif /* __GNU_VISIBLE */
  103. #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
  104. /* Thread Creation Scheduling Attributes, P1003.1c/Draft 10, p. 120 */
  105. int pthread_attr_setscope (pthread_attr_t *__attr, int __contentionscope);
  106. int pthread_attr_getscope (const pthread_attr_t *__attr,
  107. int *__contentionscope);
  108. int pthread_attr_setinheritsched (pthread_attr_t *__attr,
  109. int __inheritsched);
  110. int pthread_attr_getinheritsched (const pthread_attr_t *__attr,
  111. int *__inheritsched);
  112. int pthread_attr_setschedpolicy (pthread_attr_t *__attr, int __policy);
  113. int pthread_attr_getschedpolicy (const pthread_attr_t *__attr,
  114. int *__policy);
  115. #endif /* defined(_POSIX_THREAD_PRIORITY_SCHEDULING) */
  116. int pthread_attr_setschedparam (pthread_attr_t *__attr,
  117. const struct sched_param *__param);
  118. int pthread_attr_getschedparam (const pthread_attr_t *__attr,
  119. struct sched_param *__param);
  120. #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
  121. /* Dynamic Thread Scheduling Parameters Access, P1003.1c/Draft 10, p. 124 */
  122. int pthread_getschedparam (pthread_t __pthread, int *__policy,
  123. struct sched_param *__param);
  124. int pthread_setschedparam (pthread_t __pthread, int __policy,
  125. const struct sched_param *__param);
  126. /* Set Scheduling Priority of a Thread */
  127. int pthread_setschedprio (pthread_t thread, int prio);
  128. #endif /* defined(_POSIX_THREAD_PRIORITY_SCHEDULING) */
  129. #if __GNU_VISIBLE
  130. int pthread_getname_np(pthread_t, char *, size_t) __nonnull((2));
  131. int pthread_setname_np(pthread_t, const char *) __nonnull((2));
  132. #endif
  133. #if defined(_POSIX_THREAD_PRIO_INHERIT) || defined(_POSIX_THREAD_PRIO_PROTECT)
  134. /* Mutex Initialization Scheduling Attributes, P1003.1c/Draft 10, p. 128 */
  135. int pthread_mutexattr_setprotocol (pthread_mutexattr_t *__attr,
  136. int __protocol);
  137. int pthread_mutexattr_getprotocol (const pthread_mutexattr_t *__attr,
  138. int *__protocol);
  139. int pthread_mutexattr_setprioceiling (pthread_mutexattr_t *__attr,
  140. int __prioceiling);
  141. int pthread_mutexattr_getprioceiling (const pthread_mutexattr_t *__attr,
  142. int *__prioceiling);
  143. #endif /* _POSIX_THREAD_PRIO_INHERIT || _POSIX_THREAD_PRIO_PROTECT */
  144. #if defined(_POSIX_THREAD_PRIO_PROTECT)
  145. /* Change the Priority Ceiling of a Mutex, P1003.1c/Draft 10, p. 131 */
  146. int pthread_mutex_setprioceiling (pthread_mutex_t *__mutex,
  147. int __prioceiling, int *__old_ceiling);
  148. int pthread_mutex_getprioceiling (const pthread_mutex_t *__restrict __mutex,
  149. int *__prioceiling);
  150. #endif /* _POSIX_THREAD_PRIO_PROTECT */
  151. /* Thread Creation Attributes, P1003.1c/Draft 10, p, 140 */
  152. int pthread_attr_init (pthread_attr_t *__attr);
  153. int pthread_attr_destroy (pthread_attr_t *__attr);
  154. int pthread_attr_setstack (pthread_attr_t *attr,
  155. void *__stackaddr, size_t __stacksize);
  156. int pthread_attr_getstack (const pthread_attr_t *attr,
  157. void **__stackaddr, size_t *__stacksize);
  158. int pthread_attr_getstacksize (const pthread_attr_t *__attr,
  159. size_t *__stacksize);
  160. int pthread_attr_setstacksize (pthread_attr_t *__attr, size_t __stacksize);
  161. int pthread_attr_getstackaddr (const pthread_attr_t *__attr,
  162. void **__stackaddr);
  163. int pthread_attr_setstackaddr (pthread_attr_t *__attr, void *__stackaddr);
  164. int pthread_attr_getdetachstate (const pthread_attr_t *__attr,
  165. int *__detachstate);
  166. int pthread_attr_setdetachstate (pthread_attr_t *__attr, int __detachstate);
  167. int pthread_attr_getguardsize (const pthread_attr_t *__attr,
  168. size_t *__guardsize);
  169. int pthread_attr_setguardsize (pthread_attr_t *__attr, size_t __guardsize);
  170. /* POSIX thread APIs beyond the POSIX standard but provided
  171. * in GNU/Linux. They may be provided by other OSes for
  172. * compatibility.
  173. */
  174. #if __GNU_VISIBLE
  175. #if defined(__rtems__)
  176. int pthread_attr_setaffinity_np (pthread_attr_t *__attr,
  177. size_t __cpusetsize,
  178. const cpu_set_t *__cpuset);
  179. int pthread_attr_getaffinity_np (const pthread_attr_t *__attr,
  180. size_t __cpusetsize, cpu_set_t *__cpuset);
  181. int pthread_setaffinity_np (pthread_t __id, size_t __cpusetsize,
  182. const cpu_set_t *__cpuset);
  183. int pthread_getaffinity_np (const pthread_t __id, size_t __cpusetsize,
  184. cpu_set_t *__cpuset);
  185. int pthread_getattr_np (pthread_t __id, pthread_attr_t *__attr);
  186. #endif /* defined(__rtems__) */
  187. #endif /* __GNU_VISIBLE */
  188. /* Thread Creation, P1003.1c/Draft 10, p. 144 */
  189. int pthread_create (pthread_t *__pthread, const pthread_attr_t *__attr,
  190. void *(*__start_routine)(void *), void *__arg);
  191. /* Wait for Thread Termination, P1003.1c/Draft 10, p. 147 */
  192. int pthread_join (pthread_t __pthread, void **__value_ptr);
  193. /* Detaching a Thread, P1003.1c/Draft 10, p. 149 */
  194. int pthread_detach (pthread_t __pthread);
  195. /* Thread Termination, p1003.1c/Draft 10, p. 150 */
  196. void pthread_exit (void *__value_ptr) __dead2;
  197. /* Get Calling Thread's ID, p1003.1c/Draft 10, p. XXX */
  198. pthread_t pthread_self (void);
  199. /* Compare Thread IDs, p1003.1c/Draft 10, p. 153 */
  200. int pthread_equal (pthread_t __t1, pthread_t __t2);
  201. /* Retrieve ID of a Thread's CPU Time Clock */
  202. int pthread_getcpuclockid (pthread_t thread, clockid_t *clock_id);
  203. /* Get/Set Current Thread's Concurrency Level */
  204. int pthread_setconcurrency (int new_level);
  205. int pthread_getconcurrency (void);
  206. #if __BSD_VISIBLE || __GNU_VISIBLE
  207. void pthread_yield (void);
  208. #endif
  209. /* Dynamic Package Initialization */
  210. /* This is used to statically initialize a pthread_once_t. Example:
  211. pthread_once_t once = PTHREAD_ONCE_INIT;
  212. NOTE: This is named inconsistently -- it should be INITIALIZER. */
  213. #define PTHREAD_ONCE_INIT _PTHREAD_ONCE_INIT
  214. int pthread_once (pthread_once_t *__once_control,
  215. void (*__init_routine)(void));
  216. /* Thread-Specific Data Key Create, P1003.1c/Draft 10, p. 163 */
  217. int pthread_key_create (pthread_key_t *__key,
  218. void (*__destructor)(void *));
  219. /* Thread-Specific Data Management, P1003.1c/Draft 10, p. 165 */
  220. int pthread_setspecific (pthread_key_t __key, const void *__value);
  221. void * pthread_getspecific (pthread_key_t __key);
  222. /* Thread-Specific Data Key Deletion, P1003.1c/Draft 10, p. 167 */
  223. int pthread_key_delete (pthread_key_t __key);
  224. /* Execution of a Thread, P1003.1c/Draft 10, p. 181 */
  225. #define PTHREAD_CANCEL_ENABLE 0
  226. #define PTHREAD_CANCEL_DISABLE 1
  227. #define PTHREAD_CANCEL_DEFERRED 0
  228. #define PTHREAD_CANCEL_ASYNCHRONOUS 1
  229. #define PTHREAD_CANCELED ((void *) -1)
  230. int pthread_cancel (pthread_t __pthread);
  231. /* Setting Cancelability State, P1003.1c/Draft 10, p. 183 */
  232. int pthread_setcancelstate (int __state, int *__oldstate);
  233. int pthread_setcanceltype (int __type, int *__oldtype);
  234. void pthread_testcancel (void);
  235. /* Establishing Cancellation Handlers, P1003.1c/Draft 10, p. 184 */
  236. void _pthread_cleanup_push (struct _pthread_cleanup_context *_context,
  237. void (*_routine)(void *), void *_arg);
  238. void _pthread_cleanup_pop (struct _pthread_cleanup_context *_context,
  239. int _execute);
  240. /* It is intentional to open and close the scope in two different macros */
  241. #define pthread_cleanup_push(_routine, _arg) \
  242. do { \
  243. struct _pthread_cleanup_context _pthread_clup_ctx; \
  244. _pthread_cleanup_push(&_pthread_clup_ctx, (_routine), (_arg))
  245. #define pthread_cleanup_pop(_execute) \
  246. _pthread_cleanup_pop(&_pthread_clup_ctx, (_execute)); \
  247. } while (0)
  248. #if __GNU_VISIBLE
  249. void _pthread_cleanup_push_defer (struct _pthread_cleanup_context *_context,
  250. void (*_routine)(void *), void *_arg);
  251. void _pthread_cleanup_pop_restore (struct _pthread_cleanup_context *_context,
  252. int _execute);
  253. /* It is intentional to open and close the scope in two different macros */
  254. #define pthread_cleanup_push_defer_np(_routine, _arg) \
  255. do { \
  256. struct _pthread_cleanup_context _pthread_clup_ctx; \
  257. _pthread_cleanup_push_defer(&_pthread_clup_ctx, (_routine), (_arg))
  258. #define pthread_cleanup_pop_restore_np(_execute) \
  259. _pthread_cleanup_pop_restore(&_pthread_clup_ctx, (_execute)); \
  260. } while (0)
  261. #endif /* __GNU_VISIBLE */
  262. #if defined(_POSIX_THREAD_CPUTIME)
  263. /* Accessing a Thread CPU-time Clock, P1003.4b/D8, p. 58 */
  264. int pthread_getcpuclockid (pthread_t __pthread_id, clockid_t *__clock_id);
  265. #endif /* defined(_POSIX_THREAD_CPUTIME) */
  266. #endif /* defined(_POSIX_THREADS) */
  267. #if defined(_POSIX_BARRIERS)
  268. int pthread_barrierattr_init (pthread_barrierattr_t *__attr);
  269. int pthread_barrierattr_destroy (pthread_barrierattr_t *__attr);
  270. int pthread_barrierattr_getpshared (const pthread_barrierattr_t *__attr,
  271. int *__pshared);
  272. int pthread_barrierattr_setpshared (pthread_barrierattr_t *__attr,
  273. int __pshared);
  274. #define PTHREAD_BARRIER_SERIAL_THREAD -1
  275. int pthread_barrier_init (pthread_barrier_t *__barrier,
  276. const pthread_barrierattr_t *__attr,
  277. unsigned __count);
  278. int pthread_barrier_destroy (pthread_barrier_t *__barrier);
  279. int pthread_barrier_wait (pthread_barrier_t *__barrier);
  280. #endif /* defined(_POSIX_BARRIERS) */
  281. #if defined(_POSIX_SPIN_LOCKS)
  282. int pthread_spin_init (pthread_spinlock_t *__spinlock, int __pshared);
  283. int pthread_spin_destroy (pthread_spinlock_t *__spinlock);
  284. int pthread_spin_lock (pthread_spinlock_t *__spinlock);
  285. int pthread_spin_trylock (pthread_spinlock_t *__spinlock);
  286. int pthread_spin_unlock (pthread_spinlock_t *__spinlock);
  287. #endif /* defined(_POSIX_SPIN_LOCKS) */
  288. #if defined(_POSIX_READER_WRITER_LOCKS)
  289. /* This is used to statically initialize a pthread_rwlock_t. Example:
  290. pthread_mutex_t mutex = PTHREAD_RWLOCK_INITIALIZER;
  291. */
  292. #define PTHREAD_RWLOCK_INITIALIZER _PTHREAD_RWLOCK_INITIALIZER
  293. int pthread_rwlockattr_init (pthread_rwlockattr_t *__attr);
  294. int pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr);
  295. int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *__attr,
  296. int *__pshared);
  297. int pthread_rwlockattr_setpshared (pthread_rwlockattr_t *__attr,
  298. int __pshared);
  299. int pthread_rwlock_init (pthread_rwlock_t *__rwlock,
  300. const pthread_rwlockattr_t *__attr);
  301. int pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
  302. int pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
  303. int pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
  304. int pthread_rwlock_timedrdlock (pthread_rwlock_t *__rwlock,
  305. const struct timespec *__abstime);
  306. int pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
  307. int pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
  308. int pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
  309. int pthread_rwlock_timedwrlock (pthread_rwlock_t *__rwlock,
  310. const struct timespec *__abstime);
  311. #if __GNU_VISIBLE
  312. /* The Issue 8 standard adds pthread_rwlock_clockrdlock()
  313. * and pthread_rwlock_clockwrlock()*/
  314. int pthread_rwlock_clockrdlock(pthread_rwlock_t *__restrict, clockid_t,
  315. const struct timespec *__restrict);
  316. int pthread_rwlock_clockwrlock(pthread_rwlock_t *__restrict, clockid_t,
  317. const struct timespec *__restrict);
  318. #endif /* __GNU_VISIBLE */
  319. #endif /* defined(_POSIX_READER_WRITER_LOCKS) */
  320. #ifdef __cplusplus
  321. }
  322. #endif
  323. #endif
  324. /* end of include file */