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 10 #ifndef _LIBCPP___THREAD_SUPPORT_C11_H 11 #define _LIBCPP___THREAD_SUPPORT_C11_H 12 13 #include <__chrono/convert_to_timespec.h> 14 #include <__chrono/duration.h> 15 #include <__config> 16 #include <ctime> 17 #include <errno.h> 18 #include <threads.h> 19 20 #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER 21 # pragma GCC system_header 22 #endif 23 24 _LIBCPP_BEGIN_NAMESPACE_STD 25 26 using __libcpp_timespec_t = ::timespec; 27 28 // 29 // Mutex 30 // 31 typedef mtx_t __libcpp_mutex_t; 32 // mtx_t is a struct so using {} for initialization is valid. 33 #define _LIBCPP_MUTEX_INITIALIZER \ 34 {} 35 36 typedef mtx_t __libcpp_recursive_mutex_t; 37 38 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t* __m) { 39 return mtx_init(__m, mtx_plain | mtx_recursive) == thrd_success ? 0 : EINVAL; 40 } 41 42 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int 43 __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t* __m) { 44 return mtx_lock(__m) == thrd_success ? 0 : EINVAL; 45 } 46 47 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS bool 48 __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t* __m) { 49 return mtx_trylock(__m) == thrd_success; 50 } 51 52 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int 53 __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t* __m) { 54 return mtx_unlock(__m) == thrd_success ? 0 : EINVAL; 55 } 56 57 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t* __m) { 58 mtx_destroy(__m); 59 return 0; 60 } 61 62 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_mutex_lock(__libcpp_mutex_t* __m) { 63 return mtx_lock(__m) == thrd_success ? 0 : EINVAL; 64 } 65 66 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS bool __libcpp_mutex_trylock(__libcpp_mutex_t* __m) { 67 return mtx_trylock(__m) == thrd_success; 68 } 69 70 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_mutex_unlock(__libcpp_mutex_t* __m) { 71 return mtx_unlock(__m) == thrd_success ? 0 : EINVAL; 72 } 73 74 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_mutex_destroy(__libcpp_mutex_t* __m) { 75 mtx_destroy(__m); 76 return 0; 77 } 78 79 // 80 // Condition Variable 81 // 82 typedef cnd_t __libcpp_condvar_t; 83 // cnd_t is a struct so using {} for initialization is valid. 84 #define _LIBCPP_CONDVAR_INITIALIZER \ 85 {} 86 87 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_condvar_signal(__libcpp_condvar_t* __cv) { 88 return cnd_signal(__cv) == thrd_success ? 0 : EINVAL; 89 } 90 91 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv) { 92 return cnd_broadcast(__cv) == thrd_success ? 0 : EINVAL; 93 } 94 95 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int 96 __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m) { 97 return cnd_wait(__cv, __m) == thrd_success ? 0 : EINVAL; 98 } 99 100 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int 101 __libcpp_condvar_timedwait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m, timespec* __ts) { 102 int __ec = cnd_timedwait(__cv, __m, __ts); 103 return __ec == thrd_timedout ? ETIMEDOUT : __ec; 104 } 105 106 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv) { 107 cnd_destroy(__cv); 108 return 0; 109 } 110 111 // 112 // Execute once 113 // 114 typedef ::once_flag __libcpp_exec_once_flag; 115 #define _LIBCPP_EXEC_ONCE_INITIALIZER ONCE_FLAG_INIT 116 117 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_execute_once(__libcpp_exec_once_flag* flag, void (*init_routine)(void)) { 118 ::call_once(flag, init_routine); 119 return 0; 120 } 121 122 // 123 // Thread id 124 // 125 typedef thrd_t __libcpp_thread_id; 126 127 // Returns non-zero if the thread ids are equal, otherwise 0 128 inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2) { 129 return thrd_equal(t1, t2) != 0; 130 } 131 132 // Returns non-zero if t1 < t2, otherwise 0 133 inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2) { 134 return t1 < t2; 135 } 136 137 // 138 // Thread 139 // 140 #define _LIBCPP_NULL_THREAD 0U 141 142 typedef thrd_t __libcpp_thread_t; 143 144 inline _LIBCPP_HIDE_FROM_ABI __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t* __t) { return *__t; } 145 146 inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_thread_isnull(const __libcpp_thread_t* __t) { 147 return __libcpp_thread_get_id(__t) == 0; 148 } 149 150 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_thread_create(__libcpp_thread_t* __t, void* (*__func)(void*), void* __arg) { 151 int __ec = thrd_create(__t, reinterpret_cast<thrd_start_t>(__func), __arg); 152 return __ec == thrd_nomem ? ENOMEM : __ec; 153 } 154 155 inline _LIBCPP_HIDE_FROM_ABI __libcpp_thread_id __libcpp_thread_get_current_id() { return thrd_current(); } 156 157 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_thread_join(__libcpp_thread_t* __t) { 158 return thrd_join(*__t, nullptr) == thrd_success ? 0 : EINVAL; 159 } 160 161 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_thread_detach(__libcpp_thread_t* __t) { 162 return thrd_detach(*__t) == thrd_success ? 0 : EINVAL; 163 } 164 165 inline _LIBCPP_HIDE_FROM_ABI void __libcpp_thread_yield() { thrd_yield(); } 166 167 inline _LIBCPP_HIDE_FROM_ABI void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns) { 168 __libcpp_timespec_t __ts = std::__convert_to_timespec<__libcpp_timespec_t>(__ns); 169 thrd_sleep(&__ts, nullptr); 170 } 171 172 // 173 // Thread local storage 174 // 175 #define _LIBCPP_TLS_DESTRUCTOR_CC /* nothing */ 176 177 typedef tss_t __libcpp_tls_key; 178 179 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_tls_create(__libcpp_tls_key* __key, void (*__at_exit)(void*)) { 180 return tss_create(__key, __at_exit) == thrd_success ? 0 : EINVAL; 181 } 182 183 inline _LIBCPP_HIDE_FROM_ABI void* __libcpp_tls_get(__libcpp_tls_key __key) { return tss_get(__key); } 184 185 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_tls_set(__libcpp_tls_key __key, void* __p) { 186 return tss_set(__key, __p) == thrd_success ? 0 : EINVAL; 187 } 188 189 _LIBCPP_END_NAMESPACE_STD 190 191 #endif // _LIBCPP___THREAD_SUPPORT_C11_H 192