pthread.h 14 KB

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