Subversion Repositories Kolibri OS

Rev

Rev 6099 | Blame | Compare with Previous | Last modification | View Log | RSS feed

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