threads.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* ISO C11 Standard: 7.26 - Thread support library <threads.h>.
  2. Copyright (C) 2018-2021 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/struct_timespec.h>
  22. #ifndef __cplusplus
  23. # define thread_local _Thread_local
  24. #endif
  25. #define TSS_DTOR_ITERATIONS 4
  26. typedef __tss_t tss_t;
  27. typedef void (*tss_dtor_t) (void*);
  28. typedef __thrd_t thrd_t;
  29. typedef int (*thrd_start_t) (void*);
  30. /* Exit and error codes. */
  31. enum
  32. {
  33. thrd_success = 0,
  34. thrd_busy = 1,
  35. thrd_error = 2,
  36. thrd_nomem = 3,
  37. thrd_timedout = 4
  38. };
  39. /* Mutex types. */
  40. enum
  41. {
  42. mtx_plain = 0,
  43. mtx_recursive = 1,
  44. mtx_timed = 2
  45. };
  46. typedef __once_flag once_flag;
  47. #define ONCE_FLAG_INIT __ONCE_FLAG_INIT
  48. typedef union
  49. {
  50. char __size[__SIZEOF_PTHREAD_MUTEX_T];
  51. long int __align __LOCK_ALIGNMENT;
  52. } mtx_t;
  53. typedef union
  54. {
  55. char __size[__SIZEOF_PTHREAD_COND_T];
  56. __extension__ long long int __align __LOCK_ALIGNMENT;
  57. } cnd_t;
  58. /* Threads functions. */
  59. /* Create a new thread executing the function __FUNC. Arguments for __FUNC
  60. are passed through __ARG. If succesful, __THR is set to new thread
  61. identifier. */
  62. extern int thrd_create (thrd_t *__thr, thrd_start_t __func, void *__arg);
  63. /* Check if __LHS and __RHS point to the same thread. */
  64. extern int thrd_equal (thrd_t __lhs, thrd_t __rhs);
  65. /* Return current thread identifier. */
  66. extern thrd_t thrd_current (void);
  67. /* Block current thread execution for at least the time pointed by
  68. __TIME_POINT. The current thread may resume if receives a signal. In
  69. that case, if __REMAINING is not NULL, the remaining time is stored in
  70. the object pointed by it. */
  71. extern int thrd_sleep (const struct timespec *__time_point,
  72. struct timespec *__remaining);
  73. /* Terminate current thread execution, cleaning up any thread local
  74. storage and freeing resources. Returns the value specified in __RES. */
  75. extern void thrd_exit (int __res) __attribute__ ((__noreturn__));
  76. /* Detach the thread identified by __THR from the current environment
  77. (it does not allow join or wait for it). */
  78. extern int thrd_detach (thrd_t __thr);
  79. /* Block current thread until execution of __THR is complete. In case that
  80. __RES is not NULL, will store the return value of __THR when exiting. */
  81. extern int thrd_join (thrd_t __thr, int *__res);
  82. /* Stop current thread execution and call the scheduler to decide which
  83. thread should execute next. The current thread may be selected by the
  84. scheduler to keep running. */
  85. extern void thrd_yield (void);
  86. #ifdef __USE_EXTERN_INLINES
  87. /* Optimizations. */
  88. __extern_inline int
  89. thrd_equal (thrd_t __thread1, thrd_t __thread2)
  90. {
  91. return __thread1 == __thread2;
  92. }
  93. #endif
  94. /* Mutex functions. */
  95. /* Creates a new mutex object with type __TYPE. If successful the new
  96. object is pointed by __MUTEX. */
  97. extern int mtx_init (mtx_t *__mutex, int __type);
  98. /* Block the current thread until the mutex pointed to by __MUTEX is
  99. unlocked. In that case current thread will not be blocked. */
  100. extern int mtx_lock (mtx_t *__mutex);
  101. /* Block the current thread until the mutex pointed by __MUTEX is unlocked
  102. or time pointed by __TIME_POINT is reached. In case the mutex is unlock,
  103. the current thread will not be blocked. */
  104. extern int mtx_timedlock (mtx_t *__restrict __mutex,
  105. const struct timespec *__restrict __time_point);
  106. /* Try to lock the mutex pointed by __MUTEX without blocking. If the mutex
  107. is free the current threads takes control of it, otherwise it returns
  108. immediately. */
  109. extern int mtx_trylock (mtx_t *__mutex);
  110. /* Unlock the mutex pointed by __MUTEX. It may potentially awake other
  111. threads waiting on this mutex. */
  112. extern int mtx_unlock (mtx_t *__mutex);
  113. /* Destroy the mutex object pointed by __MUTEX. */
  114. extern void mtx_destroy (mtx_t *__mutex);
  115. /* Call function __FUNC exactly once, even if invoked from several threads.
  116. All calls must be made with the same __FLAGS object. */
  117. extern void call_once (once_flag *__flag, void (*__func)(void));
  118. /* Condition variable functions. */
  119. /* Initialize new condition variable pointed by __COND. */
  120. extern int cnd_init (cnd_t *__cond);
  121. /* Unblock one thread that currently waits on condition variable pointed
  122. by __COND. */
  123. extern int cnd_signal (cnd_t *__cond);
  124. /* Unblock all threads currently waiting on condition variable pointed by
  125. __COND. */
  126. extern int cnd_broadcast (cnd_t *__cond);
  127. /* Block current thread on the condition variable pointed by __COND. */
  128. extern int cnd_wait (cnd_t *__cond, mtx_t *__mutex);
  129. /* Block current thread on the condition variable until condition variable
  130. pointed by __COND is signaled or time pointed by __TIME_POINT is
  131. reached. */
  132. extern int cnd_timedwait (cnd_t *__restrict __cond,
  133. mtx_t *__restrict __mutex,
  134. const struct timespec *__restrict __time_point);
  135. /* Destroy condition variable pointed by __cond and free all of its
  136. resources. */
  137. extern void cnd_destroy (cnd_t *__COND);
  138. /* Thread specific storage functions. */
  139. /* Create new thread-specific storage key and stores it in the object pointed
  140. by __TSS_ID. If __DESTRUCTOR is not NULL, the function will be called when
  141. the thread terminates. */
  142. extern int tss_create (tss_t *__tss_id, tss_dtor_t __destructor);
  143. /* Return the value held in thread-specific storage for the current thread
  144. identified by __TSS_ID. */
  145. extern void *tss_get (tss_t __tss_id);
  146. /* Sets the value of the thread-specific storage identified by __TSS_ID for
  147. the current thread to __VAL. */
  148. extern int tss_set (tss_t __tss_id, void *__val);
  149. /* Destroys the thread-specific storage identified by __TSS_ID. The
  150. destructor is not called until thrd_exit is called. */
  151. extern void tss_delete (tss_t __tss_id);
  152. __END_DECLS
  153. #endif /* _THREADS_H */