pthread.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /********************************************************************************
  2. * include/pthread.h
  3. *
  4. * Copyright (C) 2007-2009, 2011-2012, 2015-2017, 2019 Gregory Nutt. All
  5. * rights reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ********************************************************************************/
  36. #ifndef __INCLUDE_PTHREAD_H
  37. #define __INCLUDE_PTHREAD_H
  38. /********************************************************************************
  39. * Included Files
  40. ********************************************************************************/
  41. #include <nuttx/config.h> /* Default settings */
  42. #include <nuttx/compiler.h> /* Compiler settings, noreturn_function */
  43. #include <sys/types.h> /* Needed for general types */
  44. #include <sys/prctl.h> /* Needed by pthread_[set|get]name_np */
  45. #include <stdint.h> /* C99 fixed width integer types */
  46. #include <stdbool.h> /* C99 boolean types */
  47. #include <unistd.h> /* For getpid */
  48. #include <signal.h> /* Needed for sigset_t, includes this file */
  49. #include <time.h> /* Needed for struct timespec */
  50. #include <nuttx/semaphore.h> /* For sem_t and SEM_PRIO_* defines */
  51. #ifdef CONFIG_PTHREAD_SPINLOCKS
  52. /* The architecture specific spinlock.h header file must provide the
  53. * following:
  54. *
  55. * SP_LOCKED - A definition of the locked state value (usually 1)
  56. * SP_UNLOCKED - A definition of the unlocked state value (usually 0)
  57. * spinlock_t - The type of a spinlock memory object.
  58. *
  59. * SP_LOCKED and SP_UNLOCKED must constants of type spinlock_t.
  60. */
  61. # include <arch/spinlock.h>
  62. #endif
  63. /********************************************************************************
  64. * Pre-processor Definitions
  65. ********************************************************************************/
  66. /* Standard POSIX switches */
  67. #ifndef _POSIX_THREADS
  68. # define _POSIX_THREADS
  69. #endif
  70. #ifndef _POSIX_THREAD_ATTR_STACKSIZE
  71. # define _POSIX_THREAD_ATTR_STACKSIZE
  72. #endif
  73. /* Values for the process shared (pshared) attribute */
  74. #define PTHREAD_PROCESS_PRIVATE 0
  75. #define PTHREAD_PROCESS_SHARED 1
  76. /* Values for the mutext type attribute:
  77. *
  78. * PTHREAD_MUTEX_NORMAL: This type of mutex does not detect deadlock. A thread
  79. * attempting to relock this mutex without first unlocking it will deadlock.
  80. * Attempting to unlock a mutex locked by a different thread results in
  81. * undefined behavior. Attempting to unlock an unlocked mutex results in
  82. * undefined behavior.
  83. * PTHREAD_MUTEX_ERRORCHECK
  84. * This type of mutex provides error checking. A thread attempting to relock
  85. * this mutex without first unlocking it will return with an error. A thread
  86. * attempting to unlock a mutex which another thread has locked will return
  87. * with an error. A thread attempting to unlock an unlocked mutex will return
  88. * with an error.
  89. * PTHREAD_MUTEX_RECURSIVE
  90. * A thread attempting to relock this mutex without first unlocking it will
  91. * succeed in locking the mutex. The relocking deadlock which can occur with
  92. * mutexes of type PTHREAD_MUTEX_NORMAL cannot occur with this type of mutex.
  93. * Multiple locks of this mutex require the same number of unlocks to release
  94. * the mutex before another thread can acquire the mutex. A thread attempting
  95. * to unlock a mutex which another thread has locked will return with an error.
  96. * A thread attempting to unlock an unlocked mutex will return with an error.
  97. * PTHREAD_MUTEX_DEFAULT
  98. * An implementation is allowed to map this mutex to one of the other mutex
  99. * types.
  100. */
  101. #define PTHREAD_MUTEX_NORMAL 0
  102. #define PTHREAD_MUTEX_ERRORCHECK 1
  103. #define PTHREAD_MUTEX_RECURSIVE 2
  104. #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
  105. /* Valid ranges for the pthread stacksize attribute */
  106. #define PTHREAD_STACK_MIN CONFIG_PTHREAD_STACK_MIN
  107. #define PTHREAD_STACK_DEFAULT CONFIG_PTHREAD_STACK_DEFAULT
  108. /* Values for the pthread inheritsched attribute */
  109. #define PTHREAD_INHERIT_SCHED 0
  110. #define PTHREAD_EXPLICIT_SCHED 1
  111. /* Detach state */
  112. #define PTHREAD_CREATE_JOINABLE 0
  113. #define PTHREAD_CREATE_DETACHED 1
  114. /* Default priority */
  115. #define PTHREAD_DEFAULT_PRIORITY 100
  116. /* Cancellation states used by pthread_setcancelstate() */
  117. #define PTHREAD_CANCEL_ENABLE (0)
  118. #define PTHREAD_CANCEL_DISABLE (1)
  119. /* Cancellation types used by pthread_setcanceltype() */
  120. #define PTHREAD_CANCEL_DEFERRED (0)
  121. #define PTHREAD_CANCEL_ASYNCHRONOUS (1)
  122. /* Thread return value when a pthread is canceled */
  123. #define PTHREAD_CANCELED ((FAR void*)ERROR)
  124. /* Used to initialize a pthread_once_t */
  125. #define PTHREAD_ONCE_INIT (false)
  126. /* This is returned by pthread_barrier_wait. It must not match any errno
  127. * in errno.h
  128. */
  129. #define PTHREAD_BARRIER_SERIAL_THREAD 0x1000
  130. /* Values for protocol mutex attribute */
  131. #define PTHREAD_PRIO_NONE SEM_PRIO_NONE
  132. #define PTHREAD_PRIO_INHERIT SEM_PRIO_INHERIT
  133. #define PTHREAD_PRIO_PROTECT SEM_PRIO_PROTECT
  134. /* Values for robust argument of pthread_mutexattr_get/setrobust
  135. *
  136. * PTHREAD_MUTEX_STALLED - No special actions are taken if the owner of the
  137. * mutex is terminated while holding the mutex lock. This can lead to
  138. * deadlocks if no other thread can unlock the mutex. This is the standard
  139. * default value (NuttX permits you to override that default behavior
  140. * with a configuration option).
  141. *
  142. * PTHREAD_MUTEX_ROBUST - If the process containing the owning thread of a
  143. * robust mutex terminates while holding the mutex lock, the next thread
  144. * that acquires the mutex will be notified about the termination by the
  145. * return value EOWNERDEAD from the locking function. If the owning thread
  146. * of a robust mutex terminates while holding the mutex lock, the next
  147. * thread that attempts to acquire the mutex may be notified about the
  148. * termination by the return value EOWNERDEAD. The notified thread can
  149. * then attempt to make the state protected by the mutex consistent again,
  150. * and if successful can mark the mutex state as consistent by calling
  151. * pthread_mutex_consistent(). After a subsequent successful call to
  152. * pthread_mutex_unlock(), the mutex lock will be released and can be used
  153. * normally by other threads. If the mutex is unlocked without a call to
  154. * pthread_mutex_consistent(), it will be in a permanently unusable state
  155. * and all attempts to lock the mutex will fail with the error
  156. * ENOTRECOVERABLE. The only permissible operation on such a mutex is
  157. * pthread_mutex_destroy().
  158. */
  159. #define PTHREAD_MUTEX_STALLED 0
  160. #define PTHREAD_MUTEX_ROBUST 1
  161. /* Values for struct pthread_mutex_s flags. These are non-standard and
  162. * intended only for internal use within the OS.
  163. */
  164. #define _PTHREAD_MFLAGS_ROBUST (1 << 0) /* Robust (NORMAL) mutex */
  165. #define _PTHREAD_MFLAGS_INCONSISTENT (1 << 1) /* Mutex is in an inconsistent state */
  166. #define _PTHREAD_MFLAGS_NRECOVERABLE (1 << 2) /* Inconsistent mutex has been unlocked */
  167. /* Definitions to map some non-standard, BSD thread management interfaces to
  168. * the non-standard Linux-like prctl() interface. Since these are simple
  169. * mappings to prctl, they will return 0 on success and -1 on failure with the
  170. * error number in errno. This is an inconsistency with the pthread interfaces.
  171. */
  172. #define pthread_setname_np(thread, name) \
  173. prctl((int)PR_SET_NAME_EXT, (char*)name, (int)thread)
  174. #define pthread_getname_np(thread, name) \
  175. prctl((int)PR_GET_NAME_EXT, (char*)name, (int)thread)
  176. /********************************************************************************
  177. * Public Type Definitions
  178. ********************************************************************************/
  179. #ifdef __cplusplus
  180. extern "C"
  181. {
  182. #endif
  183. /* pthread-specific types */
  184. #ifndef __PTHREAD_KEY_T_DEFINED
  185. typedef int pthread_key_t;
  186. #define __PTHREAD_KEY_T_DEFINED 1
  187. #endif
  188. #ifndef __PTHREAD_ADDR_T_DEFINED
  189. typedef FAR void *pthread_addr_t;
  190. #define __PTHREAD_ADDR_T_DEFINED 1
  191. #endif
  192. typedef CODE pthread_addr_t (*pthread_startroutine_t)(pthread_addr_t);
  193. typedef pthread_startroutine_t pthread_func_t;
  194. struct pthread_attr_s
  195. {
  196. uint8_t priority; /* Priority of the pthread */
  197. uint8_t policy; /* Pthread scheduler policy */
  198. uint8_t inheritsched; /* Inherit parent priority/policy? */
  199. uint8_t detachstate; /* Initialize to the detach state */
  200. #ifdef CONFIG_SCHED_SPORADIC
  201. uint8_t low_priority; /* Low scheduling priority */
  202. uint8_t max_repl; /* Maximum pending replenishments */
  203. #endif
  204. #ifdef CONFIG_SMP
  205. cpu_set_t affinity; /* Set of permitted CPUs for the thread */
  206. #endif
  207. FAR void *stackaddr; /* Address of memory to be used as stack */
  208. size_t stacksize; /* Size of the stack allocated for the pthread */
  209. #ifdef CONFIG_SCHED_SPORADIC
  210. struct timespec repl_period; /* Replenishment period */
  211. struct timespec budget; /* Initial budget */
  212. #endif
  213. };
  214. #ifndef __PTHREAD_ATTR_T_DEFINED
  215. typedef struct pthread_attr_s pthread_attr_t;
  216. #define __PTHREAD_ATTR_T_DEFINED 1
  217. #endif
  218. #ifndef __PTHREAD_T_DEFINED
  219. typedef pid_t pthread_t;
  220. #define __PTHREAD_T_DEFINED 1
  221. #endif
  222. struct pthread_condattr_s
  223. {
  224. clockid_t clockid;
  225. };
  226. #ifndef __PTHREAD_CONDATTR_T_DEFINED
  227. typedef struct pthread_condattr_s pthread_condattr_t;
  228. #define __PTHREAD_CONDATTR_T_DEFINED 1
  229. #endif
  230. struct pthread_cond_s
  231. {
  232. sem_t sem;
  233. clockid_t clockid;
  234. };
  235. #ifndef __PTHREAD_COND_T_DEFINED
  236. typedef struct pthread_cond_s pthread_cond_t;
  237. #define __PTHREAD_COND_T_DEFINED 1
  238. #endif
  239. #define PTHREAD_COND_INITIALIZER {SEM_INITIALIZER(0), CLOCK_REALTIME }
  240. struct pthread_mutexattr_s
  241. {
  242. uint8_t pshared : 1; /* PTHREAD_PROCESS_PRIVATE or PTHREAD_PROCESS_SHARED */
  243. #ifdef CONFIG_PRIORITY_INHERITANCE
  244. uint8_t proto : 2; /* See PTHREAD_PRIO_* definitions */
  245. #endif
  246. #ifdef CONFIG_PTHREAD_MUTEX_TYPES
  247. uint8_t type : 2; /* Type of the mutex. See PTHREAD_MUTEX_* definitions */
  248. #endif
  249. #ifdef CONFIG_PTHREAD_MUTEX_BOTH
  250. uint8_t robust : 1; /* PTHREAD_MUTEX_STALLED or PTHREAD_MUTEX_ROBUST */
  251. #endif
  252. };
  253. #ifndef __PTHREAD_MUTEXATTR_T_DEFINED
  254. typedef struct pthread_mutexattr_s pthread_mutexattr_t;
  255. #define __PTHREAD_MUTEXATTR_T_DEFINED 1
  256. #endif
  257. struct pthread_mutex_s
  258. {
  259. #ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
  260. /* Supports a singly linked list */
  261. FAR struct pthread_mutex_s *flink;
  262. #endif
  263. /* Payload */
  264. sem_t sem; /* Semaphore underlying the implementation of the mutex */
  265. pid_t pid; /* ID of the holder of the mutex */
  266. #ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
  267. uint8_t flags; /* See _PTHREAD_MFLAGS_* */
  268. #endif
  269. #ifdef CONFIG_PTHREAD_MUTEX_TYPES
  270. uint8_t type; /* Type of the mutex. See PTHREAD_MUTEX_* definitions */
  271. int16_t nlocks; /* The number of recursive locks held */
  272. #endif
  273. };
  274. #ifndef __PTHREAD_MUTEX_T_DEFINED
  275. typedef struct pthread_mutex_s pthread_mutex_t;
  276. #define __PTHREAD_MUTEX_T_DEFINED 1
  277. #endif
  278. #ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
  279. # ifdef CONFIG_PTHREAD_MUTEX_DEFAULT_UNSAFE
  280. # define __PTHREAD_MUTEX_DEFAULT_FLAGS 0
  281. # else
  282. # define __PTHREAD_MUTEX_DEFAULT_FLAGS _PTHREAD_MFLAGS_ROBUST
  283. # endif
  284. #endif
  285. #if defined(CONFIG_PTHREAD_MUTEX_TYPES) && !defined(CONFIG_PTHREAD_MUTEX_UNSAFE)
  286. # define PTHREAD_MUTEX_INITIALIZER {NULL, SEM_INITIALIZER(1), -1, \
  287. __PTHREAD_MUTEX_DEFAULT_FLAGS, \
  288. PTHREAD_MUTEX_DEFAULT, 0}
  289. # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
  290. {NULL, SEM_INITIALIZER(1), -1, \
  291. __PTHREAD_MUTEX_DEFAULT_FLAGS, \
  292. PTHREAD_MUTEX_RECURSIVE, 0}
  293. #elif defined(CONFIG_PTHREAD_MUTEX_TYPES)
  294. # define PTHREAD_MUTEX_INITIALIZER {SEM_INITIALIZER(1), -1, \
  295. PTHREAD_MUTEX_DEFAULT, 0}
  296. # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
  297. {SEM_INITIALIZER(1), -1, \
  298. PTHREAD_MUTEX_RECURSIVE, 0}
  299. #elif !defined(CONFIG_PTHREAD_MUTEX_UNSAFE)
  300. # define PTHREAD_MUTEX_INITIALIZER {NULL, SEM_INITIALIZER(1), -1,\
  301. __PTHREAD_MUTEX_DEFAULT_FLAGS}
  302. #else
  303. # define PTHREAD_MUTEX_INITIALIZER {SEM_INITIALIZER(1), -1}
  304. #endif
  305. struct pthread_barrierattr_s
  306. {
  307. int pshared;
  308. };
  309. #ifndef __PTHREAD_BARRIERATTR_T_DEFINED
  310. typedef struct pthread_barrierattr_s pthread_barrierattr_t;
  311. #define __PTHREAD_BARRIERATTR_T_DEFINED 1
  312. #endif
  313. struct pthread_barrier_s
  314. {
  315. sem_t sem;
  316. unsigned int count;
  317. };
  318. #ifndef __PTHREAD_BARRIER_T_DEFINED
  319. typedef struct pthread_barrier_s pthread_barrier_t;
  320. #define __PTHREAD_BARRIER_T_DEFINED 1
  321. #endif
  322. #ifndef __PTHREAD_ONCE_T_DEFINED
  323. typedef bool pthread_once_t;
  324. #define __PTHREAD_ONCE_T_DEFINED 1
  325. #endif
  326. struct pthread_rwlock_s
  327. {
  328. pthread_mutex_t lock;
  329. pthread_cond_t cv;
  330. unsigned int num_readers;
  331. unsigned int num_writers;
  332. bool write_in_progress;
  333. };
  334. typedef struct pthread_rwlock_s pthread_rwlock_t;
  335. typedef int pthread_rwlockattr_t;
  336. #define PTHREAD_RWLOCK_INITIALIZER {PTHREAD_MUTEX_INITIALIZER, \
  337. PTHREAD_COND_INITIALIZER, \
  338. 0, 0, false}
  339. #ifdef CONFIG_PTHREAD_SPINLOCKS
  340. #ifndef __PTHREAD_SPINLOCK_T_DEFINED
  341. /* This (non-standard) structure represents a pthread spinlock */
  342. struct pthread_spinlock_s
  343. {
  344. volatile spinlock_t sp_lock; /* Indicates if the spinlock is locked or
  345. * not. See the values SP_LOCKED and
  346. * SP_UNLOCKED. */
  347. pthread_t sp_holder; /* ID of the thread that holds the spinlock */
  348. };
  349. /* It is referenced via this standard type */
  350. typedef FAR struct pthread_spinlock_s pthread_spinlock_t;
  351. #define __PTHREAD_SPINLOCK_T_DEFINED 1
  352. #endif
  353. #endif /* CONFIG_PTHREAD_SPINLOCKS */
  354. #ifdef CONFIG_PTHREAD_CLEANUP
  355. /* This type describes the pthread cleanup callback (non-standard) */
  356. typedef CODE void (*pthread_cleanup_t)(FAR void *arg);
  357. #endif
  358. /* Forward references */
  359. struct sched_param; /* Defined in sched.h */
  360. /********************************************************************************
  361. * Public Function Prototypes
  362. ********************************************************************************/
  363. /* Initializes a thread attributes object (attr) with default values for all of
  364. * the individual attributes used by a given implementation.
  365. */
  366. int pthread_attr_init(FAR pthread_attr_t *attr);
  367. /* An attributes object can be deleted when it is no longer needed. */
  368. int pthread_attr_destroy(FAR pthread_attr_t *attr);
  369. /* Set or obtain the default scheduling algorithm */
  370. int pthread_attr_setschedpolicy(FAR pthread_attr_t *attr, int policy);
  371. int pthread_attr_getschedpolicy(FAR const pthread_attr_t *attr, FAR int *policy);
  372. int pthread_attr_setschedparam(FAR pthread_attr_t *attr,
  373. FAR const struct sched_param *param);
  374. int pthread_attr_getschedparam(FAR const pthread_attr_t *attr,
  375. FAR struct sched_param *param);
  376. int pthread_attr_setinheritsched(FAR pthread_attr_t *attr,
  377. int inheritsched);
  378. int pthread_attr_getinheritsched(FAR const pthread_attr_t *attr,
  379. FAR int *inheritsched);
  380. int pthread_attr_getdetachstate(FAR const pthread_attr_t *attr,
  381. FAR int *detachstate);
  382. int pthread_attr_setdetachstate(FAR pthread_attr_t *attr,
  383. int detachstate);
  384. #ifdef CONFIG_SMP
  385. /* Set or obtain thread affinity attributes */
  386. int pthread_attr_setaffinity_np(FAR pthread_attr_t *attr,
  387. size_t cpusetsize,
  388. FAR const cpu_set_t *cpuset);
  389. int pthread_attr_getaffinity_np(FAR const pthread_attr_t *attr,
  390. size_t cpusetsize, cpu_set_t *cpuset);
  391. #endif
  392. /* Set or obtain the default stack size */
  393. int pthread_attr_setstacksize(FAR pthread_attr_t *attr, size_t stacksize);
  394. int pthread_attr_getstacksize(FAR const pthread_attr_t *attr,
  395. FAR size_t *stackaddr);
  396. /* Set or obtain stack address and size attributes */
  397. int pthread_attr_setstack(FAR pthread_attr_t *attr,
  398. FAR void *stackaddr, long stacksize);
  399. int pthread_attr_getstack(FAR pthread_attr_t *attr,
  400. FAR void **stackaddr, FAR long *stacksize);
  401. /* Get run-time stack address and size */
  402. FAR void *pthread_get_stackaddr_np(pthread_t thread);
  403. ssize_t pthread_get_stacksize_np(pthread_t thread);
  404. /* To create a thread object and runnable thread, a routine must be specified
  405. * as the new thread's start routine. An argument may be passed to this
  406. * routine, as an untyped address; an untyped address may also be returned as
  407. * the routine's value. An attributes object may be used to specify details
  408. * about the kind of thread being created.
  409. */
  410. int pthread_create(FAR pthread_t *thread, FAR const pthread_attr_t *attr,
  411. pthread_startroutine_t startroutine, pthread_addr_t arg);
  412. /* A thread object may be "detached" to specify that the return value and
  413. * completion status will not be requested.
  414. */
  415. int pthread_detach(pthread_t thread);
  416. /* A thread may terminate it's own execution or the execution of another
  417. * thread.
  418. */
  419. void pthread_exit(pthread_addr_t value) noreturn_function;
  420. int pthread_cancel(pthread_t thread);
  421. int pthread_setcancelstate(int state, FAR int *oldstate);
  422. int pthread_setcanceltype(int type, FAR int *oldtype);
  423. void pthread_testcancel(void);
  424. /* A thread may set up cleanup functions to execute when the thread exits or
  425. * is canceled.
  426. */
  427. #ifdef CONFIG_PTHREAD_CLEANUP
  428. void pthread_cleanup_pop(int execute);
  429. void pthread_cleanup_push(pthread_cleanup_t routine, FAR void *arg);
  430. #endif
  431. /* A thread can await termination of another thread and retrieve the return
  432. * value of the thread.
  433. */
  434. int pthread_join(pthread_t thread, FAR pthread_addr_t *value);
  435. /* A thread may tell the scheduler that its processor can be made available. */
  436. void pthread_yield(void);
  437. /* A thread may obtain a copy of its own thread handle. */
  438. #define pthread_self() ((pthread_t)getpid())
  439. /* Compare two thread IDs. */
  440. #define pthread_equal(t1,t2) ((t1) == (t2))
  441. /* Thread scheduling parameters */
  442. int pthread_getschedparam(pthread_t thread, FAR int *policy,
  443. FAR struct sched_param *param);
  444. int pthread_setschedparam(pthread_t thread, int policy,
  445. FAR const struct sched_param *param);
  446. int pthread_setschedprio(pthread_t thread, int prio);
  447. #ifdef CONFIG_SMP
  448. /* Thread affinity */
  449. int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize,
  450. FAR const cpu_set_t *cpuset);
  451. int pthread_getaffinity_np(pthread_t thread, size_t cpusetsize,
  452. FAR cpu_set_t *cpuset);
  453. #endif
  454. /* Thread-specific Data Interfaces */
  455. int pthread_key_create(FAR pthread_key_t *key,
  456. CODE void (*destructor)(FAR void *));
  457. int pthread_setspecific(pthread_key_t key, FAR const void *value);
  458. FAR void *pthread_getspecific(pthread_key_t key);
  459. int pthread_key_delete(pthread_key_t key);
  460. /* Create, operate on, and destroy mutex attributes. */
  461. int pthread_mutexattr_init(FAR pthread_mutexattr_t *attr);
  462. int pthread_mutexattr_destroy(FAR pthread_mutexattr_t *attr);
  463. int pthread_mutexattr_getpshared(FAR const pthread_mutexattr_t *attr,
  464. FAR int *pshared);
  465. int pthread_mutexattr_setpshared(FAR pthread_mutexattr_t *attr,
  466. int pshared);
  467. int pthread_mutexattr_gettype(FAR const pthread_mutexattr_t *attr,
  468. FAR int *type);
  469. int pthread_mutexattr_settype(FAR pthread_mutexattr_t *attr, int type);
  470. int pthread_mutexattr_getprotocol(FAR const pthread_mutexattr_t *attr,
  471. FAR int *protocol);
  472. int pthread_mutexattr_setprotocol(FAR pthread_mutexattr_t *attr,
  473. int protocol);
  474. int pthread_mutexattr_getrobust(FAR const pthread_mutexattr_t *attr,
  475. FAR int *robust);
  476. int pthread_mutexattr_setrobust(FAR pthread_mutexattr_t *attr,
  477. int robust);
  478. /* The following routines create, delete, lock and unlock mutexes. */
  479. int pthread_mutex_init(FAR pthread_mutex_t *mutex,
  480. FAR const pthread_mutexattr_t *attr);
  481. int pthread_mutex_destroy(FAR pthread_mutex_t *mutex);
  482. int pthread_mutex_lock(FAR pthread_mutex_t *mutex);
  483. int pthread_mutex_timedlock(FAR pthread_mutex_t *mutex,
  484. FAR const struct timespec *abs_timeout);
  485. int pthread_mutex_trylock(FAR pthread_mutex_t *mutex);
  486. int pthread_mutex_unlock(FAR pthread_mutex_t *mutex);
  487. #ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
  488. /* Make sure that the pthread mutex is in a consistent state */
  489. int pthread_mutex_consistent(FAR pthread_mutex_t *mutex);
  490. #endif
  491. /* Operations on condition variables */
  492. int pthread_condattr_init(FAR pthread_condattr_t *attr);
  493. int pthread_condattr_destroy(FAR pthread_condattr_t *attr);
  494. int pthread_condattr_getclock(FAR const pthread_condattr_t *attr,
  495. clockid_t *clock_id);
  496. int pthread_condattr_setclock(FAR pthread_condattr_t *attr,
  497. clockid_t clock_id);
  498. /* A thread can create and delete condition variables. */
  499. int pthread_cond_init(FAR pthread_cond_t *cond,
  500. FAR const pthread_condattr_t *attr);
  501. int pthread_cond_destroy(FAR pthread_cond_t *cond);
  502. /* A thread can signal to and broadcast on a condition variable. */
  503. int pthread_cond_broadcast(FAR pthread_cond_t *cond);
  504. int pthread_cond_signal(FAR pthread_cond_t *cond);
  505. /* A thread can wait for a condition variable to be signalled or broadcast. */
  506. int pthread_cond_wait(FAR pthread_cond_t *cond, FAR pthread_mutex_t *mutex);
  507. /* A thread can perform a timed wait on a condition variable. */
  508. int pthread_cond_timedwait(FAR pthread_cond_t *cond,
  509. FAR pthread_mutex_t *mutex,
  510. FAR const struct timespec *abstime);
  511. int pthread_cond_clockwait(FAR pthread_cond_t *cond,
  512. FAR pthread_mutex_t *mutex,
  513. clockid_t clockid,
  514. FAR const struct timespec *abstime);
  515. /* Barrier attributes */
  516. int pthread_barrierattr_destroy(FAR pthread_barrierattr_t *attr);
  517. int pthread_barrierattr_init(FAR pthread_barrierattr_t *attr);
  518. int pthread_barrierattr_getpshared(FAR const pthread_barrierattr_t *attr,
  519. FAR int *pshared);
  520. int pthread_barrierattr_setpshared(FAR pthread_barrierattr_t *attr,
  521. int pshared);
  522. /* Barriers */
  523. int pthread_barrier_destroy(FAR pthread_barrier_t *barrier);
  524. int pthread_barrier_init(FAR pthread_barrier_t *barrier,
  525. FAR const pthread_barrierattr_t *attr,
  526. unsigned int count);
  527. int pthread_barrier_wait(FAR pthread_barrier_t *barrier);
  528. /* Pthread initialization */
  529. int pthread_once(FAR pthread_once_t *once_control,
  530. CODE void (*init_routine)(void));
  531. /* Pthread rwlock */
  532. int pthread_rwlock_destroy(FAR pthread_rwlock_t *rw_lock);
  533. int pthread_rwlock_init(FAR pthread_rwlock_t *rw_lock,
  534. FAR const pthread_rwlockattr_t *attr);
  535. int pthread_rwlock_rdlock(pthread_rwlock_t *lock);
  536. int pthread_rwlock_timedrdlock(FAR pthread_rwlock_t *lock,
  537. FAR const struct timespec *abstime);
  538. int pthread_rwlock_clockrdlock(FAR pthread_rwlock_t *lock,
  539. clockid_t clockid,
  540. FAR const struct timespec *abstime);
  541. int pthread_rwlock_tryrdlock(FAR pthread_rwlock_t *lock);
  542. int pthread_rwlock_wrlock(FAR pthread_rwlock_t *lock);
  543. int pthread_rwlock_timedwrlock(FAR pthread_rwlock_t *lock,
  544. FAR const struct timespec *abstime);
  545. int pthread_rwlock_clockwrlock(FAR pthread_rwlock_t *lock,
  546. clockid_t clockid,
  547. FAR const struct timespec *abstime);
  548. int pthread_rwlock_trywrlock(FAR pthread_rwlock_t *lock);
  549. int pthread_rwlock_unlock(FAR pthread_rwlock_t *lock);
  550. /* Pthread signal management APIs */
  551. int pthread_kill(pthread_t thread, int sig);
  552. int pthread_sigmask(int how, FAR const sigset_t *set, FAR sigset_t *oset);
  553. #ifdef CONFIG_PTHREAD_SPINLOCKS
  554. /* Pthread spinlocks */
  555. int pthread_spin_init(FAR pthread_spinlock_t *lock, int pshared);
  556. int pthread_spin_destroy(FAR pthread_spinlock_t *lock);
  557. int pthread_spin_lock(FAR pthread_spinlock_t *lock);
  558. int pthread_spin_trylock(FAR pthread_spinlock_t *lock);
  559. int pthread_spin_unlock(FAR pthread_spinlock_t *lock);
  560. #endif
  561. #ifdef __cplusplus
  562. }
  563. #endif
  564. /********************************************************************************
  565. * Minimal Type Definitions
  566. ********************************************************************************/
  567. #else /* __INCLUDE_PTHREAD_H */
  568. /********************************************************************************
  569. * Included Files
  570. ********************************************************************************/
  571. #include <sys/types.h>
  572. #include <stdbool.h>
  573. /********************************************************************************
  574. * Public Type Definitions
  575. ********************************************************************************/
  576. /* Avoid circular dependencies by assuring that simple type definitions are
  577. * available in any inclusion ordering.
  578. */
  579. #ifndef __PTHREAD_KEY_T_DEFINED
  580. typedef int pthread_key_t;
  581. # define __PTHREAD_KEY_T_DEFINED 1
  582. #endif
  583. #ifndef __PTHREAD_ADDR_T_DEFINED
  584. typedef FAR void *pthread_addr_t;
  585. # define __PTHREAD_ADDR_T_DEFINED 1
  586. #endif
  587. #ifndef __PTHREAD_ATTR_T_DEFINED
  588. struct pthread_attr_s;
  589. typedef struct pthread_attr_s pthread_attr_t;
  590. # define __PTHREAD_ATTR_T_DEFINED 1
  591. #endif
  592. #ifndef __PTHREAD_T_DEFINED
  593. typedef pid_t pthread_t;
  594. # define __PTHREAD_T_DEFINED 1
  595. #endif
  596. #ifndef __PTHREAD_CONDATTR_T_DEFINED
  597. typedef struct pthread_condattr_s pthread_condattr_t;
  598. # define __PTHREAD_CONDATTR_T_DEFINED 1
  599. #endif
  600. #ifndef __PTHREAD_COND_T_DEFINED
  601. struct pthread_cond_s;
  602. typedef struct pthread_cond_s pthread_cond_t;
  603. # define __PTHREAD_COND_T_DEFINED 1
  604. #endif
  605. #ifndef __PTHREAD_MUTEXATTR_T_DEFINED
  606. struct pthread_mutexattr_s;
  607. typedef struct pthread_mutexattr_s pthread_mutexattr_t;
  608. # define __PTHREAD_MUTEXATTR_T_DEFINED 1
  609. #endif
  610. #ifndef __PTHREAD_MUTEX_T_DEFINED
  611. struct pthread_mutex_s;
  612. typedef struct pthread_mutex_s pthread_mutex_t;
  613. # define __PTHREAD_MUTEX_T_DEFINED 1
  614. #endif
  615. #ifndef __PTHREAD_BARRIERATTR_T_DEFINED
  616. struct pthread_barrierattr_s;
  617. typedef struct pthread_barrierattr_s pthread_barrierattr_t;
  618. # define __PTHREAD_BARRIERATTR_T_DEFINED 1
  619. #endif
  620. #ifndef __PTHREAD_BARRIER_T_DEFINED
  621. struct pthread_barrier_s;
  622. typedef struct pthread_barrier_s pthread_barrier_t;
  623. # define __PTHREAD_BARRIER_T_DEFINED 1
  624. #endif
  625. #ifdef CONFIG_PTHREAD_SPINLOCKS
  626. #ifndef __PTHREAD_SPINLOCK_T_DEFINED
  627. struct pthread_spinlock_s;
  628. typedef FAR struct pthread_spinlock_s pthread_spinlock_t;
  629. #define __PTHREAD_SPINLOCK_T_DEFINED 1
  630. #endif
  631. #endif /* CONFIG_PTHREAD_SPINLOCKS */
  632. #ifndef __PTHREAD_ONCE_T_DEFINED
  633. typedef bool pthread_once_t;
  634. # define __PTHREAD_ONCE_T_DEFINED 1
  635. #endif
  636. #endif /* __INCLUDE_PTHREAD_H */