pthread.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. /* Condition Variable Initialization Attributes, P1003.1c/Draft 10, p. 96 */
  67. int pthread_condattr_init (pthread_condattr_t *__attr);
  68. int pthread_condattr_destroy (pthread_condattr_t *__attr);
  69. int pthread_condattr_getclock (const pthread_condattr_t *__restrict __attr,
  70. clockid_t *__restrict __clock_id);
  71. int pthread_condattr_setclock (pthread_condattr_t *__attr,
  72. clockid_t __clock_id);
  73. int pthread_condattr_getpshared (const pthread_condattr_t *__attr,
  74. int *__pshared);
  75. int pthread_condattr_setpshared (pthread_condattr_t *__attr, int __pshared);
  76. /* Initializing and Destroying a Condition Variable, P1003.1c/Draft 10, p. 87 */
  77. int pthread_cond_init (pthread_cond_t *__cond,
  78. const pthread_condattr_t *__attr);
  79. int pthread_cond_destroy (pthread_cond_t *__mutex);
  80. /* This is used to statically initialize a pthread_cond_t. Example:
  81. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  82. */
  83. #define PTHREAD_COND_INITIALIZER _PTHREAD_COND_INITIALIZER
  84. /* Broadcasting and Signaling a Condition, P1003.1c/Draft 10, p. 101 */
  85. int pthread_cond_signal (pthread_cond_t *__cond);
  86. int pthread_cond_broadcast (pthread_cond_t *__cond);
  87. /* Waiting on a Condition, P1003.1c/Draft 10, p. 105 */
  88. int pthread_cond_wait (pthread_cond_t *__cond, pthread_mutex_t *__mutex);
  89. int pthread_cond_timedwait (pthread_cond_t *__cond,
  90. pthread_mutex_t *__mutex,
  91. const struct timespec *__abstime);
  92. #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
  93. /* Thread Creation Scheduling Attributes, P1003.1c/Draft 10, p. 120 */
  94. int pthread_attr_setscope (pthread_attr_t *__attr, int __contentionscope);
  95. int pthread_attr_getscope (const pthread_attr_t *__attr,
  96. int *__contentionscope);
  97. int pthread_attr_setinheritsched (pthread_attr_t *__attr,
  98. int __inheritsched);
  99. int pthread_attr_getinheritsched (const pthread_attr_t *__attr,
  100. int *__inheritsched);
  101. int pthread_attr_setschedpolicy (pthread_attr_t *__attr, int __policy);
  102. int pthread_attr_getschedpolicy (const pthread_attr_t *__attr,
  103. int *__policy);
  104. #endif /* defined(_POSIX_THREAD_PRIORITY_SCHEDULING) */
  105. int pthread_attr_setschedparam (pthread_attr_t *__attr,
  106. const struct sched_param *__param);
  107. int pthread_attr_getschedparam (const pthread_attr_t *__attr,
  108. struct sched_param *__param);
  109. #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
  110. /* Dynamic Thread Scheduling Parameters Access, P1003.1c/Draft 10, p. 124 */
  111. int pthread_getschedparam (pthread_t __pthread, int *__policy,
  112. struct sched_param *__param);
  113. int pthread_setschedparam (pthread_t __pthread, int __policy,
  114. const struct sched_param *__param);
  115. /* Set Scheduling Priority of a Thread */
  116. int pthread_setschedprio (pthread_t thread, int prio);
  117. #endif /* defined(_POSIX_THREAD_PRIORITY_SCHEDULING) */
  118. #if __GNU_VISIBLE
  119. int pthread_getname_np(pthread_t, char *, size_t) __nonnull((2));
  120. int pthread_setname_np(pthread_t, const char *) __nonnull((2));
  121. #endif
  122. #if defined(_POSIX_THREAD_PRIO_INHERIT) || defined(_POSIX_THREAD_PRIO_PROTECT)
  123. /* Mutex Initialization Scheduling Attributes, P1003.1c/Draft 10, p. 128 */
  124. int pthread_mutexattr_setprotocol (pthread_mutexattr_t *__attr,
  125. int __protocol);
  126. int pthread_mutexattr_getprotocol (const pthread_mutexattr_t *__attr,
  127. int *__protocol);
  128. int pthread_mutexattr_setprioceiling (pthread_mutexattr_t *__attr,
  129. int __prioceiling);
  130. int pthread_mutexattr_getprioceiling (const pthread_mutexattr_t *__attr,
  131. int *__prioceiling);
  132. #endif /* _POSIX_THREAD_PRIO_INHERIT || _POSIX_THREAD_PRIO_PROTECT */
  133. #if defined(_POSIX_THREAD_PRIO_PROTECT)
  134. /* Change the Priority Ceiling of a Mutex, P1003.1c/Draft 10, p. 131 */
  135. int pthread_mutex_setprioceiling (pthread_mutex_t *__mutex,
  136. int __prioceiling, int *__old_ceiling);
  137. int pthread_mutex_getprioceiling (const pthread_mutex_t *__restrict __mutex,
  138. int *__prioceiling);
  139. #endif /* _POSIX_THREAD_PRIO_PROTECT */
  140. /* Thread Creation Attributes, P1003.1c/Draft 10, p, 140 */
  141. int pthread_attr_init (pthread_attr_t *__attr);
  142. int pthread_attr_destroy (pthread_attr_t *__attr);
  143. int pthread_attr_setstack (pthread_attr_t *attr,
  144. void *__stackaddr, size_t __stacksize);
  145. int pthread_attr_getstack (const pthread_attr_t *attr,
  146. void **__stackaddr, size_t *__stacksize);
  147. int pthread_attr_getstacksize (const pthread_attr_t *__attr,
  148. size_t *__stacksize);
  149. int pthread_attr_setstacksize (pthread_attr_t *__attr, size_t __stacksize);
  150. int pthread_attr_getstackaddr (const pthread_attr_t *__attr,
  151. void **__stackaddr);
  152. int pthread_attr_setstackaddr (pthread_attr_t *__attr, void *__stackaddr);
  153. int pthread_attr_getdetachstate (const pthread_attr_t *__attr,
  154. int *__detachstate);
  155. int pthread_attr_setdetachstate (pthread_attr_t *__attr, int __detachstate);
  156. int pthread_attr_getguardsize (const pthread_attr_t *__attr,
  157. size_t *__guardsize);
  158. int pthread_attr_setguardsize (pthread_attr_t *__attr, size_t __guardsize);
  159. /* POSIX thread APIs beyond the POSIX standard but provided
  160. * in GNU/Linux. They may be provided by other OSes for
  161. * compatibility.
  162. */
  163. #if __GNU_VISIBLE
  164. #if defined(__rtems__)
  165. int pthread_attr_setaffinity_np (pthread_attr_t *__attr,
  166. size_t __cpusetsize,
  167. const cpu_set_t *__cpuset);
  168. int pthread_attr_getaffinity_np (const pthread_attr_t *__attr,
  169. size_t __cpusetsize, cpu_set_t *__cpuset);
  170. int pthread_setaffinity_np (pthread_t __id, size_t __cpusetsize,
  171. const cpu_set_t *__cpuset);
  172. int pthread_getaffinity_np (const pthread_t __id, size_t __cpusetsize,
  173. cpu_set_t *__cpuset);
  174. int pthread_getattr_np (pthread_t __id, pthread_attr_t *__attr);
  175. #endif /* defined(__rtems__) */
  176. #endif /* __GNU_VISIBLE */
  177. /* Thread Creation, P1003.1c/Draft 10, p. 144 */
  178. int pthread_create (pthread_t *__pthread, const pthread_attr_t *__attr,
  179. void *(*__start_routine)(void *), void *__arg);
  180. /* Wait for Thread Termination, P1003.1c/Draft 10, p. 147 */
  181. int pthread_join (pthread_t __pthread, void **__value_ptr);
  182. /* Detaching a Thread, P1003.1c/Draft 10, p. 149 */
  183. int pthread_detach (pthread_t __pthread);
  184. /* Thread Termination, p1003.1c/Draft 10, p. 150 */
  185. void pthread_exit (void *__value_ptr) __dead2;
  186. /* Get Calling Thread's ID, p1003.1c/Draft 10, p. XXX */
  187. pthread_t pthread_self (void);
  188. /* Compare Thread IDs, p1003.1c/Draft 10, p. 153 */
  189. int pthread_equal (pthread_t __t1, pthread_t __t2);
  190. /* Retrieve ID of a Thread's CPU Time Clock */
  191. int pthread_getcpuclockid (pthread_t thread, clockid_t *clock_id);
  192. /* Get/Set Current Thread's Concurrency Level */
  193. int pthread_setconcurrency (int new_level);
  194. int pthread_getconcurrency (void);
  195. #if __BSD_VISIBLE || __GNU_VISIBLE
  196. void pthread_yield (void);
  197. #endif
  198. /* Dynamic Package Initialization */
  199. /* This is used to statically initialize a pthread_once_t. Example:
  200. pthread_once_t once = PTHREAD_ONCE_INIT;
  201. NOTE: This is named inconsistently -- it should be INITIALIZER. */
  202. #define PTHREAD_ONCE_INIT _PTHREAD_ONCE_INIT
  203. int pthread_once (pthread_once_t *__once_control,
  204. void (*__init_routine)(void));
  205. /* Thread-Specific Data Key Create, P1003.1c/Draft 10, p. 163 */
  206. int pthread_key_create (pthread_key_t *__key,
  207. void (*__destructor)(void *));
  208. /* Thread-Specific Data Management, P1003.1c/Draft 10, p. 165 */
  209. int pthread_setspecific (pthread_key_t __key, const void *__value);
  210. void * pthread_getspecific (pthread_key_t __key);
  211. /* Thread-Specific Data Key Deletion, P1003.1c/Draft 10, p. 167 */
  212. int pthread_key_delete (pthread_key_t __key);
  213. /* Execution of a Thread, P1003.1c/Draft 10, p. 181 */
  214. #define PTHREAD_CANCEL_ENABLE 0
  215. #define PTHREAD_CANCEL_DISABLE 1
  216. #define PTHREAD_CANCEL_DEFERRED 0
  217. #define PTHREAD_CANCEL_ASYNCHRONOUS 1
  218. #define PTHREAD_CANCELED ((void *) -1)
  219. int pthread_cancel (pthread_t __pthread);
  220. /* Setting Cancelability State, P1003.1c/Draft 10, p. 183 */
  221. int pthread_setcancelstate (int __state, int *__oldstate);
  222. int pthread_setcanceltype (int __type, int *__oldtype);
  223. void pthread_testcancel (void);
  224. /* Establishing Cancellation Handlers, P1003.1c/Draft 10, p. 184 */
  225. void _pthread_cleanup_push (struct _pthread_cleanup_context *_context,
  226. void (*_routine)(void *), void *_arg);
  227. void _pthread_cleanup_pop (struct _pthread_cleanup_context *_context,
  228. int _execute);
  229. /* It is intentional to open and close the scope in two different macros */
  230. #define pthread_cleanup_push(_routine, _arg) \
  231. do { \
  232. struct _pthread_cleanup_context _pthread_clup_ctx; \
  233. _pthread_cleanup_push(&_pthread_clup_ctx, (_routine), (_arg))
  234. #define pthread_cleanup_pop(_execute) \
  235. _pthread_cleanup_pop(&_pthread_clup_ctx, (_execute)); \
  236. } while (0)
  237. #if __GNU_VISIBLE
  238. void _pthread_cleanup_push_defer (struct _pthread_cleanup_context *_context,
  239. void (*_routine)(void *), void *_arg);
  240. void _pthread_cleanup_pop_restore (struct _pthread_cleanup_context *_context,
  241. int _execute);
  242. /* It is intentional to open and close the scope in two different macros */
  243. #define pthread_cleanup_push_defer_np(_routine, _arg) \
  244. do { \
  245. struct _pthread_cleanup_context _pthread_clup_ctx; \
  246. _pthread_cleanup_push_defer(&_pthread_clup_ctx, (_routine), (_arg))
  247. #define pthread_cleanup_pop_restore_np(_execute) \
  248. _pthread_cleanup_pop_restore(&_pthread_clup_ctx, (_execute)); \
  249. } while (0)
  250. #endif /* __GNU_VISIBLE */
  251. #if defined(_POSIX_THREAD_CPUTIME)
  252. /* Accessing a Thread CPU-time Clock, P1003.4b/D8, p. 58 */
  253. int pthread_getcpuclockid (pthread_t __pthread_id, clockid_t *__clock_id);
  254. #endif /* defined(_POSIX_THREAD_CPUTIME) */
  255. #endif /* defined(_POSIX_THREADS) */
  256. #if defined(_POSIX_BARRIERS)
  257. int pthread_barrierattr_init (pthread_barrierattr_t *__attr);
  258. int pthread_barrierattr_destroy (pthread_barrierattr_t *__attr);
  259. int pthread_barrierattr_getpshared (const pthread_barrierattr_t *__attr,
  260. int *__pshared);
  261. int pthread_barrierattr_setpshared (pthread_barrierattr_t *__attr,
  262. int __pshared);
  263. #define PTHREAD_BARRIER_SERIAL_THREAD -1
  264. int pthread_barrier_init (pthread_barrier_t *__barrier,
  265. const pthread_barrierattr_t *__attr,
  266. unsigned __count);
  267. int pthread_barrier_destroy (pthread_barrier_t *__barrier);
  268. int pthread_barrier_wait (pthread_barrier_t *__barrier);
  269. #endif /* defined(_POSIX_BARRIERS) */
  270. #if defined(_POSIX_SPIN_LOCKS)
  271. int pthread_spin_init (pthread_spinlock_t *__spinlock, int __pshared);
  272. int pthread_spin_destroy (pthread_spinlock_t *__spinlock);
  273. int pthread_spin_lock (pthread_spinlock_t *__spinlock);
  274. int pthread_spin_trylock (pthread_spinlock_t *__spinlock);
  275. int pthread_spin_unlock (pthread_spinlock_t *__spinlock);
  276. #endif /* defined(_POSIX_SPIN_LOCKS) */
  277. #if defined(_POSIX_READER_WRITER_LOCKS)
  278. /* This is used to statically initialize a pthread_rwlock_t. Example:
  279. pthread_mutex_t mutex = PTHREAD_RWLOCK_INITIALIZER;
  280. */
  281. #define PTHREAD_RWLOCK_INITIALIZER _PTHREAD_RWLOCK_INITIALIZER
  282. int pthread_rwlockattr_init (pthread_rwlockattr_t *__attr);
  283. int pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr);
  284. int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *__attr,
  285. int *__pshared);
  286. int pthread_rwlockattr_setpshared (pthread_rwlockattr_t *__attr,
  287. int __pshared);
  288. int pthread_rwlock_init (pthread_rwlock_t *__rwlock,
  289. const pthread_rwlockattr_t *__attr);
  290. int pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
  291. int pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
  292. int pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
  293. int pthread_rwlock_timedrdlock (pthread_rwlock_t *__rwlock,
  294. const struct timespec *__abstime);
  295. int pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
  296. int pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
  297. int pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
  298. int pthread_rwlock_timedwrlock (pthread_rwlock_t *__rwlock,
  299. const struct timespec *__abstime);
  300. #endif /* defined(_POSIX_READER_WRITER_LOCKS) */
  301. #ifdef __cplusplus
  302. }
  303. #endif
  304. #endif
  305. /* end of include file */