pthread.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. /*
  35. * Scheduling policies required by IEEE Std 1003.1-2001
  36. */
  37. #define SCHED_OTHER 0 /* Behavior can be FIFO or RR, or not */
  38. #define SCHED_FIFO 1
  39. #define SCHED_RR 2
  40. enum {
  41. PTHREAD_CANCEL_ASYNCHRONOUS = 0,
  42. PTHREAD_CANCEL_ENABLE,
  43. PTHREAD_CANCEL_DEFERRED,
  44. PTHREAD_CANCEL_DISABLE,
  45. PTHREAD_CANCELED
  46. };
  47. enum {
  48. PTHREAD_MUTEX_NORMAL = 0,
  49. PTHREAD_MUTEX_RECURSIVE = 1,
  50. PTHREAD_MUTEX_ERRORCHECK = 2,
  51. PTHREAD_MUTEX_ERRORCHECK_NP = PTHREAD_MUTEX_ERRORCHECK,
  52. PTHREAD_MUTEX_RECURSIVE_NP = PTHREAD_MUTEX_RECURSIVE,
  53. PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
  54. };
  55. /* init value for pthread_once_t */
  56. #define PTHREAD_ONCE_INIT 0
  57. enum {
  58. PTHREAD_PRIO_INHERIT =0,
  59. PTHREAD_PRIO_NONE,
  60. PTHREAD_PRIO_PROTECT,
  61. };
  62. #define PTHREAD_PROCESS_PRIVATE 0
  63. #define PTHREAD_PROCESS_SHARED 1
  64. #define PTHREAD_SCOPE_PROCESS 0
  65. #define PTHREAD_SCOPE_SYSTEM 1
  66. struct sched_param {
  67. int sched_priority;
  68. };
  69. struct pthread_attr
  70. {
  71. void* stack_base;
  72. rt_uint16_t stack_size; /* stack size of thread */
  73. rt_uint8_t priority; /* priority of thread */
  74. rt_uint8_t detachstate; /* detach state */
  75. rt_uint8_t policy; /* scheduler policy */
  76. rt_uint8_t inheritsched; /* Inherit parent prio/policy */
  77. };
  78. typedef struct pthread_attr pthread_attr_t;
  79. struct pthread_mutex
  80. {
  81. pthread_mutexattr_t attr;
  82. struct rt_mutex lock;
  83. };
  84. typedef struct pthread_mutex pthread_mutex_t;
  85. struct pthread_cond
  86. {
  87. pthread_condattr_t attr;
  88. struct rt_semaphore sem;
  89. };
  90. typedef struct pthread_cond pthread_cond_t;
  91. struct pthread_rwlock
  92. {
  93. pthread_rwlockattr_t attr;
  94. pthread_mutex_t rw_mutex; /* basic lock on this struct */
  95. pthread_cond_t rw_condreaders; /* for reader threads waiting */
  96. pthread_cond_t rw_condwriters; /* for writer threads waiting */
  97. int rw_nwaitreaders; /* the number waiting */
  98. int rw_nwaitwriters; /* the number waiting */
  99. int rw_refcount;
  100. };
  101. typedef struct pthread_rwlock pthread_rwlock_t;
  102. /* spinlock implementation, (ADVANCED REALTIME THREADS)*/
  103. struct pthread_spinlock
  104. {
  105. int lock;
  106. };
  107. typedef struct pthread_spinlock pthread_spinlock_t;
  108. struct pthread_barrier
  109. {
  110. int count;
  111. pthread_cond_t cond;
  112. pthread_mutex_t mutex;
  113. };
  114. typedef struct pthread_barrier pthread_barrier_t;
  115. /* pthread thread interface */
  116. int pthread_attr_destroy(pthread_attr_t *attr);
  117. int pthread_attr_init(pthread_attr_t *attr);
  118. int pthread_init (void);
  119. int pthread_create (pthread_t *tid, const pthread_attr_t *attr,
  120. void *(*start) (void *), void *arg);
  121. int pthread_detach (pthread_t thread);
  122. int pthread_join (pthread_t thread, void **value_ptr);
  123. rt_inline int pthread_equal (pthread_t t1, pthread_t t2)
  124. {
  125. return t1 == t2;
  126. }
  127. rt_inline pthread_t pthread_self (void)
  128. {
  129. return rt_thread_self();
  130. }
  131. void pthread_exit (void *value_ptr);
  132. int pthread_once(pthread_once_t * once_control, void (*init_routine) (void));
  133. /* pthread cleanup */
  134. void pthread_cleanup_pop(int execute);
  135. void pthread_cleanup_push(void (*routine)(void*), void *arg);
  136. /* pthread cancel */
  137. int pthread_cancel(pthread_t thread);
  138. void pthread_testcancel(void);
  139. int pthread_setcancelstate(int state, int *oldstate);
  140. int pthread_setcanceltype(int type, int *oldtype);
  141. int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void));
  142. int pthread_kill(pthread_t thread, int sig);
  143. /* pthread mutex interface */
  144. int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
  145. int pthread_mutex_destroy(pthread_mutex_t *mutex);
  146. int pthread_mutex_lock(pthread_mutex_t *mutex);
  147. int pthread_mutex_unlock(pthread_mutex_t *mutex);
  148. int pthread_mutex_trylock(pthread_mutex_t *mutex);
  149. int pthread_mutexattr_init(pthread_mutexattr_t *attr);
  150. int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
  151. int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type);
  152. int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
  153. int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);
  154. int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared);
  155. /* pthread condition interface */
  156. int pthread_condattr_destroy(pthread_condattr_t *attr);
  157. int pthread_condattr_init(pthread_condattr_t *attr);
  158. /* ADVANCED REALTIME feature in IEEE Std 1003.1, 2004 Edition */
  159. int pthread_condattr_getclock(const pthread_condattr_t *attr,
  160. clockid_t *clock_id);
  161. int pthread_condattr_setclock(pthread_condattr_t *attr,
  162. clockid_t clock_id);
  163. int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
  164. int pthread_cond_destroy(pthread_cond_t *cond);
  165. int pthread_cond_broadcast(pthread_cond_t *cond);
  166. int pthread_cond_signal(pthread_cond_t *cond);
  167. int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
  168. int pthread_cond_timedwait(pthread_cond_t *cond,
  169. pthread_mutex_t * mutex,
  170. const struct timespec *abstime);
  171. /* pthread rwlock interface */
  172. int pthread_rwlockattr_init (pthread_rwlockattr_t *attr);
  173. int pthread_rwlockattr_destroy (pthread_rwlockattr_t *attr);
  174. int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *attr, int *pshared);
  175. int pthread_rwlockattr_setpshared (pthread_rwlockattr_t *attr, int pshared);
  176. int pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);
  177. int pthread_rwlock_destroy (pthread_rwlock_t *rwlock);
  178. int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock);
  179. int pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock);
  180. int pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock, const struct timespec *abstime);
  181. int pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock, const struct timespec *abstime);
  182. int pthread_rwlock_unlock (pthread_rwlock_t *rwlock);
  183. int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock);
  184. int pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock);
  185. /* pthread spinlock interface */
  186. int pthread_spin_init (pthread_spinlock_t *lock, int pshared);
  187. int pthread_spin_destroy (pthread_spinlock_t *lock);
  188. int pthread_spin_lock (pthread_spinlock_t * lock);
  189. int pthread_spin_trylock (pthread_spinlock_t * lock);
  190. int pthread_spin_unlock (pthread_spinlock_t * lock);
  191. /* pthread barrier interface */
  192. int pthread_barrierattr_destroy(pthread_barrierattr_t *attr);
  193. int pthread_barrierattr_init(pthread_barrierattr_t *attr);
  194. int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr, int *pshared);
  195. int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared);
  196. int pthread_barrier_destroy(pthread_barrier_t *barrier);
  197. int pthread_barrier_init(pthread_barrier_t *barrier,
  198. const pthread_barrierattr_t *attr, unsigned count);
  199. int pthread_barrier_wait(pthread_barrier_t *barrier);
  200. #endif