| 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___CXX03___THREAD_ID_H |
| 11 |
#define _LIBCPP___CXX03___THREAD_ID_H |
| 12 |
|
| 13 |
#include <__cxx03/__config> |
| 14 |
#include <__cxx03/__fwd/functional.h> |
| 15 |
#include <__cxx03/__fwd/ostream.h> |
| 16 |
#include <__cxx03/__thread/support.h> |
| 17 |
|
| 18 |
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 19 |
# pragma GCC system_header |
| 20 |
#endif |
| 21 |
|
| 22 |
_LIBCPP_BEGIN_NAMESPACE_STD |
| 23 |
|
| 24 |
#ifndef _LIBCPP_HAS_NO_THREADS |
| 25 |
class _LIBCPP_EXPORTED_FROM_ABI __thread_id; |
| 26 |
|
| 27 |
namespace this_thread { |
| 28 |
|
| 29 |
_LIBCPP_HIDE_FROM_ABI __thread_id get_id() _NOEXCEPT; |
| 30 |
|
| 31 |
} // namespace this_thread |
| 32 |
|
| 33 |
template <> |
| 34 |
struct hash<__thread_id>; |
| 35 |
|
| 36 |
class _LIBCPP_TEMPLATE_VIS __thread_id { |
| 37 |
// FIXME: pthread_t is a pointer on Darwin but a long on Linux. |
| 38 |
// NULL is the no-thread value on Darwin. Someone needs to check |
| 39 |
// on other platforms. We assume 0 works everywhere for now. |
| 40 |
__libcpp_thread_id __id_; |
| 41 |
|
| 42 |
static _LIBCPP_HIDE_FROM_ABI bool |
| 43 |
__lt_impl(__thread_id __x, __thread_id __y) _NOEXCEPT { // id==0 is always less than any other thread_id |
| 44 |
if (__x.__id_ == 0) |
| 45 |
return __y.__id_ != 0; |
| 46 |
if (__y.__id_ == 0) |
| 47 |
return false; |
| 48 |
return __libcpp_thread_id_less(__x.__id_, __y.__id_); |
| 49 |
} |
| 50 |
|
| 51 |
public: |
| 52 |
_LIBCPP_HIDE_FROM_ABI __thread_id() _NOEXCEPT : __id_(0) {} |
| 53 |
|
| 54 |
_LIBCPP_HIDE_FROM_ABI void __reset() { __id_ = 0; } |
| 55 |
|
| 56 |
friend _LIBCPP_HIDE_FROM_ABI bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT; |
| 57 |
friend _LIBCPP_HIDE_FROM_ABI bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT; |
| 58 |
|
| 59 |
template <class _CharT, class _Traits> |
| 60 |
friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& |
| 61 |
operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id); |
| 62 |
|
| 63 |
private: |
| 64 |
_LIBCPP_HIDE_FROM_ABI __thread_id(__libcpp_thread_id __id) : __id_(__id) {} |
| 65 |
|
| 66 |
_LIBCPP_HIDE_FROM_ABI friend __libcpp_thread_id __get_underlying_id(const __thread_id __id) { return __id.__id_; } |
| 67 |
|
| 68 |
friend __thread_id this_thread::get_id() _NOEXCEPT; |
| 69 |
friend class _LIBCPP_EXPORTED_FROM_ABI thread; |
| 70 |
friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>; |
| 71 |
}; |
| 72 |
|
| 73 |
inline _LIBCPP_HIDE_FROM_ABI bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT { |
| 74 |
// Don't pass id==0 to underlying routines |
| 75 |
if (__x.__id_ == 0) |
| 76 |
return __y.__id_ == 0; |
| 77 |
if (__y.__id_ == 0) |
| 78 |
return false; |
| 79 |
return __libcpp_thread_id_equal(__x.__id_, __y.__id_); |
| 80 |
} |
| 81 |
|
| 82 |
inline _LIBCPP_HIDE_FROM_ABI bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__x == __y); } |
| 83 |
|
| 84 |
inline _LIBCPP_HIDE_FROM_ABI bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT { |
| 85 |
return __thread_id::__lt_impl(__x.__id_, __y.__id_); |
| 86 |
} |
| 87 |
|
| 88 |
inline _LIBCPP_HIDE_FROM_ABI bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__y < __x); } |
| 89 |
inline _LIBCPP_HIDE_FROM_ABI bool operator>(__thread_id __x, __thread_id __y) _NOEXCEPT { return __y < __x; } |
| 90 |
inline _LIBCPP_HIDE_FROM_ABI bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__x < __y); } |
| 91 |
|
| 92 |
namespace this_thread { |
| 93 |
|
| 94 |
inline _LIBCPP_HIDE_FROM_ABI __thread_id get_id() _NOEXCEPT { return __libcpp_thread_get_current_id(); } |
| 95 |
|
| 96 |
} // namespace this_thread |
| 97 |
|
| 98 |
#endif // !_LIBCPP_HAS_NO_THREADS |
| 99 |
|
| 100 |
_LIBCPP_END_NAMESPACE_STD |
| 101 |
|
| 102 |
#endif // _LIBCPP___CXX03___THREAD_ID_H |