threads.h 6.5 KB

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