|
| 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_ID_H |
| 11 | +#define _LIBCPP___THREAD_ID_H |
| 12 | + |
| 13 | +#include <__compare/ordering.h> |
| 14 | +#include <__config> |
| 15 | +#include <__fwd/hash.h> |
| 16 | +#include <__threading_support> |
| 17 | +#include <iosfwd> |
| 18 | + |
| 19 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 20 | +# pragma GCC system_header |
| 21 | +#endif |
| 22 | + |
| 23 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 24 | + |
| 25 | +#ifndef _LIBCPP_HAS_NO_THREADS |
| 26 | + |
| 27 | +class _LIBCPP_EXPORTED_FROM_ABI thread; |
| 28 | +class _LIBCPP_EXPORTED_FROM_ABI __thread_id; |
| 29 | + |
| 30 | +namespace this_thread |
| 31 | +{ |
| 32 | + |
| 33 | +_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT; |
| 34 | + |
| 35 | +} // namespace this_thread |
| 36 | + |
| 37 | +template<> struct hash<__thread_id>; |
| 38 | + |
| 39 | +class _LIBCPP_TEMPLATE_VIS __thread_id |
| 40 | +{ |
| 41 | + // FIXME: pthread_t is a pointer on Darwin but a long on Linux. |
| 42 | + // NULL is the no-thread value on Darwin. Someone needs to check |
| 43 | + // on other platforms. We assume 0 works everywhere for now. |
| 44 | + __libcpp_thread_id __id_; |
| 45 | + |
| 46 | + static _LIBCPP_HIDE_FROM_ABI |
| 47 | + bool __lt_impl(__thread_id __x, __thread_id __y) _NOEXCEPT |
| 48 | + { // id==0 is always less than any other thread_id |
| 49 | + if (__x.__id_ == 0) return __y.__id_ != 0; |
| 50 | + if (__y.__id_ == 0) return false; |
| 51 | + return __libcpp_thread_id_less(__x.__id_, __y.__id_); |
| 52 | + } |
| 53 | + |
| 54 | +public: |
| 55 | + _LIBCPP_INLINE_VISIBILITY |
| 56 | + __thread_id() _NOEXCEPT : __id_(0) {} |
| 57 | + |
| 58 | + _LIBCPP_INLINE_VISIBILITY |
| 59 | + void __reset() { __id_ = 0; } |
| 60 | + |
| 61 | + friend _LIBCPP_HIDE_FROM_ABI bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT; |
| 62 | +#if _LIBCPP_STD_VER <= 17 |
| 63 | + friend _LIBCPP_HIDE_FROM_ABI bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT; |
| 64 | +#else // _LIBCPP_STD_VER <= 17 |
| 65 | + friend _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(__thread_id __x, __thread_id __y) noexcept; |
| 66 | +#endif // _LIBCPP_STD_VER <= 17 |
| 67 | + |
| 68 | + template<class _CharT, class _Traits> |
| 69 | + friend |
| 70 | + _LIBCPP_INLINE_VISIBILITY |
| 71 | + basic_ostream<_CharT, _Traits>& |
| 72 | + operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id); |
| 73 | + |
| 74 | +private: |
| 75 | + _LIBCPP_INLINE_VISIBILITY |
| 76 | + __thread_id(__libcpp_thread_id __id) : __id_(__id) {} |
| 77 | + |
| 78 | + _LIBCPP_HIDE_FROM_ABI friend __libcpp_thread_id __get_underlying_id(const __thread_id __id) { return __id.__id_; } |
| 79 | + |
| 80 | + friend __thread_id this_thread::get_id() _NOEXCEPT; |
| 81 | + friend class _LIBCPP_EXPORTED_FROM_ABI thread; |
| 82 | + friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>; |
| 83 | +}; |
| 84 | + |
| 85 | +inline _LIBCPP_HIDE_FROM_ABI |
| 86 | +bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT { |
| 87 | + // Don't pass id==0 to underlying routines |
| 88 | + if (__x.__id_ == 0) |
| 89 | + return __y.__id_ == 0; |
| 90 | + if (__y.__id_ == 0) |
| 91 | + return false; |
| 92 | + return __libcpp_thread_id_equal(__x.__id_, __y.__id_); |
| 93 | +} |
| 94 | + |
| 95 | +#if _LIBCPP_STD_VER <= 17 |
| 96 | + |
| 97 | +inline _LIBCPP_HIDE_FROM_ABI |
| 98 | +bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT { |
| 99 | + return !(__x == __y); |
| 100 | +} |
| 101 | + |
| 102 | +inline _LIBCPP_HIDE_FROM_ABI |
| 103 | +bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT { |
| 104 | + return __thread_id::__lt_impl(__x.__id_, __y.__id_); |
| 105 | +} |
| 106 | + |
| 107 | +inline _LIBCPP_HIDE_FROM_ABI bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__y < __x); } |
| 108 | +inline _LIBCPP_HIDE_FROM_ABI bool operator>(__thread_id __x, __thread_id __y) _NOEXCEPT { return __y < __x; } |
| 109 | +inline _LIBCPP_HIDE_FROM_ABI bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__x < __y); } |
| 110 | + |
| 111 | +#else // _LIBCPP_STD_VER <= 17 |
| 112 | + |
| 113 | +inline _LIBCPP_HIDE_FROM_ABI |
| 114 | +strong_ordering operator<=>(__thread_id __x, __thread_id __y) noexcept { |
| 115 | + if (__x == __y) |
| 116 | + return strong_ordering::equal; |
| 117 | + if (__thread_id::__lt_impl(__x, __y)) |
| 118 | + return strong_ordering::less; |
| 119 | + return strong_ordering::greater; |
| 120 | +} |
| 121 | + |
| 122 | +#endif // _LIBCPP_STD_VER <= 17 |
| 123 | + |
| 124 | +namespace this_thread |
| 125 | +{ |
| 126 | + |
| 127 | +_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT; |
| 128 | + |
| 129 | +} // namespace this_thread |
| 130 | + |
| 131 | +namespace this_thread |
| 132 | +{ |
| 133 | + |
| 134 | +inline _LIBCPP_INLINE_VISIBILITY |
| 135 | +__thread_id |
| 136 | +get_id() _NOEXCEPT |
| 137 | +{ |
| 138 | + return __libcpp_thread_get_current_id(); |
| 139 | +} |
| 140 | + |
| 141 | +} // namespace this_thread |
| 142 | + |
| 143 | +#endif // !_LIBCPP_HAS_NO_THREADS |
| 144 | + |
| 145 | +_LIBCPP_END_NAMESPACE_STD |
| 146 | + |
| 147 | +#endif // _LIBCPP___THREAD_ID_H |
0 commit comments