| 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 | #ifndef _GLIBCXX_THREAD |
| 30 | #define _GLIBCXX_THREAD 1 |
| 31 | |
| 32 | #pragma GCC system_header |
| 33 | |
| 34 | #if __cplusplus < 201103L |
| 35 | # include <bits/c++0x_warning.h> |
| 36 | #else |
| 37 | |
| 38 | #include <chrono> |
| 39 | #include <memory> |
| 40 | #include <tuple> |
| 41 | #include <cerrno> |
| 42 | #include <bits/functexcept.h> |
| 43 | #include <bits/functional_hash.h> |
| 44 | #include <bits/invoke.h> |
| 45 | #include <bits/gthr.h> |
| 46 | |
| 47 | #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) |
| 48 | |
| 49 | namespace std _GLIBCXX_VISIBILITY(default) |
| 50 | { |
| 51 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | class thread |
| 63 | { |
| 64 | public: |
| 65 | |
| 66 | |
| 67 | struct _State |
| 68 | { |
| 69 | virtual ~_State(); |
| 70 | virtual void _M_run() = 0; |
| 71 | }; |
| 72 | using _State_ptr = unique_ptr<_State>; |
| 73 | |
| 74 | typedef __gthread_t native_handle_type; |
| 75 | |
| 76 | |
| 77 | class id |
| 78 | { |
| 79 | native_handle_type _M_thread; |
| 80 | |
| 81 | public: |
| 82 | id() noexcept : _M_thread() { } |
| 83 | |
| 84 | explicit |
| 85 | id(native_handle_type __id) : _M_thread(__id) { } |
| 86 | |
| 87 | private: |
| 88 | friend class thread; |
| 89 | friend class hash<thread::id>; |
| 90 | |
| 91 | friend bool |
| 92 | operator==(thread::id __x, thread::id __y) noexcept; |
| 93 | |
| 94 | friend bool |
| 95 | operator<(thread::id __x, thread::id __y) noexcept; |
| 96 | |
| 97 | template<class _CharT, class _Traits> |
| 98 | friend basic_ostream<_CharT, _Traits>& |
| 99 | operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id); |
| 100 | }; |
| 101 | |
| 102 | private: |
| 103 | id _M_id; |
| 104 | |
| 105 | public: |
| 106 | thread() noexcept = default; |
| 107 | |
| 108 | |
| 109 | thread(thread&) = delete; |
| 110 | thread(const thread&) = delete; |
| 111 | thread(const thread&&) = delete; |
| 112 | |
| 113 | thread(thread&& __t) noexcept |
| 114 | { swap(__t); } |
| 115 | |
| 116 | template<typename _Callable, typename... _Args> |
| 117 | explicit |
| 118 | thread(_Callable&& __f, _Args&&... __args) |
| 119 | { |
| 120 | #ifdef GTHR_ACTIVE_PROXY |
| 121 | |
| 122 | auto __depend = reinterpret_cast<void(*)()>(&pthread_create); |
| 123 | #else |
| 124 | auto __depend = nullptr; |
| 125 | #endif |
| 126 | _M_start_thread(_S_make_state( |
| 127 | __make_invoker(std::forward<_Callable>(__f), |
| 128 | std::forward<_Args>(__args)...)), |
| 129 | __depend); |
| 130 | } |
| 131 | |
| 132 | ~thread() |
| 133 | { |
| 134 | if (joinable()) |
| 135 | std::terminate(); |
| 136 | } |
| 137 | |
| 138 | thread& operator=(const thread&) = delete; |
| 139 | |
| 140 | thread& operator=(thread&& __t) noexcept |
| 141 | { |
| 142 | if (joinable()) |
| 143 | std::terminate(); |
| 144 | swap(__t); |
| 145 | return *this; |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | swap(thread& __t) noexcept |
| 150 | { std::swap(_M_id, __t._M_id); } |
| 151 | |
| 152 | bool |
| 153 | joinable() const noexcept |
| 154 | { return !(_M_id == id()); } |
| 155 | |
| 156 | void |
| 157 | join(); |
| 158 | |
| 159 | void |
| 160 | detach(); |
| 161 | |
| 162 | thread::id |
| 163 | get_id() const noexcept |
| 164 | { return _M_id; } |
| 165 | |
| 166 | |
| 167 | |
| 168 | native_handle_type |
| 169 | native_handle() |
| 170 | { return _M_id._M_thread; } |
| 171 | |
| 172 | |
| 173 | static unsigned int |
| 174 | hardware_concurrency() noexcept; |
| 175 | |
| 176 | private: |
| 177 | template<typename _Callable> |
| 178 | struct _State_impl : public _State |
| 179 | { |
| 180 | _Callable _M_func; |
| 181 | |
| 182 | _State_impl(_Callable&& __f) : _M_func(std::forward<_Callable>(__f)) |
| 183 | { } |
| 184 | |
| 185 | void |
| 186 | _M_run() { _M_func(); } |
| 187 | }; |
| 188 | |
| 189 | void |
| 190 | _M_start_thread(_State_ptr, void (*)()); |
| 191 | |
| 192 | template<typename _Callable> |
| 193 | static _State_ptr |
| 194 | _S_make_state(_Callable&& __f) |
| 195 | { |
| 196 | using _Impl = _State_impl<_Callable>; |
| 197 | return _State_ptr{new _Impl{std::forward<_Callable>(__f)}}; |
| 198 | } |
| 199 | #if _GLIBCXX_THREAD_ABI_COMPAT |
| 200 | public: |
| 201 | struct _Impl_base; |
| 202 | typedef shared_ptr<_Impl_base> __shared_base_type; |
| 203 | struct _Impl_base |
| 204 | { |
| 205 | __shared_base_type _M_this_ptr; |
| 206 | virtual ~_Impl_base() = default; |
| 207 | virtual void _M_run() = 0; |
| 208 | }; |
| 209 | |
| 210 | private: |
| 211 | void |
| 212 | _M_start_thread(__shared_base_type, void (*)()); |
| 213 | |
| 214 | void |
| 215 | _M_start_thread(__shared_base_type); |
| 216 | #endif |
| 217 | |
| 218 | private: |
| 219 | |
| 220 | template<typename _Tuple> |
| 221 | struct _Invoker |
| 222 | { |
| 223 | _Tuple _M_t; |
| 224 | |
| 225 | template<size_t _Index> |
| 226 | static __tuple_element_t<_Index, _Tuple>&& |
| 227 | _S_declval(); |
| 228 | |
| 229 | template<size_t... _Ind> |
| 230 | auto |
| 231 | _M_invoke(_Index_tuple<_Ind...>) |
| 232 | noexcept(noexcept(std::__invoke(_S_declval<_Ind>()...))) |
| 233 | -> decltype(std::__invoke(_S_declval<_Ind>()...)) |
| 234 | { return std::__invoke(std::get<_Ind>(std::move(_M_t))...); } |
| 235 | |
| 236 | using _Indices |
| 237 | = typename _Build_index_tuple<tuple_size<_Tuple>::value>::__type; |
| 238 | |
| 239 | auto |
| 240 | operator()() |
| 241 | noexcept(noexcept(std::declval<_Invoker&>()._M_invoke(_Indices()))) |
| 242 | -> decltype(std::declval<_Invoker&>()._M_invoke(_Indices())) |
| 243 | { return _M_invoke(_Indices()); } |
| 244 | }; |
| 245 | |
| 246 | template<typename... _Tp> |
| 247 | using __decayed_tuple = tuple<typename std::decay<_Tp>::type...>; |
| 248 | |
| 249 | public: |
| 250 | |
| 251 | |
| 252 | template<typename _Callable, typename... _Args> |
| 253 | static _Invoker<__decayed_tuple<_Callable, _Args...>> |
| 254 | __make_invoker(_Callable&& __callable, _Args&&... __args) |
| 255 | { |
| 256 | return { __decayed_tuple<_Callable, _Args...>{ |
| 257 | std::forward<_Callable>(__callable), std::forward<_Args>(__args)... |
| 258 | } }; |
| 259 | } |
| 260 | }; |
| 261 | |
| 262 | inline void |
| 263 | swap(thread& __x, thread& __y) noexcept |
| 264 | { __x.swap(__y); } |
| 265 | |
| 266 | inline bool |
| 267 | operator==(thread::id __x, thread::id __y) noexcept |
| 268 | { |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | return __x._M_thread == __y._M_thread; |
| 274 | } |
| 275 | |
| 276 | inline bool |
| 277 | operator!=(thread::id __x, thread::id __y) noexcept |
| 278 | { return !(__x == __y); } |
| 279 | |
| 280 | inline bool |
| 281 | operator<(thread::id __x, thread::id __y) noexcept |
| 282 | { |
| 283 | |
| 284 | |
| 285 | return __x._M_thread < __y._M_thread; |
| 286 | } |
| 287 | |
| 288 | inline bool |
| 289 | operator<=(thread::id __x, thread::id __y) noexcept |
| 290 | { return !(__y < __x); } |
| 291 | |
| 292 | inline bool |
| 293 | operator>(thread::id __x, thread::id __y) noexcept |
| 294 | { return __y < __x; } |
| 295 | |
| 296 | inline bool |
| 297 | operator>=(thread::id __x, thread::id __y) noexcept |
| 298 | { return !(__x < __y); } |
| 299 | |
| 300 | |
| 301 | |
| 302 | template<> |
| 303 | struct hash<thread::id> |
| 304 | : public __hash_base<size_t, thread::id> |
| 305 | { |
| 306 | size_t |
| 307 | operator()(const thread::id& __id) const noexcept |
| 308 | { return std::_Hash_impl::hash(__id._M_thread); } |
| 309 | }; |
| 310 | |
| 311 | template<class _CharT, class _Traits> |
| 312 | inline basic_ostream<_CharT, _Traits>& |
| 313 | operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id) |
| 314 | { |
| 315 | if (__id == thread::id()) |
| 316 | return __out << "thread::id of a non-executing thread"; |
| 317 | else |
| 318 | return __out << __id._M_thread; |
| 319 | } |
| 320 | |
| 321 | _GLIBCXX_END_NAMESPACE_VERSION |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | namespace this_thread |
| 328 | { |
| 329 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 330 | |
| 331 | |
| 332 | inline thread::id |
| 333 | get_id() noexcept |
| 334 | { |
| 335 | #ifdef __GLIBC__ |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | if (!__gthread_active_p()) |
| 341 | return thread::id(1); |
| 342 | #endif |
| 343 | return thread::id(__gthread_self()); |
| 344 | } |
| 345 | |
| 346 | |
| 347 | inline void |
| 348 | yield() noexcept |
| 349 | { |
| 350 | #ifdef _GLIBCXX_USE_SCHED_YIELD |
| 351 | __gthread_yield(); |
| 352 | #endif |
| 353 | } |
| 354 | |
| 355 | void |
| 356 | __sleep_for(chrono::seconds, chrono::nanoseconds); |
| 357 | |
| 358 | |
| 359 | template<typename _Rep, typename _Period> |
| 360 | inline void |
| 361 | sleep_for(const chrono::duration<_Rep, _Period>& __rtime) |
| 362 | { |
| 363 | if (__rtime <= __rtime.zero()) |
| 364 | return; |
| 365 | auto __s = chrono::duration_cast<chrono::seconds>(__rtime); |
| 366 | auto __ns = chrono::duration_cast<chrono::nanoseconds>(__rtime - __s); |
| 367 | #ifdef _GLIBCXX_USE_NANOSLEEP |
| 368 | __gthread_time_t __ts = |
| 369 | { |
| 370 | static_cast<std::time_t>(__s.count()), |
| 371 | static_cast<long>(__ns.count()) |
| 372 | }; |
| 373 | while (::nanosleep(&__ts, &__ts) == -1 && errno == EINTR) |
| 374 | { } |
| 375 | #else |
| 376 | __sleep_for(__s, __ns); |
| 377 | #endif |
| 378 | } |
| 379 | |
| 380 | |
| 381 | template<typename _Clock, typename _Duration> |
| 382 | inline void |
| 383 | sleep_until(const chrono::time_point<_Clock, _Duration>& __atime) |
| 384 | { |
| 385 | auto __now = _Clock::now(); |
| 386 | if (_Clock::is_steady) |
| 387 | { |
| 388 | if (__now < __atime) |
| 389 | sleep_for(__atime - __now); |
| 390 | return; |
| 391 | } |
| 392 | while (__now < __atime) |
| 393 | { |
| 394 | sleep_for(__atime - __now); |
| 395 | __now = _Clock::now(); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | _GLIBCXX_END_NAMESPACE_VERSION |
| 400 | } |
| 401 | |
| 402 | |
| 403 | |
| 404 | } |
| 405 | |
| 406 | #endif |
| 407 | |
| 408 | #endif |
| 409 | |
| 410 | #endif |
| 411 | |