pthread.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * File : pthread.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-10-26 Bernard the first version
  13. */
  14. #ifndef __PTHREAD_H__
  15. #define __PTHREAD_H__
  16. #include <rtthread.h>
  17. #include <errno.h>
  18. #include <sys/types.h>
  19. #define PTHREAD_KEY_MAX 8
  20. #define PTHREAD_COND_INITIALIZER {-1, 0}
  21. #define PTHREAD_RWLOCK_INITIALIZER {-1, 0}
  22. #define PTHREAD_MUTEX_INITIALIZER {-1, 0}
  23. #define PTHREAD_CREATE_JOINABLE 0x00
  24. #define PTHREAD_CREATE_DETACHED 0x01
  25. #define PTHREAD_EXPLICIT_SCHED 0
  26. #define PTHREAD_INHERIT_SCHED 1
  27. typedef rt_thread_t pthread_t;
  28. typedef long pthread_condattr_t;
  29. typedef long pthread_rwlockattr_t;
  30. typedef long pthread_mutexattr_t;
  31. typedef long pthread_barrierattr_t;
  32. typedef int pthread_key_t;
  33. typedef int pthread_once_t;
  34. enum {
  35. PTHREAD_CANCEL_ASYNCHRONOUS = 0,
  36. PTHREAD_CANCEL_ENABLE,
  37. PTHREAD_CANCEL_DEFERRED,
  38. PTHREAD_CANCEL_DISABLE,
  39. PTHREAD_CANCELED
  40. };
  41. enum {
  42. PTHREAD_MUTEX_NORMAL = 0,
  43. PTHREAD_MUTEX_RECURSIVE = 1,
  44. PTHREAD_MUTEX_ERRORCHECK = 2,
  45. PTHREAD_MUTEX_ERRORCHECK_NP = PTHREAD_MUTEX_ERRORCHECK,
  46. PTHREAD_MUTEX_RECURSIVE_NP = PTHREAD_MUTEX_RECURSIVE,
  47. PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
  48. };
  49. /* init value for pthread_once_t */
  50. #define PTHREAD_ONCE_INIT 0
  51. enum {
  52. PTHREAD_PRIO_INHERIT =0,
  53. PTHREAD_PRIO_NONE,
  54. PTHREAD_PRIO_PROTECT,
  55. };
  56. #define PTHREAD_PROCESS_PRIVATE 0
  57. #define PTHREAD_PROCESS_SHARED 1
  58. #define PTHREAD_SCOPE_PROCESS 0
  59. #define PTHREAD_SCOPE_SYSTEM 1
  60. struct pthread_attr
  61. {
  62. void* stack_base;
  63. rt_uint16_t stack_size; /* stack size of thread */
  64. rt_uint8_t priority; /* priority of thread */
  65. rt_uint8_t detachstate; /* detach state */
  66. rt_uint8_t policy; /* scheduler policy */
  67. rt_uint8_t inheritsched; /* Inherit parent prio/policy */
  68. };
  69. typedef struct pthread_attr pthread_attr_t;
  70. struct pthread_mutex
  71. {
  72. pthread_mutexattr_t attr;
  73. struct rt_mutex lock;
  74. };
  75. typedef struct pthread_mutex pthread_mutex_t;
  76. struct pthread_cond
  77. {
  78. pthread_condattr_t attr;
  79. struct rt_semaphore sem;
  80. };
  81. typedef struct pthread_cond pthread_cond_t;
  82. struct pthread_rwlock
  83. {
  84. pthread_rwlockattr_t attr;
  85. pthread_mutex_t rw_mutex; /* basic lock on this struct */
  86. pthread_cond_t rw_condreaders; /* for reader threads waiting */
  87. pthread_cond_t rw_condwriters; /* for writer threads waiting */
  88. int rw_nwaitreaders; /* the number waiting */
  89. int rw_nwaitwriters; /* the number waiting */
  90. int rw_refcount;
  91. };
  92. typedef struct pthread_rwlock pthread_rwlock_t;
  93. /* spinlock implementation, (ADVANCED REALTIME THREADS)*/
  94. struct pthread_spinlock
  95. {
  96. int lock;
  97. };
  98. typedef struct pthread_spinlock pthread_spinlock_t;
  99. struct pthread_barrier
  100. {
  101. int count;
  102. pthread_cond_t cond;
  103. pthread_mutex_t mutex;
  104. };
  105. typedef struct pthread_barrier pthread_barrier_t;
  106. /* pthread thread interface */
  107. int pthread_attr_destroy(pthread_attr_t *attr);
  108. int pthread_attr_init(pthread_attr_t *attr);
  109. int pthread_init (void);
  110. int pthread_create (pthread_t *tid, const pthread_attr_t *attr,
  111. void *(*start) (void *), void *arg);
  112. int pthread_detach (pthread_t thread);
  113. int pthread_join (pthread_t thread, void **value_ptr);
  114. rt_inline int pthread_equal (pthread_t t1, pthread_t t2)
  115. {
  116. return t1 == t2;
  117. }
  118. rt_inline pthread_t pthread_self (void)
  119. {
  120. return rt_thread_self();
  121. }
  122. void pthread_exit (void *value_ptr);
  123. int pthread_once(pthread_once_t * once_control, void (*init_routine) (void));
  124. /* pthread cleanup */
  125. void pthread_cleanup_pop(int execute);
  126. void pthread_cleanup_push(void (*routine)(void*), void *arg);
  127. /* pthread cancel */
  128. int pthread_cancel(pthread_t thread);
  129. void pthread_testcancel(void);
  130. int pthread_setcancelstate(int state, int *oldstate);
  131. int pthread_setcanceltype(int type, int *oldtype);
  132. int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void));
  133. int pthread_kill(pthread_t thread, int sig);
  134. /* pthread mutex interface */
  135. int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
  136. int pthread_mutex_destroy(pthread_mutex_t *mutex);
  137. int pthread_mutex_lock(pthread_mutex_t *mutex);
  138. int pthread_mutex_unlock(pthread_mutex_t *mutex);
  139. int pthread_mutex_trylock(pthread_mutex_t *mutex);
  140. int pthread_mutexattr_init(pthread_mutexattr_t *attr);
  141. int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
  142. int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type);
  143. int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
  144. int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);
  145. int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared);
  146. /* pthread condition interface */
  147. int pthread_condattr_destroy(pthread_condattr_t *attr);
  148. int pthread_condattr_init(pthread_condattr_t *attr);
  149. /* ADVANCED REALTIME feature in IEEE Std 1003.1, 2004 Edition */
  150. int pthread_condattr_getclock(const pthread_condattr_t *attr,
  151. clockid_t *clock_id);
  152. int pthread_condattr_setclock(pthread_condattr_t *attr,
  153. clockid_t clock_id);
  154. int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
  155. int pthread_cond_destroy(pthread_cond_t *cond);
  156. int pthread_cond_broadcast(pthread_cond_t *cond);
  157. int pthread_cond_signal(pthread_cond_t *cond);
  158. int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
  159. int pthread_cond_timedwait(pthread_cond_t *cond,
  160. pthread_mutex_t * mutex,
  161. const struct timespec *abstime);
  162. /* pthread rwlock interface */
  163. int pthread_rwlockattr_init (pthread_rwlockattr_t *attr);
  164. int pthread_rwlockattr_destroy (pthread_rwlockattr_t *attr);
  165. int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *attr, int *pshared);
  166. int pthread_rwlockattr_setpshared (pthread_rwlockattr_t *attr, int pshared);
  167. int pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);
  168. int pthread_rwlock_destroy (pthread_rwlock_t *rwlock);
  169. int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock);
  170. int pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock);
  171. int pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock, const struct timespec *abstime);
  172. int pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock, const struct timespec *abstime);
  173. int pthread_rwlock_unlock (pthread_rwlock_t *rwlock);
  174. int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock);
  175. int pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock);
  176. /* pthread spinlock interface */
  177. int pthread_spin_init (pthread_spinlock_t *lock, int pshared);
  178. int pthread_spin_destroy (pthread_spinlock_t *lock);
  179. int pthread_spin_lock (pthread_spinlock_t * lock);
  180. int pthread_spin_trylock (pthread_spinlock_t * lock);
  181. int pthread_spin_unlock (pthread_spinlock_t * lock);
  182. /* pthread barrier interface */
  183. int pthread_barrierattr_destroy(pthread_barrierattr_t *attr);
  184. int pthread_barrierattr_init(pthread_barrierattr_t *attr);
  185. int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr, int *pshared);
  186. int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared);
  187. int pthread_barrier_destroy(pthread_barrier_t *barrier);
  188. int pthread_barrier_init(pthread_barrier_t *barrier,
  189. const pthread_barrierattr_t *attr, unsigned count);
  190. int pthread_barrier_wait(pthread_barrier_t *barrier);
  191. /* Signal Generation and Delivery, P1003.1b-1993, p. 63
  192. NOTE: P1003.1c/D10, p. 34 adds sigev_notify_function and
  193. sigev_notify_attributes to the sigevent structure. */
  194. union sigval {
  195. int sival_int; /* Integer signal value */
  196. void *sival_ptr; /* Pointer signal value */
  197. };
  198. struct sigevent {
  199. int sigev_notify; /* Notification type */
  200. int sigev_signo; /* Signal number */
  201. union sigval sigev_value; /* Signal value */
  202. void (*sigev_notify_function)( union sigval );
  203. /* Notification function */
  204. pthread_attr_t *sigev_notify_attributes; /* Notification Attributes */
  205. };
  206. #endif