1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
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
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___THREAD_JTHREAD_H
11 #define _LIBCPP___THREAD_JTHREAD_H
12 
13 #include <__availability>
14 #include <__config>
15 #include <__functional/invoke.h>
16 #include <__stop_token/stop_source.h>
17 #include <__stop_token/stop_token.h>
18 #include <__thread/thread.h>
19 #include <__threading_support>
20 #include <__type_traits/decay.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 
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 #  pragma GCC system_header
29 #endif
30 
31 _LIBCPP_PUSH_MACROS
32 #include <__undef_macros>
33 
34 #if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
35 
36 _LIBCPP_BEGIN_NAMESPACE_STD
37 
38 class _LIBCPP_AVAILABILITY_SYNC jthread {
39 public:
40   // types
41   using id                 = thread::id;
42   using native_handle_type = thread::native_handle_type;
43 
44   // [thread.jthread.cons], constructors, move, and assignment
45   _LIBCPP_HIDE_FROM_ABI jthread() noexcept : __stop_source_(std::nostopstate) {}
46 
47   template <class _Fun, class... _Args>
48   _LIBCPP_HIDE_FROM_ABI explicit jthread(_Fun&& __fun, _Args&&... __args)
49     requires(!std::is_same_v<remove_cvref_t<_Fun>, jthread>)
50       : __stop_source_(),
51         __thread_(__init_thread(__stop_source_, std::forward<_Fun>(__fun), std::forward<_Args>(__args)...)) {
52     static_assert(is_constructible_v<decay_t<_Fun>, _Fun>);
53     static_assert((is_constructible_v<decay_t<_Args>, _Args> && ...));
54     static_assert(is_invocable_v<decay_t<_Fun>, decay_t<_Args>...> ||
55                   is_invocable_v<decay_t<_Fun>, stop_token, decay_t<_Args>...>);
56   }
57 
58   _LIBCPP_HIDE_FROM_ABI ~jthread() {
59     if (joinable()) {
60       request_stop();
61       join();
62     }
63   }
64 
65   jthread(const jthread&) = delete;
66 
67   _LIBCPP_HIDE_FROM_ABI jthread(jthread&&) noexcept = default;
68 
69   jthread& operator=(const jthread&) = delete;
70 
71   _LIBCPP_HIDE_FROM_ABI jthread& operator=(jthread&& __other) noexcept {
72     if (this != &__other) {
73       if (joinable()) {
74         request_stop();
75         join();
76       }
77       __stop_source_ = std::move(__other.__stop_source_);
78       __thread_      = std::move(__other.__thread_);
79     }
80 
81     return *this;
82   }
83 
84   // [thread.jthread.mem], members
85   _LIBCPP_HIDE_FROM_ABI void swap(jthread& __other) noexcept {
86     std::swap(__stop_source_, __other.__stop_source_);
87     std::swap(__thread_, __other.__thread_);
88   }
89 
90   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool joinable() const noexcept { return get_id() != id(); }
91 
92   _LIBCPP_HIDE_FROM_ABI void join() { __thread_.join(); }
93 
94   _LIBCPP_HIDE_FROM_ABI void detach() { __thread_.detach(); }
95 
96   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI id get_id() const noexcept { return __thread_.get_id(); }
97 
98   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return __thread_.native_handle(); }
99 
100   // [thread.jthread.stop], stop token handling
101   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_source get_stop_source() noexcept { return __stop_source_; }
102 
103   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_token get_stop_token() const noexcept { return __stop_source_.get_token(); }
104 
105   _LIBCPP_HIDE_FROM_ABI bool request_stop() noexcept { return __stop_source_.request_stop(); }
106 
107   // [thread.jthread.special], specialized algorithms
108   _LIBCPP_HIDE_FROM_ABI friend void swap(jthread& __lhs, jthread& __rhs) noexcept { __lhs.swap(__rhs); }
109 
110   // [thread.jthread.static], static members
111   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static unsigned int hardware_concurrency() noexcept {
112     return thread::hardware_concurrency();
113   }
114 
115 private:
116   template <class _Fun, class... _Args>
117   _LIBCPP_HIDE_FROM_ABI static thread __init_thread(const stop_source& __ss, _Fun&& __fun, _Args&&... __args) {
118     if constexpr (is_invocable_v<decay_t<_Fun>, stop_token, decay_t<_Args>...>) {
119       return thread(std::forward<_Fun>(__fun), __ss.get_token(), std::forward<_Args>(__args)...);
120     } else {
121       return thread(std::forward<_Fun>(__fun), std::forward<_Args>(__args)...);
122     }
123   }
124 
125   stop_source __stop_source_;
126   thread __thread_;
127 };
128 
129 _LIBCPP_END_NAMESPACE_STD
130 
131 #endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
132 
133 _LIBCPP_POP_MACROS
134 
135 #endif // _LIBCPP___THREAD_JTHREAD_H
136