pthread.h 28 KB

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