threads.texi 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  1. @node Threads
  2. @c @node Threads, Dynamic Linker, 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.
  22. All types and function prototypes are declared in the header file
  23. @file{threads.h}. When compiling for C, some functionality is also
  24. available in @file{stdlib.h}.
  25. It is recommended that GNU systems use the functionality in
  26. @file{pthread.h} instead of @file{thread.h}, as they are more portable
  27. between C and C++.
  28. @menu
  29. * ISO C Threads Return Values:: Symbolic constants that represent a
  30. function's return value.
  31. * ISO C Thread Management:: Support for basic threading.
  32. * Call Once:: Single-call functions and macros.
  33. * ISO C Mutexes:: A low-level mechanism for mutual exclusion.
  34. * ISO C Condition Variables:: High-level objects for thread synchronization.
  35. * ISO C Thread-local Storage:: Functions to support thread-local storage.
  36. @end menu
  37. @node ISO C Threads Return Values
  38. @subsection Return Values
  39. The ISO C thread specification provides the following enumeration
  40. constants for return values from functions in the API:
  41. @vtable @code
  42. @item thrd_timedout
  43. @standards{C11, threads.h}
  44. A specified time was reached without acquiring the requested resource,
  45. usually a mutex or condition variable.
  46. @item thrd_success
  47. @standards{C11, threads.h}
  48. The requested operation succeeded.
  49. @item thrd_busy
  50. @standards{C11, threads.h}
  51. The requested operation failed because a requested resource is already
  52. in use.
  53. @item thrd_error
  54. @standards{C11, threads.h}
  55. The requested operation failed.
  56. @item thrd_nomem
  57. @standards{C11, threads.h}
  58. The requested operation failed because it was unable to allocate
  59. enough memory.
  60. @end vtable
  61. @node ISO C Thread Management
  62. @subsection Creation and Control
  63. @cindex thread creation
  64. @cindex thread control
  65. @cindex thread management
  66. @Theglibc{} implements a set of functions that allow the user to easily
  67. create and use threads. Additional functionality is provided to control
  68. the behavior of threads.
  69. The following data types are defined for managing threads:
  70. @deftp {Data Type} thrd_t
  71. @standards{C11, threads.h}
  72. A unique object that identifies a thread.
  73. @end deftp
  74. @deftp {Data Type} thrd_start_t
  75. @standards{C11, threads.h}
  76. This data type is an @code{int (*) (void *)} typedef that is passed to
  77. @code{thrd_create} when creating a new thread. It should point to the
  78. first function that thread will run.
  79. @end deftp
  80. The following functions are used for working with threads:
  81. @deftypefun int thrd_create (thrd_t *@var{thr}, thrd_start_t @var{func}, void *@var{arg})
  82. @standards{C11, threads.h}
  83. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  84. @code{thrd_create} creates a new thread that will execute the function
  85. @var{func}. The object pointed to by @var{arg} will be used as the
  86. argument to @var{func}. If successful, @var{thr} is set to the new
  87. thread identifier.
  88. This function may return @code{thrd_success}, @code{thrd_nomem}, or
  89. @code{thrd_error}.
  90. @end deftypefun
  91. @deftypefun thrd_t thrd_current (void)
  92. @standards{C11, threads.h}
  93. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  94. This function returns the identifier of the calling thread.
  95. @end deftypefun
  96. @deftypefun int thrd_equal (thrd_t @var{lhs}, thrd_t @var{rhs})
  97. @standards{C11, threads.h}
  98. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  99. @code{thrd_equal} checks whether @var{lhs} and @var{rhs} refer to the
  100. same thread. If @var{lhs} and @var{rhs} are different threads, this
  101. function returns @math{0}; otherwise, the return value is non-zero.
  102. @end deftypefun
  103. @deftypefun int thrd_sleep (const struct timespec *@var{time_point}, struct timespec *@var{remaining})
  104. @standards{C11, threads.h}
  105. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  106. @code{thrd_sleep} blocks the execution of the current thread for at
  107. least until the elapsed time pointed to by @var{time_point} has been
  108. reached. This function does not take an absolute time, but a duration
  109. that the thread is required to be blocked. @xref{Time Basics}, and
  110. @ref{Time Types}.
  111. The thread may wake early if a signal that is not ignored is received.
  112. In such a case, if @code{remaining} is not NULL, the remaining time
  113. duration is stored in the object pointed to by
  114. @var{remaining}.
  115. @code{thrd_sleep} returns @math{0} if it blocked for at least the
  116. amount of time in @code{time_point}, @math{-1} if it was interrupted
  117. by a signal, or a negative number on failure.
  118. @end deftypefun
  119. @deftypefun void thrd_yield (void)
  120. @standards{C11, threads.h}
  121. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  122. @code{thrd_yield} provides a hint to the implementation to reschedule
  123. the execution of the current thread, allowing other threads to run.
  124. @end deftypefun
  125. @deftypefun {_Noreturn void} thrd_exit (int @var{res})
  126. @standards{C11, threads.h}
  127. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  128. @code{thrd_exit} terminates execution of the calling thread and sets
  129. its result code to @var{res}.
  130. If this function is called from a single-threaded process, the call is
  131. equivalent to calling @code{exit} with @code{EXIT_SUCCESS}
  132. (@pxref{Normal Termination}). Also note that returning from a
  133. function that started a thread is equivalent to calling
  134. @code{thrd_exit}.
  135. @end deftypefun
  136. @deftypefun int thrd_detach (thrd_t @var{thr})
  137. @standards{C11, threads.h}
  138. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  139. @code{thrd_detach} detaches the thread identified by @code{thr} from
  140. the current control thread. The resources held by the detached thread
  141. will be freed automatically once the thread exits. The parent thread
  142. will never be notified by any @var{thr} signal.
  143. Calling @code{thrd_detach} on a thread that was previously detached or
  144. joined by another thread results in undefined behavior.
  145. This function returns either @code{thrd_success} or @code{thrd_error}.
  146. @end deftypefun
  147. @deftypefun int thrd_join (thrd_t @var{thr}, int *@var{res})
  148. @standards{C11, threads.h}
  149. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  150. @code{thrd_join} blocks the current thread until the thread identified
  151. by @code{thr} finishes execution. If @code{res} is not NULL, the
  152. result code of the thread is put into the location pointed to by
  153. @var{res}. The termination of the thread @dfn{synchronizes-with} the
  154. completion of this function, meaning both threads have arrived at a
  155. common point in their execution.
  156. Calling @code{thrd_join} on a thread that was previously detached or
  157. joined by another thread results in undefined behavior.
  158. This function returns either @code{thrd_success} or @code{thrd_error}.
  159. @end deftypefun
  160. @node Call Once
  161. @subsection Call Once
  162. @cindex call once
  163. @cindex single-call functions
  164. In order to guarantee single access to a function, @theglibc{}
  165. implements a @dfn{call once function} to ensure a function is only
  166. called once in the presence of multiple, potentially calling threads.
  167. @deftp {Data Type} once_flag
  168. @standards{C11, threads.h}
  169. A complete object type capable of holding a flag used by @code{call_once}.
  170. As of C23, @code{once_flag} is also defined in @file{stdlib.h},
  171. but only for C, not for C++.
  172. @end deftp
  173. @defvr Macro ONCE_FLAG_INIT
  174. @standards{C11, threads.h}
  175. This value is used to initialize an object of type @code{once_flag}.
  176. As of C23, @code{ONCE_FLAG_INIT} is also defined in @file{stdlib.h},
  177. but only for C, not for C++.
  178. @end defvr
  179. @deftypefun void call_once (once_flag *@var{flag}, void (*@var{func}) (void))
  180. @standards{C11, threads.h}
  181. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  182. @code{call_once} calls function @var{func} exactly once, even if
  183. invoked from several threads. The completion of the function
  184. @var{func} synchronizes-with all previous or subsequent calls to
  185. @code{call_once} with the same @code{flag} variable.
  186. @end deftypefun
  187. These definitions are from C11, where they appear in @file{threads.h}.
  188. In C23, they appear in @file{stdlib.h} as well as in @file{threads.h}.
  189. @node ISO C Mutexes
  190. @subsection Mutexes
  191. @cindex mutex
  192. @cindex mutual exclusion
  193. To have better control of resources and how threads access them,
  194. @theglibc{} implements a @dfn{mutex} object, which can help avoid race
  195. conditions and other concurrency issues. The term ``mutex'' refers to
  196. mutual exclusion.
  197. The fundamental data type for a mutex is the @code{mtx_t}:
  198. @deftp {Data Type} mtx_t
  199. @standards{C11, threads.h}
  200. The @code{mtx_t} data type uniquely identifies a mutex object.
  201. @end deftp
  202. The ISO C standard defines several types of mutexes. They are
  203. represented by the following symbolic constants:
  204. @vtable @code
  205. @item mtx_plain
  206. @standards{C11, threads.h}
  207. A mutex that does not support timeout, or test and return.
  208. @item mtx_recursive
  209. @standards{C11, threads.h}
  210. A mutex that supports recursive locking, which means that the owning
  211. thread can lock it more than once without causing deadlock.
  212. @item mtx_timed
  213. @standards{C11, threads.h}
  214. A mutex that supports timeout.
  215. @end vtable
  216. The following functions are used for working with mutexes:
  217. @deftypefun int mtx_init (mtx_t *@var{mutex}, int @var{type})
  218. @standards{C11, threads.h}
  219. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  220. @code{mtx_init} creates a new mutex object with type @var{type}. The
  221. object pointed to by @var{mutex} is set to the identifier of the newly
  222. created mutex.
  223. Not all combinations of mutex types are valid for the @code{type}
  224. argument. Valid uses of mutex types for the @code{type} argument are:
  225. @table @code
  226. @item mtx_plain
  227. A non-recursive mutex that does not support timeout.
  228. @item mtx_timed
  229. A non-recursive mutex that does support timeout.
  230. @item mtx_plain | mtx_recursive
  231. A recursive mutex that does not support timeout.
  232. @item mtx_timed | mtx_recursive
  233. A recursive mutex that does support timeout.
  234. @end table
  235. This function returns either @code{thrd_success} or @code{thrd_error}.
  236. @end deftypefun
  237. @deftypefun int mtx_lock (mtx_t *@var{mutex})
  238. @standards{C11, threads.h}
  239. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  240. @code{mtx_lock} blocks the current thread until the mutex pointed to
  241. by @var{mutex} is locked. The behavior is undefined if the current
  242. thread has already locked the mutex and the mutex is not recursive.
  243. Prior calls to @code{mtx_unlock} on the same mutex synchronize-with
  244. this operation (if this operation succeeds), and all lock/unlock
  245. operations on any given mutex form a single total order (similar to
  246. the modification order of an atomic).
  247. This function returns either @code{thrd_success} or @code{thrd_error}.
  248. @end deftypefun
  249. @deftypefun int mtx_timedlock (mtx_t *restrict @var{mutex}, const struct timespec *restrict @var{time_point})
  250. @standards{C11, threads.h}
  251. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  252. @code{mtx_timedlock} blocks the current thread until the mutex pointed
  253. to by @var{mutex} is locked or until the calendar time pointed to by
  254. @var{time_point} has been reached. Since this function takes an
  255. absolute time, if a duration is required, the calendar time must be
  256. calculated manually. @xref{Time Basics}, and @ref{Calendar Time}.
  257. If the current thread has already locked the mutex and the mutex is
  258. not recursive, or if the mutex does not support timeout, the behavior
  259. of this function is undefined.
  260. Prior calls to @code{mtx_unlock} on the same mutex synchronize-with
  261. this operation (if this operation succeeds), and all lock/unlock
  262. operations on any given mutex form a single total order (similar to
  263. the modification order of an atomic).
  264. This function returns either @code{thrd_success} or @code{thrd_error}.
  265. @end deftypefun
  266. @deftypefun int mtx_trylock (mtx_t *@var{mutex})
  267. @standards{C11, threads.h}
  268. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  269. @code{mtx_trylock} tries to lock the mutex pointed to by @var{mutex}
  270. without blocking. It returns immediately if the mutex is already
  271. locked.
  272. Prior calls to @code{mtx_unlock} on the same mutex synchronize-with
  273. this operation (if this operation succeeds), and all lock/unlock
  274. operations on any given mutex form a single total order (similar to
  275. the modification order of an atomic).
  276. This function returns @code{thrd_success} if the lock was obtained,
  277. @code{thrd_busy} if the mutex is already locked, and @code{thrd_error}
  278. on failure.
  279. @end deftypefun
  280. @deftypefun int mtx_unlock (mtx_t *@var{mutex})
  281. @standards{C11, threads.h}
  282. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  283. @code{mtx_unlock} unlocks the mutex pointed to by @var{mutex}. The
  284. behavior is undefined if the mutex is not locked by the calling
  285. thread.
  286. This function synchronizes-with subsequent @code{mtx_lock},
  287. @code{mtx_trylock}, and @code{mtx_timedlock} calls on the same mutex.
  288. All lock/unlock operations on any given mutex form a single total
  289. order (similar to the modification order of an atomic).
  290. This function returns either @code{thrd_success} or @code{thrd_error}.
  291. @end deftypefun
  292. @deftypefun void mtx_destroy (mtx_t *@var{mutex})
  293. @standards{C11, threads.h}
  294. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  295. @code{mtx_destroy} destroys the mutex pointed to by @var{mutex}. If
  296. there are any threads waiting on the mutex, the behavior is
  297. undefined.
  298. @end deftypefun
  299. @node ISO C Condition Variables
  300. @subsection Condition Variables
  301. @cindex condvar
  302. @cindex condition variables
  303. Mutexes are not the only synchronization mechanisms available. For
  304. some more complex tasks, @theglibc{} also implements @dfn{condition
  305. variables}, which allow the programmer to think at a higher level when
  306. solving complex synchronization problems. They are used to
  307. synchronize threads waiting on a certain condition to happen.
  308. The fundamental data type for condition variables is the @code{cnd_t}:
  309. @deftp {Data Type} cnd_t
  310. @standards{C11, threads.h}
  311. The @code{cnd_t} uniquely identifies a condition variable object.
  312. @end deftp
  313. The following functions are used for working with condition variables:
  314. @deftypefun int cnd_init (cnd_t *@var{cond})
  315. @standards{C11, threads.h}
  316. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  317. @code{cnd_init} initializes a new condition variable, identified by
  318. @var{cond}.
  319. This function may return @code{thrd_success}, @code{thrd_nomem}, or
  320. @code{thrd_error}.
  321. @end deftypefun
  322. @deftypefun int cnd_signal (cnd_t *@var{cond})
  323. @standards{C11, threads.h}
  324. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  325. @code{cnd_signal} unblocks one thread that is currently waiting on the
  326. condition variable pointed to by @var{cond}. If a thread is
  327. successfully unblocked, this function returns @code{thrd_success}. If
  328. no threads are blocked, this function does nothing and returns
  329. @code{thrd_success}. Otherwise, this function returns
  330. @code{thrd_error}.
  331. @end deftypefun
  332. @deftypefun int cnd_broadcast (cnd_t *@var{cond})
  333. @standards{C11, threads.h}
  334. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  335. @code{cnd_broadcast} unblocks all the threads that are currently
  336. waiting on the condition variable pointed to by @var{cond}. This
  337. function returns @code{thrd_success} on success. If no threads are
  338. blocked, this function does nothing and returns
  339. @code{thrd_success}. Otherwise, this function returns
  340. @code{thrd_error}.
  341. @end deftypefun
  342. @deftypefun int cnd_wait (cnd_t *@var{cond}, mtx_t *@var{mutex})
  343. @standards{C11, threads.h}
  344. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  345. @code{cnd_wait} atomically unlocks the mutex pointed to by @var{mutex}
  346. and blocks on the condition variable pointed to by @var{cond} until
  347. the thread is signaled by @code{cnd_signal} or @code{cnd_broadcast}.
  348. The mutex is locked again before the function returns.
  349. This function returns either @code{thrd_success} or @code{thrd_error}.
  350. @end deftypefun
  351. @deftypefun int cnd_timedwait (cnd_t *restrict @var{cond}, mtx_t *restrict @var{mutex}, const struct timespec *restrict @var{time_point})
  352. @standards{C11, threads.h}
  353. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  354. @code{cnd_timedwait} atomically unlocks the mutex pointed to by
  355. @var{mutex} and blocks on the condition variable pointed to by
  356. @var{cond} until the thread is signaled by @code{cnd_signal} or
  357. @code{cnd_broadcast}, or until the calendar time pointed to by
  358. @var{time_point} has been reached. The mutex is locked again before
  359. the function returns.
  360. As for @code{mtx_timedlock}, since this function takes an absolute
  361. time, if a duration is required, the calendar time must be calculated
  362. manually. @xref{Time Basics}, and @ref{Calendar Time}.
  363. This function may return @code{thrd_success}, @code{thrd_nomem}, or
  364. @code{thrd_error}.
  365. @end deftypefun
  366. @deftypefun void cnd_destroy (cnd_t *@var{cond})
  367. @standards{C11, threads.h}
  368. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  369. @code{cnd_destroy} destroys the condition variable pointed to by
  370. @var{cond}. If there are threads waiting on @var{cond}, the behavior
  371. is undefined.
  372. @end deftypefun
  373. @node ISO C Thread-local Storage
  374. @subsection Thread-local Storage
  375. @cindex thread-local storage
  376. @Theglibc{} implements functions to provide @dfn{thread-local
  377. storage}, a mechanism by which variables can be defined to have unique
  378. per-thread storage, lifetimes that match the thread lifetime, and
  379. destructors that cleanup the unique per-thread storage.
  380. Several data types and macros exist for working with thread-local
  381. storage:
  382. @deftp {Data Type} tss_t
  383. @standards{C11, threads.h}
  384. The @code{tss_t} data type identifies a thread-specific storage
  385. object. Even if shared, every thread will have its own instance of
  386. the variable, with different values.
  387. @end deftp
  388. @deftp {Data Type} tss_dtor_t
  389. @standards{C11, threads.h}
  390. The @code{tss_dtor_t} is a function pointer of type @code{void (*)
  391. (void *)}, to be used as a thread-specific storage destructor. The
  392. function will be called when the current thread calls @code{thrd_exit}
  393. (but never when calling @code{tss_delete} or @code{exit}).
  394. @end deftp
  395. @defvr Macro thread_local
  396. @standards{C11, threads.h}
  397. @code{thread_local} is used to mark a variable with thread storage
  398. duration, which means it is created when the thread starts and cleaned
  399. up when the thread ends.
  400. @emph{Note:} For C++, C++11 or later is required to use the
  401. @code{thread_local} keyword.
  402. @end defvr
  403. @defvr Macro TSS_DTOR_ITERATIONS
  404. @standards{C11, threads.h}
  405. @code{TSS_DTOR_ITERATIONS} is an integer constant expression
  406. representing the maximum number of iterations over all thread-local
  407. destructors at the time of thread termination. This value provides a
  408. bounded limit to the destruction of thread-local storage; e.g.,
  409. consider a destructor that creates more thread-local storage.
  410. @end defvr
  411. The following functions are used to manage thread-local storage:
  412. @deftypefun int tss_create (tss_t *@var{tss_key}, tss_dtor_t @var{destructor})
  413. @standards{C11, threads.h}
  414. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  415. @code{tss_create} creates a new thread-specific storage key and stores
  416. it in the object pointed to by @var{tss_key}. Although the same key
  417. value may be used by different threads, the values bound to the key by
  418. @code{tss_set} are maintained on a per-thread basis and persist for
  419. the life of the calling thread.
  420. If @code{destructor} is not NULL, a destructor function will be set,
  421. and called when the thread finishes its execution by calling
  422. @code{thrd_exit}.
  423. This function returns @code{thrd_success} if @code{tss_key} is
  424. successfully set to a unique value for the thread; otherwise,
  425. @code{thrd_error} is returned and the value of @code{tss_key} is
  426. undefined.
  427. @end deftypefun
  428. @deftypefun int tss_set (tss_t @var{tss_key}, void *@var{val})
  429. @standards{C11, threads.h}
  430. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  431. @code{tss_set} sets the value of the thread-specific storage
  432. identified by @var{tss_key} for the current thread to @var{val}.
  433. Different threads may set different values to the same key.
  434. This function returns either @code{thrd_success} or @code{thrd_error}.
  435. @end deftypefun
  436. @deftypefun {void *} tss_get (tss_t @var{tss_key})
  437. @standards{C11, threads.h}
  438. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  439. @code{tss_get} returns the value identified by @var{tss_key} held in
  440. thread-specific storage for the current thread. Different threads may
  441. get different values identified by the same key. On failure,
  442. @code{tss_get} returns zero.
  443. @end deftypefun
  444. @deftypefun void tss_delete (tss_t @var{tss_key})
  445. @standards{C11, threads.h}
  446. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  447. @code{tss_delete} destroys the thread-specific storage identified by
  448. @var{tss_key}.
  449. @end deftypefun
  450. @node POSIX Threads
  451. @section POSIX Threads
  452. @cindex pthreads
  453. This section describes the @glibcadj{} POSIX Threads implementation.
  454. @menu
  455. * Creating and Destroying Threads::
  456. * Thread-specific Data:: Support for creating and
  457. managing thread-specific data
  458. * Waiting with Explicit Clocks:: Functions for waiting with an
  459. explicit clock specification.
  460. * POSIX Semaphores:: Support for process and thread
  461. synchronization using semaphores
  462. * POSIX Barriers:: Support for process and thread
  463. synchronization using barriers
  464. * POSIX Spin Locks:: Support for process and thread
  465. synchronization using spinlocks
  466. * POSIX Mutexes:: Support for mutual exclusion
  467. * POSIX Threads Other APIs:: Other Standard functions
  468. * Non-POSIX Extensions:: Additional functions to extend
  469. POSIX Thread functionality
  470. @end menu
  471. @node Creating and Destroying Threads
  472. @subsection Creating and Destroying Threads
  473. @deftypefun int pthread_create (pthread_t *@var{newthread}, const pthread_attr_t *@var{attr}, void *(*@var{start_routine}) (void *), void *@var{arg})
  474. This function creates a new thread with attributes @var{attr}. This
  475. thread will call @var{start_routine} and pass it @var{arg}. If
  476. @var{start_routine} returns, the thread will exit and the return value
  477. will become the thread's exit value. The new thread's ID is stored in
  478. @var{newthread}. Returns 0 on success.
  479. @manpagefunctionstub{pthread_create, 3}
  480. @end deftypefun
  481. @deftypefun int pthread_detach (pthread_t @var{th})
  482. Indicates that thread @var{th} must clean up after itself
  483. automatically when it exits, as the parent thread will not call
  484. @code{pthread_join} on it.
  485. @manpagefunctionstub{pthread_detach, 3}
  486. @end deftypefun
  487. @deftypefun int pthread_join (pthread_t @var{th}, void **@var{thread_return})
  488. Waits for thread @var{th} to exit, and stores its return value in
  489. @var{thread_return}.
  490. @manpagefunctionstub{pthread_join, 3}
  491. @end deftypefun
  492. @deftypefun int pthread_kill (pthread_t @var{th}, int @var{signal})
  493. Sends signal @var{signal} to thread @var{th}.
  494. @manpagefunctionstub{pthread_kill, 3}
  495. @end deftypefun
  496. @deftypefun pthread_t pthread_self (void)
  497. Returns the ID of the thread which performed the call.
  498. @manpagefunctionstub{pthread_self, 3}
  499. @end deftypefun
  500. Each thread has a set of attributes which are passed to
  501. @code{pthread_create} via the @code{pthread_attr_t} type, which should
  502. be considered an opaque type.
  503. @deftypefun int pthread_attr_init (pthread_attr_t *@var{attr})
  504. Initializes @var{attr} to its default values and allocates any
  505. resources required. Once initialized, @var{attr} can be modified by
  506. other @code{pthread_attr_*} functions, or used by
  507. @code{pthread_create}.
  508. @manpagefunctionstub{pthread_attr_init, 3}
  509. @end deftypefun
  510. @deftypefun int pthread_attr_destroy (pthread_attr_t *@var{attr})
  511. When no longer needed, @var{attr} should be destroyed with this
  512. function, which releases any resources allocated. Note that
  513. @var{attr} is only needed for the @code{pthread_create} call, not for
  514. the running thread itself.
  515. @manpagefunctionstub{pthread_attr_destroy, 3}
  516. @end deftypefun
  517. @deftypefun int pthread_attr_setdetachstate (pthread_attr_t *@var{attr}, int @var{detachstate})
  518. Sets the detach state attribute for @var{attr}. This attribute may be one of the following:
  519. @table @code
  520. @item PTHREAD_CREATE_DETACHED
  521. Causes the created thread to be detached, that is, as if
  522. @code{pthread_detach} had been called on it.
  523. @item PTHREAD_CREATE_JOINABLE
  524. Causes the created thread to be joinable, that is, @code{pthread_join}
  525. must be called on it.
  526. @end table
  527. @manpagefunctionstub{pthread_attr_setdetachstate, 3}
  528. @end deftypefun
  529. @deftypefun int pthread_attr_getdetachstate (const pthread_attr_t *@var{attr}, int *@var{detachstate})
  530. Gets the detach state attribute from @var{attr}.
  531. @manpagefunctionstub{pthread_attr_getdetachstate, 3}
  532. @end deftypefun
  533. @node Thread-specific Data
  534. @subsection Thread-specific Data
  535. The @glibcadj{} implements functions to allow users to create and manage
  536. data specific to a thread. Such data may be destroyed at thread exit,
  537. if a destructor is provided. The following functions are defined:
  538. @deftypefun int pthread_key_create (pthread_key_t *@var{key}, void (*@var{destructor})(void*))
  539. @standards{POSIX, pthread.h}
  540. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  541. @c pthread_key_create ok
  542. @c KEY_UNUSED ok
  543. @c KEY_USABLE ok
  544. Create a thread-specific data key for the calling thread, referenced by
  545. @var{key}.
  546. Objects declared with the C++11 @code{thread_local} keyword are destroyed
  547. before thread-specific data, so they should not be used in thread-specific
  548. data destructors or even as members of the thread-specific data, since the
  549. latter is passed as an argument to the destructor function.
  550. @end deftypefun
  551. @deftypefun int pthread_key_delete (pthread_key_t @var{key})
  552. @standards{POSIX, pthread.h}
  553. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  554. @c pthread_key_delete ok
  555. @c This uses atomic compare and exchange to increment the seq number
  556. @c after testing it's not a KEY_UNUSED seq number.
  557. @c KEY_UNUSED dup ok
  558. Destroy the thread-specific data @var{key} in the calling thread. The
  559. destructor for the thread-specific data is not called during destruction, nor
  560. is it called during thread exit.
  561. @end deftypefun
  562. @deftypefun {void *} pthread_getspecific (pthread_key_t @var{key})
  563. @standards{POSIX, pthread.h}
  564. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  565. @c pthread_getspecific ok
  566. Return the thread-specific data associated with @var{key} in the calling
  567. thread.
  568. @end deftypefun
  569. @deftypefun int pthread_setspecific (pthread_key_t @var{key}, const void *@var{value})
  570. @standards{POSIX, pthread.h}
  571. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
  572. @c pthread_setspecific @asucorrupt @ascuheap @acucorrupt @acsmem
  573. @c a level2 block may be allocated by a signal handler after
  574. @c another call already made a decision to allocate it, thus losing
  575. @c the allocated value. the seq number is updated before the
  576. @c value, which might cause an earlier-generation value to seem
  577. @c current if setspecific is cancelled or interrupted by a signal
  578. @c KEY_UNUSED ok
  579. @c calloc dup @ascuheap @acsmem
  580. Associate the thread-specific @var{value} with @var{key} in the calling thread.
  581. @end deftypefun
  582. @node Waiting with Explicit Clocks
  583. @subsection Functions for Waiting According to a Specific Clock
  584. @Theglibc{} provides several waiting functions that expect an explicit
  585. @code{clockid_t} argument. These functions were all adopted by
  586. POSIX.1-2024.
  587. @comment pthread.h
  588. @deftypefun int pthread_cond_clockwait (pthread_cond_t *@var{cond}, pthread_mutex_t *@var{mutex}, clockid_t @var{clockid}, const struct timespec *@var{abstime})
  589. @standards{POSIX-1.2024, pthread.h}
  590. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  591. @c If exactly the same function with arguments is called from a signal
  592. @c handler that interrupts between the mutex unlock and sleep then it
  593. @c will unlock the mutex twice resulting in undefined behaviour. Keep
  594. @c in mind that the unlock and sleep are only atomic with respect to other
  595. @c threads (really a happens-after relationship for pthread_cond_broadcast
  596. @c and pthread_cond_signal).
  597. @c In the AC case we would cancel the thread and the mutex would remain
  598. @c locked and we can't recover from that.
  599. Behaves like @code{pthread_cond_timedwait} except the time @var{abstime} is
  600. measured against the clock specified by @var{clockid} rather than the clock
  601. specified or defaulted when @code{pthread_cond_init} was called. Currently,
  602. @var{clockid} must be either @code{CLOCK_MONOTONIC} or
  603. @code{CLOCK_REALTIME}.
  604. @end deftypefun
  605. @comment pthread.h
  606. @deftypefun int pthread_rwlock_clockrdlock (pthread_rwlock_t *@var{rwlock}, clockid_t @var{clockid}, const struct timespec *@var{abstime})
  607. @standards{POSIX-1.2024, pthread.h}
  608. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  609. Behaves like @code{pthread_rwlock_timedrdlock} except the time
  610. @var{abstime} is measured against the clock specified by @var{clockid}
  611. rather than @code{CLOCK_REALTIME}. Currently, @var{clockid} must be either
  612. @code{CLOCK_MONOTONIC} or @code{CLOCK_REALTIME}, otherwise @code{EINVAL} is
  613. returned.
  614. @end deftypefun
  615. @comment pthread.h
  616. @deftypefun int pthread_rwlock_clockwrlock (pthread_rwlock_t *@var{rwlock}, clockid_t @var{clockid}, const struct timespec *@var{abstime})
  617. @standards{POSIX-1.2024, pthread.h}
  618. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  619. Behaves like @code{pthread_rwlock_timedwrlock} except the time
  620. @var{abstime} is measured against the clock specified by @var{clockid}
  621. rather than @code{CLOCK_REALTIME}. Currently, @var{clockid} must be either
  622. @code{CLOCK_MONOTONIC} or @code{CLOCK_REALTIME}, otherwise @code{EINVAL} is
  623. returned.
  624. @end deftypefun
  625. @node POSIX Semaphores
  626. @subsection POSIX Semaphores
  627. @deftypefun int sem_init (sem_t *@var{sem}, int @var{pshared}, unsigned int @var{value})
  628. @standards{POSIX.1-2008, semaphore.h}
  629. @safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@acucorrupt{}}}
  630. @manpagefunctionstub{sem_init,3}
  631. @c Does not atomically update sem_t therefore AC-unsafe
  632. @c because it can leave sem_t partially initialized.
  633. @end deftypefun
  634. @deftypefun int sem_destroy (sem_t *@var{sem})
  635. @standards{POSIX.1-2008, semaphore.h}
  636. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  637. @manpagefunctionstub{sem_destroy,3}
  638. @c Function does nothing and is therefore always safe.
  639. @end deftypefun
  640. @deftypefun {sem_t *} sem_open (const char *@var{name}, int @var{oflag}, ...)
  641. @standards{POSIX.1-2008, semaphore.h}
  642. @safety{@prelim{}@mtsafe{}@asunsafe{@asuinit{}}@acunsafe{@acuinit{}}}
  643. @manpagefunctionstub{sem_open,3}
  644. @c pthread_once asuinit
  645. @c
  646. @c We are AC-Unsafe because we use pthread_once to initialize
  647. @c a global variable that holds the location of the mounted
  648. @c shmfs on Linux.
  649. @end deftypefun
  650. @deftypefun int sem_close (sem_t *@var{sem})
  651. @standards{POSIX.1-2008, semaphore.h}
  652. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  653. @manpagefunctionstub{sem_close,3}
  654. @c lll_lock asulock aculock
  655. @c twalk mtsrace{:root}
  656. @c
  657. @c We are AS-unsafe because we take a non-recursive lock.
  658. @c We are AC-unsafe because several internal data structures
  659. @c are not updated atomically.
  660. @end deftypefun
  661. @deftypefun int sem_unlink (const char *@var{name})
  662. @standards{POSIX.1-2008, semaphore.h}
  663. @safety{@prelim{}@mtsafe{}@asunsafe{@asuinit{}}@acunsafe{@acucorrupt{}}}
  664. @manpagefunctionstub{sem_unlink,3}
  665. @c pthread_once asuinit acucorrupt aculock
  666. @c mempcpy acucorrupt
  667. @end deftypefun
  668. @deftypefun int sem_wait (sem_t *@var{sem})
  669. @standards{POSIX.1-2008, semaphore.h}
  670. @safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@acucorrupt{}}}
  671. @manpagefunctionstub{sem_wait,3}
  672. @c atomic_fetch_add_relaxed (nwaiters) acucorrupt
  673. @c
  674. @c Given the use atomic operations this function seems
  675. @c to be AS-safe. It is AC-unsafe because there is still
  676. @c a window between atomic_fetch_add_relaxed and the pthread_push
  677. @c of the handler that undoes that operation. A cancellation
  678. @c at that point would fail to remove the process from the
  679. @c waiters count.
  680. @end deftypefun
  681. @deftypefun int sem_timedwait (sem_t *@var{sem}, const struct timespec *@var{abstime})
  682. @standards{POSIX.1-2008, semaphore.h}
  683. @safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@acucorrupt{}}}
  684. @manpagefunctionstub{sem_timedwait,3}
  685. @c Same safety issues as sem_wait.
  686. @end deftypefun
  687. @deftypefun int sem_clockwait (sem_t *@var{sem}, clockid_t @var{clockid}, const struct timespec *@var{abstime})
  688. @standards{POSIX.1-2024, semaphore.h}
  689. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  690. Behaves like @code{sem_timedwait} except the time @var{abstime} is measured
  691. against the clock specified by @var{clockid} rather than
  692. @code{CLOCK_REALTIME}. Currently, @var{clockid} must be either
  693. @code{CLOCK_MONOTONIC} or @code{CLOCK_REALTIME}.
  694. @end deftypefun
  695. @deftypefun int sem_trywait (sem_t *@var{sem})
  696. @standards{POSIX.1-2008, semaphore.h}
  697. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  698. @manpagefunctionstub{sem_trywait,3}
  699. @c All atomic operations are safe in all contexts.
  700. @end deftypefun
  701. @deftypefun int sem_post (sem_t *@var{sem})
  702. @standards{POSIX.1-2008, semaphore.h}
  703. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  704. @manpagefunctionstub{sem_post,3}
  705. @c Same safety as sem_trywait.
  706. @end deftypefun
  707. @deftypefun int sem_getvalue (sem_t *@var{sem}, int *@var{sval})
  708. @standards{POSIX.1-2008, semaphore.h}
  709. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  710. @manpagefunctionstub{sem_getvalue,3}
  711. @c Atomic write of a value is safe in all contexts.
  712. @end deftypefun
  713. @node POSIX Barriers
  714. @subsection POSIX Barriers
  715. A POSIX barrier works as follows: a file-local or global
  716. @code{pthread_barrier_t} object is initialized via
  717. @code{pthread_barrier_init} to require @var{count} threads to wait on
  718. it. After that, up to @var{count}-1 threads will wait on the barrier
  719. via @code{pthread_barrier_wait}. None of these calls will return
  720. until @var{count} threads are waiting via the next call to
  721. @code{pthread_barrier_wait}, at which point, all of these calls will
  722. return. The net result is that @var{count} threads will be
  723. synchronized at that point. At some point after this, the barrier is
  724. destroyed via @code{pthread_barrier_destroy}. Note that a barrier
  725. must be destroyed before being re-initialized, to ensure that all
  726. threads are properly synchronized, but need not be destroyed and
  727. re-initialized before being reused.
  728. @deftypefun int pthread_barrier_init (pthread_barrier_t *@var{barrier}, const pthread_barrierattr_t *@var{attr}, unsigned int @var{count})
  729. @standards{POSIX, pthread.h}
  730. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  731. This function initializes a barrier to synchronize @var{count}
  732. threads. The barrier must be uninitialized or destroyed before it is
  733. initialized; attempting to initialize an in-use barrier results in
  734. undefined behavior.
  735. The @var{attr} argument to @code{pthread_barrier_init} is typically
  736. NULL for a process-private barrier, but may be used to share a barrier
  737. across processes (documentation TBD).
  738. On success, 0 is returned. On error, one of the following is returned:
  739. @table @code
  740. @item EINVAL
  741. Either @var{count} is zero, or is large enough to cause an internal
  742. overflow.
  743. @end table
  744. @end deftypefun
  745. @deftypefun int pthread_barrier_wait (pthread_barrier_t *@var{barrier})
  746. @standards{POSIX, pthread.h}
  747. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  748. This function synchronizes threads. The first @var{count}-1 threads
  749. that wait on @var{barrier} will just wait. The next thread that waits
  750. on @var{barrier} will cause all @var{count} threads' calls to return.
  751. The @var{barrier} must be initialized with @code{pthread_barrier_init}
  752. and not yet destroyed with @code{pthread_barrier_destroy}.
  753. The return value of this function is
  754. @code{PTHREAD_BARRIER_SERIAL_THREAD} for one thread (it is unspecified
  755. which thread) and 0 for the remainder, for each batch of @var{count}
  756. threads synchronized. After such a batch is synchronized, the
  757. @var{barrier} will begin synchronizing the next @var{count} threads.
  758. @end deftypefun
  759. @deftypefun int pthread_barrier_destroy (pthread_barrier_t *@var{barrier})
  760. @standards{POSIX, pthread.h}
  761. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  762. Destroys @var{barrier} and releases any resources it may have
  763. allocated. A barrier must not be destroyed if any thread is waiting
  764. on it, or if it was not initialized. This call always succeeds and
  765. returns 0.
  766. @end deftypefun
  767. @node POSIX Spin Locks
  768. @subsection POSIX Spin Locks
  769. A spinlock is a low overhead lock suitable for use in a realtime
  770. thread where it's known that the thread won't be paused by the
  771. scheduler. Non-realtime threads should use mutexes instead.
  772. @deftypefun int pthread_spin_init (pthread_spinlock_t *@var{lock}, int @var{pshared})
  773. Initializes a spinlock. @var{pshared} is one of:
  774. @table @code
  775. @item PTHREAD_PROCESS_PRIVATE
  776. This spinlock is private to the process which created it.
  777. @item PTHREAD_PROCESS_SHARED
  778. This spinlock is shared across any process that can access it, for
  779. example through shared memory.
  780. @end table
  781. @manpagefunctionstub{pthread_spin_init, 3}
  782. @end deftypefun
  783. @deftypefun int pthread_spin_destroy (pthread_spinlock_t *@var{lock})
  784. Destroys a spinlock and releases any resources it held.
  785. @manpagefunctionstub{pthread_spin_destroy, 3}
  786. @end deftypefun
  787. @deftypefun int pthread_spin_lock (pthread_spinlock_t *@var{lock})
  788. Locks a spinlock. Only one thread at a time can lock a spinlock. If
  789. another thread has locked this spinlock, the calling thread waits
  790. until it is unlocked, then attempts to lock it.
  791. @manpagefunctionstub{pthread_spin_lock, 3}
  792. @end deftypefun
  793. @deftypefun int pthread_spin_unlock (pthread_spinlock_t *@var{lock})
  794. Unlocks a spinlock. If one or more threads are waiting for the lock
  795. to be unlocked, one of them (unspecified which) will succeed in
  796. locking it, and will return from @code{pthread_spin_lock}).
  797. @manpagefunctionstub{pthread_spin_unlock, 3}
  798. @end deftypefun
  799. @deftypefun int pthread_spin_trylock (pthread_spinlock_t *@var{lock})
  800. Like @code{pthread_spin_unlock} but returns 0 if the lock was
  801. unlocked, or EBUSY if it was locked.
  802. @manpagefunctionstub{pthread_spin_trylock, 3}
  803. @end deftypefun
  804. @node POSIX Mutexes
  805. @subsection POSIX Mutexes
  806. A @emph{mutex}, or ``mutual exclusion'', is a way of guaranteeing that
  807. only one thread at a time is able to execute a protected bit of code
  808. (or access any other resource). Two or more threads trying to execute
  809. the same code at the same time, will instead take turns, according to
  810. the mutex.
  811. A mutex is much like a spinlock, but implemented in a way that is more
  812. appropriate for use in non-realtime threads, and is more
  813. resource-conserving.
  814. @deftypefun int pthread_mutex_init (pthread_mutex_t *@var{mutex}, const pthread_mutexattr_t *@var{mutexattr})
  815. Initiailizes a mutex.
  816. @manpagefunctionstub{pthread_mutex_init, 3}
  817. @end deftypefun
  818. @deftypefun int pthread_mutex_destroy (pthread_mutex_t *@var{mutex})
  819. Destroys a no-longer-needed mutex.
  820. @manpagefunctionstub{pthread_mutex_destroy, 3}
  821. @end deftypefun
  822. @deftypefun int pthread_mutex_lock (pthread_mutex_t *@var{mutex})
  823. Only one thread at a time may lock @var{mutex}, and must unlock it
  824. when appropriate. If a thread calls @code{pthread_mutex_lock} while
  825. @var{mutex} is locked by another thread, the calling thread will wait
  826. until @var{mutex} is unlocked, then attempt to lock it. Since there
  827. may be many threads waiting at the same time, the calling thread may
  828. need to repeat this wait-and-try many times before it successfully
  829. locks @var{mutex}, at which point the call to
  830. @code{pthread_mutex_locks} returns succesfully.
  831. This function may fail with the following:
  832. @table @code
  833. @item EAGAIN
  834. Too many locks were attempted.
  835. @item EDEADLK
  836. The calling thread already holds a lock on @var{mutex}.
  837. @item EINVAL
  838. @var{mutex} has an invalid kind, or an invalid priority was requested.
  839. @item ENOTRECOVERABLE
  840. The thread holding the lock died in a way that the system cannot recover from.
  841. @item EOWNERDEAD
  842. The thread holding the lock died in a way that the system can recover from.
  843. @end table
  844. @manpagefunctionstub{pthread_mutex_lock, 3}
  845. @end deftypefun
  846. @deftypefun int pthread_mutex_trylock (pthread_mutex_t *@var{mutex})
  847. Like @code{pthread_mutex_lock} but if the lock cannot be immediately
  848. obtained, returns EBUSY.
  849. @manpagefunctionstub{pthread_mutex_trylock, 3}
  850. @end deftypefun
  851. @deftypefun int pthread_mutex_unlock (pthread_mutex_t *@var{mutex})
  852. Unlocks @var{mutex}. Returns EPERM if the calling thread doesn't hold
  853. the lock on @var{mutex}.
  854. @manpagefunctionstub{pthread_mutex_unlock, 3}
  855. @end deftypefun
  856. @deftypefun int pthread_mutex_clocklock (pthread_mutex_t *@var{mutex}, clockid_t @var{clockid}, const struct timespec *@var{abstime})
  857. @end deftypefun
  858. @deftypefun int pthread_mutex_timedlock (pthread_mutex_t *@var{mutex}, const struct timespec *@var{abstime})
  859. These two functions act like @code{pthread_mutex_lock} with the
  860. exception that the call will not wait past time @var{abstime}, as
  861. reported by @var{clockid} or (for @code{pthread_mutex_timedlock})
  862. @code{CLOCK_REALTIME}. If @var{abstime} is reached and the mutex
  863. still cannot be locked, an @code{ETIMEDOUT} error is returned.
  864. If the time had already passed when these functions
  865. are called, and the mutex cannot be immediately locked, the function
  866. times out immediately.
  867. @end deftypefun
  868. @deftypefun int pthread_mutexattr_init (const pthread_mutexattr_t *@var{attr})
  869. Initializes @var{attr} with default values.
  870. @manpagefunctionstub{pthread_mutexattr_init, 3}
  871. @end deftypefun
  872. @deftypefun int pthread_mutexattr_destroy (pthread_mutexattr_t *@var{attr})
  873. Destroys @var{attr} and releases any resources it may have allocated.
  874. @manpagefunctionstub{pthread_mutexattr_destroy, 3}
  875. @end deftypefun
  876. @deftypefun int pthread_mutexattr_settype (pthread_mutexattr_t *@var{attr}, int @var{kind})
  877. This functions allow you to change what kind of mutex a mutex is, by
  878. changing the attributes used to initialize it. The values for
  879. @var{kind} are:
  880. @table @code
  881. @item PTHREAD_MUTEX_NORMAL
  882. No attempt to detect deadlock is performed; a thread will deadlock if
  883. it tries to lock this mutex yet already holds a lock to it.
  884. Attempting to unlock a mutex not locked by the calling thread results
  885. in undefined behavior.
  886. @item PTHREAD_MUTEX_ERRORCHECK
  887. Attemps to relock a mutex, or unlock a mutex not held, will result in an error.
  888. @item PTHREAD_MUTEX_RECURSIVE
  889. Attempts to relock a mutex already held succeed, but require a
  890. matching number of unlocks to release it. Attempts to unlock a mutex
  891. not held will result in an error.
  892. @item PTHREAD_MUTEX_DEFAULT
  893. Attemps to relock a mutex, or unlock a mutex not held, will result in
  894. undefined behavior. This is the default.
  895. @end table
  896. @end deftypefun
  897. @deftypefun int pthread_mutexattr_gettype (const pthread_mutexattr_t *@var{attr}, int *@var{kind})
  898. This function gets the kind of mutex @var{mutex} is.
  899. @end deftypefun
  900. @node POSIX Threads Other APIs
  901. @subsection POSIX Threads Other APIs
  902. @deftypefun int pthread_equal (pthread_t @var{thread1}, pthread_t @var{thread2})
  903. Compares two thread IDs. If they are the same, returns nonzero, else returns zero.
  904. @manpagefunctionstub{pthread_equal, 3}
  905. @end deftypefun
  906. @deftypefun int pthread_getcpuclockid (pthread_t @var{th}, __clockid_t *@var{clock_id})
  907. Get the clock associated with @var{th}.
  908. @manpagefunctionstub{pthread_getcpuclockid, 3}
  909. @end deftypefun
  910. @deftypefun int pthread_once (pthread_once_t *@var{once_control}, void (*@var{init_routine}) (void))
  911. Calls @var{init_routine} once for each @var{once_control}, which must
  912. be statically initalized to @code{PTHREAD_ONCE_INIT}. Subsequent
  913. calls to @code{pthread_once} with the same @var{once_control} do not
  914. call @var{init_routine}, even in multi-threaded environments.
  915. @manpagefunctionstub{pthread_once, 3}
  916. @end deftypefun
  917. @deftypefun int pthread_sigmask (int @var{how}, const __sigset_t *@var{newmask}, __sigset_t *@var{oldmask})
  918. @manpagefunctionstub{pthread_sigmask, 3}
  919. @end deftypefun
  920. @node Non-POSIX Extensions
  921. @subsection Non-POSIX Extensions
  922. In addition to implementing the POSIX API for threads, @theglibc{} provides
  923. additional functions and interfaces to provide functionality not specified in
  924. the standard.
  925. @menu
  926. * Default Thread Attributes:: Setting default attributes for
  927. threads in a process.
  928. * Initial Thread Signal Mask:: Setting the initial mask of threads.
  929. * Thread CPU Affinity:: Limiting which CPUs can run a thread.
  930. * Joining Threads:: Wait for a thread to terminate.
  931. * Thread Names:: Changing the name of a thread.
  932. * Single-Threaded:: Detecting single-threaded execution.
  933. * Restartable Sequences:: Linux-specific restartable sequences
  934. integration.
  935. @end menu
  936. @node Default Thread Attributes
  937. @subsubsection Setting Process-wide defaults for thread attributes
  938. @Theglibc{} provides non-standard API functions to set and get the default
  939. attributes used in the creation of threads in a process.
  940. @deftypefun int pthread_getattr_default_np (pthread_attr_t *@var{attr})
  941. @standards{GNU, pthread.h}
  942. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  943. @c Takes lock around read from default_pthread_attr.
  944. Get the default attribute values and set @var{attr} to match. This
  945. function returns @math{0} on success and a non-zero error code on
  946. failure.
  947. @end deftypefun
  948. @deftypefun int pthread_setattr_default_np (pthread_attr_t *@var{attr})
  949. @standards{GNU, pthread.h}
  950. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{}}}
  951. @c pthread_setattr_default_np @ascuheap @asulock @aculock @acsmem
  952. @c check_sched_policy_attr ok
  953. @c check_sched_priority_attr ok
  954. @c sched_get_priority_min dup ok
  955. @c sched_get_priority_max dup ok
  956. @c check_stacksize_attr ok
  957. @c lll_lock @asulock @aculock
  958. @c free dup @ascuheap @acsmem
  959. @c realloc dup @ascuheap @acsmem
  960. @c memcpy dup ok
  961. @c lll_unlock @asulock @aculock
  962. Set the default attribute values to match the values in @var{attr}. The
  963. function returns @math{0} on success and a non-zero error code on failure.
  964. The following error codes are defined for this function:
  965. @table @code
  966. @item EINVAL
  967. At least one of the values in @var{attr} does not qualify as valid for the
  968. attributes or the stack address is set in the attribute.
  969. @item ENOMEM
  970. The system does not have sufficient memory.
  971. @end table
  972. @end deftypefun
  973. @node Initial Thread Signal Mask
  974. @subsubsection Controlling the Initial Signal Mask of a New Thread
  975. @Theglibc{} provides a way to specify the initial signal mask of a
  976. thread created using @code{pthread_create}, passing a thread attribute
  977. object configured for this purpose.
  978. @deftypefun int pthread_attr_setsigmask_np (pthread_attr_t *@var{attr}, const sigset_t *@var{sigmask})
  979. @standards{GNU, pthread.h}
  980. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  981. Change the initial signal mask specified by @var{attr}. If
  982. @var{sigmask} is not @code{NULL}, the initial signal mask for new
  983. threads created with @var{attr} is set to @code{*@var{sigmask}}. If
  984. @var{sigmask} is @code{NULL}, @var{attr} will no longer specify an
  985. explicit signal mask, so that the initial signal mask of the new
  986. thread is inherited from the thread that calls @code{pthread_create}.
  987. This function returns zero on success, and @code{ENOMEM} on memory
  988. allocation failure.
  989. @end deftypefun
  990. @deftypefun int pthread_attr_getsigmask_np (const pthread_attr_t *@var{attr}, sigset_t *@var{sigmask})
  991. @standards{GNU, pthread.h}
  992. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  993. Retrieve the signal mask stored in @var{attr} and copy it to
  994. @code{*@var{sigmask}}. If the signal mask has not been set, return
  995. the special constant @code{PTHREAD_ATTR_NO_SIGMASK_NP}, otherwise
  996. return zero.
  997. @c Move this to the documentation of pthread_getattr_np once it exists.
  998. Obtaining the signal mask only works if it has been previously stored
  999. by @code{pthread_attr_setsigmask_np}. For example, the
  1000. @code{pthread_getattr_np} function does not obtain the current signal
  1001. mask of the specified thread, and @code{pthread_attr_getsigmask_np}
  1002. will subsequently report the signal mask as unset.
  1003. @end deftypefun
  1004. @deftypevr Macro int PTHREAD_ATTR_NO_SIGMASK_NP
  1005. The special value returned by @code{pthread_attr_setsigmask_np} to
  1006. indicate that no signal mask has been set for the attribute.
  1007. @end deftypevr
  1008. It is possible to create a new thread with a specific signal mask
  1009. without using these functions. On the thread that calls
  1010. @code{pthread_create}, the required steps for the general case are:
  1011. @enumerate 1
  1012. @item
  1013. Mask all signals, and save the old signal mask, using
  1014. @code{pthread_sigmask}. This ensures that the new thread will be
  1015. created with all signals masked, so that no signals can be delivered
  1016. to the thread until the desired signal mask is set.
  1017. @item
  1018. Call @code{pthread_create} to create the new thread, passing the
  1019. desired signal mask to the thread start routine (which could be a
  1020. wrapper function for the actual thread start routine). It may be
  1021. necessary to make a copy of the desired signal mask on the heap, so
  1022. that the life-time of the copy extends to the point when the start
  1023. routine needs to access the signal mask.
  1024. @item
  1025. Restore the thread's signal mask, to the set that was saved in the
  1026. first step.
  1027. @end enumerate
  1028. The start routine for the created thread needs to locate the desired
  1029. signal mask and use @code{pthread_sigmask} to apply it to the thread.
  1030. If the signal mask was copied to a heap allocation, the copy should be
  1031. freed.
  1032. @node Thread CPU Affinity
  1033. @subsubsection Thread CPU Affinity
  1034. Processes and threads normally run on any available CPU. However,
  1035. they can be given an @emph{affinity} to one or more CPUs, which limits
  1036. them to the CPU set specified.
  1037. @deftypefun int pthread_attr_setaffinity_np (pthread_attr_t *@var{attr}, size_t @var{cpusetsize}, const cpu_set_t *@var{cpuset})
  1038. Sets the CPU affinity in @var{attr}. The CPU affinity
  1039. controls which CPUs a thread may execute on. @xref{CPU Affinity}.
  1040. @manpagefunctionstub{pthread_attr_setaffinity_np, 3}
  1041. @end deftypefun
  1042. @deftypefun int pthread_attr_getaffinity_np (const pthread_attr_t *@var{attr}, size_t @var{cpusetsize}, cpu_set_t *@var{cpuset})
  1043. Gets the CPU affinity settings from @var{attr}.
  1044. @manpagefunctionstub{pthread_attr_getaffinity_np, 3}
  1045. @end deftypefun
  1046. @deftypefun int pthread_setaffinity_np (pthread_t *@var{th}, size_t @var{cpusetsize}, const cpu_set_t *@var{cpuset})
  1047. Sets the CPU affinity for thread @var{th}. The CPU affinity controls
  1048. which CPUs a thread may execute on. @xref{CPU Affinity}.
  1049. @manpagefunctionstub{pthread_setaffinity_np, 3}
  1050. @end deftypefun
  1051. @deftypefun int pthread_getaffinity_np (const pthread_t *@var{th}, size_t @var{cpusetsize}, cpu_set_t *@var{cpuset})
  1052. Gets the CPU affinity for thread @var{th}. The CPU affinity controls
  1053. which CPUs a thread may execute on. @xref{CPU Affinity}.
  1054. @manpagefunctionstub{pthread_getaffinity_np, 3}
  1055. @end deftypefun
  1056. @node Joining Threads
  1057. @subsubsection Wait for a thread to terminate
  1058. @Theglibc{} provides several extensions to the @code{pthread_join}
  1059. function.
  1060. @comment pthread.h
  1061. @comment GNU extension
  1062. @deftypefun int pthread_tryjoin_np (pthread_t *@var{thread}, void **@var{thread_return})
  1063. @standards{GNU, pthread.h}
  1064. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  1065. Behaves like @code{pthread_join} except that it will return @code{EBUSY}
  1066. immediately if the thread specified by @var{thread} has not yet terminated.
  1067. @end deftypefun
  1068. @comment pthread.h
  1069. @comment GNU extension
  1070. @deftypefun int pthread_timedjoin_np (pthread_t *@var{thread}, void **@var{thread_return}, const struct timespec *@var{abstime})
  1071. @standards{GNU, pthread.h}
  1072. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  1073. Behaves like @code{pthread_tryjoin_np} except that it will block until the
  1074. absolute time @var{abstime} measured against @code{CLOCK_REALTIME} is
  1075. reached if the thread has not terminated by that time and return
  1076. @code{EBUSY}. If @var{abstime} is equal to @code{NULL} then the function
  1077. will wait forever in the same way as @code{pthread_join}.
  1078. @end deftypefun
  1079. @comment pthread.h
  1080. @comment GNU extension
  1081. @deftypefun int pthread_clockjoin_np (pthread_t *@var{thread}, void **@var{thread_return}, clockid_t @var{clockid}, const struct timespec *@var{abstime})
  1082. @standards{GNU, pthread.h}
  1083. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  1084. Behaves like @code{pthread_timedjoin_np} except that the absolute time in
  1085. @var{abstime} is measured against the clock specified by @var{clockid}.
  1086. Currently, @var{clockid} must be either @code{CLOCK_MONOTONIC} or
  1087. @code{CLOCK_REALTIME}.
  1088. @end deftypefun
  1089. The @code{sem_clockwait} function also works using a @code{clockid_t}
  1090. argument. @xref{POSIX Semaphores}.
  1091. @node Thread Names
  1092. @subsubsection Thread Names
  1093. @deftypefun int pthread_setname_np (pthread_t @var{th}, const char *@var{name})
  1094. Gives thread @var{th} the name @var{name}. This name shows up in
  1095. @code{ps} when it's listing individual threads. @var{name} is a
  1096. NUL-terminated string of no more than 15 non-NUL characters.
  1097. @manpagefunctionstub{pthread_setname_np, 3}
  1098. @end deftypefun
  1099. @deftypefun int pthread_getname_np (pthread_t @var{th}, char *@var{buf}, size_t @var{buflen})
  1100. Retrieves the name of thread @var{th}.
  1101. @manpagefunctionstub{pthread_getname_np, 3}
  1102. @end deftypefun
  1103. @node Single-Threaded
  1104. @subsubsection Detecting Single-Threaded Execution
  1105. Multi-threaded programs require synchronization among threads. This
  1106. synchronization can be costly even if there is just a single thread
  1107. and no data is shared between multiple processors. @Theglibc{} offers
  1108. an interface to detect whether the process is in single-threaded mode.
  1109. Applications can use this information to avoid synchronization, for
  1110. example by using regular instructions to load and store memory instead
  1111. of atomic instructions, or using relaxed memory ordering instead of
  1112. stronger memory ordering.
  1113. @deftypevar char __libc_single_threaded
  1114. @standards{GNU, sys/single_threaded.h}
  1115. This variable is non-zero if the current process is definitely
  1116. single-threaded. If it is zero, the process may be multi-threaded,
  1117. or @theglibc{} cannot determine at this point of the program execution
  1118. whether the process is single-threaded or not.
  1119. Applications must never write to this variable.
  1120. @end deftypevar
  1121. Most applications should perform the same actions whether or not
  1122. @code{__libc_single_threaded} is true, except with less
  1123. synchronization. If this rule is followed, a process that
  1124. subsequently becomes multi-threaded is already in a consistent state.
  1125. For example, in order to increment a reference count, the following
  1126. code can be used:
  1127. @smallexample
  1128. if (__libc_single_threaded)
  1129. atomic_fetch_add (&reference_count, 1, memory_order_relaxed);
  1130. else
  1131. atomic_fetch_add (&reference_count, 1, memory_order_acq_rel);
  1132. @end smallexample
  1133. @c Note: No memory order on __libc_single_threaded. The
  1134. @c implementation must ensure that exit of the critical
  1135. @c (second-to-last) thread happens-before setting
  1136. @c __libc_single_threaded to true. Otherwise, acquire MO might be
  1137. @c needed for reading the variable in some scenarios, and that would
  1138. @c completely defeat its purpose.
  1139. This still requires some form of synchronization on the
  1140. single-threaded branch, so it can be beneficial not to declare the
  1141. reference count as @code{_Atomic}, and use the GCC @code{__atomic}
  1142. built-ins. @xref{__atomic Builtins,, Built-in Functions for Memory
  1143. Model Aware Atomic Operations, gcc, Using the GNU Compiler Collection
  1144. (GCC)}. Then the code to increment a reference count looks like this:
  1145. @smallexample
  1146. if (__libc_single_threaded)
  1147. ++reference_count;
  1148. else
  1149. __atomic_fetch_add (&reference_count, 1, __ATOMIC_ACQ_REL);
  1150. @end smallexample
  1151. (Depending on the data associated with the reference count, it may be
  1152. possible to use the weaker @code{__ATOMIC_RELAXED} memory ordering on
  1153. the multi-threaded branch.)
  1154. Several functions in @theglibc{} can change the value of the
  1155. @code{__libc_single_threaded} variable. For example, creating new
  1156. threads using the @code{pthread_create} or @code{thrd_create} function
  1157. sets the variable to false. This can also happen indirectly, say via
  1158. a call to @code{dlopen}. Therefore, applications need to make a copy
  1159. of the value of @code{__libc_single_threaded} if after such a function
  1160. call, behavior must match the value as it was before the call, like
  1161. this:
  1162. @smallexample
  1163. bool single_threaded = __libc_single_threaded;
  1164. if (single_threaded)
  1165. prepare_single_threaded ();
  1166. else
  1167. prepare_multi_thread ();
  1168. void *handle = dlopen (shared_library_name, RTLD_NOW);
  1169. lookup_symbols (handle);
  1170. if (single_threaded)
  1171. cleanup_single_threaded ();
  1172. else
  1173. cleanup_multi_thread ();
  1174. @end smallexample
  1175. Since the value of @code{__libc_single_threaded} can change from true
  1176. to false during the execution of the program, it is not useful for
  1177. selecting optimized function implementations in IFUNC resolvers.
  1178. Atomic operations can also be used on mappings shared among
  1179. single-threaded processes. This means that a compiler must not use
  1180. @code{__libc_single_threaded} to optimize atomic operations, unless it
  1181. is able to prove that the memory is not shared.
  1182. @strong{Implementation Note:} The @code{__libc_single_threaded}
  1183. variable is not declared as @code{volatile} because it is expected
  1184. that compilers optimize a sequence of single-threaded checks into one
  1185. check, for example if several reference counts are updated. The
  1186. current implementation in @theglibc{} does not set the
  1187. @code{__libc_single_threaded} variable to a true value if a process
  1188. turns single-threaded again. Future versions of @theglibc{} may do
  1189. this, but only as the result of function calls which imply an acquire
  1190. (compiler) barrier. (Some compilers assume that well-known functions
  1191. such as @code{malloc} do not write to global variables, and setting
  1192. @code{__libc_single_threaded} would introduce a data race and
  1193. undefined behavior.) In any case, an application must not write to
  1194. @code{__libc_single_threaded} even if it has joined the last
  1195. application-created thread because future versions of @theglibc{} may
  1196. create background threads after the first thread has been created, and
  1197. the application has no way of knowing that these threads are present.
  1198. @node Restartable Sequences
  1199. @subsubsection Restartable Sequences
  1200. This section describes restartable sequences integration for
  1201. @theglibc{}. This functionality is only available on Linux.
  1202. @deftp {Data Type} {struct rseq}
  1203. @standards{Linux, sys/rseq.h}
  1204. The type of the restartable sequences area. Future versions
  1205. of Linux may add additional fields to the end of this structure.
  1206. Users need to obtain the address of the restartable sequences area using
  1207. the thread pointer and the @code{__rseq_offset} variable, described
  1208. below.
  1209. One use of the restartable sequences area is to read the current CPU
  1210. number from its @code{cpu_id} field, as an inline version of
  1211. @code{sched_getcpu}. @Theglibc{} sets the @code{cpu_id} field to
  1212. @code{RSEQ_CPU_ID_REGISTRATION_FAILED} if registration failed or was
  1213. explicitly disabled.
  1214. Furthermore, users can store the address of a @code{struct rseq_cs}
  1215. object into the @code{rseq_cs} field of @code{struct rseq}, thus
  1216. informing the kernel that the thread enters a restartable sequence
  1217. critical section. This pointer and the code areas it itself points to
  1218. must not be left pointing to memory areas which are freed or re-used.
  1219. Several approaches can guarantee this. If the application or library
  1220. can guarantee that the memory used to hold the @code{struct rseq_cs} and
  1221. the code areas it refers to are never freed or re-used, no special
  1222. action must be taken. Else, before that memory is re-used of freed, the
  1223. application is responsible for setting the @code{rseq_cs} field to
  1224. @code{NULL} in each thread's restartable sequence area to guarantee that
  1225. it does not leak dangling references. Because the application does not
  1226. typically have knowledge of libraries' use of restartable sequences, it
  1227. is recommended that libraries using restartable sequences which may end
  1228. up freeing or re-using their memory set the @code{rseq_cs} field to
  1229. @code{NULL} before returning from library functions which use
  1230. restartable sequences.
  1231. The manual for the @code{rseq} system call can be found
  1232. at @uref{https://git.kernel.org/pub/scm/libs/librseq/librseq.git/tree/doc/man/rseq.2}.
  1233. @end deftp
  1234. @deftypevar {ptrdiff_t} __rseq_offset
  1235. @standards{Linux, sys/rseq.h}
  1236. This variable contains the offset between the thread pointer (as defined
  1237. by @code{__builtin_thread_pointer} or the thread pointer register for
  1238. the architecture) and the restartable sequences area. This value is the
  1239. same for all threads in the process. If the restartable sequences area
  1240. is located at a lower address than the location to which the thread
  1241. pointer points, the value is negative.
  1242. @end deftypevar
  1243. @deftypevar {unsigned int} __rseq_size
  1244. @standards{Linux, sys/rseq.h}
  1245. This variable is either zero (if restartable sequence registration
  1246. failed or has been disabled) or the size of the restartable sequence
  1247. registration. This can be different from the size of @code{struct rseq}
  1248. if the kernel has extended the size of the registration. If
  1249. registration is successful, @code{__rseq_size} is at least 20 (the
  1250. initially active size of @code{struct rseq}).
  1251. Previous versions of @theglibc{} set this to 32 even if the kernel only
  1252. supported the initial area of 20 bytes because the value included unused
  1253. padding at the end of the restartable sequence area.
  1254. @end deftypevar
  1255. @deftypevar {unsigned int} __rseq_flags
  1256. @standards{Linux, sys/rseq.h}
  1257. The flags used during restartable sequence registration with the kernel.
  1258. Currently zero.
  1259. @end deftypevar
  1260. @deftypevr Macro int RSEQ_SIG
  1261. @standards{Linux, sys/rseq.h}
  1262. Each supported architecture provides a @code{RSEQ_SIG} macro in
  1263. @file{sys/rseq.h} which contains a signature. That signature is
  1264. expected to be present in the code before each restartable sequences
  1265. abort handler. Failure to provide the expected signature may terminate
  1266. the process with a segmentation fault.
  1267. @end deftypevr
  1268. @c FIXME these are undocumented:
  1269. @c pthread_atfork
  1270. @c pthread_attr_destroy
  1271. @c pthread_attr_getaffinity_np
  1272. @c pthread_attr_getdetachstate
  1273. @c pthread_attr_getguardsize
  1274. @c pthread_attr_getinheritsched
  1275. @c pthread_attr_getschedparam
  1276. @c pthread_attr_getschedpolicy
  1277. @c pthread_attr_getscope
  1278. @c pthread_attr_getstack
  1279. @c pthread_attr_getstackaddr
  1280. @c pthread_attr_getstacksize
  1281. @c pthread_attr_init
  1282. @c pthread_attr_setaffinity_np
  1283. @c pthread_attr_setdetachstate
  1284. @c pthread_attr_setguardsize
  1285. @c pthread_attr_setinheritsched
  1286. @c pthread_attr_setschedparam
  1287. @c pthread_attr_setschedpolicy
  1288. @c pthread_attr_setscope
  1289. @c pthread_attr_setstack
  1290. @c pthread_attr_setstackaddr
  1291. @c pthread_attr_setstacksize
  1292. @c pthread_barrierattr_destroy
  1293. @c pthread_barrierattr_getpshared
  1294. @c pthread_barrierattr_init
  1295. @c pthread_barrierattr_setpshared
  1296. @c pthread_barrier_destroy
  1297. @c pthread_barrier_init
  1298. @c pthread_barrier_wait
  1299. @c pthread_cancel
  1300. @c pthread_cleanup_push
  1301. @c pthread_cleanup_pop
  1302. @c pthread_condattr_destroy
  1303. @c pthread_condattr_getclock
  1304. @c pthread_condattr_getpshared
  1305. @c pthread_condattr_init
  1306. @c pthread_condattr_setclock
  1307. @c pthread_condattr_setpshared
  1308. @c pthread_cond_broadcast
  1309. @c pthread_cond_destroy
  1310. @c pthread_cond_init
  1311. @c pthread_cond_signal
  1312. @c pthread_cond_timedwait
  1313. @c pthread_cond_wait
  1314. @c pthread_create
  1315. @c pthread_detach
  1316. @c pthread_equal
  1317. @c pthread_exit
  1318. @c pthread_getaffinity_np
  1319. @c pthread_getattr_np
  1320. @c pthread_getconcurrency
  1321. @c pthread_getcpuclockid
  1322. @c pthread_getname_np
  1323. @c pthread_getschedparam
  1324. @c pthread_join
  1325. @c pthread_kill
  1326. @c pthread_kill_other_threads_np
  1327. @c pthread_mutexattr_destroy
  1328. @c pthread_mutexattr_getkind_np
  1329. @c pthread_mutexattr_getprioceiling
  1330. @c pthread_mutexattr_getprotocol
  1331. @c pthread_mutexattr_getpshared
  1332. @c pthread_mutexattr_getrobust
  1333. @c pthread_mutexattr_getrobust_np
  1334. @c pthread_mutexattr_gettype
  1335. @c pthread_mutexattr_init
  1336. @c pthread_mutexattr_setkind_np
  1337. @c pthread_mutexattr_setprioceiling
  1338. @c pthread_mutexattr_setprotocol
  1339. @c pthread_mutexattr_setpshared
  1340. @c pthread_mutexattr_setrobust
  1341. @c pthread_mutexattr_setrobust_np
  1342. @c pthread_mutexattr_settype
  1343. @c pthread_mutex_consistent
  1344. @c pthread_mutex_consistent_np
  1345. @c pthread_mutex_destroy
  1346. @c pthread_mutex_getprioceiling
  1347. @c pthread_mutex_init
  1348. @c pthread_mutex_lock
  1349. @c pthread_mutex_setprioceiling
  1350. @c pthread_mutex_timedlock
  1351. @c pthread_mutex_trylock
  1352. @c pthread_mutex_unlock
  1353. @c pthread_once
  1354. @c pthread_rwlockattr_destroy
  1355. @c pthread_rwlockattr_getkind_np
  1356. @c pthread_rwlockattr_getpshared
  1357. @c pthread_rwlockattr_init
  1358. @c pthread_rwlockattr_setkind_np
  1359. @c pthread_rwlockattr_setpshared
  1360. @c pthread_rwlock_destroy
  1361. @c pthread_rwlock_init
  1362. @c pthread_rwlock_rdlock
  1363. @c pthread_rwlock_timedrdlock
  1364. @c pthread_rwlock_timedwrlock
  1365. @c pthread_rwlock_tryrdlock
  1366. @c pthread_rwlock_trywrlock
  1367. @c pthread_rwlock_unlock
  1368. @c pthread_rwlock_wrlock
  1369. @c pthread_self
  1370. @c pthread_setaffinity_np
  1371. @c pthread_setcancelstate
  1372. @c pthread_setcanceltype
  1373. @c pthread_setconcurrency
  1374. @c pthread_setname_np
  1375. @c pthread_setschedparam
  1376. @c pthread_setschedprio
  1377. @c pthread_sigmask
  1378. @c pthread_sigqueue
  1379. @c pthread_spin_destroy
  1380. @c pthread_spin_init
  1381. @c pthread_spin_lock
  1382. @c pthread_spin_trylock
  1383. @c pthread_spin_unlock
  1384. @c pthread_testcancel
  1385. @c pthread_yield