pthread.h 9.6 KB

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