2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___THREADING_SUPPORT
11 #define _LIBCPP___THREADING_SUPPORT
13 #include <__availability>
14 #include <__chrono/convert_to_timespec.h>
15 #include <__chrono/duration.h>
17 #include <__fwd/hash.h>
22 # include <__support/ibm/nanosleep.h>
25 #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
26 # pragma GCC system_header
29 #if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
30 # include <__external_threading>
31 #elif !defined(_LIBCPP_HAS_NO_THREADS)
33 #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
34 // Some platforms require <bits/atomic_wide_counter.h> in order for
35 // PTHREAD_COND_INITIALIZER to be expanded. Normally that would come
36 // in via <pthread.h>, but it's a non-modular header on those platforms,
37 // so libc++'s <math.h> usually absorbs atomic_wide_counter.h into the
38 // module with <math.h> and makes atomic_wide_counter.h invisible.
39 // Include <math.h> here to work around that.
44 #elif defined(_LIBCPP_HAS_THREAD_API_C11)
48 #if defined(_LIBCPP_HAS_THREAD_API_WIN32)
49 #define _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_EXPORTED_FROM_ABI
51 #define _LIBCPP_THREAD_ABI_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY
54 typedef ::timespec __libcpp_timespec_t;
55 #endif // !defined(_LIBCPP_HAS_NO_THREADS)
57 _LIBCPP_BEGIN_NAMESPACE_STD
59 #if !defined(_LIBCPP_HAS_NO_THREADS)
61 #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
63 typedef pthread_mutex_t __libcpp_mutex_t;
64 #define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
66 typedef pthread_mutex_t __libcpp_recursive_mutex_t;
69 typedef pthread_cond_t __libcpp_condvar_t;
70 #define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER
73 typedef pthread_once_t __libcpp_exec_once_flag;
74 #define _LIBCPP_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT
78 typedef unsigned long long __libcpp_thread_id;
80 typedef pthread_t __libcpp_thread_id;
84 #define _LIBCPP_NULL_THREAD ((__libcpp_thread_t()))
85 typedef pthread_t __libcpp_thread_t;
87 // Thread Local Storage
88 typedef pthread_key_t __libcpp_tls_key;
90 #define _LIBCPP_TLS_DESTRUCTOR_CC
91 #elif defined(_LIBCPP_HAS_THREAD_API_C11)
93 typedef mtx_t __libcpp_mutex_t;
94 // mtx_t is a struct so using {} for initialization is valid.
95 #define _LIBCPP_MUTEX_INITIALIZER {}
97 typedef mtx_t __libcpp_recursive_mutex_t;
100 typedef cnd_t __libcpp_condvar_t;
101 // cnd_t is a struct so using {} for initialization is valid.
102 #define _LIBCPP_CONDVAR_INITIALIZER {}
105 typedef ::once_flag __libcpp_exec_once_flag;
106 #define _LIBCPP_EXEC_ONCE_INITIALIZER ONCE_FLAG_INIT
109 typedef thrd_t __libcpp_thread_id;
112 #define _LIBCPP_NULL_THREAD 0U
114 typedef thrd_t __libcpp_thread_t;
116 // Thread Local Storage
117 typedef tss_t __libcpp_tls_key;
119 #define _LIBCPP_TLS_DESTRUCTOR_CC
120 #elif !defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
122 typedef void* __libcpp_mutex_t;
123 #define _LIBCPP_MUTEX_INITIALIZER 0
125 #if defined(_M_IX86) || defined(__i386__) || defined(_M_ARM) || defined(__arm__)
126 typedef void* __libcpp_recursive_mutex_t[6];
127 #elif defined(_M_AMD64) || defined(__x86_64__) || defined(_M_ARM64) || defined(__aarch64__)
128 typedef void* __libcpp_recursive_mutex_t[5];
130 # error Unsupported architecture
133 // Condition Variable
134 typedef void* __libcpp_condvar_t;
135 #define _LIBCPP_CONDVAR_INITIALIZER 0
138 typedef void* __libcpp_exec_once_flag;
139 #define _LIBCPP_EXEC_ONCE_INITIALIZER 0
142 typedef long __libcpp_thread_id;
145 #define _LIBCPP_NULL_THREAD 0U
147 typedef void* __libcpp_thread_t;
149 // Thread Local Storage
150 typedef long __libcpp_tls_key;
152 #define _LIBCPP_TLS_DESTRUCTOR_CC __stdcall
153 #endif // !defined(_LIBCPP_HAS_THREAD_API_PTHREAD) && !defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
155 #if !defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
157 _LIBCPP_THREAD_ABI_VISIBILITY
158 int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m);
160 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
161 int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m);
163 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
164 bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m);
166 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
167 int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t *__m);
169 _LIBCPP_THREAD_ABI_VISIBILITY
170 int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m);
172 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
173 int __libcpp_mutex_lock(__libcpp_mutex_t *__m);
175 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
176 bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m);
178 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
179 int __libcpp_mutex_unlock(__libcpp_mutex_t *__m);
181 _LIBCPP_THREAD_ABI_VISIBILITY
182 int __libcpp_mutex_destroy(__libcpp_mutex_t *__m);
184 // Condition variable
185 _LIBCPP_THREAD_ABI_VISIBILITY
186 int __libcpp_condvar_signal(__libcpp_condvar_t* __cv);
188 _LIBCPP_THREAD_ABI_VISIBILITY
189 int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv);
191 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
192 int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m);
194 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
195 int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
196 __libcpp_timespec_t *__ts);
198 _LIBCPP_THREAD_ABI_VISIBILITY
199 int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv);
202 _LIBCPP_THREAD_ABI_VISIBILITY
203 int __libcpp_execute_once(__libcpp_exec_once_flag *__flag,
204 void (*__init_routine)());
207 _LIBCPP_THREAD_ABI_VISIBILITY
208 bool __libcpp_thread_id_equal(__libcpp_thread_id __t1, __libcpp_thread_id __t2);
210 _LIBCPP_THREAD_ABI_VISIBILITY
211 bool __libcpp_thread_id_less(__libcpp_thread_id __t1, __libcpp_thread_id __t2);
214 _LIBCPP_THREAD_ABI_VISIBILITY
215 bool __libcpp_thread_isnull(const __libcpp_thread_t *__t);
217 _LIBCPP_THREAD_ABI_VISIBILITY
218 int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
221 _LIBCPP_THREAD_ABI_VISIBILITY
222 __libcpp_thread_id __libcpp_thread_get_current_id();
224 _LIBCPP_THREAD_ABI_VISIBILITY
225 __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t);
227 _LIBCPP_THREAD_ABI_VISIBILITY
228 int __libcpp_thread_join(__libcpp_thread_t *__t);
230 _LIBCPP_THREAD_ABI_VISIBILITY
231 int __libcpp_thread_detach(__libcpp_thread_t *__t);
233 _LIBCPP_THREAD_ABI_VISIBILITY
234 void __libcpp_thread_yield();
236 _LIBCPP_THREAD_ABI_VISIBILITY
237 void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns);
239 // Thread local storage
240 _LIBCPP_THREAD_ABI_VISIBILITY
241 int __libcpp_tls_create(__libcpp_tls_key* __key,
242 void(_LIBCPP_TLS_DESTRUCTOR_CC* __at_exit)(void*));
244 _LIBCPP_THREAD_ABI_VISIBILITY
245 void *__libcpp_tls_get(__libcpp_tls_key __key);
247 _LIBCPP_THREAD_ABI_VISIBILITY
248 int __libcpp_tls_set(__libcpp_tls_key __key, void *__p);
250 #endif // !defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
252 #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
254 int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m)
256 pthread_mutexattr_t __attr;
257 int __ec = pthread_mutexattr_init(&__attr);
260 __ec = pthread_mutexattr_settype(&__attr, PTHREAD_MUTEX_RECURSIVE);
262 pthread_mutexattr_destroy(&__attr);
265 __ec = pthread_mutex_init(__m, &__attr);
267 pthread_mutexattr_destroy(&__attr);
270 __ec = pthread_mutexattr_destroy(&__attr);
272 pthread_mutex_destroy(__m);
278 int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m)
280 return pthread_mutex_lock(__m);
283 bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m)
285 return pthread_mutex_trylock(__m) == 0;
288 int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t *__m)
290 return pthread_mutex_unlock(__m);
293 int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m)
295 return pthread_mutex_destroy(__m);
298 int __libcpp_mutex_lock(__libcpp_mutex_t *__m)
300 return pthread_mutex_lock(__m);
303 bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m)
305 return pthread_mutex_trylock(__m) == 0;
308 int __libcpp_mutex_unlock(__libcpp_mutex_t *__m)
310 return pthread_mutex_unlock(__m);
313 int __libcpp_mutex_destroy(__libcpp_mutex_t *__m)
315 return pthread_mutex_destroy(__m);
318 // Condition Variable
319 int __libcpp_condvar_signal(__libcpp_condvar_t *__cv)
321 return pthread_cond_signal(__cv);
324 int __libcpp_condvar_broadcast(__libcpp_condvar_t *__cv)
326 return pthread_cond_broadcast(__cv);
329 int __libcpp_condvar_wait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m)
331 return pthread_cond_wait(__cv, __m);
334 int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
335 __libcpp_timespec_t *__ts)
337 return pthread_cond_timedwait(__cv, __m, __ts);
340 int __libcpp_condvar_destroy(__libcpp_condvar_t *__cv)
342 return pthread_cond_destroy(__cv);
346 int __libcpp_execute_once(__libcpp_exec_once_flag *__flag,
347 void (*__init_routine)()) {
348 return pthread_once(__flag, __init_routine);
352 // Returns non-zero if the thread ids are equal, otherwise 0
353 bool __libcpp_thread_id_equal(__libcpp_thread_id __t1, __libcpp_thread_id __t2)
358 // Returns non-zero if t1 < t2, otherwise 0
359 bool __libcpp_thread_id_less(__libcpp_thread_id __t1, __libcpp_thread_id __t2)
365 bool __libcpp_thread_isnull(const __libcpp_thread_t *__t) {
366 return __libcpp_thread_get_id(__t) == 0;
369 int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
372 return pthread_create(__t, nullptr, __func, __arg);
375 __libcpp_thread_id __libcpp_thread_get_current_id()
377 const __libcpp_thread_t __current_thread = pthread_self();
378 return __libcpp_thread_get_id(&__current_thread);
381 __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t)
390 int __libcpp_thread_join(__libcpp_thread_t *__t)
392 return pthread_join(*__t, nullptr);
395 int __libcpp_thread_detach(__libcpp_thread_t *__t)
397 return pthread_detach(*__t);
400 void __libcpp_thread_yield()
405 void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns)
407 __libcpp_timespec_t __ts = _VSTD::__convert_to_timespec<__libcpp_timespec_t>(__ns);
408 while (nanosleep(&__ts, &__ts) == -1 && errno == EINTR);
411 // Thread local storage
412 int __libcpp_tls_create(__libcpp_tls_key *__key, void (*__at_exit)(void *))
414 return pthread_key_create(__key, __at_exit);
417 void *__libcpp_tls_get(__libcpp_tls_key __key)
419 return pthread_getspecific(__key);
422 int __libcpp_tls_set(__libcpp_tls_key __key, void *__p)
424 return pthread_setspecific(__key, __p);
427 #elif defined(_LIBCPP_HAS_THREAD_API_C11)
429 int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m)
431 return mtx_init(__m, mtx_plain | mtx_recursive) == thrd_success ? 0 : EINVAL;
434 int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m)
436 return mtx_lock(__m) == thrd_success ? 0 : EINVAL;
439 bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m)
441 return mtx_trylock(__m) == thrd_success;
444 int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t *__m)
446 return mtx_unlock(__m) == thrd_success ? 0 : EINVAL;
449 int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m)
455 int __libcpp_mutex_lock(__libcpp_mutex_t *__m)
457 return mtx_lock(__m) == thrd_success ? 0 : EINVAL;
460 bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m)
462 return mtx_trylock(__m) == thrd_success;
465 int __libcpp_mutex_unlock(__libcpp_mutex_t *__m)
467 return mtx_unlock(__m) == thrd_success ? 0 : EINVAL;
470 int __libcpp_mutex_destroy(__libcpp_mutex_t *__m)
476 // Condition Variable
477 int __libcpp_condvar_signal(__libcpp_condvar_t *__cv)
479 return cnd_signal(__cv) == thrd_success ? 0 : EINVAL;
482 int __libcpp_condvar_broadcast(__libcpp_condvar_t *__cv)
484 return cnd_broadcast(__cv) == thrd_success ? 0 : EINVAL;
487 int __libcpp_condvar_wait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m)
489 return cnd_wait(__cv, __m) == thrd_success ? 0 : EINVAL;
492 int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
495 int __ec = cnd_timedwait(__cv, __m, __ts);
496 return __ec == thrd_timedout ? ETIMEDOUT : __ec;
499 int __libcpp_condvar_destroy(__libcpp_condvar_t *__cv)
506 int __libcpp_execute_once(__libcpp_exec_once_flag *flag,
507 void (*init_routine)(void)) {
508 ::call_once(flag, init_routine);
513 // Returns non-zero if the thread ids are equal, otherwise 0
514 bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2)
516 return thrd_equal(t1, t2) != 0;
519 // Returns non-zero if t1 < t2, otherwise 0
520 bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2)
526 bool __libcpp_thread_isnull(const __libcpp_thread_t *__t) {
527 return __libcpp_thread_get_id(__t) == 0;
530 int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
533 int __ec = thrd_create(__t, reinterpret_cast<thrd_start_t>(__func), __arg);
534 return __ec == thrd_nomem ? ENOMEM : __ec;
537 __libcpp_thread_id __libcpp_thread_get_current_id()
539 return thrd_current();
542 __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t)
547 int __libcpp_thread_join(__libcpp_thread_t *__t)
549 return thrd_join(*__t, nullptr) == thrd_success ? 0 : EINVAL;
552 int __libcpp_thread_detach(__libcpp_thread_t *__t)
554 return thrd_detach(*__t) == thrd_success ? 0 : EINVAL;
557 void __libcpp_thread_yield()
562 void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns)
564 __libcpp_timespec_t __ts = _VSTD::__convert_to_timespec<__libcpp_timespec_t>(__ns);
565 thrd_sleep(&__ts, nullptr);
568 // Thread local storage
569 int __libcpp_tls_create(__libcpp_tls_key *__key, void (*__at_exit)(void *))
571 return tss_create(__key, __at_exit) == thrd_success ? 0 : EINVAL;
574 void *__libcpp_tls_get(__libcpp_tls_key __key)
576 return tss_get(__key);
579 int __libcpp_tls_set(__libcpp_tls_key __key, void *__p)
581 return tss_set(__key, __p) == thrd_success ? 0 : EINVAL;
586 #endif // !_LIBCPP_HAS_NO_THREADS
588 _LIBCPP_END_NAMESPACE_STD
590 #endif // _LIBCPP___THREADING_SUPPORT