1 //  Copyright (c) 2018 Thomas Heller
2 //
3 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
4 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 /// \file hpx/runtime/threads/thread_id_type.hpp
7 
8 #ifndef HPX_THREADS_THREAD_ID_TYPE_HPP
9 #define HPX_THREADS_THREAD_ID_TYPE_HPP
10 
11 #include <hpx/config/constexpr.hpp>
12 #include <hpx/config/export_definitions.hpp>
13 
14 #include <cstddef>
15 #include <functional>
16 #include <iosfwd>
17 
18 namespace hpx {
19 namespace threads {
20     class HPX_EXPORT thread_data;
21 
22     struct thread_id_type
23     {
thread_id_typehpx::threads::thread_id_type24         constexpr thread_id_type()
25           : thrd_(nullptr)
26         {
27         }
thread_id_typehpx::threads::thread_id_type28         explicit constexpr thread_id_type(thread_data* thrd)
29           : thrd_(thrd)
30         {
31         }
32 
33         thread_id_type(thread_id_type const&) = default;
34         thread_id_type& operator=(thread_id_type const&) = default;
35 
operator ->hpx::threads::thread_id_type36         constexpr thread_data* operator->() const
37         {
38             return thrd_;
39         }
40 
operator *hpx::threads::thread_id_type41         constexpr thread_data& operator*() const
42         {
43             return *thrd_;
44         }
45 
operator boolhpx::threads::thread_id_type46         explicit constexpr operator bool() const
47         {
48             return nullptr != thrd_;
49         }
50 
gethpx::threads::thread_id_type51         constexpr thread_data* get() const
52         {
53             return thrd_;
54         }
55 
resethpx::threads::thread_id_type56         HPX_CXX14_CONSTEXPR void reset()
57         {
58             thrd_ = nullptr;
59         }
60 
operator ==(std::nullptr_t,thread_id_type const & rhs)61         friend constexpr bool operator==(
62             std::nullptr_t, thread_id_type const& rhs)
63         {
64             return nullptr == rhs.thrd_;
65         }
66 
operator !=(std::nullptr_t,thread_id_type const & rhs)67         friend constexpr bool operator!=(
68             std::nullptr_t, thread_id_type const& rhs)
69         {
70             return nullptr != rhs.thrd_;
71         }
72 
operator ==(thread_id_type const & lhs,std::nullptr_t)73         friend constexpr bool operator==(
74             thread_id_type const& lhs, std::nullptr_t)
75         {
76             return nullptr == lhs.thrd_;
77         }
78 
operator !=(thread_id_type const & lhs,std::nullptr_t)79         friend constexpr bool operator!=(
80             thread_id_type const& lhs, std::nullptr_t)
81         {
82             return nullptr != lhs.thrd_;
83         }
84 
operator ==(thread_id_type const & lhs,thread_id_type const & rhs)85         friend constexpr bool operator==(
86             thread_id_type const& lhs, thread_id_type const& rhs)
87         {
88             return lhs.thrd_ == rhs.thrd_;
89         }
90 
operator !=(thread_id_type const & lhs,thread_id_type const & rhs)91         friend constexpr bool operator!=(
92             thread_id_type const& lhs, thread_id_type const& rhs)
93         {
94             return lhs.thrd_ != rhs.thrd_;
95         }
96 
operator <(thread_id_type const & lhs,thread_id_type const & rhs)97         friend HPX_CXX14_CONSTEXPR bool operator<(
98             thread_id_type const& lhs, thread_id_type const& rhs)
99         {
100             return std::less<void const*>{}(lhs.thrd_, rhs.thrd_);
101         }
102 
operator >(thread_id_type const & lhs,thread_id_type const & rhs)103         friend HPX_CXX14_CONSTEXPR bool operator>(
104             thread_id_type const& lhs, thread_id_type const& rhs)
105         {
106             return std::less<void const*>{}(rhs.thrd_, lhs.thrd_);
107         }
108 
operator <=(thread_id_type const & lhs,thread_id_type const & rhs)109         friend HPX_CXX14_CONSTEXPR bool operator<=(
110             thread_id_type const& lhs, thread_id_type const& rhs)
111         {
112             return !(rhs > lhs);
113         }
114 
operator >=(thread_id_type const & lhs,thread_id_type const & rhs)115         friend HPX_CXX14_CONSTEXPR bool operator>=(
116             thread_id_type const& lhs, thread_id_type const& rhs)
117         {
118             return !(rhs < lhs);
119         }
120 
121         template <typename Char, typename Traits>
operator <<(std::basic_ostream<Char,Traits> & os,thread_id_type const & id)122         friend std::basic_ostream<Char, Traits>& operator<<(
123             std::basic_ostream<Char, Traits>& os, thread_id_type const& id)
124         {
125             os << id.get();
126             return os;
127         }
128 
129     private:
130         thread_data* thrd_;
131     };
132 
133     HPX_CONSTEXPR_OR_CONST thread_id_type invalid_thread_id;
134 }
135 }
136 
137 namespace std {
138 template <>
139 struct hash<::hpx::threads::thread_id_type>
140 {
operator ()std::hash141     std::size_t operator()(::hpx::threads::thread_id_type const& v) const
142         noexcept
143     {
144         std::hash<const ::hpx::threads::thread_data*> hasher_;
145         return hasher_(v.get());
146     }
147 };
148 }
149 
150 #endif
151