threads.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* ISO C11 Standard: 7.26 - Thread support library <threads.h>.
  2. Copyright (C) 2018-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _THREADS_H
  16. #define _THREADS_H 1
  17. #include <features.h>
  18. #include <time.h>
  19. __BEGIN_DECLS
  20. #include <bits/thread-shared-types.h>
  21. #include <bits/types/once_flag.h>
  22. #include <bits/types/struct_timespec.h>
  23. #if (!defined __STDC_VERSION__ \
  24. || __STDC_VERSION__ <= 201710L \
  25. || !__GNUC_PREREQ (13, 0)) && !defined __cplusplus
  26. # define thread_local _Thread_local
  27. #endif
  28. #define TSS_DTOR_ITERATIONS 4
  29. typedef __tss_t tss_t;
  30. typedef void (*tss_dtor_t) (void*);
  31. typedef __thrd_t thrd_t;
  32. typedef int (*thrd_start_t) (void*);
  33. /* Exit and error codes. */
  34. enum
  35. {
  36. thrd_success = 0,
  37. thrd_busy = 1,
  38. thrd_error = 2,
  39. thrd_nomem = 3,
  40. thrd_timedout = 4
  41. };
  42. /* Mutex types. */
  43. enum
  44. {
  45. mtx_plain = 0,
  46. mtx_recursive = 1,
  47. mtx_timed = 2
  48. };
  49. typedef union
  50. {
  51. char __size[__SIZEOF_PTHREAD_MUTEX_T];
  52. long int __align __LOCK_ALIGNMENT;
  53. } mtx_t;
  54. typedef union
  55. {
  56. char __size[__SIZEOF_PTHREAD_COND_T];
  57. __extension__ long long int __align __LOCK_ALIGNMENT;
  58. } cnd_t;
  59. /* Threads functions. */
  60. /* Create a new thread executing the function __FUNC. Arguments for __FUNC
  61. are passed through __ARG. If successful, __THR is set to new thread
  62. identifier. */
  63. extern int thrd_create (thrd_t *__thr, thrd_start_t __func, void *__arg);
  64. /* Check if __LHS and __RHS point to the same thread. */
  65. extern int thrd_equal (thrd_t __lhs, thrd_t __rhs);
  66. /* Return current thread identifier. */
  67. extern thrd_t thrd_current (void);
  68. /* Block current thread execution for at least the time pointed by
  69. __TIME_POINT. The current thread may resume if receives a signal. In
  70. that case, if __REMAINING is not NULL, the remaining time is stored in
  71. the object pointed by it. */
  72. #ifndef __USE_TIME64_REDIRECTS
  73. extern int thrd_sleep (const struct timespec *__time_point,
  74. struct timespec *__remaining);
  75. #else
  76. # ifdef __REDIRECT
  77. extern int __REDIRECT (thrd_sleep, (const struct timespec *__time_point,
  78. struct timespec *__remaining),
  79. __thrd_sleep64);
  80. # else
  81. # define thrd_sleep __thrd_sleep64
  82. # endif
  83. #endif
  84. /* Terminate current thread execution, cleaning up any thread local
  85. storage and freeing resources. Returns the value specified in __RES. */
  86. extern void thrd_exit (int __res) __attribute__ ((__noreturn__));
  87. /* Detach the thread identified by __THR from the current environment
  88. (it does not allow join or wait for it). */
  89. extern int thrd_detach (thrd_t __thr);
  90. /* Block current thread until execution of __THR is complete. In case that
  91. __RES is not NULL, will store the return value of __THR when exiting. */
  92. extern int thrd_join (thrd_t __thr, int *__res);
  93. /* Stop current thread execution and call the scheduler to decide which
  94. thread should execute next. The current thread may be selected by the
  95. scheduler to keep running. */
  96. extern void thrd_yield (void);
  97. #ifdef __USE_EXTERN_INLINES
  98. /* Optimizations. */
  99. __extern_inline int
  100. thrd_equal (thrd_t __thread1, thrd_t __thread2)
  101. {
  102. return __thread1 == __thread2;
  103. }
  104. #endif
  105. /* Mutex functions. */
  106. /* Creates a new mutex object with type __TYPE. If successful the new
  107. object is pointed by __MUTEX. */
  108. extern int mtx_init (mtx_t *__mutex, int __type);
  109. /* Block the current thread until the mutex pointed to by __MUTEX is
  110. unlocked. In that case current thread will not be blocked. */
  111. extern int mtx_lock (mtx_t *__mutex);
  112. /* Block the current thread until the mutex pointed by __MUTEX is unlocked
  113. or time pointed by __TIME_POINT is reached. In case the mutex is unlock,
  114. the current thread will not be blocked. */
  115. #ifndef __USE_TIME64_REDIRECTS
  116. extern int mtx_timedlock (mtx_t *__restrict __mutex,
  117. const struct timespec *__restrict __time_point);
  118. #else
  119. # ifdef __REDIRECT
  120. extern int __REDIRECT (mtx_timedlock, (mtx_t *__restrict __mutex,
  121. const struct timespec *__restrict
  122. __time_point),
  123. __mtx_timedlock64);
  124. # else
  125. # define mtx_timedlock __mtx_timedlock64
  126. # endif
  127. #endif
  128. /* Try to lock the mutex pointed by __MUTEX without blocking. If the mutex
  129. is free the current threads takes control of it, otherwise it returns
  130. immediately. */
  131. extern int mtx_trylock (mtx_t *__mutex);
  132. /* Unlock the mutex pointed by __MUTEX. It may potentially awake other
  133. threads waiting on this mutex. */
  134. extern int mtx_unlock (mtx_t *__mutex);
  135. /* Destroy the mutex object pointed by __MUTEX. */
  136. extern void mtx_destroy (mtx_t *__mutex);
  137. /* Call function __FUNC exactly once, even if invoked from several threads.
  138. All calls must be made with the same __FLAGS object. */
  139. extern void call_once (once_flag *__flag, void (*__func)(void));
  140. /* Condition variable functions. */
  141. /* Initialize new condition variable pointed by __COND. */
  142. extern int cnd_init (cnd_t *__cond);
  143. /* Unblock one thread that currently waits on condition variable pointed
  144. by __COND. */
  145. extern int cnd_signal (cnd_t *__cond);
  146. /* Unblock all threads currently waiting on condition variable pointed by
  147. __COND. */
  148. extern int cnd_broadcast (cnd_t *__cond);
  149. /* Block current thread on the condition variable pointed by __COND. */
  150. extern int cnd_wait (cnd_t *__cond, mtx_t *__mutex);
  151. /* Block current thread on the condition variable until condition variable
  152. pointed by __COND is signaled or time pointed by __TIME_POINT is
  153. reached. */
  154. #ifndef __USE_TIME64_REDIRECTS
  155. extern int cnd_timedwait (cnd_t *__restrict __cond,
  156. mtx_t *__restrict __mutex,
  157. const struct timespec *__restrict __time_point);
  158. #else
  159. # ifdef __REDIRECT
  160. extern int __REDIRECT (cnd_timedwait, (cnd_t *__restrict __cond,
  161. mtx_t *__restrict __mutex,
  162. const struct timespec *__restrict
  163. __time_point),
  164. __cnd_timedwait64);
  165. # else
  166. # define cnd_timedwait __cnd_timedwait64
  167. # endif
  168. #endif
  169. /* Destroy condition variable pointed by __cond and free all of its
  170. resources. */
  171. extern void cnd_destroy (cnd_t *__COND);
  172. /* Thread specific storage functions. */
  173. /* Create new thread-specific storage key and stores it in the object pointed
  174. by __TSS_ID. If __DESTRUCTOR is not NULL, the function will be called when
  175. the thread terminates. */
  176. extern int tss_create (tss_t *__tss_id, tss_dtor_t __destructor);
  177. /* Return the value held in thread-specific storage for the current thread
  178. identified by __TSS_ID. */
  179. extern void *tss_get (tss_t __tss_id);
  180. /* Sets the value of the thread-specific storage identified by __TSS_ID for
  181. the current thread to __VAL. */
  182. extern int tss_set (tss_t __tss_id, void *__val);
  183. /* Destroys the thread-specific storage identified by __TSS_ID. The
  184. destructor is not called until thrd_exit is called. */
  185. extern void tss_delete (tss_t __tss_id);
  186. __END_DECLS
  187. #endif /* _THREADS_H */