pthread.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___THREAD_SUPPORT_PTHREAD_H
  10. #define _LIBCPP___THREAD_SUPPORT_PTHREAD_H
  11. #include <__chrono/convert_to_timespec.h>
  12. #include <__chrono/duration.h>
  13. #include <__config>
  14. #include <ctime>
  15. #include <errno.h>
  16. #include <pthread.h>
  17. #include <sched.h>
  18. #ifdef __MVS__
  19. # include <__support/ibm/nanosleep.h>
  20. #endif
  21. // Some platforms require <bits/atomic_wide_counter.h> in order for
  22. // PTHREAD_COND_INITIALIZER to be expanded. Normally that would come
  23. // in via <pthread.h>, but it's a non-modular header on those platforms,
  24. // so libc++'s <math.h> usually absorbs atomic_wide_counter.h into the
  25. // module with <math.h> and makes atomic_wide_counter.h invisible.
  26. // Include <math.h> here to work around that.
  27. // This checks wheter a Clang module is built
  28. #if __building_module(std)
  29. # include <math.h>
  30. #endif
  31. #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
  32. # pragma GCC system_header
  33. #endif
  34. _LIBCPP_BEGIN_NAMESPACE_STD
  35. using __libcpp_timespec_t = ::timespec;
  36. //
  37. // Mutex
  38. //
  39. typedef pthread_mutex_t __libcpp_mutex_t;
  40. #define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
  41. typedef pthread_mutex_t __libcpp_recursive_mutex_t;
  42. inline _LIBCPP_HIDE_FROM_ABI int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t* __m) {
  43. pthread_mutexattr_t __attr;
  44. int __ec = pthread_mutexattr_init(&__attr);
  45. if (__ec)
  46. return __ec;
  47. __ec = pthread_mutexattr_settype(&__attr, PTHREAD_MUTEX_RECURSIVE);
  48. if (__ec) {
  49. pthread_mutexattr_destroy(&__attr);
  50. return __ec;
  51. }
  52. __ec = pthread_mutex_init(__m, &__attr);
  53. if (__ec) {
  54. pthread_mutexattr_destroy(&__attr);
  55. return __ec;
  56. }
  57. __ec = pthread_mutexattr_destroy(&__attr);
  58. if (__ec) {
  59. pthread_mutex_destroy(__m);
  60. return __ec;
  61. }
  62. return 0;
  63. }
  64. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int
  65. __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t* __m) {
  66. return pthread_mutex_lock(__m);
  67. }
  68. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS bool
  69. __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t* __m) {
  70. return pthread_mutex_trylock(__m) == 0;
  71. }
  72. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int
  73. __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t* __m) {
  74. return pthread_mutex_unlock(__m);
  75. }
  76. inline _LIBCPP_HIDE_FROM_ABI int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t* __m) {
  77. return pthread_mutex_destroy(__m);
  78. }
  79. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_mutex_lock(__libcpp_mutex_t* __m) {
  80. return pthread_mutex_lock(__m);
  81. }
  82. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS bool __libcpp_mutex_trylock(__libcpp_mutex_t* __m) {
  83. return pthread_mutex_trylock(__m) == 0;
  84. }
  85. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_mutex_unlock(__libcpp_mutex_t* __m) {
  86. return pthread_mutex_unlock(__m);
  87. }
  88. inline _LIBCPP_HIDE_FROM_ABI int __libcpp_mutex_destroy(__libcpp_mutex_t* __m) { return pthread_mutex_destroy(__m); }
  89. //
  90. // Condition Variable
  91. //
  92. typedef pthread_cond_t __libcpp_condvar_t;
  93. #define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER
  94. inline _LIBCPP_HIDE_FROM_ABI int __libcpp_condvar_signal(__libcpp_condvar_t* __cv) { return pthread_cond_signal(__cv); }
  95. inline _LIBCPP_HIDE_FROM_ABI int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv) {
  96. return pthread_cond_broadcast(__cv);
  97. }
  98. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int
  99. __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m) {
  100. return pthread_cond_wait(__cv, __m);
  101. }
  102. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int
  103. __libcpp_condvar_timedwait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m, __libcpp_timespec_t* __ts) {
  104. return pthread_cond_timedwait(__cv, __m, __ts);
  105. }
  106. inline _LIBCPP_HIDE_FROM_ABI int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv) {
  107. return pthread_cond_destroy(__cv);
  108. }
  109. //
  110. // Execute once
  111. //
  112. typedef pthread_once_t __libcpp_exec_once_flag;
  113. #define _LIBCPP_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT
  114. inline _LIBCPP_HIDE_FROM_ABI int __libcpp_execute_once(__libcpp_exec_once_flag* __flag, void (*__init_routine)()) {
  115. return pthread_once(__flag, __init_routine);
  116. }
  117. //
  118. // Thread id
  119. //
  120. #if defined(__MVS__)
  121. typedef unsigned long long __libcpp_thread_id;
  122. #else
  123. typedef pthread_t __libcpp_thread_id;
  124. #endif
  125. // Returns non-zero if the thread ids are equal, otherwise 0
  126. inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_thread_id_equal(__libcpp_thread_id __t1, __libcpp_thread_id __t2) {
  127. return __t1 == __t2;
  128. }
  129. // Returns non-zero if t1 < t2, otherwise 0
  130. inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_thread_id_less(__libcpp_thread_id __t1, __libcpp_thread_id __t2) {
  131. return __t1 < __t2;
  132. }
  133. //
  134. // Thread
  135. //
  136. #define _LIBCPP_NULL_THREAD ((__libcpp_thread_t()))
  137. typedef pthread_t __libcpp_thread_t;
  138. inline _LIBCPP_HIDE_FROM_ABI __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t* __t) {
  139. #if defined(__MVS__)
  140. return __t->__;
  141. #else
  142. return *__t;
  143. #endif
  144. }
  145. inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_thread_isnull(const __libcpp_thread_t* __t) {
  146. return __libcpp_thread_get_id(__t) == 0;
  147. }
  148. inline _LIBCPP_HIDE_FROM_ABI int __libcpp_thread_create(__libcpp_thread_t* __t, void* (*__func)(void*), void* __arg) {
  149. return pthread_create(__t, nullptr, __func, __arg);
  150. }
  151. inline _LIBCPP_HIDE_FROM_ABI __libcpp_thread_id __libcpp_thread_get_current_id() {
  152. const __libcpp_thread_t __current_thread = pthread_self();
  153. return __libcpp_thread_get_id(&__current_thread);
  154. }
  155. inline _LIBCPP_HIDE_FROM_ABI int __libcpp_thread_join(__libcpp_thread_t* __t) { return pthread_join(*__t, nullptr); }
  156. inline _LIBCPP_HIDE_FROM_ABI int __libcpp_thread_detach(__libcpp_thread_t* __t) { return pthread_detach(*__t); }
  157. inline _LIBCPP_HIDE_FROM_ABI void __libcpp_thread_yield() { sched_yield(); }
  158. inline _LIBCPP_HIDE_FROM_ABI void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns) {
  159. __libcpp_timespec_t __ts = std::__convert_to_timespec<__libcpp_timespec_t>(__ns);
  160. while (nanosleep(&__ts, &__ts) == -1 && errno == EINTR)
  161. ;
  162. }
  163. //
  164. // Thread local storage
  165. //
  166. #define _LIBCPP_TLS_DESTRUCTOR_CC /* nothing */
  167. typedef pthread_key_t __libcpp_tls_key;
  168. inline _LIBCPP_HIDE_FROM_ABI int __libcpp_tls_create(__libcpp_tls_key* __key, void (*__at_exit)(void*)) {
  169. return pthread_key_create(__key, __at_exit);
  170. }
  171. inline _LIBCPP_HIDE_FROM_ABI void* __libcpp_tls_get(__libcpp_tls_key __key) { return pthread_getspecific(__key); }
  172. inline _LIBCPP_HIDE_FROM_ABI int __libcpp_tls_set(__libcpp_tls_key __key, void* __p) {
  173. return pthread_setspecific(__key, __p);
  174. }
  175. _LIBCPP_END_NAMESPACE_STD
  176. #endif // _LIBCPP___THREAD_SUPPORT_PTHREAD_H