29 #ifndef _GLIBCXX_THREAD
30 #define _GLIBCXX_THREAD 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
40 #if defined(_GLIBCXX_HAS_GTHREADS)
41 #include <bits/gthr.h>
47 #if __cplusplus > 201703L
51 #ifdef _GLIBCXX_USE_NANOSLEEP
59 namespace std _GLIBCXX_VISIBILITY(default)
61 _GLIBCXX_BEGIN_NAMESPACE_VERSION
80 virtual void _M_run() = 0;
84 typedef __gthread_t native_handle_type;
89 native_handle_type _M_thread;
92 id() noexcept : _M_thread() { }
95 id(native_handle_type __id) : _M_thread(__id) { }
102 operator==(thread::id __x, thread::id __y) noexcept;
105 operator<(thread::id __x, thread::id __y) noexcept;
107 template<class _CharT, class _Traits>
108 friend basic_ostream<_CharT, _Traits>&
109 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id);
118 template<typename _Tp>
119 using __not_same = __not_<is_same<__remove_cvref_t<_Tp>, thread>>;
122 thread() noexcept = default;
124 template<typename _Callable, typename... _Args,
125 typename = _Require<__not_same<_Callable>>>
127 thread(_Callable&& __f, _Args&&... __args)
129 static_assert( __is_invocable<typename decay<_Callable>::type,
130 typename decay<_Args>::type...>::value,
131 "std::thread arguments must be invocable after conversion to rvalues"
134 #ifdef GTHR_ACTIVE_PROXY
136 auto __depend = reinterpret_cast<void(*)()>(&pthread_create);
138 auto __depend = nullptr;
141 using _Invoker_type = _Invoker<__decayed_tuple<_Callable, _Args...>>;
143 _M_start_thread(_S_make_state<_Invoker_type>(
144 std::forward<_Callable>(__f), std::forward<_Args>(__args)...),
154 thread(const thread&) = delete;
156 thread(thread&& __t) noexcept
159 thread& operator=(const thread&) = delete;
161 thread& operator=(thread&& __t) noexcept
170 swap(thread& __t) noexcept
171 { std::swap(_M_id, __t._M_id); }
174 joinable() const noexcept
175 { return !(_M_id == id()); }
184 get_id() const noexcept
191 { return _M_id._M_thread; }
195 hardware_concurrency() noexcept;
198 template<typename _Callable>
199 struct _State_impl : public _State
203 template<typename... _Args>
204 _State_impl(_Args&&... __args)
205 : _M_func{{std::forward<_Args>(__args)...}}
209 _M_run() { _M_func(); }
213 _M_start_thread(_State_ptr, void (*)());
215 template<typename _Callable, typename... _Args>
217 _S_make_state(_Args&&... __args)
219 using _Impl = _State_impl<_Callable>;
220 return _State_ptr{new _Impl{std::forward<_Args>(__args)...}};
222 #if _GLIBCXX_THREAD_ABI_COMPAT
225 typedef shared_ptr<_Impl_base> __shared_base_type;
228 __shared_base_type _M_this_ptr;
229 virtual ~_Impl_base() = default;
230 virtual void _M_run() = 0;
235 _M_start_thread(__shared_base_type, void (*)());
238 _M_start_thread(__shared_base_type);
243 template<typename _Tuple>
250 template<typename _Fn, typename... _Args>
251 struct __result<tuple<_Fn, _Args...>>
252 : __invoke_result<_Fn, _Args...>
255 template<size_t... _Ind>
256 typename __result<_Tuple>::type
257 _M_invoke(_Index_tuple<_Ind...>)
258 { return std::__invoke(std::get<_Ind>(std::move(_M_t))...); }
260 typename __result<_Tuple>::type
264 = typename _Build_index_tuple<tuple_size<_Tuple>::value>::__type;
265 return _M_invoke(_Indices());
269 template<typename... _Tp>
270 using __decayed_tuple = tuple<typename decay<_Tp>::type...>;
275 template<typename _Callable, typename... _Args>
276 static _Invoker<__decayed_tuple<_Callable, _Args...>>
277 __make_invoker(_Callable&& __callable, _Args&&... __args)
279 return { __decayed_tuple<_Callable, _Args...>{
280 std::forward<_Callable>(__callable), std::forward<_Args>(__args)...
286 swap(thread& __x, thread& __y) noexcept
290 operator==(thread::id __x, thread::id __y) noexcept
296 return __x._M_thread == __y._M_thread;
300 operator!=(thread::id __x, thread::id __y) noexcept
301 { return !(__x == __y); }
304 operator<(thread::id __x, thread::id __y) noexcept
308 return __x._M_thread < __y._M_thread;
312 operator<=(thread::id __x, thread::id __y) noexcept
313 { return !(__y < __x); }
316 operator>(thread::id __x, thread::id __y) noexcept
317 { return __y < __x; }
320 operator>=(thread::id __x, thread::id __y) noexcept
321 { return !(__x < __y); }
326 struct hash<thread::id>
327 : public __hash_base<size_t, thread::id>
330 operator()(const thread::id& __id) const noexcept
331 { return std::_Hash_impl::hash(__id._M_thread); }
334 template<class _CharT, class _Traits>
335 inline basic_ostream<_CharT, _Traits>&
336 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
338 if (__id == thread::id())
339 return __out << "thread::id of a non-executing thread";
341 return __out << __id._M_thread;
349 namespace this_thread
360 if (!__gthread_active_p())
361 return thread::id(1);
363 return thread::id(__gthread_self());
370 #ifdef _GLIBCXX_USE_SCHED_YIELD
376 __sleep_for(chrono::seconds, chrono::nanoseconds);
379 template<typename _Rep, typename _Period>
381 sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
383 if (__rtime <= __rtime.zero())
385 auto __s = chrono::duration_cast<chrono::seconds>(__rtime);
386 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
387 #ifdef _GLIBCXX_USE_NANOSLEEP
388 __gthread_time_t __ts =
390 static_cast<std::time_t>(__s.count()),
391 static_cast<long>(__ns.count())
393 while (::nanosleep(&__ts, &__ts) == -1 && errno == EINTR)
396 __sleep_for(__s, __ns);
401 template<typename _Clock, typename _Duration>
403 sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
405 auto __now = _Clock::now();
406 if (_Clock::is_steady)
409 sleep_for(__atime - __now);
412 while (__now < __atime)
414 sleep_for(__atime - __now);
415 __now = _Clock::now();
422 #ifdef __cpp_lib_jthread
427 using id = std::thread::id;
428 using native_handle_type = std::thread::native_handle_type;
431 : _M_stop_source{nostopstate}
434 template<typename _Callable, typename... _Args,
435 typename = enable_if_t<!is_same_v<remove_cvref_t<_Callable>,
438 jthread(_Callable&& __f, _Args&&... __args)
439 : _M_thread{_S_create(_M_stop_source, std::forward<_Callable>(__f),
440 std::forward<_Args>(__args)...)}
443 jthread(const jthread&) = delete;
444 jthread(jthread&&) noexcept = default;
456 operator=(const jthread&) = delete;
459 operator=(jthread&&) noexcept = default;
462 swap(jthread& __other) noexcept
464 std::swap(_M_stop_source, __other._M_stop_source);
465 std::swap(_M_thread, __other._M_thread);
469 joinable() const noexcept
471 return _M_thread.joinable();
487 get_id() const noexcept
489 return _M_thread.get_id();
492 [[nodiscard]] native_handle_type
495 return _M_thread.native_handle();
498 [[nodiscard]] static unsigned
499 hardware_concurrency() noexcept
501 return std::thread::hardware_concurrency();
504 [[nodiscard]] stop_source
505 get_stop_source() noexcept
507 return _M_stop_source;
510 [[nodiscard]] stop_token
511 get_stop_token() const noexcept
513 return _M_stop_source.get_token();
516 bool request_stop() noexcept
518 return _M_stop_source.request_stop();
521 friend void swap(jthread& __lhs, jthread& __rhs) noexcept
527 template<typename _Callable, typename... _Args>
529 _S_create(stop_source& __ssrc, _Callable&& __f, _Args&&... __args)
531 if constexpr(is_invocable_v<decay_t<_Callable>, stop_token,
533 return thread{std::forward<_Callable>(__f), __ssrc.get_token(),
534 std::forward<_Args>(__args)...};
537 static_assert(is_invocable_v<decay_t<_Callable>,
539 "std::thread arguments must be invocable after"
540 " conversion to rvalues");
541 return thread{std::forward<_Callable>(__f),
542 std::forward<_Args>(__args)...};
546 stop_source _M_stop_source;
547 std::thread _M_thread;
550 _GLIBCXX_END_NAMESPACE_VERSION