pthread.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2010-10-26 Bernard the first version
  23. */
  24. #ifndef __PTHREAD_H__
  25. #define __PTHREAD_H__
  26. #include <rtthread.h>
  27. #include <posix_types.h>
  28. #define PTHREAD_KEY_MAX 8
  29. #define PTHREAD_COND_INITIALIZER {-1, 0}
  30. #define PTHREAD_RWLOCK_INITIALIZER {-1, 0}
  31. #define PTHREAD_MUTEX_INITIALIZER {-1, 0}
  32. #define PTHREAD_CREATE_JOINABLE 0x00
  33. #define PTHREAD_CREATE_DETACHED 0x01
  34. #define PTHREAD_EXPLICIT_SCHED 0
  35. #define PTHREAD_INHERIT_SCHED 1
  36. typedef rt_thread_t pthread_t;
  37. typedef long pthread_condattr_t;
  38. typedef long pthread_rwlockattr_t;
  39. typedef long pthread_mutexattr_t;
  40. typedef long pthread_barrierattr_t;
  41. typedef int pthread_key_t;
  42. typedef int pthread_once_t;
  43. enum
  44. {
  45. PTHREAD_CANCEL_ASYNCHRONOUS = 0,
  46. PTHREAD_CANCEL_ENABLE,
  47. PTHREAD_CANCEL_DEFERRED,
  48. PTHREAD_CANCEL_DISABLE,
  49. PTHREAD_CANCELED
  50. };
  51. enum
  52. {
  53. PTHREAD_MUTEX_NORMAL = 0,
  54. PTHREAD_MUTEX_RECURSIVE = 1,
  55. PTHREAD_MUTEX_ERRORCHECK = 2,
  56. PTHREAD_MUTEX_ERRORCHECK_NP = PTHREAD_MUTEX_ERRORCHECK,
  57. PTHREAD_MUTEX_RECURSIVE_NP = PTHREAD_MUTEX_RECURSIVE,
  58. PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
  59. };
  60. /* init value for pthread_once_t */
  61. #define PTHREAD_ONCE_INIT 0
  62. enum
  63. {
  64. PTHREAD_PRIO_INHERIT =0,
  65. PTHREAD_PRIO_NONE,
  66. PTHREAD_PRIO_PROTECT,
  67. };
  68. #define PTHREAD_PROCESS_PRIVATE 0
  69. #define PTHREAD_PROCESS_SHARED 1
  70. #define PTHREAD_SCOPE_PROCESS 0
  71. #define PTHREAD_SCOPE_SYSTEM 1
  72. struct pthread_attr
  73. {
  74. void* stack_base;
  75. rt_uint32_t stack_size; /* stack size of thread */
  76. rt_uint8_t priority; /* priority of thread */
  77. rt_uint8_t detachstate; /* detach state */
  78. rt_uint8_t policy; /* scheduler policy */
  79. rt_uint8_t inheritsched; /* Inherit parent prio/policy */
  80. };
  81. typedef struct pthread_attr pthread_attr_t;
  82. struct pthread_mutex
  83. {
  84. pthread_mutexattr_t attr;
  85. struct rt_mutex lock;
  86. };
  87. typedef struct pthread_mutex pthread_mutex_t;
  88. struct pthread_cond
  89. {
  90. pthread_condattr_t attr;
  91. struct rt_semaphore sem;
  92. };
  93. typedef struct pthread_cond pthread_cond_t;
  94. struct pthread_rwlock
  95. {
  96. pthread_rwlockattr_t attr;
  97. pthread_mutex_t rw_mutex; /* basic lock on this struct */
  98. pthread_cond_t rw_condreaders; /* for reader threads waiting */
  99. pthread_cond_t rw_condwriters; /* for writer threads waiting */
  100. int rw_nwaitreaders; /* the number of reader threads waiting */
  101. int rw_nwaitwriters; /* the number of writer threads waiting */
  102. int rw_refcount; /* 0: unlocked, -1: locked by writer, > 0 locked by n readers */
  103. };
  104. typedef struct pthread_rwlock pthread_rwlock_t;
  105. /* spinlock implementation, (ADVANCED REALTIME THREADS)*/
  106. struct pthread_spinlock
  107. {
  108. int lock;
  109. };
  110. typedef struct pthread_spinlock pthread_spinlock_t;
  111. struct pthread_barrier
  112. {
  113. int count;
  114. pthread_cond_t cond;
  115. pthread_mutex_t mutex;
  116. };
  117. typedef struct pthread_barrier pthread_barrier_t;
  118. /* pthread thread interface */
  119. int pthread_attr_destroy(pthread_attr_t *attr);
  120. int pthread_attr_init(pthread_attr_t *attr);
  121. int pthread_attr_setdetachstate(pthread_attr_t *attr, int state);
  122. int pthread_attr_getdetachstate(pthread_attr_t const *attr, int *state);
  123. int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
  124. int pthread_attr_getschedpolicy(pthread_attr_t const *attr, int *policy);
  125. int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stack_size);
  126. int pthread_attr_getstacksize(pthread_attr_t const *attr, size_t *stack_size);
  127. int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stack_addr);
  128. int pthread_attr_getstackaddr(pthread_attr_t const *attr, void **stack_addr);
  129. int pthread_attr_setstack(pthread_attr_t *attr,
  130. void *stack_base,
  131. size_t stack_size);
  132. int pthread_attr_getstack(pthread_attr_t const *attr,
  133. void **stack_base,
  134. size_t *stack_size);
  135. int pthread_system_init(void);
  136. int pthread_create (pthread_t *tid, const pthread_attr_t *attr,
  137. void *(*start) (void *), void *arg);
  138. int pthread_detach (pthread_t thread);
  139. int pthread_join (pthread_t thread, void **value_ptr);
  140. rt_inline int pthread_equal (pthread_t t1, pthread_t t2)
  141. {
  142. return t1 == t2;
  143. }
  144. rt_inline pthread_t pthread_self (void)
  145. {
  146. return rt_thread_self();
  147. }
  148. void pthread_exit (void *value_ptr);
  149. int pthread_once(pthread_once_t * once_control, void (*init_routine) (void));
  150. /* pthread cleanup */
  151. void pthread_cleanup_pop(int execute);
  152. void pthread_cleanup_push(void (*routine)(void*), void *arg);
  153. /* pthread cancel */
  154. int pthread_cancel(pthread_t thread);
  155. void pthread_testcancel(void);
  156. int pthread_setcancelstate(int state, int *oldstate);
  157. int pthread_setcanceltype(int type, int *oldtype);
  158. int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void));
  159. int pthread_kill(pthread_t thread, int sig);
  160. /* pthread mutex interface */
  161. int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
  162. int pthread_mutex_destroy(pthread_mutex_t *mutex);
  163. int pthread_mutex_lock(pthread_mutex_t *mutex);
  164. int pthread_mutex_unlock(pthread_mutex_t *mutex);
  165. int pthread_mutex_trylock(pthread_mutex_t *mutex);
  166. int pthread_mutexattr_init(pthread_mutexattr_t *attr);
  167. int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
  168. int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type);
  169. int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
  170. int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);
  171. int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared);
  172. /* pthread condition interface */
  173. int pthread_condattr_destroy(pthread_condattr_t *attr);
  174. int pthread_condattr_init(pthread_condattr_t *attr);
  175. /* ADVANCED REALTIME feature in IEEE Std 1003.1, 2004 Edition */
  176. int pthread_condattr_getclock(const pthread_condattr_t *attr,
  177. clockid_t *clock_id);
  178. int pthread_condattr_setclock(pthread_condattr_t *attr,
  179. clockid_t clock_id);
  180. int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
  181. int pthread_cond_destroy(pthread_cond_t *cond);
  182. int pthread_cond_broadcast(pthread_cond_t *cond);
  183. int pthread_cond_signal(pthread_cond_t *cond);
  184. int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
  185. int pthread_cond_timedwait(pthread_cond_t *cond,
  186. pthread_mutex_t *mutex,
  187. const struct timespec *abstime);
  188. /* pthread rwlock interface */
  189. int pthread_rwlockattr_init (pthread_rwlockattr_t *attr);
  190. int pthread_rwlockattr_destroy (pthread_rwlockattr_t *attr);
  191. int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *attr, int *pshared);
  192. int pthread_rwlockattr_setpshared (pthread_rwlockattr_t *attr, int pshared);
  193. int pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);
  194. int pthread_rwlock_destroy (pthread_rwlock_t *rwlock);
  195. int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock);
  196. int pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock);
  197. int pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock, const struct timespec *abstime);
  198. int pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock, const struct timespec *abstime);
  199. int pthread_rwlock_unlock (pthread_rwlock_t *rwlock);
  200. int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock);
  201. int pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock);
  202. /* pthread spinlock interface */
  203. int pthread_spin_init (pthread_spinlock_t *lock, int pshared);
  204. int pthread_spin_destroy (pthread_spinlock_t *lock);
  205. int pthread_spin_lock (pthread_spinlock_t * lock);
  206. int pthread_spin_trylock (pthread_spinlock_t * lock);
  207. int pthread_spin_unlock (pthread_spinlock_t * lock);
  208. /* pthread barrier interface */
  209. int pthread_barrierattr_destroy(pthread_barrierattr_t *attr);
  210. int pthread_barrierattr_init(pthread_barrierattr_t *attr);
  211. int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr, int *pshared);
  212. int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared);
  213. int pthread_barrier_destroy(pthread_barrier_t *barrier);
  214. int pthread_barrier_init(pthread_barrier_t *barrier,
  215. const pthread_barrierattr_t *attr,
  216. unsigned count);
  217. int pthread_barrier_wait(pthread_barrier_t *barrier);
  218. /* Signal Generation and Delivery, P1003.1b-1993, p. 63
  219. NOTE: P1003.1c/D10, p. 34 adds sigev_notify_function and
  220. sigev_notify_attributes to the sigevent structure. */
  221. #if 0
  222. union sigval
  223. {
  224. int sival_int; /* Integer signal value */
  225. void *sival_ptr; /* Pointer signal value */
  226. };
  227. #endif
  228. struct sigevent
  229. {
  230. int sigev_notify; /* Notification type */
  231. int sigev_signo; /* Signal number */
  232. union sigval sigev_value; /* Signal value */
  233. void (*sigev_notify_function)( union sigval );
  234. /* Notification function */
  235. pthread_attr_t *sigev_notify_attributes; /* Notification Attributes */
  236. };
  237. /* posix clock and timer */
  238. #define MILLISECOND_PER_SECOND 1000UL
  239. #define MICROSECOND_PER_SECOND 1000000UL
  240. #define NANOSECOND_PER_SECOND 1000000000UL
  241. #define MILLISECOND_PER_TICK (MILLISECOND_PER_SECOND / RT_TICK_PER_SECOND)
  242. #define MICROSECOND_PER_TICK (MICROSECOND_PER_SECOND / RT_TICK_PER_SECOND)
  243. #define NANOSECOND_PER_TICK (NANOSECOND_PER_SECOND / RT_TICK_PER_SECOND)
  244. #ifndef CLOCK_REALTIME
  245. #define CLOCK_REALTIME 0
  246. #endif
  247. int clock_getres (clockid_t clockid, struct timespec *res);
  248. int clock_gettime (clockid_t clockid, struct timespec *tp);
  249. int clock_settime (clockid_t clockid, const struct timespec *tp);
  250. #endif