Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / include / __thread / jthread.h
blobfc86b13afb13431da71eaf0ab2c63dadac0f2953
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 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___THREAD_JTHREAD_H
11 #define _LIBCPP___THREAD_JTHREAD_H
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>
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 # pragma GCC system_header
29 #endif
31 #if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
33 _LIBCPP_BEGIN_NAMESPACE_STD
35 class _LIBCPP_AVAILABILITY_SYNC jthread {
36 public:
37 // types
38 using id = thread::id;
39 using native_handle_type = thread::native_handle_type;
41 // [thread.jthread.cons], constructors, move, and assignment
42 _LIBCPP_HIDE_FROM_ABI jthread() noexcept : __stop_source_(std::nostopstate) {}
44 template <class _Fun, class... _Args>
45 _LIBCPP_HIDE_FROM_ABI explicit jthread(_Fun&& __fun, _Args&&... __args)
46 requires(!std::is_same_v<remove_cvref_t<_Fun>, jthread>)
47 : __stop_source_(),
48 __thread_(__init_thread(__stop_source_, std::forward<_Fun>(__fun), std::forward<_Args>(__args)...)) {
49 static_assert(is_constructible_v<decay_t<_Fun>, _Fun>);
50 static_assert((is_constructible_v<decay_t<_Args>, _Args> && ...));
51 static_assert(is_invocable_v<decay_t<_Fun>, decay_t<_Args>...> ||
52 is_invocable_v<decay_t<_Fun>, stop_token, decay_t<_Args>...>);
55 _LIBCPP_HIDE_FROM_ABI ~jthread() {
56 if (joinable()) {
57 request_stop();
58 join();
62 jthread(const jthread&) = delete;
64 _LIBCPP_HIDE_FROM_ABI jthread(jthread&&) noexcept = default;
66 jthread& operator=(const jthread&) = delete;
68 _LIBCPP_HIDE_FROM_ABI jthread& operator=(jthread&& __other) noexcept {
69 if (this != &__other) {
70 if (joinable()) {
71 request_stop();
72 join();
74 __stop_source_ = std::move(__other.__stop_source_);
75 __thread_ = std::move(__other.__thread_);
78 return *this;
81 // [thread.jthread.mem], members
82 _LIBCPP_HIDE_FROM_ABI void swap(jthread& __other) noexcept {
83 std::swap(__stop_source_, __other.__stop_source_);
84 std::swap(__thread_, __other.__thread_);
87 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool joinable() const noexcept { return get_id() != id(); }
89 _LIBCPP_HIDE_FROM_ABI void join() { __thread_.join(); }
91 _LIBCPP_HIDE_FROM_ABI void detach() { __thread_.detach(); }
93 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI id get_id() const noexcept { return __thread_.get_id(); }
95 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return __thread_.native_handle(); }
97 // [thread.jthread.stop], stop token handling
98 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_source get_stop_source() noexcept { return __stop_source_; }
100 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_token get_stop_token() const noexcept { return __stop_source_.get_token(); }
102 _LIBCPP_HIDE_FROM_ABI bool request_stop() noexcept { return __stop_source_.request_stop(); }
104 // [thread.jthread.special], specialized algorithms
105 _LIBCPP_HIDE_FROM_ABI friend void swap(jthread& __lhs, jthread& __rhs) noexcept { __lhs.swap(__rhs); }
107 // [thread.jthread.static], static members
108 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static unsigned int hardware_concurrency() noexcept {
109 return thread::hardware_concurrency();
112 private:
113 template <class _Fun, class... _Args>
114 _LIBCPP_HIDE_FROM_ABI static thread __init_thread(const stop_source& __ss, _Fun&& __fun, _Args&&... __args) {
115 if constexpr (is_invocable_v<decay_t<_Fun>, stop_token, decay_t<_Args>...>) {
116 return thread(std::forward<_Fun>(__fun), __ss.get_token(), std::forward<_Args>(__args)...);
117 } else {
118 return thread(std::forward<_Fun>(__fun), std::forward<_Args>(__args)...);
122 stop_source __stop_source_;
123 thread __thread_;
126 _LIBCPP_END_NAMESPACE_STD
128 #endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
130 #endif // _LIBCPP___THREAD_JTHREAD_H