2 //===----------------------------------------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_THREADING_SUPPORT
12 #define _LIBCPP_THREADING_SUPPORT
18 #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
19 #pragma GCC system_header
22 #if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
23 # include <__external_threading>
24 #elif !defined(_LIBCPP_HAS_NO_THREADS)
26 #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
29 #elif defined(_LIBCPP_HAS_THREAD_API_WIN32)
32 #include <fibersapi.h>
35 #if defined(_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) || \
36 defined(_LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL)
37 #define _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_FUNC_VIS
39 #define _LIBCPP_THREAD_ABI_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY
42 #if defined(__FreeBSD__) && defined(__clang__) && __has_attribute(no_thread_safety_analysis)
43 #define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis))
45 #define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
48 _LIBCPP_BEGIN_NAMESPACE_STD
50 #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
52 typedef pthread_mutex_t __libcpp_mutex_t;
53 #define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
55 typedef pthread_mutex_t __libcpp_recursive_mutex_t;
58 typedef pthread_cond_t __libcpp_condvar_t;
59 #define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER
62 typedef pthread_once_t __libcpp_exec_once_flag;
63 #define _LIBCPP_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT
66 typedef pthread_t __libcpp_thread_id;
69 #define _LIBCPP_NULL_THREAD 0U
71 typedef pthread_t __libcpp_thread_t;
73 // Thrad Local Storage
74 typedef pthread_key_t __libcpp_tls_key;
76 #define _LIBCPP_TLS_DESTRUCTOR_CC
79 typedef SRWLOCK __libcpp_mutex_t;
80 #define _LIBCPP_MUTEX_INITIALIZER SRWLOCK_INIT
82 typedef CRITICAL_SECTION __libcpp_recursive_mutex_t;
85 typedef CONDITION_VARIABLE __libcpp_condvar_t;
86 #define _LIBCPP_CONDVAR_INITIALIZER CONDITION_VARIABLE_INIT
89 typedef INIT_ONCE __libcpp_exec_once_flag;
90 #define _LIBCPP_EXEC_ONCE_INITIALIZER INIT_ONCE_STATIC_INIT
93 typedef DWORD __libcpp_thread_id;
96 #define _LIBCPP_NULL_THREAD 0U
98 typedef HANDLE __libcpp_thread_t;
100 // Thread Local Storage
101 typedef DWORD __libcpp_tls_key;
103 #define _LIBCPP_TLS_DESTRUCTOR_CC WINAPI
107 _LIBCPP_THREAD_ABI_VISIBILITY
108 int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m);
110 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
111 int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m);
113 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
114 bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m);
116 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
117 int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t *__m);
119 _LIBCPP_THREAD_ABI_VISIBILITY
120 int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m);
122 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
123 int __libcpp_mutex_lock(__libcpp_mutex_t *__m);
125 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
126 bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m);
128 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
129 int __libcpp_mutex_unlock(__libcpp_mutex_t *__m);
131 _LIBCPP_THREAD_ABI_VISIBILITY
132 int __libcpp_mutex_destroy(__libcpp_mutex_t *__m);
134 // Condition variable
135 _LIBCPP_THREAD_ABI_VISIBILITY
136 int __libcpp_condvar_signal(__libcpp_condvar_t* __cv);
138 _LIBCPP_THREAD_ABI_VISIBILITY
139 int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv);
141 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
142 int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m);
144 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
145 int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
148 _LIBCPP_THREAD_ABI_VISIBILITY
149 int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv);
152 _LIBCPP_THREAD_ABI_VISIBILITY
153 int __libcpp_execute_once(__libcpp_exec_once_flag *flag,
154 void (*init_routine)(void));
157 _LIBCPP_THREAD_ABI_VISIBILITY
158 bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2);
160 _LIBCPP_THREAD_ABI_VISIBILITY
161 bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2);
164 _LIBCPP_THREAD_ABI_VISIBILITY
165 bool __libcpp_thread_isnull(const __libcpp_thread_t *__t);
167 _LIBCPP_THREAD_ABI_VISIBILITY
168 int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
171 _LIBCPP_THREAD_ABI_VISIBILITY
172 __libcpp_thread_id __libcpp_thread_get_current_id();
174 _LIBCPP_THREAD_ABI_VISIBILITY
175 __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t);
177 _LIBCPP_THREAD_ABI_VISIBILITY
178 int __libcpp_thread_join(__libcpp_thread_t *__t);
180 _LIBCPP_THREAD_ABI_VISIBILITY
181 int __libcpp_thread_detach(__libcpp_thread_t *__t);
183 _LIBCPP_THREAD_ABI_VISIBILITY
184 void __libcpp_thread_yield();
186 _LIBCPP_THREAD_ABI_VISIBILITY
187 void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns);
189 // Thread local storage
190 _LIBCPP_THREAD_ABI_VISIBILITY
191 int __libcpp_tls_create(__libcpp_tls_key* __key,
192 void(_LIBCPP_TLS_DESTRUCTOR_CC* __at_exit)(void*));
194 _LIBCPP_THREAD_ABI_VISIBILITY
195 void *__libcpp_tls_get(__libcpp_tls_key __key);
197 _LIBCPP_THREAD_ABI_VISIBILITY
198 int __libcpp_tls_set(__libcpp_tls_key __key, void *__p);
200 #if !defined(_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) || \
201 defined(_LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL)
203 #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
205 int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m)
207 pthread_mutexattr_t attr;
208 int __ec = pthread_mutexattr_init(&attr);
211 __ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
213 pthread_mutexattr_destroy(&attr);
216 __ec = pthread_mutex_init(__m, &attr);
218 pthread_mutexattr_destroy(&attr);
221 __ec = pthread_mutexattr_destroy(&attr);
223 pthread_mutex_destroy(__m);
229 int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m)
231 return pthread_mutex_lock(__m);
234 bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m)
236 return pthread_mutex_trylock(__m) == 0;
239 int __libcpp_recursive_mutex_unlock(__libcpp_mutex_t *__m)
241 return pthread_mutex_unlock(__m);
244 int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m)
246 return pthread_mutex_destroy(__m);
249 int __libcpp_mutex_lock(__libcpp_mutex_t *__m)
251 return pthread_mutex_lock(__m);
254 bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m)
256 return pthread_mutex_trylock(__m) == 0;
259 int __libcpp_mutex_unlock(__libcpp_mutex_t *__m)
261 return pthread_mutex_unlock(__m);
264 int __libcpp_mutex_destroy(__libcpp_mutex_t *__m)
266 return pthread_mutex_destroy(__m);
269 // Condition Variable
270 int __libcpp_condvar_signal(__libcpp_condvar_t *__cv)
272 return pthread_cond_signal(__cv);
275 int __libcpp_condvar_broadcast(__libcpp_condvar_t *__cv)
277 return pthread_cond_broadcast(__cv);
280 int __libcpp_condvar_wait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m)
282 return pthread_cond_wait(__cv, __m);
285 int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
288 return pthread_cond_timedwait(__cv, __m, __ts);
291 int __libcpp_condvar_destroy(__libcpp_condvar_t *__cv)
293 return pthread_cond_destroy(__cv);
297 int __libcpp_execute_once(__libcpp_exec_once_flag *flag,
298 void (*init_routine)(void)) {
299 return pthread_once(flag, init_routine);
303 // Returns non-zero if the thread ids are equal, otherwise 0
304 bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2)
306 return pthread_equal(t1, t2) != 0;
309 // Returns non-zero if t1 < t2, otherwise 0
310 bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2)
316 bool __libcpp_thread_isnull(const __libcpp_thread_t *__t) {
320 int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
323 return pthread_create(__t, 0, __func, __arg);
326 __libcpp_thread_id __libcpp_thread_get_current_id()
328 return pthread_self();
331 __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t)
336 int __libcpp_thread_join(__libcpp_thread_t *__t)
338 return pthread_join(*__t, 0);
341 int __libcpp_thread_detach(__libcpp_thread_t *__t)
343 return pthread_detach(*__t);
346 void __libcpp_thread_yield()
351 void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns)
353 using namespace chrono;
354 seconds __s = duration_cast<seconds>(__ns);
356 typedef decltype(__ts.tv_sec) ts_sec;
357 _LIBCPP_CONSTEXPR ts_sec __ts_sec_max = numeric_limits<ts_sec>::max();
359 if (__s.count() < __ts_sec_max)
361 __ts.tv_sec = static_cast<ts_sec>(__s.count());
362 __ts.tv_nsec = static_cast<decltype(__ts.tv_nsec)>((__ns - __s).count());
366 __ts.tv_sec = __ts_sec_max;
367 __ts.tv_nsec = 999999999; // (10^9 - 1)
370 while (nanosleep(&__ts, &__ts) == -1 && errno == EINTR);
373 // Thread local storage
374 int __libcpp_tls_create(__libcpp_tls_key *__key, void (*__at_exit)(void *))
376 return pthread_key_create(__key, __at_exit);
379 void *__libcpp_tls_get(__libcpp_tls_key __key)
381 return pthread_getspecific(__key);
384 int __libcpp_tls_set(__libcpp_tls_key __key, void *__p)
386 return pthread_setspecific(__key, __p);
389 #elif defined(_LIBCPP_HAS_THREAD_API_WIN32)
392 int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m)
394 InitializeCriticalSection(__m);
398 int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m)
400 EnterCriticalSection(__m);
404 bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m)
406 return TryEnterCriticalSection(__m) != 0;
409 int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t *__m)
411 LeaveCriticalSection(__m);
415 int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m)
417 DeleteCriticalSection(__m);
421 int __libcpp_mutex_lock(__libcpp_mutex_t *__m)
423 AcquireSRWLockExclusive(__m);
427 bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m)
429 return TryAcquireSRWLockExclusive(__m) != 0;
432 int __libcpp_mutex_unlock(__libcpp_mutex_t *__m)
434 ReleaseSRWLockExclusive(__m);
438 int __libcpp_mutex_destroy(__libcpp_mutex_t *__m)
440 static_cast<void>(__m);
444 // Condition Variable
445 int __libcpp_condvar_signal(__libcpp_condvar_t *__cv)
447 WakeConditionVariable(__cv);
451 int __libcpp_condvar_broadcast(__libcpp_condvar_t *__cv)
453 WakeAllConditionVariable(__cv);
457 int __libcpp_condvar_wait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m)
459 SleepConditionVariableSRW(__cv, __m, INFINITE, 0);
463 int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
466 using namespace _VSTD::chrono;
468 auto duration = seconds(__ts->tv_sec) + nanoseconds(__ts->tv_nsec);
470 system_clock::time_point(duration_cast<system_clock::duration>(duration));
471 auto timeout_ms = duration_cast<milliseconds>(abstime - system_clock::now());
473 if (!SleepConditionVariableSRW(__cv, __m,
474 timeout_ms.count() > 0 ? timeout_ms.count()
478 auto __ec = GetLastError();
479 return __ec == ERROR_TIMEOUT ? ETIMEDOUT : __ec;
484 int __libcpp_condvar_destroy(__libcpp_condvar_t *__cv)
486 static_cast<void>(__cv);
491 static inline _LIBCPP_ALWAYS_INLINE BOOL CALLBACK
492 __libcpp_init_once_execute_once_thunk(PINIT_ONCE __init_once, PVOID __parameter,
495 static_cast<void>(__init_once);
496 static_cast<void>(__context);
498 void (*init_routine)(void) = reinterpret_cast<void (*)(void)>(__parameter);
503 int __libcpp_execute_once(__libcpp_exec_once_flag *__flag,
504 void (*__init_routine)(void))
506 if (!InitOnceExecuteOnce(__flag, __libcpp_init_once_execute_once_thunk,
507 reinterpret_cast<void *>(__init_routine), NULL))
508 return GetLastError();
513 bool __libcpp_thread_id_equal(__libcpp_thread_id __lhs,
514 __libcpp_thread_id __rhs)
516 return __lhs == __rhs;
519 bool __libcpp_thread_id_less(__libcpp_thread_id __lhs, __libcpp_thread_id __rhs)
521 return __lhs < __rhs;
525 struct __libcpp_beginthreadex_thunk_data
527 void *(*__func)(void *);
531 static inline _LIBCPP_ALWAYS_INLINE unsigned WINAPI
532 __libcpp_beginthreadex_thunk(void *__raw_data)
535 static_cast<__libcpp_beginthreadex_thunk_data *>(__raw_data);
536 auto *__func = __data->__func;
537 void *__arg = __data->__arg;
539 return static_cast<unsigned>(reinterpret_cast<uintptr_t>(__func(__arg)));
542 bool __libcpp_thread_isnull(const __libcpp_thread_t *__t) {
546 int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
549 auto *__data = new __libcpp_beginthreadex_thunk_data;
550 __data->__func = __func;
551 __data->__arg = __arg;
553 *__t = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0,
554 __libcpp_beginthreadex_thunk,
555 __data, 0, nullptr));
559 return GetLastError();
562 __libcpp_thread_id __libcpp_thread_get_current_id()
564 return GetCurrentThreadId();
567 __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t)
569 return GetThreadId(*__t);
572 int __libcpp_thread_join(__libcpp_thread_t *__t)
574 if (WaitForSingleObjectEx(*__t, INFINITE, FALSE) == WAIT_FAILED)
575 return GetLastError();
576 if (!CloseHandle(*__t))
577 return GetLastError();
581 int __libcpp_thread_detach(__libcpp_thread_t *__t)
583 if (!CloseHandle(*__t))
584 return GetLastError();
588 void __libcpp_thread_yield()
593 void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns)
595 using namespace chrono;
596 // round-up to the nearest milisecond
598 duration_cast<milliseconds>(__ns + chrono::nanoseconds(999999));
599 // FIXME(compnerd) this should be an alertable sleep (WFSO or SleepEx)
603 // Thread Local Storage
604 int __libcpp_tls_create(__libcpp_tls_key* __key,
605 void(_LIBCPP_TLS_DESTRUCTOR_CC* __at_exit)(void*))
607 *__key = FlsAlloc(__at_exit);
608 if (*__key == FLS_OUT_OF_INDEXES)
609 return GetLastError();
613 void *__libcpp_tls_get(__libcpp_tls_key __key)
615 return FlsGetValue(__key);
618 int __libcpp_tls_set(__libcpp_tls_key __key, void *__p)
620 if (!FlsSetValue(__key, __p))
621 return GetLastError();
625 #endif // _LIBCPP_HAS_THREAD_API_PTHREAD
627 #endif // !_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL || _LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL
629 _LIBCPP_END_NAMESPACE_STD
631 #endif // !_LIBCPP_HAS_NO_THREADS
633 #endif // _LIBCPP_THREADING_SUPPORT