Subversion Repositories Kolibri OS

Rev

Rev 4921 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  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.  
  21. #ifndef __PTHREAD_h
  22. #define __PTHREAD_h
  23.  
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27.  
  28. #include <unistd.h>
  29.  
  30. #if defined(_POSIX_THREADS)
  31.  
  32. #include <sys/types.h>
  33. #include <time.h>
  34. #include <sched.h>
  35. #include <sys/cdefs.h>
  36.  
  37. struct _pthread_cleanup_context {
  38.   void (*_routine)(void *);
  39.   void *_arg;
  40.   int _canceltype;
  41.   struct _pthread_cleanup_context *_previous;
  42. };
  43.  
  44. /* Register Fork Handlers */
  45. int     _EXFUN(pthread_atfork,(void (*prepare)(void), void (*parent)(void),
  46.                    void (*child)(void)));
  47.          
  48. /* Mutex Initialization Attributes, P1003.1c/Draft 10, p. 81 */
  49.  
  50. int     _EXFUN(pthread_mutexattr_init, (pthread_mutexattr_t *__attr));
  51. int     _EXFUN(pthread_mutexattr_destroy, (pthread_mutexattr_t *__attr));
  52. int     _EXFUN(pthread_mutexattr_getpshared,
  53.                 (_CONST pthread_mutexattr_t *__attr, int  *__pshared));
  54. int     _EXFUN(pthread_mutexattr_setpshared,
  55.                 (pthread_mutexattr_t *__attr, int __pshared));
  56.  
  57. #if defined(_UNIX98_THREAD_MUTEX_ATTRIBUTES)
  58.  
  59. /* Single UNIX Specification 2 Mutex Attributes types */
  60.  
  61. int _EXFUN(pthread_mutexattr_gettype,
  62.                 (_CONST pthread_mutexattr_t *__attr, int *__kind));
  63. int _EXFUN(pthread_mutexattr_settype,
  64.                 (pthread_mutexattr_t *__attr, int __kind));
  65.  
  66. #endif
  67.  
  68. /* Initializing and Destroying a Mutex, P1003.1c/Draft 10, p. 87 */
  69.  
  70. int     _EXFUN(pthread_mutex_init,
  71.         (pthread_mutex_t *__mutex, _CONST pthread_mutexattr_t *__attr));
  72. int     _EXFUN(pthread_mutex_destroy, (pthread_mutex_t *__mutex));
  73.  
  74. /* This is used to statically initialize a pthread_mutex_t. Example:
  75.  
  76.     pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  77.  */
  78.  
  79. #define PTHREAD_MUTEX_INITIALIZER  ((pthread_mutex_t) 0xFFFFFFFF)
  80.  
  81. /*  Locking and Unlocking a Mutex, P1003.1c/Draft 10, p. 93
  82.     NOTE: P1003.4b/D8 adds pthread_mutex_timedlock(), p. 29 */
  83.  
  84. int     _EXFUN(pthread_mutex_lock, (pthread_mutex_t *__mutex));
  85. int     _EXFUN(pthread_mutex_trylock, (pthread_mutex_t *__mutex));
  86. int     _EXFUN(pthread_mutex_unlock, (pthread_mutex_t *__mutex));
  87.  
  88. #if defined(_POSIX_TIMEOUTS)
  89.  
  90. int     _EXFUN(pthread_mutex_timedlock,
  91.         (pthread_mutex_t *__mutex, _CONST struct timespec *__timeout));
  92.  
  93. #endif /* _POSIX_TIMEOUTS */
  94.  
  95. /* Condition Variable Initialization Attributes, P1003.1c/Draft 10, p. 96 */
  96.  
  97. int     _EXFUN(pthread_condattr_init, (pthread_condattr_t *__attr));
  98. int     _EXFUN(pthread_condattr_destroy, (pthread_condattr_t *__attr));
  99. int     _EXFUN(pthread_condattr_getpshared,
  100.                 (_CONST pthread_condattr_t *__attr, int *__pshared));
  101. int     _EXFUN(pthread_condattr_setpshared,
  102.                 (pthread_condattr_t *__attr, int __pshared));
  103.  
  104. /* Initializing and Destroying a Condition Variable, P1003.1c/Draft 10, p. 87 */
  105.  
  106. int     _EXFUN(pthread_cond_init,
  107.         (pthread_cond_t *__cond, _CONST pthread_condattr_t *__attr));
  108. int     _EXFUN(pthread_cond_destroy, (pthread_cond_t *__mutex));
  109.  
  110. /* This is used to statically initialize a pthread_cond_t. Example:
  111.  
  112.     pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  113.  */
  114.  
  115. #define PTHREAD_COND_INITIALIZER  ((pthread_cond_t) 0xFFFFFFFF)
  116.  
  117. /* Broadcasting and Signaling a Condition, P1003.1c/Draft 10, p. 101 */
  118.  
  119. int     _EXFUN(pthread_cond_signal, (pthread_cond_t *__cond));
  120. int     _EXFUN(pthread_cond_broadcast, (pthread_cond_t *__cond));
  121.  
  122. /* Waiting on a Condition, P1003.1c/Draft 10, p. 105 */
  123.  
  124. int     _EXFUN(pthread_cond_wait,
  125.         (pthread_cond_t *__cond, pthread_mutex_t *__mutex));
  126.  
  127. int     _EXFUN(pthread_cond_timedwait,
  128.                 (pthread_cond_t *__cond, pthread_mutex_t *__mutex,
  129.                 _CONST struct timespec *__abstime));
  130.  
  131. #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
  132.  
  133. /* Thread Creation Scheduling Attributes, P1003.1c/Draft 10, p. 120 */
  134.  
  135. int     _EXFUN(pthread_attr_setscope,
  136.                 (pthread_attr_t *__attr, int __contentionscope));
  137. int     _EXFUN(pthread_attr_getscope,
  138.         (_CONST pthread_attr_t *__attr, int *__contentionscope));
  139. int     _EXFUN(pthread_attr_setinheritsched,
  140.         (pthread_attr_t *__attr, int __inheritsched));
  141. int     _EXFUN(pthread_attr_getinheritsched,
  142.         (_CONST pthread_attr_t *__attr, int *__inheritsched));
  143. int     _EXFUN(pthread_attr_setschedpolicy,
  144.         (pthread_attr_t *__attr, int __policy));
  145. int     _EXFUN(pthread_attr_getschedpolicy,
  146.         (_CONST pthread_attr_t *__attr, int *__policy));
  147.  
  148. #endif /* defined(_POSIX_THREAD_PRIORITY_SCHEDULING) */
  149.  
  150. int     _EXFUN(pthread_attr_setschedparam,
  151.         (pthread_attr_t *__attr, _CONST struct sched_param *__param));
  152. int     _EXFUN(pthread_attr_getschedparam,
  153.         (_CONST pthread_attr_t *__attr, struct sched_param *__param));
  154.  
  155. #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
  156.  
  157. /* Dynamic Thread Scheduling Parameters Access, P1003.1c/Draft 10, p. 124 */
  158.  
  159. int     _EXFUN(pthread_getschedparam,
  160.         (pthread_t __pthread, int *__policy, struct sched_param *__param));
  161. int     _EXFUN(pthread_setschedparam,
  162.         (pthread_t __pthread, int __policy, struct sched_param *__param));
  163.  
  164. #endif /* defined(_POSIX_THREAD_PRIORITY_SCHEDULING) */
  165.  
  166. #if defined(_POSIX_THREAD_PRIO_INHERIT) || defined(_POSIX_THREAD_PRIO_PROTECT)
  167.  
  168. /* Mutex Initialization Scheduling Attributes, P1003.1c/Draft 10, p. 128 */
  169.  
  170. int     _EXFUN(pthread_mutexattr_setprotocol,
  171.         (pthread_mutexattr_t *__attr, int __protocol));
  172. int     _EXFUN(pthread_mutexattr_getprotocol,
  173.         (_CONST pthread_mutexattr_t *__attr, int *__protocol));
  174. int     _EXFUN(pthread_mutexattr_setprioceiling,
  175.         (pthread_mutexattr_t *__attr, int __prioceiling));
  176. int     _EXFUN(pthread_mutexattr_getprioceiling,
  177.         (_CONST pthread_mutexattr_t *__attr, int *__prioceiling));
  178.  
  179. #endif /* _POSIX_THREAD_PRIO_INHERIT || _POSIX_THREAD_PRIO_PROTECT */
  180.  
  181. #if defined(_POSIX_THREAD_PRIO_PROTECT)
  182.  
  183. /* Change the Priority Ceiling of a Mutex, P1003.1c/Draft 10, p. 131 */
  184.  
  185. int     _EXFUN(pthread_mutex_setprioceiling,
  186.         (pthread_mutex_t *__mutex, int __prioceiling, int *__old_ceiling));
  187. int     _EXFUN(pthread_mutex_getprioceiling,
  188.         (pthread_mutex_t *__mutex, int *__prioceiling));
  189.  
  190. #endif /* _POSIX_THREAD_PRIO_PROTECT */
  191.  
  192. /* Thread Creation Attributes, P1003.1c/Draft 10, p, 140 */
  193.  
  194. int     _EXFUN(pthread_attr_init, (pthread_attr_t *__attr));
  195. int     _EXFUN(pthread_attr_destroy, (pthread_attr_t *__attr));
  196. int     _EXFUN(pthread_attr_setstack, (pthread_attr_t *attr,
  197.         void *__stackaddr, size_t __stacksize));
  198. int     _EXFUN(pthread_attr_getstack, (_CONST pthread_attr_t *attr,
  199.         void **__stackaddr, size_t *__stacksize));
  200. int     _EXFUN(pthread_attr_getstacksize,
  201.         (_CONST pthread_attr_t *__attr, size_t *__stacksize));
  202. int     _EXFUN(pthread_attr_setstacksize,
  203.         (pthread_attr_t *__attr, size_t __stacksize));
  204. int     _EXFUN(pthread_attr_getstackaddr,
  205.         (_CONST pthread_attr_t *__attr, void **__stackaddr));
  206. int     _EXFUN(pthread_attr_setstackaddr,
  207.         (pthread_attr_t  *__attr, void *__stackaddr));
  208. int     _EXFUN(pthread_attr_getdetachstate,
  209.         (_CONST pthread_attr_t *__attr, int *__detachstate));
  210. int     _EXFUN(pthread_attr_setdetachstate,
  211.         (pthread_attr_t *__attr, int __detachstate));
  212. int     _EXFUN(pthread_attr_getguardsize,
  213.         (_CONST pthread_attr_t *__attr, size_t *__guardsize));
  214. int     _EXFUN(pthread_attr_setguardsize,
  215.         (pthread_attr_t *__attr, size_t __guardsize));
  216.  
  217. /* POSIX thread APIs beyond the POSIX standard but provided
  218.  * in GNU/Linux. They may be provided by other OSes for
  219.  * compatibility.
  220.  */
  221. #if defined(__GNU_VISIBLE)
  222. #if defined(__rtems__)
  223. int     _EXFUN(pthread_attr_setaffinity_np,
  224.         (pthread_attr_t *__attr, size_t __cpusetsize,
  225.         const cpu_set_t *__cpuset));
  226. int     _EXFUN(pthread_attr_getaffinity_np,
  227.         (const pthread_attr_t *__attr, size_t __cpusetsize,
  228.         cpu_set_t *__cpuset));
  229.  
  230. int     _EXFUN(pthread_setaffinity_np,
  231.         (pthread_t __id, size_t __cpusetsize, const cpu_set_t *__cpuset));
  232. int     _EXFUN(pthread_getaffinity_np,
  233.         (const pthread_t __id, size_t __cpusetsize, cpu_set_t *__cpuset));
  234.  
  235. int     _EXFUN(pthread_getattr_np,
  236.         (pthread_t __id, pthread_attr_t *__attr));
  237. #endif /* defined(__rtems__) */
  238. #endif /* defined(__GNU_VISIBLE) */
  239.  
  240. /* Thread Creation, P1003.1c/Draft 10, p. 144 */
  241.  
  242. int     _EXFUN(pthread_create,
  243.         (pthread_t *__pthread, _CONST pthread_attr_t  *__attr,
  244.         void *(*__start_routine)( void * ), void *__arg));
  245.  
  246. /* Wait for Thread Termination, P1003.1c/Draft 10, p. 147 */
  247.  
  248. int     _EXFUN(pthread_join, (pthread_t __pthread, void **__value_ptr));
  249.  
  250. /* Detaching a Thread, P1003.1c/Draft 10, p. 149 */
  251.  
  252. int     _EXFUN(pthread_detach, (pthread_t __pthread));
  253.  
  254. /* Thread Termination, p1003.1c/Draft 10, p. 150 */
  255.  
  256. void    _EXFUN(pthread_exit, (void *__value_ptr));
  257.  
  258. /* Get Calling Thread's ID, p1003.1c/Draft 10, p. XXX */
  259.  
  260. pthread_t       _EXFUN(pthread_self, (void));
  261.  
  262. /* Compare Thread IDs, p1003.1c/Draft 10, p. 153 */
  263.  
  264. int     _EXFUN(pthread_equal, (pthread_t __t1, pthread_t __t2));
  265.  
  266. /* Dynamic Package Initialization */
  267.  
  268. /* This is used to statically initialize a pthread_once_t. Example:
  269.  
  270.     pthread_once_t once = PTHREAD_ONCE_INIT;
  271.  
  272.     NOTE:  This is named inconsistently -- it should be INITIALIZER.  */
  273.  
  274. #define PTHREAD_ONCE_INIT  { 1, 0 }  /* is initialized and not run */
  275.  
  276. int     _EXFUN(pthread_once,
  277.         (pthread_once_t *__once_control, void (*__init_routine)(void)));
  278.  
  279. /* Thread-Specific Data Key Create, P1003.1c/Draft 10, p. 163 */
  280.  
  281. int     _EXFUN(pthread_key_create,
  282.         (pthread_key_t *__key, void (*__destructor)( void * )));
  283.  
  284. /* Thread-Specific Data Management, P1003.1c/Draft 10, p. 165 */
  285.  
  286. int     _EXFUN(pthread_setspecific,
  287.         (pthread_key_t __key, _CONST void *__value));
  288. void *  _EXFUN(pthread_getspecific, (pthread_key_t __key));
  289.  
  290. /* Thread-Specific Data Key Deletion, P1003.1c/Draft 10, p. 167 */
  291.  
  292. int     _EXFUN(pthread_key_delete, (pthread_key_t __key));
  293.  
  294. /* Execution of a Thread, P1003.1c/Draft 10, p. 181 */
  295.  
  296. #define PTHREAD_CANCEL_ENABLE  0
  297. #define PTHREAD_CANCEL_DISABLE 1
  298.  
  299. #define PTHREAD_CANCEL_DEFERRED 0
  300. #define PTHREAD_CANCEL_ASYNCHRONOUS 1
  301.  
  302. #define PTHREAD_CANCELED ((void *) -1)
  303.  
  304. int     _EXFUN(pthread_cancel, (pthread_t __pthread));
  305.  
  306. /* Setting Cancelability State, P1003.1c/Draft 10, p. 183 */
  307.  
  308. int     _EXFUN(pthread_setcancelstate, (int __state, int *__oldstate));
  309. int     _EXFUN(pthread_setcanceltype, (int __type, int *__oldtype));
  310. void    _EXFUN(pthread_testcancel, (void));
  311.  
  312. /* Establishing Cancellation Handlers, P1003.1c/Draft 10, p. 184 */
  313.  
  314. void    _EXFUN(_pthread_cleanup_push,
  315.         (struct _pthread_cleanup_context *_context,
  316.         void (*_routine)(void *), void *_arg));
  317.  
  318. void    _EXFUN(_pthread_cleanup_pop,
  319.         (struct _pthread_cleanup_context *_context,
  320.         int _execute));
  321.  
  322. /* It is intentional to open and close the scope in two different macros */
  323. #define pthread_cleanup_push(_routine, _arg) \
  324.   do { \
  325.     struct _pthread_cleanup_context _pthread_clup_ctx; \
  326.     _pthread_cleanup_push(&_pthread_clup_ctx, (_routine), (_arg))
  327.  
  328. #define pthread_cleanup_pop(_execute) \
  329.     _pthread_cleanup_pop(&_pthread_clup_ctx, (_execute)); \
  330.   } while (0)
  331.  
  332. #if defined(_GNU_SOURCE)
  333. void    _EXFUN(_pthread_cleanup_push_defer,
  334.         (struct _pthread_cleanup_context *_context,
  335.         void (*_routine)(void *), void *_arg));
  336.  
  337. void    _EXFUN(_pthread_cleanup_pop_restore,
  338.         (struct _pthread_cleanup_context *_context,
  339.         int _execute));
  340.  
  341. /* It is intentional to open and close the scope in two different macros */
  342. #define pthread_cleanup_push_defer_np(_routine, _arg) \
  343.   do { \
  344.     struct _pthread_cleanup_context _pthread_clup_ctx; \
  345.     _pthread_cleanup_push_defer(&_pthread_clup_ctx, (_routine), (_arg))
  346.  
  347. #define pthread_cleanup_pop_restore_np(_execute) \
  348.     _pthread_cleanup_pop_restore(&_pthread_clup_ctx, (_execute)); \
  349.   } while (0)
  350. #endif /* defined(_GNU_SOURCE) */
  351.  
  352. #if defined(_POSIX_THREAD_CPUTIME)
  353.  
  354. /* Accessing a Thread CPU-time Clock, P1003.4b/D8, p. 58 */
  355.  
  356. int     _EXFUN(pthread_getcpuclockid,
  357.         (pthread_t __pthread_id, clockid_t *__clock_id));
  358.  
  359. #endif /* defined(_POSIX_THREAD_CPUTIME) */
  360.  
  361.  
  362. #endif /* defined(_POSIX_THREADS) */
  363.  
  364. #if defined(_POSIX_BARRIERS)
  365.  
  366. int     _EXFUN(pthread_barrierattr_init, (pthread_barrierattr_t *__attr));
  367. int     _EXFUN(pthread_barrierattr_destroy, (pthread_barrierattr_t *__attr));
  368. int     _EXFUN(pthread_barrierattr_getpshared,
  369.         (_CONST pthread_barrierattr_t *__attr, int *__pshared));
  370. int     _EXFUN(pthread_barrierattr_setpshared,
  371.         (pthread_barrierattr_t *__attr, int __pshared));
  372.  
  373. #define PTHREAD_BARRIER_SERIAL_THREAD -1
  374.  
  375. int     _EXFUN(pthread_barrier_init,
  376.         (pthread_barrier_t *__barrier,
  377.         _CONST pthread_barrierattr_t *__attr, unsigned __count));
  378. int     _EXFUN(pthread_barrier_destroy, (pthread_barrier_t *__barrier));
  379. int     _EXFUN(pthread_barrier_wait,(pthread_barrier_t *__barrier));
  380.  
  381. #endif /* defined(_POSIX_BARRIERS) */
  382.  
  383. #if defined(_POSIX_SPIN_LOCKS)
  384.  
  385. int     _EXFUN(pthread_spin_init,
  386.         (pthread_spinlock_t *__spinlock, int __pshared));
  387. int     _EXFUN(pthread_spin_destroy, (pthread_spinlock_t *__spinlock));
  388. int     _EXFUN(pthread_spin_lock, (pthread_spinlock_t *__spinlock));
  389. int     _EXFUN(pthread_spin_trylock, (pthread_spinlock_t *__spinlock));
  390. int     _EXFUN(pthread_spin_unlock, (pthread_spinlock_t *__spinlock));
  391.  
  392. #endif /* defined(_POSIX_SPIN_LOCKS) */
  393.  
  394. #if defined(_POSIX_READER_WRITER_LOCKS)
  395.  
  396. /* This is used to statically initialize a pthread_rwlock_t. Example:
  397.  
  398.     pthread_mutex_t mutex = PTHREAD_RWLOCK_INITIALIZER;
  399.  */
  400.  
  401. #define PTHREAD_RWLOCK_INITIALIZER  ((pthread_rwlock_t) 0xFFFFFFFF)
  402.  
  403. int     _EXFUN(pthread_rwlockattr_init, (pthread_rwlockattr_t *__attr));
  404. int     _EXFUN(pthread_rwlockattr_destroy, (pthread_rwlockattr_t *__attr));
  405. int     _EXFUN(pthread_rwlockattr_getpshared,
  406.         (_CONST pthread_rwlockattr_t *__attr, int *__pshared));
  407. int     _EXFUN(pthread_rwlockattr_setpshared,
  408.         (pthread_rwlockattr_t *__attr, int __pshared));
  409.  
  410. int     _EXFUN(pthread_rwlock_init,
  411.         (pthread_rwlock_t *__rwlock, _CONST pthread_rwlockattr_t *__attr));
  412. int     _EXFUN(pthread_rwlock_destroy, (pthread_rwlock_t *__rwlock));
  413. int     _EXFUN(pthread_rwlock_rdlock,(pthread_rwlock_t *__rwlock));
  414. int     _EXFUN(pthread_rwlock_tryrdlock,(pthread_rwlock_t *__rwlock));
  415. int     _EXFUN(pthread_rwlock_timedrdlock,
  416.         (pthread_rwlock_t *__rwlock, _CONST struct timespec *__abstime));
  417. int     _EXFUN(pthread_rwlock_unlock,(pthread_rwlock_t *__rwlock));
  418. int     _EXFUN(pthread_rwlock_wrlock,(pthread_rwlock_t *__rwlock));
  419. int     _EXFUN(pthread_rwlock_trywrlock,(pthread_rwlock_t *__rwlock));
  420. int     _EXFUN(pthread_rwlock_timedwrlock,
  421.         (pthread_rwlock_t *__rwlock, _CONST struct timespec *__abstime));
  422.  
  423. #endif /* defined(_POSIX_READER_WRITER_LOCKS) */
  424.  
  425.  
  426. #ifdef __cplusplus
  427. }
  428. #endif
  429.  
  430. #endif
  431. /* end of include file */
  432.