2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___THREAD_JTHREAD_H
11 #define _LIBCPP___THREAD_JTHREAD_H
14 #include <__stop_token/stop_source.h>
15 #include <__stop_token/stop_token.h>
16 #include <__thread/id.h>
17 #include <__thread/support.h>
18 #include <__thread/thread.h>
19 #include <__type_traits/decay.h>
20 #include <__type_traits/invoke.h>
21 #include <__type_traits/is_constructible.h>
22 #include <__type_traits/is_same.h>
23 #include <__type_traits/remove_cvref.h>
24 #include <__utility/forward.h>
25 #include <__utility/move.h>
26 #include <__utility/swap.h>
28 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29 # pragma GCC system_header
33 #include <__undef_macros>
35 #if _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_THREADS
37 _LIBCPP_BEGIN_NAMESPACE_STD
39 class _LIBCPP_AVAILABILITY_SYNC jthread
{
42 using id
= thread::id
;
43 using native_handle_type
= thread::native_handle_type
;
45 // [thread.jthread.cons], constructors, move, and assignment
46 _LIBCPP_HIDE_FROM_ABI
jthread() noexcept
: __stop_source_(std::nostopstate
) {}
48 template <class _Fun
, class... _Args
>
49 _LIBCPP_HIDE_FROM_ABI
explicit jthread(_Fun
&& __fun
, _Args
&&... __args
)
50 requires(!std::is_same_v
<remove_cvref_t
<_Fun
>, jthread
>)
52 __thread_(__init_thread(__stop_source_
, std::forward
<_Fun
>(__fun
), std::forward
<_Args
>(__args
)...)) {
53 static_assert(is_constructible_v
<decay_t
<_Fun
>, _Fun
>);
54 static_assert((is_constructible_v
<decay_t
<_Args
>, _Args
> && ...));
55 static_assert(is_invocable_v
<decay_t
<_Fun
>, decay_t
<_Args
>...> ||
56 is_invocable_v
<decay_t
<_Fun
>, stop_token
, decay_t
<_Args
>...>);
59 _LIBCPP_HIDE_FROM_ABI
~jthread() {
66 jthread(const jthread
&) = delete;
68 _LIBCPP_HIDE_FROM_ABI
jthread(jthread
&&) noexcept
= default;
70 jthread
& operator=(const jthread
&) = delete;
72 _LIBCPP_HIDE_FROM_ABI jthread
& operator=(jthread
&& __other
) noexcept
{
73 if (this != &__other
) {
78 __stop_source_
= std::move(__other
.__stop_source_
);
79 __thread_
= std::move(__other
.__thread_
);
85 // [thread.jthread.mem], members
86 _LIBCPP_HIDE_FROM_ABI
void swap(jthread
& __other
) noexcept
{
87 std::swap(__stop_source_
, __other
.__stop_source_
);
88 std::swap(__thread_
, __other
.__thread_
);
91 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
bool joinable() const noexcept
{ return get_id() != id(); }
93 _LIBCPP_HIDE_FROM_ABI
void join() { __thread_
.join(); }
95 _LIBCPP_HIDE_FROM_ABI
void detach() { __thread_
.detach(); }
97 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI id
get_id() const noexcept
{ return __thread_
.get_id(); }
99 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI native_handle_type
native_handle() { return __thread_
.native_handle(); }
101 // [thread.jthread.stop], stop token handling
102 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI stop_source
get_stop_source() noexcept
{ return __stop_source_
; }
104 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI stop_token
get_stop_token() const noexcept
{ return __stop_source_
.get_token(); }
106 _LIBCPP_HIDE_FROM_ABI
bool request_stop() noexcept
{ return __stop_source_
.request_stop(); }
108 // [thread.jthread.special], specialized algorithms
109 _LIBCPP_HIDE_FROM_ABI
friend void swap(jthread
& __lhs
, jthread
& __rhs
) noexcept
{ __lhs
.swap(__rhs
); }
111 // [thread.jthread.static], static members
112 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
static unsigned int hardware_concurrency() noexcept
{
113 return thread::hardware_concurrency();
117 template <class _Fun
, class... _Args
>
118 _LIBCPP_HIDE_FROM_ABI
static thread
__init_thread(const stop_source
& __ss
, _Fun
&& __fun
, _Args
&&... __args
) {
119 if constexpr (is_invocable_v
<decay_t
<_Fun
>, stop_token
, decay_t
<_Args
>...>) {
120 return thread(std::forward
<_Fun
>(__fun
), __ss
.get_token(), std::forward
<_Args
>(__args
)...);
122 return thread(std::forward
<_Fun
>(__fun
), std::forward
<_Args
>(__args
)...);
126 stop_source __stop_source_
;
130 _LIBCPP_END_NAMESPACE_STD
132 #endif // _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_THREADS
136 #endif // _LIBCPP___THREAD_JTHREAD_H