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>
15 #include <__thread/poll_with_backoff.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)
36 #elif defined(_LIBCPP_HAS_THREAD_API_C11)
40 #if defined(_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) || \
41 defined(_LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL) || \
42 defined(_LIBCPP_HAS_THREAD_API_WIN32)
43 #define _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_FUNC_VIS
45 #define _LIBCPP_THREAD_ABI_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY
48 #if defined(__FreeBSD__) && defined(__clang__) && __has_attribute(no_thread_safety_analysis)
49 #define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis))
51 #define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
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_LIBRARY_EXTERNAL) || \
253 defined(_LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL))
255 #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
257 _LIBCPP_HIDE_FROM_ABI inline
258 int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m)
260 pthread_mutexattr_t attr;
261 int __ec = pthread_mutexattr_init(&attr);
264 __ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
266 pthread_mutexattr_destroy(&attr);
269 __ec = pthread_mutex_init(__m, &attr);
271 pthread_mutexattr_destroy(&attr);
274 __ec = pthread_mutexattr_destroy(&attr);
276 pthread_mutex_destroy(__m);
282 _LIBCPP_HIDE_FROM_ABI inline
283 int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m)
285 return pthread_mutex_lock(__m);
288 _LIBCPP_HIDE_FROM_ABI inline
289 bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m)
291 return pthread_mutex_trylock(__m) == 0;
294 _LIBCPP_HIDE_FROM_ABI inline
295 int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t *__m)
297 return pthread_mutex_unlock(__m);
300 _LIBCPP_HIDE_FROM_ABI inline
301 int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m)
303 return pthread_mutex_destroy(__m);
306 _LIBCPP_HIDE_FROM_ABI inline
307 int __libcpp_mutex_lock(__libcpp_mutex_t *__m)
309 return pthread_mutex_lock(__m);
312 _LIBCPP_HIDE_FROM_ABI inline
313 bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m)
315 return pthread_mutex_trylock(__m) == 0;
318 _LIBCPP_HIDE_FROM_ABI inline
319 int __libcpp_mutex_unlock(__libcpp_mutex_t *__m)
321 return pthread_mutex_unlock(__m);
324 _LIBCPP_HIDE_FROM_ABI inline
325 int __libcpp_mutex_destroy(__libcpp_mutex_t *__m)
327 return pthread_mutex_destroy(__m);
330 // Condition Variable
331 _LIBCPP_HIDE_FROM_ABI inline
332 int __libcpp_condvar_signal(__libcpp_condvar_t *__cv)
334 return pthread_cond_signal(__cv);
337 _LIBCPP_HIDE_FROM_ABI inline
338 int __libcpp_condvar_broadcast(__libcpp_condvar_t *__cv)
340 return pthread_cond_broadcast(__cv);
343 _LIBCPP_HIDE_FROM_ABI inline
344 int __libcpp_condvar_wait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m)
346 return pthread_cond_wait(__cv, __m);
349 _LIBCPP_HIDE_FROM_ABI inline
350 int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
351 __libcpp_timespec_t *__ts)
353 return pthread_cond_timedwait(__cv, __m, __ts);
356 _LIBCPP_HIDE_FROM_ABI inline
357 int __libcpp_condvar_destroy(__libcpp_condvar_t *__cv)
359 return pthread_cond_destroy(__cv);
363 _LIBCPP_HIDE_FROM_ABI inline
364 int __libcpp_execute_once(__libcpp_exec_once_flag *flag,
365 void (*init_routine)()) {
366 return pthread_once(flag, init_routine);
370 // Returns non-zero if the thread ids are equal, otherwise 0
371 _LIBCPP_HIDE_FROM_ABI inline
372 bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2)
377 // Returns non-zero if t1 < t2, otherwise 0
378 _LIBCPP_HIDE_FROM_ABI inline
379 bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2)
385 _LIBCPP_HIDE_FROM_ABI inline
386 bool __libcpp_thread_isnull(const __libcpp_thread_t *__t) {
387 return __libcpp_thread_get_id(__t) == 0;
390 _LIBCPP_HIDE_FROM_ABI inline
391 int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
394 return pthread_create(__t, nullptr, __func, __arg);
397 _LIBCPP_HIDE_FROM_ABI inline
398 __libcpp_thread_id __libcpp_thread_get_current_id()
400 const __libcpp_thread_t thread = pthread_self();
401 return __libcpp_thread_get_id(&thread);
404 _LIBCPP_HIDE_FROM_ABI inline
405 __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t)
414 _LIBCPP_HIDE_FROM_ABI inline
415 int __libcpp_thread_join(__libcpp_thread_t *__t)
417 return pthread_join(*__t, nullptr);
420 _LIBCPP_HIDE_FROM_ABI inline
421 int __libcpp_thread_detach(__libcpp_thread_t *__t)
423 return pthread_detach(*__t);
426 _LIBCPP_HIDE_FROM_ABI inline
427 void __libcpp_thread_yield()
432 _LIBCPP_HIDE_FROM_ABI inline
433 void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns)
435 __libcpp_timespec_t __ts = _VSTD::__convert_to_timespec<__libcpp_timespec_t>(__ns);
436 while (nanosleep(&__ts, &__ts) == -1 && errno == EINTR);
439 // Thread local storage
440 _LIBCPP_HIDE_FROM_ABI inline
441 int __libcpp_tls_create(__libcpp_tls_key *__key, void (*__at_exit)(void *))
443 return pthread_key_create(__key, __at_exit);
446 _LIBCPP_HIDE_FROM_ABI inline
447 void *__libcpp_tls_get(__libcpp_tls_key __key)
449 return pthread_getspecific(__key);
452 _LIBCPP_HIDE_FROM_ABI inline
453 int __libcpp_tls_set(__libcpp_tls_key __key, void *__p)
455 return pthread_setspecific(__key, __p);
458 #elif defined(_LIBCPP_HAS_THREAD_API_C11)
460 _LIBCPP_HIDE_FROM_ABI inline
461 int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m)
463 return mtx_init(__m, mtx_plain | mtx_recursive) == thrd_success ? 0 : EINVAL;
466 _LIBCPP_HIDE_FROM_ABI inline
467 int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m)
469 return mtx_lock(__m) == thrd_success ? 0 : EINVAL;
472 _LIBCPP_HIDE_FROM_ABI inline
473 bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m)
475 return mtx_trylock(__m) == thrd_success;
478 _LIBCPP_HIDE_FROM_ABI inline
479 int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t *__m)
481 return mtx_unlock(__m) == thrd_success ? 0 : EINVAL;
484 _LIBCPP_HIDE_FROM_ABI inline
485 int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m)
491 _LIBCPP_HIDE_FROM_ABI inline
492 int __libcpp_mutex_lock(__libcpp_mutex_t *__m)
494 return mtx_lock(__m) == thrd_success ? 0 : EINVAL;
497 _LIBCPP_HIDE_FROM_ABI inline
498 bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m)
500 return mtx_trylock(__m) == thrd_success;
503 _LIBCPP_HIDE_FROM_ABI inline
504 int __libcpp_mutex_unlock(__libcpp_mutex_t *__m)
506 return mtx_unlock(__m) == thrd_success ? 0 : EINVAL;
509 _LIBCPP_HIDE_FROM_ABI inline
510 int __libcpp_mutex_destroy(__libcpp_mutex_t *__m)
516 // Condition Variable
517 _LIBCPP_HIDE_FROM_ABI inline
518 int __libcpp_condvar_signal(__libcpp_condvar_t *__cv)
520 return cnd_signal(__cv) == thrd_success ? 0 : EINVAL;
523 _LIBCPP_HIDE_FROM_ABI inline
524 int __libcpp_condvar_broadcast(__libcpp_condvar_t *__cv)
526 return cnd_broadcast(__cv) == thrd_success ? 0 : EINVAL;
529 _LIBCPP_HIDE_FROM_ABI inline
530 int __libcpp_condvar_wait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m)
532 return cnd_wait(__cv, __m) == thrd_success ? 0 : EINVAL;
535 _LIBCPP_HIDE_FROM_ABI inline
536 int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
539 int __ec = cnd_timedwait(__cv, __m, __ts);
540 return __ec == thrd_timedout ? ETIMEDOUT : __ec;
543 _LIBCPP_HIDE_FROM_ABI inline
544 int __libcpp_condvar_destroy(__libcpp_condvar_t *__cv)
551 _LIBCPP_HIDE_FROM_ABI inline
552 int __libcpp_execute_once(__libcpp_exec_once_flag *flag,
553 void (*init_routine)(void)) {
554 ::call_once(flag, init_routine);
559 // Returns non-zero if the thread ids are equal, otherwise 0
560 _LIBCPP_HIDE_FROM_ABI inline
561 bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2)
563 return thrd_equal(t1, t2) != 0;
566 // Returns non-zero if t1 < t2, otherwise 0
567 _LIBCPP_HIDE_FROM_ABI inline
568 bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2)
574 _LIBCPP_HIDE_FROM_ABI inline
575 bool __libcpp_thread_isnull(const __libcpp_thread_t *__t) {
576 return __libcpp_thread_get_id(__t) == 0;
579 _LIBCPP_HIDE_FROM_ABI inline
580 int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
583 int __ec = thrd_create(__t, reinterpret_cast<thrd_start_t>(__func), __arg);
584 return __ec == thrd_nomem ? ENOMEM : __ec;
587 _LIBCPP_HIDE_FROM_ABI inline
588 __libcpp_thread_id __libcpp_thread_get_current_id()
590 return thrd_current();
593 _LIBCPP_HIDE_FROM_ABI inline
594 __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t)
599 _LIBCPP_HIDE_FROM_ABI inline
600 int __libcpp_thread_join(__libcpp_thread_t *__t)
602 return thrd_join(*__t, nullptr) == thrd_success ? 0 : EINVAL;
605 _LIBCPP_HIDE_FROM_ABI inline
606 int __libcpp_thread_detach(__libcpp_thread_t *__t)
608 return thrd_detach(*__t) == thrd_success ? 0 : EINVAL;
611 _LIBCPP_HIDE_FROM_ABI inline
612 void __libcpp_thread_yield()
617 _LIBCPP_HIDE_FROM_ABI inline
618 void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns)
620 __libcpp_timespec_t __ts = _VSTD::__convert_to_timespec<__libcpp_timespec_t>(__ns);
621 thrd_sleep(&__ts, nullptr);
624 // Thread local storage
625 _LIBCPP_HIDE_FROM_ABI inline
626 int __libcpp_tls_create(__libcpp_tls_key *__key, void (*__at_exit)(void *))
628 return tss_create(__key, __at_exit) == thrd_success ? 0 : EINVAL;
631 _LIBCPP_HIDE_FROM_ABI inline
632 void *__libcpp_tls_get(__libcpp_tls_key __key)
634 return tss_get(__key);
637 _LIBCPP_HIDE_FROM_ABI inline
638 int __libcpp_tls_set(__libcpp_tls_key __key, void *__p)
640 return tss_set(__key, __p) == thrd_success ? 0 : EINVAL;
646 #endif // !_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL || _LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL
648 class _LIBCPP_TYPE_VIS thread;
649 class _LIBCPP_TYPE_VIS __thread_id;
651 namespace this_thread
654 _LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
656 } // namespace this_thread
658 template<> struct hash<__thread_id>;
660 class _LIBCPP_TEMPLATE_VIS __thread_id
662 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
663 // NULL is the no-thread value on Darwin. Someone needs to check
664 // on other platforms. We assume 0 works everywhere for now.
665 __libcpp_thread_id __id_;
668 _LIBCPP_INLINE_VISIBILITY
669 __thread_id() _NOEXCEPT : __id_(0) {}
671 friend _LIBCPP_INLINE_VISIBILITY
672 bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
673 { // don't pass id==0 to underlying routines
674 if (__x.__id_ == 0) return __y.__id_ == 0;
675 if (__y.__id_ == 0) return false;
676 return __libcpp_thread_id_equal(__x.__id_, __y.__id_);
678 friend _LIBCPP_INLINE_VISIBILITY
679 bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
680 {return !(__x == __y);}
681 friend _LIBCPP_INLINE_VISIBILITY
682 bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
683 { // id==0 is always less than any other thread_id
684 if (__x.__id_ == 0) return __y.__id_ != 0;
685 if (__y.__id_ == 0) return false;
686 return __libcpp_thread_id_less(__x.__id_, __y.__id_);
688 friend _LIBCPP_INLINE_VISIBILITY
689 bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
690 {return !(__y < __x);}
691 friend _LIBCPP_INLINE_VISIBILITY
692 bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
694 friend _LIBCPP_INLINE_VISIBILITY
695 bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
696 {return !(__x < __y);}
698 _LIBCPP_INLINE_VISIBILITY
699 void __reset() { __id_ = 0; }
701 template<class _CharT, class _Traits>
703 _LIBCPP_INLINE_VISIBILITY
704 basic_ostream<_CharT, _Traits>&
705 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id);
708 _LIBCPP_INLINE_VISIBILITY
709 __thread_id(__libcpp_thread_id __id) : __id_(__id) {}
711 friend __thread_id this_thread::get_id() _NOEXCEPT;
712 friend class _LIBCPP_TYPE_VIS thread;
713 friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
716 namespace this_thread
719 inline _LIBCPP_INLINE_VISIBILITY
723 return __libcpp_thread_get_current_id();
726 } // namespace this_thread
728 #endif // !_LIBCPP_HAS_NO_THREADS
730 _LIBCPP_END_NAMESPACE_STD
732 #endif // _LIBCPP_THREADING_SUPPORT