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 <__config>
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>
27
28#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29# pragma GCC system_header
30#endif
31
32_LIBCPP_PUSH_MACROS
33#include <__undef_macros>
34
35#if _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_THREADS
36
37_LIBCPP_BEGIN_NAMESPACE_STD
38
39class _LIBCPP_AVAILABILITY_SYNC jthread {
40public:
41 // types
42 using id = thread::id;
43 using native_handle_type = thread::native_handle_type;
44
45 // [thread.jthread.cons], constructors, move, and assignment
46 _LIBCPP_HIDE_FROM_ABI jthread() noexcept : __stop_source_(std::nostopstate) {}
47
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>)
51 : __stop_source_(),
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>...>);
57 }
58
59 _LIBCPP_HIDE_FROM_ABI ~jthread() {
60 if (joinable()) {
61 request_stop();
62 join();
63 }
64 }
65
66 jthread(const jthread&) = delete;
67
68 _LIBCPP_HIDE_FROM_ABI jthread(jthread&&) noexcept = default;
69
70 jthread& operator=(const jthread&) = delete;
71
72 _LIBCPP_HIDE_FROM_ABI jthread& operator=(jthread&& __other) noexcept {
73 if (this != &__other) {
74 if (joinable()) {
75 request_stop();
76 join();
77 }
78 __stop_source_ = std::move(__other.__stop_source_);
79 __thread_ = std::move(__other.__thread_);
80 }
81
82 return *this;
83 }
84
85 // [thread.jthread.mem], members
86 _LIBCPP_HIDE_FROM_ABI void swap(jthread& __other) noexcept {
87 std::swap(x&: __stop_source_, y&: __other.__stop_source_);
88 std::swap(x&: __thread_, y&: __other.__thread_);
89 }
90
91 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool joinable() const noexcept { return get_id() != id(); }
92
93 _LIBCPP_HIDE_FROM_ABI void join() { __thread_.join(); }
94
95 _LIBCPP_HIDE_FROM_ABI void detach() { __thread_.detach(); }
96
97 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI id get_id() const noexcept { return __thread_.get_id(); }
98
99 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return __thread_.native_handle(); }
100
101 // [thread.jthread.stop], stop token handling
102 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_source get_stop_source() noexcept { return __stop_source_; }
103
104 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_token get_stop_token() const noexcept { return __stop_source_.get_token(); }
105
106 _LIBCPP_HIDE_FROM_ABI bool request_stop() noexcept { return __stop_source_.request_stop(); }
107
108 // [thread.jthread.special], specialized algorithms
109 _LIBCPP_HIDE_FROM_ABI friend void swap(jthread& __lhs, jthread& __rhs) noexcept { __lhs.swap(other&: __rhs); }
110
111 // [thread.jthread.static], static members
112 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static unsigned int hardware_concurrency() noexcept {
113 return thread::hardware_concurrency();
114 }
115
116private:
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)...);
121 } else {
122 return thread(std::forward<_Fun>(__fun), std::forward<_Args>(__args)...);
123 }
124 }
125
126 stop_source __stop_source_;
127 thread __thread_;
128};
129
130_LIBCPP_END_NAMESPACE_STD
131
132#endif // _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_THREADS
133
134_LIBCPP_POP_MACROS
135
136#endif // _LIBCPP___THREAD_JTHREAD_H
137