pthread.h 7.6 KB

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