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 #if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN) 32 33 _LIBCPP_BEGIN_NAMESPACE_STD 34 35 class _LIBCPP_AVAILABILITY_SYNC jthread { 36 public: 37 // types 38 using id = thread::id; 39 using native_handle_type = thread::native_handle_type; 40 41 // [thread.jthread.cons], constructors, move, and assignment 42 _LIBCPP_HIDE_FROM_ABI jthread() noexcept : __stop_source_(std::nostopstate) {} 43 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>...>); 53 } 54 55 _LIBCPP_HIDE_FROM_ABI ~jthread() { 56 if (joinable()) { 57 request_stop(); 58 join(); 59 } 60 } 61 62 jthread(const jthread&) = delete; 63 64 _LIBCPP_HIDE_FROM_ABI jthread(jthread&&) noexcept = default; 65 66 jthread& operator=(const jthread&) = delete; 67 68 _LIBCPP_HIDE_FROM_ABI jthread& operator=(jthread&& __other) noexcept { 69 if (this != &__other) { 70 if (joinable()) { 71 request_stop(); 72 join(); 73 } 74 __stop_source_ = std::move(__other.__stop_source_); 75 __thread_ = std::move(__other.__thread_); 76 } 77 78 return *this; 79 } 80 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_); 85 } 86 87 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool joinable() const noexcept { return get_id() != id(); } 88 89 _LIBCPP_HIDE_FROM_ABI void join() { __thread_.join(); } 90 91 _LIBCPP_HIDE_FROM_ABI void detach() { __thread_.detach(); } 92 93 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI id get_id() const noexcept { return __thread_.get_id(); } 94 95 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return __thread_.native_handle(); } 96 97 // [thread.jthread.stop], stop token handling 98 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_source get_stop_source() noexcept { return __stop_source_; } 99 100 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_token get_stop_token() const noexcept { return __stop_source_.get_token(); } 101 102 _LIBCPP_HIDE_FROM_ABI bool request_stop() noexcept { return __stop_source_.request_stop(); } 103 104 // [thread.jthread.special], specialized algorithms 105 _LIBCPP_HIDE_FROM_ABI friend void swap(jthread& __lhs, jthread& __rhs) noexcept { __lhs.swap(__rhs); } 106 107 // [thread.jthread.static], static members 108 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static unsigned int hardware_concurrency() noexcept { 109 return thread::hardware_concurrency(); 110 } 111 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)...); 119 } 120 } 121 122 stop_source __stop_source_; 123 thread __thread_; 124 }; 125 126 _LIBCPP_END_NAMESPACE_STD 127 128 #endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN) 129 130 #endif // _LIBCPP___THREAD_JTHREAD_H 131