pthread.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * Amazon FreeRTOS POSIX V1.1.0
  3. * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * http://aws.amazon.com/freertos
  23. * http://www.FreeRTOS.org
  24. */
  25. /**
  26. * @file pthread.h
  27. * @brief Threads.
  28. *
  29. * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/pthread.h.html
  30. */
  31. #ifndef _FREERTOS_POSIX_PTHREAD_H_
  32. #define _FREERTOS_POSIX_PTHREAD_H_
  33. /* FreeRTOS+POSIX includes. POSIX states that this header shall make symbols
  34. * defined in sched.h and time.h visible. */
  35. #include "FreeRTOS_POSIX/sched.h"
  36. #include "FreeRTOS_POSIX/time.h"
  37. /**
  38. * @name pthread detach state.
  39. */
  40. /**@{ */
  41. #define PTHREAD_CREATE_DETACHED 0 /**< Detached. */
  42. #define PTHREAD_CREATE_JOINABLE 1 /**< Joinable (default). */
  43. /**@} */
  44. /**
  45. * @name Returned to a single thread after a successful pthread_barrier_wait.
  46. *
  47. * @brief POSIX specifies that "The constant PTHREAD_BARRIER_SERIAL_THREAD is defined in
  48. * <pthread.h> and its value shall be distinct from any other value returned by pthread_barrier_wait()."
  49. * So it's defined as negative to distinguish it from the errnos, which are positive.
  50. */
  51. #define PTHREAD_BARRIER_SERIAL_THREAD ( -2 )
  52. /**
  53. * @name Mutex types.
  54. */
  55. /**@{ */
  56. #ifndef PTHREAD_MUTEX_NORMAL
  57. #define PTHREAD_MUTEX_NORMAL 0 /**< Non-robust, deadlock on relock, does not remember owner. */
  58. #endif
  59. #ifndef PTHREAD_MUTEX_ERRORCHECK
  60. #define PTHREAD_MUTEX_ERRORCHECK 1 /**< Non-robust, error on relock, remembers owner. */
  61. #endif
  62. #ifndef PTHREAD_MUTEX_RECURSIVE
  63. #define PTHREAD_MUTEX_RECURSIVE 2 /**< Non-robust, recursive relock, remembers owner. */
  64. #endif
  65. #ifndef PTHREAD_MUTEX_DEFAULT
  66. #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL /**< PTHREAD_MUTEX_NORMAL (default). */
  67. #endif
  68. /**@} */
  69. /**
  70. * @name Compile-time initializers.
  71. *
  72. * @brief To use PTHREAD_COND_INITIALIZER, posixconfigENABLE_PTHREAD_COND_T needs to be set to 1
  73. * in port specific POSIX config file.
  74. *
  75. * To use PTHREAD_MUTEX_INITIALIZER, posixconfigENABLE_PTHREAD_MUTEX_T needs to be set to 1 in
  76. * port specific POSIX config file.
  77. */
  78. /**@{ */
  79. #if posixconfigENABLE_PTHREAD_COND_T == 1
  80. #define PTHREAD_COND_INITIALIZER FREERTOS_POSIX_COND_INITIALIZER /**< pthread_cond_t. */
  81. #endif
  82. #if posixconfigENABLE_PTHREAD_MUTEX_T == 1
  83. #define PTHREAD_MUTEX_INITIALIZER FREERTOS_POSIX_MUTEX_INITIALIZER /**< pthread_mutex_t. */
  84. #endif
  85. /**@} */
  86. /**
  87. * @brief Destroy the thread attributes object.
  88. *
  89. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_destroy.html
  90. *
  91. * @retval 0 - Upon successful completion.
  92. */
  93. int pthread_attr_destroy( pthread_attr_t * attr );
  94. /**
  95. * @brief Get detachstate attribute.
  96. *
  97. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_getdetachstate.html
  98. *
  99. * @retval 0 - Upon successful completion.
  100. */
  101. int pthread_attr_getdetachstate( const pthread_attr_t * attr,
  102. int * detachstate );
  103. /**
  104. * @brief Get schedparam attribute.
  105. *
  106. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_getschedparam.html
  107. *
  108. * @retval 0 - Upon successful completion.
  109. */
  110. int pthread_attr_getschedparam( const pthread_attr_t * attr,
  111. struct sched_param * param );
  112. /**
  113. * @brief Get stacksize attribute.
  114. *
  115. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_getstacksize.html
  116. *
  117. * @retval 0 - Upon successful completion.
  118. */
  119. int pthread_attr_getstacksize( const pthread_attr_t * attr,
  120. size_t * stacksize );
  121. /**
  122. * @brief Initialize the thread attributes object.
  123. *
  124. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_init.html
  125. *
  126. * @retval 0 - Upon successful completion.
  127. *
  128. * @note Currently, only stack size, sched_param, and detach state attributes
  129. * are supported. Also see pthread_attr_get*() and pthread_attr_set*().
  130. */
  131. int pthread_attr_init( pthread_attr_t * attr );
  132. /**
  133. * @brief Set detachstate attribute.
  134. *
  135. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setdetachstate.html
  136. *
  137. * @retval 0 - Upon successful completion
  138. * @retval EINVAL - The value of detachstate is not valid. Currently, supported detach states are --
  139. * PTHREAD_CREATE_DETACHED and PTHREAD_CREATE_JOINABLE.
  140. */
  141. int pthread_attr_setdetachstate( pthread_attr_t * attr,
  142. int detachstate );
  143. /**
  144. * @brief Set schedparam attribute.
  145. *
  146. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setschedparam.html
  147. *
  148. * @retval 0 - Upon successful completion.
  149. * @retval EINVAL - The value of param is not valid.
  150. * @retval ENOTSUP - An attempt was made to set the attribute to an unsupported value.
  151. */
  152. int pthread_attr_setschedparam( pthread_attr_t * attr,
  153. const struct sched_param * param );
  154. /**
  155. * @brief Set the schedpolicy attribute.
  156. *
  157. * http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setschedpolicy.html
  158. *
  159. * @retval 0 - Upon successful completion.
  160. *
  161. * @warning This function is a stub and always returns 0.
  162. */
  163. int pthread_attr_setschedpolicy( pthread_attr_t * attr,
  164. int policy );
  165. /**
  166. * @brief Set stacksize attribute.
  167. *
  168. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setstacksize.html
  169. *
  170. * @retval 0 - Upon successful completion.
  171. * @retval EINVAL - The value of stacksize is less than {PTHREAD_STACK_MIN}.
  172. */
  173. int pthread_attr_setstacksize( pthread_attr_t * attr,
  174. size_t stacksize );
  175. /**
  176. * @brief Destroy a barrier object.
  177. *
  178. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_barrier_destroy.html
  179. *
  180. * @retval 0 - Upon successful completion.
  181. *
  182. * @note This function does not validate whether there is any thread blocking on the barrier before destroying.
  183. */
  184. int pthread_barrier_destroy( pthread_barrier_t * barrier );
  185. /**
  186. * @brief Initialize a barrier object.
  187. *
  188. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_barrier_init.html
  189. *
  190. * @retval 0 - Upon successful completion.
  191. * @retval EINVAL - The value specified by count is equal to zero.
  192. * @retval ENOMEM - count cannot fit into FreeRTOS event group type OR insufficient memory exists to initialize the barrier.
  193. *
  194. * @note attr is ignored.
  195. *
  196. * @note pthread_barrier_init() is implemented with FreeRTOS event group.
  197. * To ensure count fits in event group, count may be at most 8 when configUSE_16_BIT_TICKS is 1;
  198. * it may be at most 24 otherwise. configUSE_16_BIT_TICKS is configured in application FreeRTOSConfig.h
  199. * file, which defines how many bits tick count type has. See further details and limitation about event
  200. * group and configUSE_16_BIT_TICKS in FreeRTOS site.
  201. */
  202. int pthread_barrier_init( pthread_barrier_t * barrier,
  203. const pthread_barrierattr_t * attr,
  204. unsigned count );
  205. /**
  206. * @brief Synchronize at a barrier.
  207. *
  208. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_barrier_wait.html
  209. *
  210. * @retval PTHREAD_BARRIER_SERIAL_THREAD - Upon successful completion, the first thread.
  211. * @retval 0 - Upon successful completion, other thread(s).
  212. */
  213. int pthread_barrier_wait( pthread_barrier_t * barrier );
  214. /**
  215. * @brief Thread creation.
  216. *
  217. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_create.html
  218. *
  219. * @retval 0 - Upon successful completion.
  220. * @retval EAGAIN - Insufficient memory for either thread structure or task creation.
  221. */
  222. int pthread_create( pthread_t * thread,
  223. const pthread_attr_t * attr,
  224. void *( *startroutine )( void * ),
  225. void * arg );
  226. /**
  227. * @brief Broadcast a condition.
  228. *
  229. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_broadcast.html
  230. *
  231. * @retval 0 - Upon successful completion.
  232. */
  233. int pthread_cond_broadcast( pthread_cond_t * cond );
  234. /**
  235. * @brief Destroy condition variables.
  236. *
  237. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_destroy.html
  238. *
  239. * @retval 0 - Upon successful completion.
  240. */
  241. int pthread_cond_destroy( pthread_cond_t * cond );
  242. /**
  243. * @brief Initialize condition variables.
  244. *
  245. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_init.html
  246. *
  247. * @retval 0 - Upon successful completion.
  248. * @retval ENOMEM - Insufficient memory exists to initialize the condition variable.
  249. *
  250. * @note attr is ignored and treated as NULL. Default setting is always used.
  251. */
  252. int pthread_cond_init( pthread_cond_t * cond,
  253. const pthread_condattr_t * attr );
  254. /**
  255. * @brief Signal a condition.
  256. *
  257. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_signal.html
  258. *
  259. * @retval 0 - Upon successful completion.
  260. */
  261. int pthread_cond_signal( pthread_cond_t * cond );
  262. /**
  263. * @brief Wait on a condition with a timeout.
  264. *
  265. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_timedwait.html
  266. *
  267. * @retval 0 - Upon successful completion.
  268. * @retval EINVAL - The abstime argument passed in does not refer to an initialized structure OR
  269. * the abstime parameter specified a nanoseconds field value less than zero or
  270. * greater than or equal to 1000 million.
  271. * @retval ETIMEDOUT - The time specified by abstime to pthread_cond_timedwait() has passed.
  272. */
  273. int pthread_cond_timedwait( pthread_cond_t * cond,
  274. pthread_mutex_t * mutex,
  275. const struct timespec * abstime );
  276. /**
  277. * @brief Wait on a condition.
  278. *
  279. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_wait.html
  280. *
  281. * @retval 0 - Upon successful completion.
  282. */
  283. int pthread_cond_wait( pthread_cond_t * cond,
  284. pthread_mutex_t * mutex );
  285. /**
  286. * @brief Compare thread IDs.
  287. *
  288. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_equal.html
  289. *
  290. * @retval 0 - t1 and t2 are both not NULL && equal.
  291. * @retval non-zero - otherwise.
  292. */
  293. int pthread_equal( pthread_t t1,
  294. pthread_t t2 );
  295. /**
  296. * @brief Thread termination.
  297. *
  298. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_exit.html
  299. *
  300. * @retval void - this function cannot return to its caller.
  301. */
  302. void pthread_exit( void * value_ptr );
  303. /**
  304. * @brief Dynamic thread scheduling parameters access.
  305. *
  306. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_getschedparam.html
  307. *
  308. * @retval 0 - Upon successful completion.
  309. *
  310. * @note policy is always set to SCHED_OTHER by this function.
  311. */
  312. int pthread_getschedparam( pthread_t thread,
  313. int * policy,
  314. struct sched_param * param );
  315. /**
  316. * @brief Wait for thread termination.
  317. *
  318. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_join.html
  319. *
  320. * @retval 0 - Upon successful completion.
  321. * @retval EDEADLK - The value specified by the thread argument to pthread_join() does not refer
  322. * to a joinable thread OR multiple simultaneous calls to pthread_join()
  323. * specifying the same target thread OR the value specified by the thread argument
  324. * to pthread_join() refers to the calling thread.
  325. */
  326. int pthread_join( pthread_t thread,
  327. void ** retval );
  328. /**
  329. * @brief Destroy a mutex.
  330. *
  331. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_destroy.html
  332. *
  333. * @retval 0 - Upon successful completion.
  334. *
  335. * @note If there exists a thread holding this mutex, this function returns 0 with mutex not being destroyed.
  336. */
  337. int pthread_mutex_destroy( pthread_mutex_t * mutex );
  338. /**
  339. * @brief Initialize a mutex.
  340. *
  341. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_init.html
  342. *
  343. * @retval 0 - Upon successful completion.
  344. * @retval ENOMEM - Insufficient memory exists to initialize the mutex structure.
  345. * @retval EAGAIN - Unable to initialize the mutex structure member(s).
  346. */
  347. int pthread_mutex_init( pthread_mutex_t * mutex,
  348. const pthread_mutexattr_t * attr );
  349. /**
  350. * @brief Lock a mutex.
  351. *
  352. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_lock.html
  353. *
  354. * @retval 0 - Upon successful completion.
  355. * @retval EINVAL - the abstime parameter specified a nanoseconds field value less than zero
  356. * or greater than or equal to 1000 million.
  357. * @retval EDEADLK - The mutex type is PTHREAD_MUTEX_ERRORCHECK and the current thread already
  358. * owns the mutex.
  359. */
  360. int pthread_mutex_lock( pthread_mutex_t * mutex );
  361. /**
  362. * @brief Lock a mutex with timeout.
  363. *
  364. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_timedlock.html
  365. *
  366. * @retval 0 - Upon successful completion.
  367. * @retval EINVAL - The abstime argument passed in does not refer to an initialized structure OR
  368. * the abstime parameter specified a nanoseconds field value less than zero or
  369. * greater than or equal to 1000 million.
  370. * @retval EDEADLK - The mutex type is PTHREAD_MUTEX_ERRORCHECK and the current thread already owns the mutex.
  371. * @retval ETIMEDOUT - The mutex could not be locked before the specified timeout expired.
  372. */
  373. int pthread_mutex_timedlock( pthread_mutex_t * mutex,
  374. const struct timespec * abstime );
  375. /**
  376. * @brief Attempt to lock a mutex. Fail immediately if mutex is already locked.
  377. *
  378. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_trylock.html
  379. *
  380. * @retval 0 - Upon successful completion.
  381. * @retval EINVAL - the abstime parameter specified a nanoseconds field value less than zero
  382. * or greater than or equal to 1000 million.
  383. * @retval EDEADLK - The mutex type is PTHREAD_MUTEX_ERRORCHECK and the current thread already
  384. * owns the mutex.
  385. * @retval EBUSY - The mutex could not be acquired because it was already locked.
  386. */
  387. int pthread_mutex_trylock( pthread_mutex_t * mutex );
  388. /**
  389. * @brief Unlock a mutex.
  390. *
  391. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_unlock.html
  392. *
  393. * @retval 0 - Upon successful completion.
  394. * @retval EPERM - The mutex type is PTHREAD_MUTEX_ERRORCHECK or PTHREAD_MUTEX_RECURSIVE, and
  395. * the current thread does not own the mutex.
  396. */
  397. int pthread_mutex_unlock( pthread_mutex_t * mutex );
  398. /**
  399. * @brief Destroy the mutex attributes object.
  400. *
  401. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutexattr_destroy.html
  402. *
  403. * @retval 0 - Upon successful completion.
  404. */
  405. int pthread_mutexattr_destroy( pthread_mutexattr_t * attr );
  406. /**
  407. * @brief Get the mutex type attribute.
  408. *
  409. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutexattr_gettype.html
  410. *
  411. * @retval 0 - Upon successful completion.
  412. */
  413. int pthread_mutexattr_gettype( const pthread_mutexattr_t * attr,
  414. int * type );
  415. /**
  416. * @brief Initialize the mutex attributes object.
  417. *
  418. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutexattr_init.html
  419. *
  420. * @retval 0 - Upon successful completion.
  421. *
  422. * @note Currently, only the type attribute is supported. Also see pthread_mutexattr_settype()
  423. * and pthread_mutexattr_gettype().
  424. */
  425. int pthread_mutexattr_init( pthread_mutexattr_t * attr );
  426. /**
  427. * @brief Set the mutex type attribute.
  428. *
  429. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutexattr_settype.html
  430. *
  431. * @retval 0 - Upon successful completion.
  432. * @retval EINVAL - The value type is invalid.
  433. */
  434. int pthread_mutexattr_settype( pthread_mutexattr_t * attr,
  435. int type );
  436. /**
  437. * @brief Get the calling thread ID.
  438. *
  439. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_self.html
  440. *
  441. * @retval the thread ID of the calling thread.
  442. */
  443. pthread_t pthread_self( void );
  444. /**
  445. * @brief Dynamic thread scheduling parameters access.
  446. *
  447. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_setschedparam.html
  448. *
  449. * @note policy is ignored; only priority (param.sched_priority) may be changed.
  450. *
  451. * @retval 0 - Upon successful completion.
  452. */
  453. int pthread_setschedparam( pthread_t thread,
  454. int policy,
  455. const struct sched_param * param );
  456. #endif /* _FREERTOS_POSIX_PTHREAD_H_ */