threads.texi 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. @node Threads
  2. @c @node Threads, Internal Probes, Debugging Support, Top
  3. @c %MENU% Functions, constants, and data types for working with threads
  4. @chapter Threads
  5. @cindex threads
  6. This chapter describes functions used for managing threads.
  7. @Theglibc{} provides two threading implementations: ISO C threads and
  8. POSIX threads.
  9. @menu
  10. * ISO C Threads:: Threads based on the ISO C specification.
  11. * POSIX Threads:: Threads based on the POSIX specification.
  12. @end menu
  13. @node ISO C Threads
  14. @section ISO C Threads
  15. @cindex ISO C threads
  16. @cindex C threads
  17. @pindex threads.h
  18. This section describes the @glibcadj{} ISO C threads implementation.
  19. To have a deeper understanding of this API, it is strongly recommended
  20. to read ISO/IEC 9899:2011, section 7.26, in which ISO C threads were
  21. originally specified. All types and function prototypes are declared
  22. in the header file @file{threads.h}.
  23. @menu
  24. * ISO C Threads Return Values:: Symbolic constants that represent a
  25. function's return value.
  26. * ISO C Thread Management:: Support for basic threading.
  27. * Call Once:: Single-call functions and macros.
  28. * ISO C Mutexes:: A low-level mechanism for mutual exclusion.
  29. * ISO C Condition Variables:: High-level objects for thread synchronization.
  30. * ISO C Thread-local Storage:: Functions to support thread-local storage.
  31. @end menu
  32. @node ISO C Threads Return Values
  33. @subsection Return Values
  34. The ISO C thread specification provides the following enumeration
  35. constants for return values from functions in the API:
  36. @vtable @code
  37. @item thrd_timedout
  38. @standards{C11, threads.h}
  39. A specified time was reached without acquiring the requested resource,
  40. usually a mutex or condition variable.
  41. @item thrd_success
  42. @standards{C11, threads.h}
  43. The requested operation succeeded.
  44. @item thrd_busy
  45. @standards{C11, threads.h}
  46. The requested operation failed because a requested resource is already
  47. in use.
  48. @item thrd_error
  49. @standards{C11, threads.h}
  50. The requested operation failed.
  51. @item thrd_nomem
  52. @standards{C11, threads.h}
  53. The requested operation failed because it was unable to allocate
  54. enough memory.
  55. @end vtable
  56. @node ISO C Thread Management
  57. @subsection Creation and Control
  58. @cindex thread creation
  59. @cindex thread control
  60. @cindex thread management
  61. @Theglibc{} implements a set of functions that allow the user to easily
  62. create and use threads. Additional functionality is provided to control
  63. the behavior of threads.
  64. The following data types are defined for managing threads:
  65. @deftp {Data Type} thrd_t
  66. @standards{C11, threads.h}
  67. A unique object that identifies a thread.
  68. @end deftp
  69. @deftp {Data Type} thrd_start_t
  70. @standards{C11, threads.h}
  71. This data type is an @code{int (*) (void *)} typedef that is passed to
  72. @code{thrd_create} when creating a new thread. It should point to the
  73. first function that thread will run.
  74. @end deftp
  75. The following functions are used for working with threads:
  76. @deftypefun int thrd_create (thrd_t *@var{thr}, thrd_start_t @var{func}, void *@var{arg})
  77. @standards{C11, threads.h}
  78. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  79. @code{thrd_create} creates a new thread that will execute the function
  80. @var{func}. The object pointed to by @var{arg} will be used as the
  81. argument to @var{func}. If successful, @var{thr} is set to the new
  82. thread identifier.
  83. This function may return @code{thrd_success}, @code{thrd_nomem}, or
  84. @code{thrd_error}.
  85. @end deftypefun
  86. @deftypefun thrd_t thrd_current (void)
  87. @standards{C11, threads.h}
  88. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  89. This function returns the identifier of the calling thread.
  90. @end deftypefun
  91. @deftypefun int thrd_equal (thrd_t @var{lhs}, thrd_t @var{rhs})
  92. @standards{C11, threads.h}
  93. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  94. @code{thrd_equal} checks whether @var{lhs} and @var{rhs} refer to the
  95. same thread. If @var{lhs} and @var{rhs} are different threads, this
  96. function returns @math{0}; otherwise, the return value is non-zero.
  97. @end deftypefun
  98. @deftypefun int thrd_sleep (const struct timespec *@var{time_point}, struct timespec *@var{remaining})
  99. @standards{C11, threads.h}
  100. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  101. @code{thrd_sleep} blocks the execution of the current thread for at
  102. least until the elapsed time pointed to by @var{time_point} has been
  103. reached. This function does not take an absolute time, but a duration
  104. that the thread is required to be blocked. @xref{Time Basics}, and
  105. @ref{Elapsed Time}.
  106. The thread may wake early if a signal that is not ignored is received.
  107. In such a case, if @code{remaining} is not NULL, the remaining time
  108. duration is stored in the object pointed to by
  109. @var{remaining}.
  110. @code{thrd_sleep} returns @math{0} if it blocked for at least the
  111. amount of time in @code{time_point}, @math{-1} if it was interrupted
  112. by a signal, or a negative number on failure.
  113. @end deftypefun
  114. @deftypefun void thrd_yield (void)
  115. @standards{C11, threads.h}
  116. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  117. @code{thrd_yield} provides a hint to the implementation to reschedule
  118. the execution of the current thread, allowing other threads to run.
  119. @end deftypefun
  120. @deftypefun {_Noreturn void} thrd_exit (int @var{res})
  121. @standards{C11, threads.h}
  122. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  123. @code{thrd_exit} terminates execution of the calling thread and sets
  124. its result code to @var{res}.
  125. If this function is called from a single-threaded process, the call is
  126. equivalent to calling @code{exit} with @code{EXIT_SUCCESS}
  127. (@pxref{Normal Termination}). Also note that returning from a
  128. function that started a thread is equivalent to calling
  129. @code{thrd_exit}.
  130. @end deftypefun
  131. @deftypefun int thrd_detach (thrd_t @var{thr})
  132. @standards{C11, threads.h}
  133. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  134. @code{thrd_detach} detaches the thread identified by @code{thr} from
  135. the current control thread. The resources held by the detached thread
  136. will be freed automatically once the thread exits. The parent thread
  137. will never be notified by any @var{thr} signal.
  138. Calling @code{thrd_detach} on a thread that was previously detached or
  139. joined by another thread results in undefined behavior.
  140. This function returns either @code{thrd_success} or @code{thrd_error}.
  141. @end deftypefun
  142. @deftypefun int thrd_join (thrd_t @var{thr}, int *@var{res})
  143. @standards{C11, threads.h}
  144. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  145. @code{thrd_join} blocks the current thread until the thread identified
  146. by @code{thr} finishes execution. If @code{res} is not NULL, the
  147. result code of the thread is put into the location pointed to by
  148. @var{res}. The termination of the thread @dfn{synchronizes-with} the
  149. completion of this function, meaning both threads have arrived at a
  150. common point in their execution.
  151. Calling @code{thrd_join} on a thread that was previously detached or
  152. joined by another thread results in undefined behavior.
  153. This function returns either @code{thrd_success} or @code{thrd_error}.
  154. @end deftypefun
  155. @node Call Once
  156. @subsection Call Once
  157. @cindex call once
  158. @cindex single-call functions
  159. In order to guarantee single access to a function, @theglibc{}
  160. implements a @dfn{call once function} to ensure a function is only
  161. called once in the presence of multiple, potentially calling threads.
  162. @deftp {Data Type} once_flag
  163. @standards{C11, threads.h}
  164. A complete object type capable of holding a flag used by @code{call_once}.
  165. @end deftp
  166. @defvr Macro ONCE_FLAG_INIT
  167. @standards{C11, threads.h}
  168. This value is used to initialize an object of type @code{once_flag}.
  169. @end defvr
  170. @deftypefun void call_once (once_flag *@var{flag}, void (*@var{func}) (void))
  171. @standards{C11, threads.h}
  172. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  173. @code{call_once} calls function @var{func} exactly once, even if
  174. invoked from several threads. The completion of the function
  175. @var{func} synchronizes-with all previous or subsequent calls to
  176. @code{call_once} with the same @code{flag} variable.
  177. @end deftypefun
  178. @node ISO C Mutexes
  179. @subsection Mutexes
  180. @cindex mutex
  181. @cindex mutual exclusion
  182. To have better control of resources and how threads access them,
  183. @theglibc{} implements a @dfn{mutex} object, which can help avoid race
  184. conditions and other concurrency issues. The term ``mutex'' refers to
  185. mutual exclusion.
  186. The fundamental data type for a mutex is the @code{mtx_t}:
  187. @deftp {Data Type} mtx_t
  188. @standards{C11, threads.h}
  189. The @code{mtx_t} data type uniquely identifies a mutex object.
  190. @end deftp
  191. The ISO C standard defines several types of mutexes. They are
  192. represented by the following symbolic constants:
  193. @vtable @code
  194. @item mtx_plain
  195. @standards{C11, threads.h}
  196. A mutex that does not support timeout, or test and return.
  197. @item mtx_recursive
  198. @standards{C11, threads.h}
  199. A mutex that supports recursive locking, which means that the owning
  200. thread can lock it more than once without causing deadlock.
  201. @item mtx_timed
  202. @standards{C11, threads.h}
  203. A mutex that supports timeout.
  204. @end vtable
  205. The following functions are used for working with mutexes:
  206. @deftypefun int mtx_init (mtx_t *@var{mutex}, int @var{type})
  207. @standards{C11, threads.h}
  208. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  209. @code{mtx_init} creates a new mutex object with type @var{type}. The
  210. object pointed to by @var{mutex} is set to the identifier of the newly
  211. created mutex.
  212. Not all combinations of mutex types are valid for the @code{type}
  213. argument. Valid uses of mutex types for the @code{type} argument are:
  214. @table @code
  215. @item mtx_plain
  216. A non-recursive mutex that does not support timeout.
  217. @item mtx_timed
  218. A non-recursive mutex that does support timeout.
  219. @item mtx_plain | mtx_recursive
  220. A recursive mutex that does not support timeout.
  221. @item mtx_timed | mtx_recursive
  222. A recursive mutex that does support timeout.
  223. @end table
  224. This function returns either @code{thrd_success} or @code{thrd_error}.
  225. @end deftypefun
  226. @deftypefun int mtx_lock (mtx_t *@var{mutex})
  227. @standards{C11, threads.h}
  228. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  229. @code{mtx_lock} blocks the current thread until the mutex pointed to
  230. by @var{mutex} is locked. The behavior is undefined if the current
  231. thread has already locked the mutex and the mutex is not recursive.
  232. Prior calls to @code{mtx_unlock} on the same mutex synchronize-with
  233. this operation (if this operation succeeds), and all lock/unlock
  234. operations on any given mutex form a single total order (similar to
  235. the modification order of an atomic).
  236. This function returns either @code{thrd_success} or @code{thrd_error}.
  237. @end deftypefun
  238. @deftypefun int mtx_timedlock (mtx_t *restrict @var{mutex}, const struct timespec *restrict @var{time_point})
  239. @standards{C11, threads.h}
  240. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  241. @code{mtx_timedlock} blocks the current thread until the mutex pointed
  242. to by @var{mutex} is locked or until the calendar time pointed to by
  243. @var{time_point} has been reached. Since this function takes an
  244. absolute time, if a duration is required, the calendar time must be
  245. calculated manually. @xref{Time Basics}, and @ref{Calendar Time}.
  246. If the current thread has already locked the mutex and the mutex is
  247. not recursive, or if the mutex does not support timeout, the behavior
  248. of this function is undefined.
  249. Prior calls to @code{mtx_unlock} on the same mutex synchronize-with
  250. this operation (if this operation succeeds), and all lock/unlock
  251. operations on any given mutex form a single total order (similar to
  252. the modification order of an atomic).
  253. This function returns either @code{thrd_success} or @code{thrd_error}.
  254. @end deftypefun
  255. @deftypefun int mtx_trylock (mtx_t *@var{mutex})
  256. @standards{C11, threads.h}
  257. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  258. @code{mtx_trylock} tries to lock the mutex pointed to by @var{mutex}
  259. without blocking. It returns immediately if the mutex is already
  260. locked.
  261. Prior calls to @code{mtx_unlock} on the same mutex synchronize-with
  262. this operation (if this operation succeeds), and all lock/unlock
  263. operations on any given mutex form a single total order (similar to
  264. the modification order of an atomic).
  265. This function returns @code{thrd_success} if the lock was obtained,
  266. @code{thrd_busy} if the mutex is already locked, and @code{thrd_error}
  267. on failure.
  268. @end deftypefun
  269. @deftypefun int mtx_unlock (mtx_t *@var{mutex})
  270. @standards{C11, threads.h}
  271. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  272. @code{mtx_unlock} unlocks the mutex pointed to by @var{mutex}. The
  273. behavior is undefined if the mutex is not locked by the calling
  274. thread.
  275. This function synchronizes-with subsequent @code{mtx_lock},
  276. @code{mtx_trylock}, and @code{mtx_timedlock} calls on the same mutex.
  277. All lock/unlock operations on any given mutex form a single total
  278. order (similar to the modification order of an atomic).
  279. This function returns either @code{thrd_success} or @code{thrd_error}.
  280. @end deftypefun
  281. @deftypefun void mtx_destroy (mtx_t *@var{mutex})
  282. @standards{C11, threads.h}
  283. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  284. @code{mtx_destroy} destroys the mutex pointed to by @var{mutex}. If
  285. there are any threads waiting on the mutex, the behavior is
  286. undefined.
  287. @end deftypefun
  288. @node ISO C Condition Variables
  289. @subsection Condition Variables
  290. @cindex condvar
  291. @cindex condition variables
  292. Mutexes are not the only synchronization mechanisms available. For
  293. some more complex tasks, @theglibc{} also implements @dfn{condition
  294. variables}, which allow the programmer to think at a higher level when
  295. solving complex synchronization problems. They are used to
  296. synchronize threads waiting on a certain condition to happen.
  297. The fundamental data type for condition variables is the @code{cnd_t}:
  298. @deftp {Data Type} cnd_t
  299. @standards{C11, threads.h}
  300. The @code{cnd_t} uniquely identifies a condition variable object.
  301. @end deftp
  302. The following functions are used for working with condition variables:
  303. @deftypefun int cnd_init (cnd_t *@var{cond})
  304. @standards{C11, threads.h}
  305. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  306. @code{cnd_init} initializes a new condition variable, identified by
  307. @var{cond}.
  308. This function may return @code{thrd_success}, @code{thrd_nomem}, or
  309. @code{thrd_error}.
  310. @end deftypefun
  311. @deftypefun int cnd_signal (cnd_t *@var{cond})
  312. @standards{C11, threads.h}
  313. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  314. @code{cnd_signal} unblocks one thread that is currently waiting on the
  315. condition variable pointed to by @var{cond}. If a thread is
  316. successfully unblocked, this function returns @code{thrd_success}. If
  317. no threads are blocked, this function does nothing and returns
  318. @code{thrd_success}. Otherwise, this function returns
  319. @code{thrd_error}.
  320. @end deftypefun
  321. @deftypefun int cnd_broadcast (cnd_t *@var{cond})
  322. @standards{C11, threads.h}
  323. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  324. @code{cnd_broadcast} unblocks all the threads that are currently
  325. waiting on the condition variable pointed to by @var{cond}. This
  326. function returns @code{thrd_success} on success. If no threads are
  327. blocked, this function does nothing and returns
  328. @code{thrd_success}. Otherwise, this function returns
  329. @code{thrd_error}.
  330. @end deftypefun
  331. @deftypefun int cnd_wait (cnd_t *@var{cond}, mtx_t *@var{mutex})
  332. @standards{C11, threads.h}
  333. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  334. @code{cnd_wait} atomically unlocks the mutex pointed to by @var{mutex}
  335. and blocks on the condition variable pointed to by @var{cond} until
  336. the thread is signaled by @code{cnd_signal} or @code{cnd_broadcast}.
  337. The mutex is locked again before the function returns.
  338. This function returns either @code{thrd_success} or @code{thrd_error}.
  339. @end deftypefun
  340. @deftypefun int cnd_timedwait (cnd_t *restrict @var{cond}, mtx_t *restrict @var{mutex}, const struct timespec *restrict @var{time_point})
  341. @standards{C11, threads.h}
  342. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  343. @code{cnd_timedwait} atomically unlocks the mutex pointed to by
  344. @var{mutex} and blocks on the condition variable pointed to by
  345. @var{cond} until the thread is signaled by @code{cnd_signal} or
  346. @code{cnd_broadcast}, or until the calendar time pointed to by
  347. @var{time_point} has been reached. The mutex is locked again before
  348. the function returns.
  349. As for @code{mtx_timedlock}, since this function takes an absolute
  350. time, if a duration is required, the calendar time must be calculated
  351. manually. @xref{Time Basics}, and @ref{Calendar Time}.
  352. This function may return @code{thrd_success}, @code{thrd_nomem}, or
  353. @code{thrd_error}.
  354. @end deftypefun
  355. @deftypefun void cnd_destroy (cnd_t *@var{cond})
  356. @standards{C11, threads.h}
  357. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  358. @code{cnd_destroy} destroys the condition variable pointed to by
  359. @var{cond}. If there are threads waiting on @var{cond}, the behavior
  360. is undefined.
  361. @end deftypefun
  362. @node ISO C Thread-local Storage
  363. @subsection Thread-local Storage
  364. @cindex thread-local storage
  365. @Theglibc{} implements functions to provide @dfn{thread-local
  366. storage}, a mechanism by which variables can be defined to have unique
  367. per-thread storage, lifetimes that match the thread lifetime, and
  368. destructors that cleanup the unique per-thread storage.
  369. Several data types and macros exist for working with thread-local
  370. storage:
  371. @deftp {Data Type} tss_t
  372. @standards{C11, threads.h}
  373. The @code{tss_t} data type identifies a thread-specific storage
  374. object. Even if shared, every thread will have its own instance of
  375. the variable, with different values.
  376. @end deftp
  377. @deftp {Data Type} tss_dtor_t
  378. @standards{C11, threads.h}
  379. The @code{tss_dtor_t} is a function pointer of type @code{void (*)
  380. (void *)}, to be used as a thread-specific storage destructor. The
  381. function will be called when the current thread calls @code{thrd_exit}
  382. (but never when calling @code{tss_delete} or @code{exit}).
  383. @end deftp
  384. @defvr Macro thread_local
  385. @standards{C11, threads.h}
  386. @code{thread_local} is used to mark a variable with thread storage
  387. duration, which means it is created when the thread starts and cleaned
  388. up when the thread ends.
  389. @emph{Note:} For C++, C++11 or later is required to use the
  390. @code{thread_local} keyword.
  391. @end defvr
  392. @defvr Macro TSS_DTOR_ITERATIONS
  393. @standards{C11, threads.h}
  394. @code{TSS_DTOR_ITERATIONS} is an integer constant expression
  395. representing the maximum number of iterations over all thread-local
  396. destructors at the time of thread termination. This value provides a
  397. bounded limit to the destruction of thread-local storage; e.g.,
  398. consider a destructor that creates more thread-local storage.
  399. @end defvr
  400. The following functions are used to manage thread-local storage:
  401. @deftypefun int tss_create (tss_t *@var{tss_key}, tss_dtor_t @var{destructor})
  402. @standards{C11, threads.h}
  403. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  404. @code{tss_create} creates a new thread-specific storage key and stores
  405. it in the object pointed to by @var{tss_key}. Although the same key
  406. value may be used by different threads, the values bound to the key by
  407. @code{tss_set} are maintained on a per-thread basis and persist for
  408. the life of the calling thread.
  409. If @code{destructor} is not NULL, a destructor function will be set,
  410. and called when the thread finishes its execution by calling
  411. @code{thrd_exit}.
  412. This function returns @code{thrd_success} if @code{tss_key} is
  413. successfully set to a unique value for the thread; otherwise,
  414. @code{thrd_error} is returned and the value of @code{tss_key} is
  415. undefined.
  416. @end deftypefun
  417. @deftypefun int tss_set (tss_t @var{tss_key}, void *@var{val})
  418. @standards{C11, threads.h}
  419. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  420. @code{tss_set} sets the value of the thread-specific storage
  421. identified by @var{tss_key} for the current thread to @var{val}.
  422. Different threads may set different values to the same key.
  423. This function returns either @code{thrd_success} or @code{thrd_error}.
  424. @end deftypefun
  425. @deftypefun {void *} tss_get (tss_t @var{tss_key})
  426. @standards{C11, threads.h}
  427. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  428. @code{tss_get} returns the value identified by @var{tss_key} held in
  429. thread-specific storage for the current thread. Different threads may
  430. get different values identified by the same key. On failure,
  431. @code{tss_get} returns zero.
  432. @end deftypefun
  433. @deftypefun void tss_delete (tss_t @var{tss_key})
  434. @standards{C11, threads.h}
  435. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  436. @code{tss_delete} destroys the thread-specific storage identified by
  437. @var{tss_key}.
  438. @end deftypefun
  439. @node POSIX Threads
  440. @section POSIX Threads
  441. @cindex pthreads
  442. This section describes the @glibcadj{} POSIX Threads implementation.
  443. @menu
  444. * Thread-specific Data:: Support for creating and
  445. managing thread-specific data
  446. * Non-POSIX Extensions:: Additional functions to extend
  447. POSIX Thread functionality
  448. @end menu
  449. @node Thread-specific Data
  450. @subsection Thread-specific Data
  451. The @glibcadj{} implements functions to allow users to create and manage
  452. data specific to a thread. Such data may be destroyed at thread exit,
  453. if a destructor is provided. The following functions are defined:
  454. @deftypefun int pthread_key_create (pthread_key_t *@var{key}, void (*@var{destructor})(void*))
  455. @standards{POSIX, pthread.h}
  456. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  457. @c pthread_key_create ok
  458. @c KEY_UNUSED ok
  459. @c KEY_USABLE ok
  460. Create a thread-specific data key for the calling thread, referenced by
  461. @var{key}.
  462. Objects declared with the C++11 @code{thread_local} keyword are destroyed
  463. before thread-specific data, so they should not be used in thread-specific
  464. data destructors or even as members of the thread-specific data, since the
  465. latter is passed as an argument to the destructor function.
  466. @end deftypefun
  467. @deftypefun int pthread_key_delete (pthread_key_t @var{key})
  468. @standards{POSIX, pthread.h}
  469. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  470. @c pthread_key_delete ok
  471. @c This uses atomic compare and exchange to increment the seq number
  472. @c after testing it's not a KEY_UNUSED seq number.
  473. @c KEY_UNUSED dup ok
  474. Destroy the thread-specific data @var{key} in the calling thread. The
  475. destructor for the thread-specific data is not called during destruction, nor
  476. is it called during thread exit.
  477. @end deftypefun
  478. @deftypefun void *pthread_getspecific (pthread_key_t @var{key})
  479. @standards{POSIX, pthread.h}
  480. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  481. @c pthread_getspecific ok
  482. Return the thread-specific data associated with @var{key} in the calling
  483. thread.
  484. @end deftypefun
  485. @deftypefun int pthread_setspecific (pthread_key_t @var{key}, const void *@var{value})
  486. @standards{POSIX, pthread.h}
  487. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
  488. @c pthread_setspecific @asucorrupt @ascuheap @acucorrupt @acsmem
  489. @c a level2 block may be allocated by a signal handler after
  490. @c another call already made a decision to allocate it, thus losing
  491. @c the allocated value. the seq number is updated before the
  492. @c value, which might cause an earlier-generation value to seem
  493. @c current if setspecific is cancelled or interrupted by a signal
  494. @c KEY_UNUSED ok
  495. @c calloc dup @ascuheap @acsmem
  496. Associate the thread-specific @var{value} with @var{key} in the calling thread.
  497. @end deftypefun
  498. @node Non-POSIX Extensions
  499. @subsection Non-POSIX Extensions
  500. In addition to implementing the POSIX API for threads, @theglibc{} provides
  501. additional functions and interfaces to provide functionality not specified in
  502. the standard.
  503. @menu
  504. * Default Thread Attributes:: Setting default attributes for
  505. threads in a process.
  506. @end menu
  507. @node Default Thread Attributes
  508. @subsubsection Setting Process-wide defaults for thread attributes
  509. @Theglibc{} provides non-standard API functions to set and get the default
  510. attributes used in the creation of threads in a process.
  511. @deftypefun int pthread_getattr_default_np (pthread_attr_t *@var{attr})
  512. @standards{GNU, pthread.h}
  513. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  514. @c Takes lock around read from default_pthread_attr.
  515. Get the default attribute values and set @var{attr} to match. This
  516. function returns @math{0} on success and a non-zero error code on
  517. failure.
  518. @end deftypefun
  519. @deftypefun int pthread_setattr_default_np (pthread_attr_t *@var{attr})
  520. @standards{GNU, pthread.h}
  521. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{}}}
  522. @c pthread_setattr_default_np @ascuheap @asulock @aculock @acsmem
  523. @c check_sched_policy_attr ok
  524. @c check_sched_priority_attr ok
  525. @c sched_get_priority_min dup ok
  526. @c sched_get_priority_max dup ok
  527. @c check_stacksize_attr ok
  528. @c lll_lock @asulock @aculock
  529. @c free dup @ascuheap @acsmem
  530. @c realloc dup @ascuheap @acsmem
  531. @c memcpy dup ok
  532. @c lll_unlock @asulock @aculock
  533. Set the default attribute values to match the values in @var{attr}. The
  534. function returns @math{0} on success and a non-zero error code on failure.
  535. The following error codes are defined for this function:
  536. @table @code
  537. @item EINVAL
  538. At least one of the values in @var{attr} does not qualify as valid for the
  539. attributes or the stack address is set in the attribute.
  540. @item ENOMEM
  541. The system does not have sufficient memory.
  542. @end table
  543. @end deftypefun
  544. @c FIXME these are undocumented:
  545. @c pthread_atfork
  546. @c pthread_attr_destroy
  547. @c pthread_attr_getaffinity_np
  548. @c pthread_attr_getdetachstate
  549. @c pthread_attr_getguardsize
  550. @c pthread_attr_getinheritsched
  551. @c pthread_attr_getschedparam
  552. @c pthread_attr_getschedpolicy
  553. @c pthread_attr_getscope
  554. @c pthread_attr_getstack
  555. @c pthread_attr_getstackaddr
  556. @c pthread_attr_getstacksize
  557. @c pthread_attr_init
  558. @c pthread_attr_setaffinity_np
  559. @c pthread_attr_setdetachstate
  560. @c pthread_attr_setguardsize
  561. @c pthread_attr_setinheritsched
  562. @c pthread_attr_setschedparam
  563. @c pthread_attr_setschedpolicy
  564. @c pthread_attr_setscope
  565. @c pthread_attr_setstack
  566. @c pthread_attr_setstackaddr
  567. @c pthread_attr_setstacksize
  568. @c pthread_barrierattr_destroy
  569. @c pthread_barrierattr_getpshared
  570. @c pthread_barrierattr_init
  571. @c pthread_barrierattr_setpshared
  572. @c pthread_barrier_destroy
  573. @c pthread_barrier_init
  574. @c pthread_barrier_wait
  575. @c pthread_cancel
  576. @c pthread_cleanup_push
  577. @c pthread_cleanup_pop
  578. @c pthread_condattr_destroy
  579. @c pthread_condattr_getclock
  580. @c pthread_condattr_getpshared
  581. @c pthread_condattr_init
  582. @c pthread_condattr_setclock
  583. @c pthread_condattr_setpshared
  584. @c pthread_cond_broadcast
  585. @c pthread_cond_destroy
  586. @c pthread_cond_init
  587. @c pthread_cond_signal
  588. @c pthread_cond_timedwait
  589. @c pthread_cond_wait
  590. @c pthread_create
  591. @c pthread_detach
  592. @c pthread_equal
  593. @c pthread_exit
  594. @c pthread_getaffinity_np
  595. @c pthread_getattr_np
  596. @c pthread_getconcurrency
  597. @c pthread_getcpuclockid
  598. @c pthread_getname_np
  599. @c pthread_getschedparam
  600. @c pthread_join
  601. @c pthread_kill
  602. @c pthread_kill_other_threads_np
  603. @c pthread_mutexattr_destroy
  604. @c pthread_mutexattr_getkind_np
  605. @c pthread_mutexattr_getprioceiling
  606. @c pthread_mutexattr_getprotocol
  607. @c pthread_mutexattr_getpshared
  608. @c pthread_mutexattr_getrobust
  609. @c pthread_mutexattr_getrobust_np
  610. @c pthread_mutexattr_gettype
  611. @c pthread_mutexattr_init
  612. @c pthread_mutexattr_setkind_np
  613. @c pthread_mutexattr_setprioceiling
  614. @c pthread_mutexattr_setprotocol
  615. @c pthread_mutexattr_setpshared
  616. @c pthread_mutexattr_setrobust
  617. @c pthread_mutexattr_setrobust_np
  618. @c pthread_mutexattr_settype
  619. @c pthread_mutex_consistent
  620. @c pthread_mutex_consistent_np
  621. @c pthread_mutex_destroy
  622. @c pthread_mutex_getprioceiling
  623. @c pthread_mutex_init
  624. @c pthread_mutex_lock
  625. @c pthread_mutex_setprioceiling
  626. @c pthread_mutex_timedlock
  627. @c pthread_mutex_trylock
  628. @c pthread_mutex_unlock
  629. @c pthread_once
  630. @c pthread_rwlockattr_destroy
  631. @c pthread_rwlockattr_getkind_np
  632. @c pthread_rwlockattr_getpshared
  633. @c pthread_rwlockattr_init
  634. @c pthread_rwlockattr_setkind_np
  635. @c pthread_rwlockattr_setpshared
  636. @c pthread_rwlock_destroy
  637. @c pthread_rwlock_init
  638. @c pthread_rwlock_rdlock
  639. @c pthread_rwlock_timedrdlock
  640. @c pthread_rwlock_timedwrlock
  641. @c pthread_rwlock_tryrdlock
  642. @c pthread_rwlock_trywrlock
  643. @c pthread_rwlock_unlock
  644. @c pthread_rwlock_wrlock
  645. @c pthread_self
  646. @c pthread_setaffinity_np
  647. @c pthread_setcancelstate
  648. @c pthread_setcanceltype
  649. @c pthread_setconcurrency
  650. @c pthread_setname_np
  651. @c pthread_setschedparam
  652. @c pthread_setschedprio
  653. @c pthread_sigmask
  654. @c pthread_sigqueue
  655. @c pthread_spin_destroy
  656. @c pthread_spin_init
  657. @c pthread_spin_lock
  658. @c pthread_spin_trylock
  659. @c pthread_spin_unlock
  660. @c pthread_testcancel
  661. @c pthread_timedjoin_np
  662. @c pthread_tryjoin_np
  663. @c pthread_yield