pthread.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 <posix_types.h>
  18. #define PTHREAD_KEY_MAX 8
  19. #define PTHREAD_COND_INITIALIZER {-1, 0}
  20. #define PTHREAD_RWLOCK_INITIALIZER {-1, 0}
  21. #define PTHREAD_MUTEX_INITIALIZER {-1, 0}
  22. #define PTHREAD_CREATE_JOINABLE 0x00
  23. #define PTHREAD_CREATE_DETACHED 0x01
  24. #define PTHREAD_EXPLICIT_SCHED 0
  25. #define PTHREAD_INHERIT_SCHED 1
  26. typedef rt_thread_t pthread_t;
  27. typedef long pthread_condattr_t;
  28. typedef long pthread_rwlockattr_t;
  29. typedef long pthread_mutexattr_t;
  30. typedef long pthread_barrierattr_t;
  31. typedef int pthread_key_t;
  32. typedef int pthread_once_t;
  33. enum {
  34. PTHREAD_CANCEL_ASYNCHRONOUS = 0,
  35. PTHREAD_CANCEL_ENABLE,
  36. PTHREAD_CANCEL_DEFERRED,
  37. PTHREAD_CANCEL_DISABLE,
  38. PTHREAD_CANCELED
  39. };
  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 pthread_attr
  60. {
  61. void* stack_base;
  62. rt_uint16_t stack_size; /* stack size of thread */
  63. rt_uint8_t priority; /* priority of thread */
  64. rt_uint8_t detachstate; /* detach state */
  65. rt_uint8_t policy; /* scheduler policy */
  66. rt_uint8_t inheritsched; /* Inherit parent prio/policy */
  67. };
  68. typedef struct pthread_attr pthread_attr_t;
  69. struct pthread_mutex
  70. {
  71. pthread_mutexattr_t attr;
  72. struct rt_mutex lock;
  73. };
  74. typedef struct pthread_mutex pthread_mutex_t;
  75. struct pthread_cond
  76. {
  77. pthread_condattr_t attr;
  78. struct rt_semaphore sem;
  79. };
  80. typedef struct pthread_cond pthread_cond_t;
  81. struct pthread_rwlock
  82. {
  83. pthread_rwlockattr_t attr;
  84. pthread_mutex_t rw_mutex; /* basic lock on this struct */
  85. pthread_cond_t rw_condreaders; /* for reader threads waiting */
  86. pthread_cond_t rw_condwriters; /* for writer threads waiting */
  87. int rw_nwaitreaders; /* the number waiting */
  88. int rw_nwaitwriters; /* the number waiting */
  89. int rw_refcount;
  90. };
  91. typedef struct pthread_rwlock pthread_rwlock_t;
  92. /* spinlock implementation, (ADVANCED REALTIME THREADS)*/
  93. struct pthread_spinlock
  94. {
  95. int lock;
  96. };
  97. typedef struct pthread_spinlock pthread_spinlock_t;
  98. struct pthread_barrier
  99. {
  100. int count;
  101. pthread_cond_t cond;
  102. pthread_mutex_t mutex;
  103. };
  104. typedef struct pthread_barrier pthread_barrier_t;
  105. /* pthread thread interface */
  106. int pthread_attr_destroy(pthread_attr_t *attr);
  107. int pthread_attr_init(pthread_attr_t *attr);
  108. int pthread_system_init(void);
  109. int pthread_create (pthread_t *tid, const pthread_attr_t *attr,
  110. void *(*start) (void *), void *arg);
  111. int pthread_detach (pthread_t thread);
  112. int pthread_join (pthread_t thread, void **value_ptr);
  113. rt_inline int pthread_equal (pthread_t t1, pthread_t t2)
  114. {
  115. return t1 == t2;
  116. }
  117. rt_inline pthread_t pthread_self (void)
  118. {
  119. return rt_thread_self();
  120. }
  121. void pthread_exit (void *value_ptr);
  122. int pthread_once(pthread_once_t * once_control, void (*init_routine) (void));
  123. /* pthread cleanup */
  124. void pthread_cleanup_pop(int execute);
  125. void pthread_cleanup_push(void (*routine)(void*), void *arg);
  126. /* pthread cancel */
  127. int pthread_cancel(pthread_t thread);
  128. void pthread_testcancel(void);
  129. int pthread_setcancelstate(int state, int *oldstate);
  130. int pthread_setcanceltype(int type, int *oldtype);
  131. int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void));
  132. int pthread_kill(pthread_t thread, int sig);
  133. /* pthread mutex interface */
  134. int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
  135. int pthread_mutex_destroy(pthread_mutex_t *mutex);
  136. int pthread_mutex_lock(pthread_mutex_t *mutex);
  137. int pthread_mutex_unlock(pthread_mutex_t *mutex);
  138. int pthread_mutex_trylock(pthread_mutex_t *mutex);
  139. int pthread_mutexattr_init(pthread_mutexattr_t *attr);
  140. int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
  141. int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type);
  142. int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
  143. int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);
  144. int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared);
  145. /* pthread condition interface */
  146. int pthread_condattr_destroy(pthread_condattr_t *attr);
  147. int pthread_condattr_init(pthread_condattr_t *attr);
  148. /* ADVANCED REALTIME feature in IEEE Std 1003.1, 2004 Edition */
  149. int pthread_condattr_getclock(const pthread_condattr_t *attr,
  150. clockid_t *clock_id);
  151. int pthread_condattr_setclock(pthread_condattr_t *attr,
  152. clockid_t clock_id);
  153. int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
  154. int pthread_cond_destroy(pthread_cond_t *cond);
  155. int pthread_cond_broadcast(pthread_cond_t *cond);
  156. int pthread_cond_signal(pthread_cond_t *cond);
  157. int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
  158. int pthread_cond_timedwait(pthread_cond_t *cond,
  159. pthread_mutex_t * mutex,
  160. const struct timespec *abstime);
  161. /* pthread rwlock interface */
  162. int pthread_rwlockattr_init (pthread_rwlockattr_t *attr);
  163. int pthread_rwlockattr_destroy (pthread_rwlockattr_t *attr);
  164. int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *attr, int *pshared);
  165. int pthread_rwlockattr_setpshared (pthread_rwlockattr_t *attr, int pshared);
  166. int pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);
  167. int pthread_rwlock_destroy (pthread_rwlock_t *rwlock);
  168. int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock);
  169. int pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock);
  170. int pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock, const struct timespec *abstime);
  171. int pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock, const struct timespec *abstime);
  172. int pthread_rwlock_unlock (pthread_rwlock_t *rwlock);
  173. int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock);
  174. int pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock);
  175. /* pthread spinlock interface */
  176. int pthread_spin_init (pthread_spinlock_t *lock, int pshared);
  177. int pthread_spin_destroy (pthread_spinlock_t *lock);
  178. int pthread_spin_lock (pthread_spinlock_t * lock);
  179. int pthread_spin_trylock (pthread_spinlock_t * lock);
  180. int pthread_spin_unlock (pthread_spinlock_t * lock);
  181. /* pthread barrier interface */
  182. int pthread_barrierattr_destroy(pthread_barrierattr_t *attr);
  183. int pthread_barrierattr_init(pthread_barrierattr_t *attr);
  184. int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr, int *pshared);
  185. int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared);
  186. int pthread_barrier_destroy(pthread_barrier_t *barrier);
  187. int pthread_barrier_init(pthread_barrier_t *barrier,
  188. const pthread_barrierattr_t *attr, unsigned count);
  189. int pthread_barrier_wait(pthread_barrier_t *barrier);
  190. /* Signal Generation and Delivery, P1003.1b-1993, p. 63
  191. NOTE: P1003.1c/D10, p. 34 adds sigev_notify_function and
  192. sigev_notify_attributes to the sigevent structure. */
  193. union sigval {
  194. int sival_int; /* Integer signal value */
  195. void *sival_ptr; /* Pointer signal value */
  196. };
  197. struct sigevent {
  198. int sigev_notify; /* Notification type */
  199. int sigev_signo; /* Signal number */
  200. union sigval sigev_value; /* Signal value */
  201. void (*sigev_notify_function)( union sigval );
  202. /* Notification function */
  203. pthread_attr_t *sigev_notify_attributes; /* Notification Attributes */
  204. };
  205. /* posix clock and timer */
  206. #define MILLISECOND_PER_SECOND 1000UL
  207. #define MICROSECOND_PER_SECOND 1000000UL
  208. #define NANOSECOND_PER_SECOND 1000000000UL
  209. #define MILLISECOND_PER_TICK (MILLISECOND_PER_SECOND / RT_TICK_PER_SECOND)
  210. #define MICROSECOND_PER_TICK (MICROSECOND_PER_SECOND / RT_TICK_PER_SECOND)
  211. #define NANOSECOND_PER_TICK (NANOSECOND_PER_SECOND / RT_TICK_PER_SECOND)
  212. #ifndef CLOCK_REALTIME
  213. #define CLOCK_REALTIME 0
  214. #endif
  215. int clock_getres (clockid_t clockid, struct timespec *res);
  216. int clock_gettime (clockid_t clockid, struct timespec *tp);
  217. int clock_settime (clockid_t clockid, const struct timespec *tp);
  218. #endif