| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#ifndef _GLIBCXX_THREAD_H |
| 31 |
#define _GLIBCXX_THREAD_H 1 |
| 32 |
|
| 33 |
#ifdef _GLIBCXX_SYSHDR |
| 34 |
#pragma GCC system_header |
| 35 |
#endif |
| 36 |
|
| 37 |
#if __cplusplus >= 201103L |
| 38 |
#include <bits/c++config.h> |
| 39 |
|
| 40 |
#include <iosfwd> |
| 41 |
#include <tuple> |
| 42 |
#include <bits/functional_hash.h> |
| 43 |
#include <bits/invoke.h> |
| 44 |
#include <bits/refwrap.h> |
| 45 |
#include <bits/unique_ptr.h> |
| 46 |
|
| 47 |
#ifdef _GLIBCXX_HAS_GTHREADS |
| 48 |
# include <bits/gthr.h> |
| 49 |
#else |
| 50 |
# include <errno.h> |
| 51 |
# include <bits/functexcept.h> |
| 52 |
#endif |
| 53 |
|
| 54 |
namespace std _GLIBCXX_VISIBILITY(default) |
| 55 |
{ |
| 56 |
_GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 57 |
|
| 58 |
#if __glibcxx_formatters |
| 59 |
template<typename, typename> class formatter; |
| 60 |
#endif |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
class thread |
| 85 |
{ |
| 86 |
public: |
| 87 |
#ifdef _GLIBCXX_HAS_GTHREADS |
| 88 |
using native_handle_type = __gthread_t; |
| 89 |
#else |
| 90 |
using native_handle_type = int; |
| 91 |
#endif |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
class id |
| 99 |
{ |
| 100 |
native_handle_type _M_thread; |
| 101 |
|
| 102 |
public: |
| 103 |
id() noexcept : _M_thread() { } |
| 104 |
|
| 105 |
explicit |
| 106 |
id(native_handle_type __id) : _M_thread(__id) { } |
| 107 |
|
| 108 |
private: |
| 109 |
friend class thread; |
| 110 |
friend struct hash<id>; |
| 111 |
|
| 112 |
friend bool |
| 113 |
operator==(id __x, id __y) noexcept; |
| 114 |
|
| 115 |
#if __cpp_lib_three_way_comparison |
| 116 |
friend strong_ordering |
| 117 |
operator<=>(id __x, id __y) noexcept; |
| 118 |
#else |
| 119 |
friend bool |
| 120 |
operator<(id __x, id __y) noexcept; |
| 121 |
#endif |
| 122 |
|
| 123 |
template<class _CharT, class _Traits> |
| 124 |
friend basic_ostream<_CharT, _Traits>& |
| 125 |
operator<<(basic_ostream<_CharT, _Traits>& __out, id __id); |
| 126 |
|
| 127 |
#if __glibcxx_formatters |
| 128 |
friend formatter<id, char>; |
| 129 |
friend formatter<id, wchar_t>; |
| 130 |
#endif |
| 131 |
}; |
| 132 |
|
| 133 |
private: |
| 134 |
id _M_id; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
template<typename _Tp> |
| 140 |
using __not_same = __not_<is_same<__remove_cvref_t<_Tp>, thread>>; |
| 141 |
|
| 142 |
public: |
| 143 |
thread() noexcept = default; |
| 144 |
|
| 145 |
#ifdef _GLIBCXX_HAS_GTHREADS |
| 146 |
private: |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
static void |
| 154 |
_M_thread_deps_never_run() { |
| 155 |
#ifdef GTHR_ACTIVE_PROXY |
| 156 |
reinterpret_cast<void (*)(void)>(&pthread_create)(); |
| 157 |
reinterpret_cast<void (*)(void)>(&pthread_join)(); |
| 158 |
#endif |
| 159 |
} |
| 160 |
|
| 161 |
public: |
| 162 |
template<typename _Callable, typename... _Args, |
| 163 |
typename = _Require<__not_same<_Callable>>> |
| 164 |
explicit |
| 165 |
thread(_Callable&& __f, _Args&&... __args) |
| 166 |
{ |
| 167 |
static_assert( __is_invocable<typename decay<_Callable>::type, |
| 168 |
typename decay<_Args>::type...>::value, |
| 169 |
"std::thread arguments must be invocable after conversion to rvalues" |
| 170 |
); |
| 171 |
|
| 172 |
using _Wrapper = _Call_wrapper<_Callable, _Args...>; |
| 173 |
|
| 174 |
|
| 175 |
_M_start_thread(_State_ptr(new _State_impl<_Wrapper>( |
| 176 |
std::forward<_Callable>(__f), std::forward<_Args>(__args)...)), |
| 177 |
_M_thread_deps_never_run); |
| 178 |
} |
| 179 |
#endif |
| 180 |
|
| 181 |
~thread() |
| 182 |
{ |
| 183 |
if (joinable()) |
| 184 |
std::__terminate(); |
| 185 |
} |
| 186 |
|
| 187 |
thread(const thread&) = delete; |
| 188 |
|
| 189 |
thread(thread&& __t) noexcept |
| 190 |
{ swap(__t); } |
| 191 |
|
| 192 |
thread& operator=(const thread&) = delete; |
| 193 |
|
| 194 |
thread& operator=(thread&& __t) noexcept |
| 195 |
{ |
| 196 |
if (joinable()) |
| 197 |
std::__terminate(); |
| 198 |
swap(__t); |
| 199 |
return *this; |
| 200 |
} |
| 201 |
|
| 202 |
void |
| 203 |
swap(thread& __t) noexcept |
| 204 |
{ std::swap(_M_id, __t._M_id); } |
| 205 |
|
| 206 |
bool |
| 207 |
joinable() const noexcept |
| 208 |
{ return !(_M_id == id()); } |
| 209 |
|
| 210 |
void |
| 211 |
join(); |
| 212 |
|
| 213 |
void |
| 214 |
detach(); |
| 215 |
|
| 216 |
id |
| 217 |
get_id() const noexcept |
| 218 |
{ return _M_id; } |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
native_handle_type |
| 223 |
native_handle() |
| 224 |
{ return _M_id._M_thread; } |
| 225 |
|
| 226 |
|
| 227 |
static unsigned int |
| 228 |
hardware_concurrency() noexcept; |
| 229 |
|
| 230 |
#ifdef _GLIBCXX_HAS_GTHREADS |
| 231 |
#ifndef _GLIBCXX_THREAD_IMPL |
| 232 |
private: |
| 233 |
#endif |
| 234 |
|
| 235 |
|
| 236 |
struct _State |
| 237 |
{ |
| 238 |
virtual ~_State(); |
| 239 |
virtual void _M_run() = 0; |
| 240 |
}; |
| 241 |
using _State_ptr = unique_ptr<_State>; |
| 242 |
|
| 243 |
private: |
| 244 |
template<typename _Callable> |
| 245 |
struct _State_impl : public _State |
| 246 |
{ |
| 247 |
_Callable _M_func; |
| 248 |
|
| 249 |
template<typename... _Args> |
| 250 |
_State_impl(_Args&&... __args) |
| 251 |
: _M_func(std::forward<_Args>(__args)...) |
| 252 |
{ } |
| 253 |
|
| 254 |
void |
| 255 |
_M_run() { _M_func(); } |
| 256 |
}; |
| 257 |
|
| 258 |
void |
| 259 |
_M_start_thread(_State_ptr, void (*)()); |
| 260 |
|
| 261 |
#if _GLIBCXX_THREAD_ABI_COMPAT |
| 262 |
public: |
| 263 |
struct _Impl_base; |
| 264 |
typedef shared_ptr<_Impl_base> __shared_base_type; |
| 265 |
struct _Impl_base |
| 266 |
{ |
| 267 |
__shared_base_type _M_this_ptr; |
| 268 |
virtual ~_Impl_base() = default; |
| 269 |
virtual void _M_run() = 0; |
| 270 |
}; |
| 271 |
|
| 272 |
private: |
| 273 |
void |
| 274 |
_M_start_thread(__shared_base_type, void (*)()); |
| 275 |
|
| 276 |
void |
| 277 |
_M_start_thread(__shared_base_type); |
| 278 |
#endif |
| 279 |
|
| 280 |
private: |
| 281 |
|
| 282 |
template<typename _Tuple> |
| 283 |
struct _Invoker |
| 284 |
{ |
| 285 |
template<typename... _Args> |
| 286 |
explicit |
| 287 |
_Invoker(_Args&&... __args) |
| 288 |
: _M_t(std::forward<_Args>(__args)...) |
| 289 |
{ } |
| 290 |
|
| 291 |
_Tuple _M_t; |
| 292 |
|
| 293 |
template<typename> |
| 294 |
struct __result; |
| 295 |
template<typename _Fn, typename... _Args> |
| 296 |
struct __result<tuple<_Fn, _Args...>> |
| 297 |
: __invoke_result<_Fn, _Args...> |
| 298 |
{ }; |
| 299 |
|
| 300 |
template<size_t... _Ind> |
| 301 |
typename __result<_Tuple>::type |
| 302 |
_M_invoke(_Index_tuple<_Ind...>) |
| 303 |
{ return std::__invoke(std::get<_Ind>(std::move(_M_t))...); } |
| 304 |
|
| 305 |
typename __result<_Tuple>::type |
| 306 |
operator()() |
| 307 |
{ |
| 308 |
using _Indices |
| 309 |
= typename _Build_index_tuple<tuple_size<_Tuple>::value>::__type; |
| 310 |
return _M_invoke(_Indices()); |
| 311 |
} |
| 312 |
}; |
| 313 |
|
| 314 |
public: |
| 315 |
|
| 316 |
template<typename... _Tp> |
| 317 |
using _Call_wrapper = _Invoker<tuple<typename decay<_Tp>::type...>>; |
| 318 |
|
| 319 |
#endif |
| 320 |
}; |
| 321 |
|
| 322 |
#ifndef _GLIBCXX_HAS_GTHREADS |
| 323 |
inline void thread::join() { std::__throw_system_error(EINVAL); } |
| 324 |
inline void thread::detach() { std::__throw_system_error(EINVAL); } |
| 325 |
inline unsigned int thread::hardware_concurrency() noexcept { return 0; } |
| 326 |
#endif |
| 327 |
|
| 328 |
|
| 329 |
inline void |
| 330 |
swap(thread& __x, thread& __y) noexcept |
| 331 |
{ __x.swap(__y); } |
| 332 |
|
| 333 |
|
| 334 |
inline bool |
| 335 |
operator==(thread::id __x, thread::id __y) noexcept |
| 336 |
{ |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
return __x._M_thread == __y._M_thread; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
template<> |
| 349 |
struct hash<thread::id> |
| 350 |
: public __hash_base<size_t, thread::id> |
| 351 |
{ |
| 352 |
size_t |
| 353 |
operator()(const thread::id& __id) const noexcept |
| 354 |
{ return std::_Hash_impl::hash(__id._M_thread); } |
| 355 |
}; |
| 356 |
|
| 357 |
namespace this_thread |
| 358 |
{ |
| 359 |
|
| 360 |
inline thread::id |
| 361 |
get_id() noexcept |
| 362 |
{ |
| 363 |
#ifndef _GLIBCXX_HAS_GTHREADS |
| 364 |
return thread::id(1); |
| 365 |
#elif defined _GLIBCXX_NATIVE_THREAD_ID |
| 366 |
return thread::id(_GLIBCXX_NATIVE_THREAD_ID); |
| 367 |
#else |
| 368 |
return thread::id(__gthread_self()); |
| 369 |
#endif |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
inline void |
| 374 |
yield() noexcept |
| 375 |
{ |
| 376 |
#if defined _GLIBCXX_HAS_GTHREADS && defined _GLIBCXX_USE_SCHED_YIELD |
| 377 |
__gthread_yield(); |
| 378 |
#endif |
| 379 |
} |
| 380 |
|
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
_GLIBCXX_END_NAMESPACE_VERSION |
| 386 |
} |
| 387 |
#endif |
| 388 |
|
| 389 |
#endif |