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