Path: blob/main/contrib/llvm-project/libcxx/include/__thread/jthread.h
35233 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___THREAD_JTHREAD_H10#define _LIBCPP___THREAD_JTHREAD_H1112#include <__config>13#include <__functional/invoke.h>14#include <__stop_token/stop_source.h>15#include <__stop_token/stop_token.h>16#include <__thread/support.h>17#include <__thread/thread.h>18#include <__type_traits/decay.h>19#include <__type_traits/is_constructible.h>20#include <__type_traits/is_same.h>21#include <__type_traits/remove_cvref.h>22#include <__utility/forward.h>23#include <__utility/move.h>2425#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)26# pragma GCC system_header27#endif2829_LIBCPP_PUSH_MACROS30#include <__undef_macros>3132#if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)3334_LIBCPP_BEGIN_NAMESPACE_STD3536class _LIBCPP_AVAILABILITY_SYNC jthread {37public:38// types39using id = thread::id;40using native_handle_type = thread::native_handle_type;4142// [thread.jthread.cons], constructors, move, and assignment43_LIBCPP_HIDE_FROM_ABI jthread() noexcept : __stop_source_(std::nostopstate) {}4445template <class _Fun, class... _Args>46_LIBCPP_HIDE_FROM_ABI explicit jthread(_Fun&& __fun, _Args&&... __args)47requires(!std::is_same_v<remove_cvref_t<_Fun>, jthread>)48: __stop_source_(),49__thread_(__init_thread(__stop_source_, std::forward<_Fun>(__fun), std::forward<_Args>(__args)...)) {50static_assert(is_constructible_v<decay_t<_Fun>, _Fun>);51static_assert((is_constructible_v<decay_t<_Args>, _Args> && ...));52static_assert(is_invocable_v<decay_t<_Fun>, decay_t<_Args>...> ||53is_invocable_v<decay_t<_Fun>, stop_token, decay_t<_Args>...>);54}5556_LIBCPP_HIDE_FROM_ABI ~jthread() {57if (joinable()) {58request_stop();59join();60}61}6263jthread(const jthread&) = delete;6465_LIBCPP_HIDE_FROM_ABI jthread(jthread&&) noexcept = default;6667jthread& operator=(const jthread&) = delete;6869_LIBCPP_HIDE_FROM_ABI jthread& operator=(jthread&& __other) noexcept {70if (this != &__other) {71if (joinable()) {72request_stop();73join();74}75__stop_source_ = std::move(__other.__stop_source_);76__thread_ = std::move(__other.__thread_);77}7879return *this;80}8182// [thread.jthread.mem], members83_LIBCPP_HIDE_FROM_ABI void swap(jthread& __other) noexcept {84std::swap(__stop_source_, __other.__stop_source_);85std::swap(__thread_, __other.__thread_);86}8788[[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool joinable() const noexcept { return get_id() != id(); }8990_LIBCPP_HIDE_FROM_ABI void join() { __thread_.join(); }9192_LIBCPP_HIDE_FROM_ABI void detach() { __thread_.detach(); }9394[[nodiscard]] _LIBCPP_HIDE_FROM_ABI id get_id() const noexcept { return __thread_.get_id(); }9596[[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return __thread_.native_handle(); }9798// [thread.jthread.stop], stop token handling99[[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_source get_stop_source() noexcept { return __stop_source_; }100101[[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_token get_stop_token() const noexcept { return __stop_source_.get_token(); }102103_LIBCPP_HIDE_FROM_ABI bool request_stop() noexcept { return __stop_source_.request_stop(); }104105// [thread.jthread.special], specialized algorithms106_LIBCPP_HIDE_FROM_ABI friend void swap(jthread& __lhs, jthread& __rhs) noexcept { __lhs.swap(__rhs); }107108// [thread.jthread.static], static members109[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static unsigned int hardware_concurrency() noexcept {110return thread::hardware_concurrency();111}112113private:114template <class _Fun, class... _Args>115_LIBCPP_HIDE_FROM_ABI static thread __init_thread(const stop_source& __ss, _Fun&& __fun, _Args&&... __args) {116if constexpr (is_invocable_v<decay_t<_Fun>, stop_token, decay_t<_Args>...>) {117return thread(std::forward<_Fun>(__fun), __ss.get_token(), std::forward<_Args>(__args)...);118} else {119return thread(std::forward<_Fun>(__fun), std::forward<_Args>(__args)...);120}121}122123stop_source __stop_source_;124thread __thread_;125};126127_LIBCPP_END_NAMESPACE_STD128129#endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)130131_LIBCPP_POP_MACROS132133#endif // _LIBCPP___THREAD_JTHREAD_H134135136