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