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___THREAD_ID_H
11 #define _LIBCPP___THREAD_ID_H
13 #include <__compare/ordering.h>
15 #include <__fwd/hash.h>
16 #include <__fwd/ostream.h>
17 #include <__threading_support>
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 # pragma GCC system_header
23 _LIBCPP_BEGIN_NAMESPACE_STD
25 #ifndef _LIBCPP_HAS_NO_THREADS
26 class _LIBCPP_EXPORTED_FROM_ABI __thread_id
;
28 namespace this_thread
{
30 _LIBCPP_HIDE_FROM_ABI __thread_id
get_id() _NOEXCEPT
;
32 } // namespace this_thread
35 struct hash
<__thread_id
>;
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_
;
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
46 return __y
.__id_
!= 0;
49 return __libcpp_thread_id_less(__x
.__id_
, __y
.__id_
);
53 _LIBCPP_HIDE_FROM_ABI
__thread_id() _NOEXCEPT
: __id_(0) {}
55 _LIBCPP_HIDE_FROM_ABI
void __reset() { __id_
= 0; }
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
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
);
69 _LIBCPP_HIDE_FROM_ABI
__thread_id(__libcpp_thread_id __id
) : __id_(__id
) {}
71 _LIBCPP_HIDE_FROM_ABI
friend __libcpp_thread_id
__get_underlying_id(const __thread_id __id
) { return __id
.__id_
; }
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
>;
78 inline _LIBCPP_HIDE_FROM_ABI
bool operator==(__thread_id __x
, __thread_id __y
) _NOEXCEPT
{
79 // Don't pass id==0 to underlying routines
81 return __y
.__id_
== 0;
84 return __libcpp_thread_id_equal(__x
.__id_
, __y
.__id_
);
87 # if _LIBCPP_STD_VER <= 17
89 inline _LIBCPP_HIDE_FROM_ABI
bool operator!=(__thread_id __x
, __thread_id __y
) _NOEXCEPT
{ return !(__x
== __y
); }
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_
);
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
); }
99 # else // _LIBCPP_STD_VER <= 17
101 inline _LIBCPP_HIDE_FROM_ABI strong_ordering
operator<=>(__thread_id __x
, __thread_id __y
) noexcept
{
103 return strong_ordering::equal
;
104 if (__thread_id::__lt_impl(__x
, __y
))
105 return strong_ordering::less
;
106 return strong_ordering::greater
;
109 # endif // _LIBCPP_STD_VER <= 17
111 namespace this_thread
{
113 inline _LIBCPP_HIDE_FROM_ABI __thread_id
get_id() _NOEXCEPT
{ return __libcpp_thread_get_current_id(); }
115 } // namespace this_thread
117 #endif // !_LIBCPP_HAS_NO_THREADS
119 _LIBCPP_END_NAMESPACE_STD
121 #endif // _LIBCPP___THREAD_ID_H