| 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/functional.h> |
| 16 |
#include <__fwd/ostream.h> |
| 17 |
#include <__thread/support.h> |
| 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 |
class _LIBCPP_EXPORTED_FROM_ABI __thread_id; |
| 27 |
|
| 28 |
namespace this_thread { |
| 29 |
|
| 30 |
_LIBCPP_HIDE_FROM_ABI __thread_id get_id() _NOEXCEPT; |
| 31 |
|
| 32 |
} // namespace this_thread |
| 33 |
|
| 34 |
template <> |
| 35 |
struct hash<__thread_id>; |
| 36 |
|
| 37 |
class _LIBCPP_TEMPLATE_VIS __thread_id { |
| 38 |
// FIXME: pthread_t is a pointer on Darwin but a long on Linux. |
| 39 |
// NULL is the no-thread value on Darwin. Someone needs to check |
| 40 |
// on other platforms. We assume 0 works everywhere for now. |
| 41 |
__libcpp_thread_id __id_; |
| 42 |
|
| 43 |
static _LIBCPP_HIDE_FROM_ABI bool |
| 44 |
__lt_impl(__thread_id __x, __thread_id __y) _NOEXCEPT { // id==0 is always less than any other thread_id |
| 45 |
if (__x.__id_ == 0) |
| 46 |
return __y.__id_ != 0; |
| 47 |
if (__y.__id_ == 0) |
| 48 |
return false; |
| 49 |
return __libcpp_thread_id_less(__x.__id_, __y.__id_); |
| 50 |
} |
| 51 |
|
| 52 |
public: |
| 53 |
_LIBCPP_HIDE_FROM_ABI __thread_id() _NOEXCEPT : __id_(0) {} |
| 54 |
|
| 55 |
_LIBCPP_HIDE_FROM_ABI void __reset() { __id_ = 0; } |
| 56 |
|
| 57 |
friend _LIBCPP_HIDE_FROM_ABI bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT; |
| 58 |
# if _LIBCPP_STD_VER <= 17 |
| 59 |
friend _LIBCPP_HIDE_FROM_ABI bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT; |
| 60 |
# else // _LIBCPP_STD_VER <= 17 |
| 61 |
friend _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(__thread_id __x, __thread_id __y) noexcept; |
| 62 |
# endif // _LIBCPP_STD_VER <= 17 |
| 63 |
|
| 64 |
template <class _CharT, class _Traits> |
| 65 |
friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& |
| 66 |
operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id); |
| 67 |
|
| 68 |
private: |
| 69 |
_LIBCPP_HIDE_FROM_ABI __thread_id(__libcpp_thread_id __id) : __id_(__id) {} |
| 70 |
|
| 71 |
_LIBCPP_HIDE_FROM_ABI friend __libcpp_thread_id __get_underlying_id(const __thread_id __id) { return __id.__id_; } |
| 72 |
|
| 73 |
friend __thread_id this_thread::get_id() _NOEXCEPT; |
| 74 |
friend class _LIBCPP_EXPORTED_FROM_ABI thread; |
| 75 |
friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>; |
| 76 |
}; |
| 77 |
|
| 78 |
inline _LIBCPP_HIDE_FROM_ABI bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT { |
| 79 |
// Don't pass id==0 to underlying routines |
| 80 |
if (__x.__id_ == 0) |
| 81 |
return __y.__id_ == 0; |
| 82 |
if (__y.__id_ == 0) |
| 83 |
return false; |
| 84 |
return __libcpp_thread_id_equal(__x.__id_, __y.__id_); |
| 85 |
} |
| 86 |
|
| 87 |
# if _LIBCPP_STD_VER <= 17 |
| 88 |
|
| 89 |
inline _LIBCPP_HIDE_FROM_ABI bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__x == __y); } |
| 90 |
|
| 91 |
inline _LIBCPP_HIDE_FROM_ABI bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT { |
| 92 |
return __thread_id::__lt_impl(__x.__id_, __y.__id_); |
| 93 |
} |
| 94 |
|
| 95 |
inline _LIBCPP_HIDE_FROM_ABI bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__y < __x); } |
| 96 |
inline _LIBCPP_HIDE_FROM_ABI bool operator>(__thread_id __x, __thread_id __y) _NOEXCEPT { return __y < __x; } |
| 97 |
inline _LIBCPP_HIDE_FROM_ABI bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__x < __y); } |
| 98 |
|
| 99 |
# else // _LIBCPP_STD_VER <= 17 |
| 100 |
|
| 101 |
inline _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(__thread_id __x, __thread_id __y) noexcept { |
| 102 |
if (__x == __y) |
| 103 |
return strong_ordering::equal; |
| 104 |
if (__thread_id::__lt_impl(__x, __y)) |
| 105 |
return strong_ordering::less; |
| 106 |
return strong_ordering::greater; |
| 107 |
} |
| 108 |
|
| 109 |
# endif // _LIBCPP_STD_VER <= 17 |
| 110 |
|
| 111 |
namespace this_thread { |
| 112 |
|
| 113 |
inline _LIBCPP_HIDE_FROM_ABI __thread_id get_id() _NOEXCEPT { return __libcpp_thread_get_current_id(); } |
| 114 |
|
| 115 |
} // namespace this_thread |
| 116 |
|
| 117 |
#endif // !_LIBCPP_HAS_NO_THREADS |
| 118 |
|
| 119 |
_LIBCPP_END_NAMESPACE_STD |
| 120 |
|
| 121 |
#endif // _LIBCPP___THREAD_ID_H |