threads.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /****************************************************************************
  2. * include/threads.h
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. #ifndef __INCLUDE_THREADS_H
  21. #define __INCLUDE_THREADS_H
  22. /****************************************************************************
  23. * Included Files
  24. ****************************************************************************/
  25. #include <nuttx/config.h>
  26. #include <sys/types.h>
  27. #include <pthread.h>
  28. #include <time.h>
  29. #include <errno.h>
  30. /****************************************************************************
  31. * Pre-processor Definitions
  32. ****************************************************************************/
  33. /* Indicates thread error status */
  34. #define thrd_success ((FAR void *)OK)
  35. #define thrd_timedout ((FAR void *)ETIMEDOUT)
  36. #define thrd_busy ((FAR void *)EBUSY)
  37. #define thrd_nomem ((FAR void *)ENOMEM)
  38. #define thrd_error ((FAR void *)ERROR)
  39. /* Defines the type of a mutex */
  40. #define mtx_plain 0
  41. #define mtx_recursive 1
  42. #define mtx_timed 2
  43. /* ONCE_FLAG_INIT: initializes a once_flag */
  44. #define ONCE_FLAG_INIT PTHREAD_ONCE_INIT
  45. /* thread_local: thread local type macro */
  46. #ifndef __cplusplus
  47. #define thread_local _Thread_local
  48. #endif
  49. /* tss_t: thread-specific storage pointer */
  50. #define tss_t pthread_key_t
  51. /* TSS_DTOR_ITERATIONS: maximum number of times destructors are called */
  52. /****************************************************************************
  53. * Public Type Definitions
  54. ****************************************************************************/
  55. /* thrd_t: a type identifying a thread */
  56. #define thrd_t pthread_t
  57. /* thrd_start_t: function pointer type passed to thrd_create */
  58. typedef CODE int (*thrd_start_t)(FAR void *arg)
  59. /* mtx_t : mutex identifier */
  60. #define mtx_t pthread_mutex_t
  61. /* once_flag: the type of the flag used by call_once */
  62. #define once_flag pthread_once_t
  63. /* cnd_t: condition variable identifier */
  64. #define cnd_t pthread_cond_t
  65. /* tss_dtor_t: function pointer type used for TSS destructor */
  66. typedef CODE void (*tss_dtor_t)(FAR void *);
  67. /****************************************************************************
  68. * Public Function Prototypes
  69. ****************************************************************************/
  70. /* Threads ******************************************************************/
  71. /* thrd_create: creates a thread
  72. *
  73. * int thrd_create(FAR thrd_t *thr, thrd_start_t func, FAR void *arg);
  74. */
  75. #define thrd_create(thr,func,arg) \
  76. pthread_create(thr,NULL,(pthread_startroutine_t)func,arg)
  77. /* thrd_equal: checks if two identifiers refer to the same thread
  78. *
  79. * int thrd_equal(thrd_t lhs, thrd_t rhs);
  80. */
  81. #define thrd_equal(lhs,rhs) (lhs == rhs)
  82. /* thrd_current: obtains the current thread identifier
  83. *
  84. * thrd_t thrd_current(void);
  85. */
  86. #define thrd_current() ((thrd_t)getpid())
  87. /* thrd_sleep: suspends execution of the calling thread for the given
  88. * period of time
  89. *
  90. * int thrd_sleep(FAR const struct timespec *time_point,
  91. * FAR struct timespec *remaining);
  92. */
  93. #define thrd_sleep(rqtp,rmtp) nanosleep(rqtp,rmtp)
  94. /* thrd_yield: yields the current time slice
  95. *
  96. * void thrd_yield(void);
  97. */
  98. #define thrd_yield() pthread_yield()
  99. /* thrd_exit: terminates the calling thread
  100. *
  101. * _Noreturn void thrd_exit(int res);
  102. */
  103. #define thrd_exit(res) pthread_exit((pthread_addr_t)res)
  104. /* thrd_detach: detaches a thread
  105. *
  106. * int thrd_detach(thrd_t thr);
  107. */
  108. #define thrd_detach(thr) pthread_detach(thr)
  109. /* thrd_join: blocks until a thread terminates
  110. *
  111. * int thrd_join(thrd_t thr, int *res);
  112. */
  113. static inline int thrd_join(thrd_t thr, int *res)
  114. {
  115. pthread_addr_t *value;
  116. int ret = pthread_join(thr, &value);
  117. if (res)
  118. {
  119. *res = (int)value;
  120. }
  121. return ret;
  122. }
  123. /* Mutual exclusion *********************************************************/
  124. /* mtx_init: creates a mutex
  125. *
  126. * int mtx_init(FAR mtx_t *mutex, int type);
  127. */
  128. static inline int mtx_init(FAR mtx_t *mutex, int type)
  129. {
  130. FAR pthread_mutexattr_t *pattr = NULL;
  131. pthread_mutexattr_t attr;
  132. if (type & mtx_recursive)
  133. {
  134. pthread_attr_init(&attr);
  135. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  136. pattr = &attr;
  137. }
  138. return pthread_mutex_init(mutex, pattr);
  139. }
  140. /* mtx_lock: blocks until locks a mutex
  141. *
  142. * int mtx_lock(FAR mtx_t* mutex);
  143. */
  144. #define mtx_lock(mutex) pthread_mutex_lock(mutex)
  145. /* mtx_timedlock: blocks until locks a mutex or times out
  146. *
  147. * int mtx_timedlock(FAR mtx_t *mutex, FAR const struct timespec *tp);
  148. */
  149. #define mtx_timedlock(mutex,tp) pthread_mutex_timedwait(mutex,tp)
  150. /* mtx_trylock: locks a mutex or returns without blocking if already locked
  151. *
  152. * int mtx_trylock(FAR mtx_t *mutex);
  153. */
  154. #define mtx_trylock(mutex) pthread_mutex_trylock(mutex)
  155. /* mtx_unlock: unlocks a mutex
  156. *
  157. * int mtx_unlock(FAR mtx_t *mutex);
  158. */
  159. #define mtx_unlock(mutex) pthread_mutex_unlock(mutex)
  160. /* mtx_destroy: destroys a mutex
  161. *
  162. * void mtx_destroy(FAR mtx_t *mutex);
  163. */
  164. #define mtx_destroy(mutex) pthread_mutex_destroy(mutex)
  165. /* Call once ****************************************************************/
  166. /* call_once: calls a function exactly once
  167. *
  168. * void call_once(FAR once_flag *flag, CODE void (*func)(void));
  169. */
  170. #define call_once(flag,func) pthread_once(flag,func)
  171. /* Condition variables ******************************************************/
  172. /* cnd_init: creates a condition variable
  173. *
  174. * int cnd_init(FAR cnd_t *cond);
  175. */
  176. #define cnd_init(cond) pthread_cond_init(cond,NULL)
  177. /* cnd_signal: unblocks one thread blocked on a condition variable
  178. *
  179. * int cnd_signal(FAR cnd_t *cond);
  180. */
  181. #define cnd_signal(cond) pthread_cond_signal(cond)
  182. /* cnd_broadcast: unblocks all threads blocked on a condition variable
  183. *
  184. * int cnd_broadcast(FAR cnd_t *cond);
  185. */
  186. #define cnd_broadcast(cond) pthread_cond_broadcast(cond)
  187. /* cnd_wait: blocks on a condition variable
  188. *
  189. * int cnd_wait(FAR cnd_t *cond, FAR mtx_t *mutex);
  190. */
  191. #define cnd_wait(cond,mutex) pthread_cond_wait(cond,mutex)
  192. /* cnd_timedwait: blocks on a condition variable, with a timeout
  193. *
  194. * int cnd_timedwait(FAR cnd_t *cond, FAR mtx_t *mutex,
  195. * FAR const struct timespec* tp);
  196. */
  197. #define cnd_timedwait(cond,mutex,tp) pthread_cond_timedwait(cond,mutex,tp)
  198. /* cnd_destroy: destroys a condition variable
  199. *
  200. * void cnd_destroy(FAR cnd_t *cond);
  201. */
  202. #define cnd_destroy(cond) pthread_cond_destroy(cond)
  203. /* Thread-local storage *****************************************************/
  204. /* tss_create: creates thread-specific storage pointer with a destructor
  205. *
  206. * int tss_create(FAR tss_t *tss_id, tss_dtor_t destructor);
  207. */
  208. #define tss_create(tss_id,destructor) pthread_key_create(tss_id,destructor)
  209. /* tss_get: reads from thread-specific storage
  210. *
  211. * FAR void *tss_get(tss_t tss_id);
  212. */
  213. #define tss_get(tss_id) pthread_getspecific(tss_id)
  214. /* tss_set: write to thread-specific storage
  215. *
  216. * int tss_set(tss_t tss_id, FAR void *value);
  217. */
  218. #define tss_set(tss_id,value) pthread_setspecific(tss_id,value)
  219. /* tss_delete: releases the resources held by a given thread-specific pointer
  220. *
  221. * void tss_delete(tss_t tss_id);
  222. */
  223. #define tss_delete(tss_id) pthread_key_delete(tss_id)
  224. #endif /* __INCLUDE_THREADS_H */