Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:14:04

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___THREAD_JTHREAD_H
0011 #define _LIBCPP___THREAD_JTHREAD_H
0012 
0013 #include <__config>
0014 #include <__stop_token/stop_source.h>
0015 #include <__stop_token/stop_token.h>
0016 #include <__thread/id.h>
0017 #include <__thread/support.h>
0018 #include <__thread/thread.h>
0019 #include <__type_traits/decay.h>
0020 #include <__type_traits/invoke.h>
0021 #include <__type_traits/is_constructible.h>
0022 #include <__type_traits/is_same.h>
0023 #include <__type_traits/remove_cvref.h>
0024 #include <__utility/forward.h>
0025 #include <__utility/move.h>
0026 #include <__utility/swap.h>
0027 
0028 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0029 #  pragma GCC system_header
0030 #endif
0031 
0032 _LIBCPP_PUSH_MACROS
0033 #include <__undef_macros>
0034 
0035 #if _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_THREADS
0036 
0037 _LIBCPP_BEGIN_NAMESPACE_STD
0038 
0039 class _LIBCPP_AVAILABILITY_SYNC jthread {
0040 public:
0041   // types
0042   using id                 = thread::id;
0043   using native_handle_type = thread::native_handle_type;
0044 
0045   // [thread.jthread.cons], constructors, move, and assignment
0046   _LIBCPP_HIDE_FROM_ABI jthread() noexcept : __stop_source_(std::nostopstate) {}
0047 
0048   template <class _Fun, class... _Args>
0049   _LIBCPP_HIDE_FROM_ABI explicit jthread(_Fun&& __fun, _Args&&... __args)
0050     requires(!std::is_same_v<remove_cvref_t<_Fun>, jthread>)
0051       : __stop_source_(),
0052         __thread_(__init_thread(__stop_source_, std::forward<_Fun>(__fun), std::forward<_Args>(__args)...)) {
0053     static_assert(is_constructible_v<decay_t<_Fun>, _Fun>);
0054     static_assert((is_constructible_v<decay_t<_Args>, _Args> && ...));
0055     static_assert(is_invocable_v<decay_t<_Fun>, decay_t<_Args>...> ||
0056                   is_invocable_v<decay_t<_Fun>, stop_token, decay_t<_Args>...>);
0057   }
0058 
0059   _LIBCPP_HIDE_FROM_ABI ~jthread() {
0060     if (joinable()) {
0061       request_stop();
0062       join();
0063     }
0064   }
0065 
0066   jthread(const jthread&) = delete;
0067 
0068   _LIBCPP_HIDE_FROM_ABI jthread(jthread&&) noexcept = default;
0069 
0070   jthread& operator=(const jthread&) = delete;
0071 
0072   _LIBCPP_HIDE_FROM_ABI jthread& operator=(jthread&& __other) noexcept {
0073     if (this != &__other) {
0074       if (joinable()) {
0075         request_stop();
0076         join();
0077       }
0078       __stop_source_ = std::move(__other.__stop_source_);
0079       __thread_      = std::move(__other.__thread_);
0080     }
0081 
0082     return *this;
0083   }
0084 
0085   // [thread.jthread.mem], members
0086   _LIBCPP_HIDE_FROM_ABI void swap(jthread& __other) noexcept {
0087     std::swap(__stop_source_, __other.__stop_source_);
0088     std::swap(__thread_, __other.__thread_);
0089   }
0090 
0091   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool joinable() const noexcept { return get_id() != id(); }
0092 
0093   _LIBCPP_HIDE_FROM_ABI void join() { __thread_.join(); }
0094 
0095   _LIBCPP_HIDE_FROM_ABI void detach() { __thread_.detach(); }
0096 
0097   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI id get_id() const noexcept { return __thread_.get_id(); }
0098 
0099   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return __thread_.native_handle(); }
0100 
0101   // [thread.jthread.stop], stop token handling
0102   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_source get_stop_source() noexcept { return __stop_source_; }
0103 
0104   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_token get_stop_token() const noexcept { return __stop_source_.get_token(); }
0105 
0106   _LIBCPP_HIDE_FROM_ABI bool request_stop() noexcept { return __stop_source_.request_stop(); }
0107 
0108   // [thread.jthread.special], specialized algorithms
0109   _LIBCPP_HIDE_FROM_ABI friend void swap(jthread& __lhs, jthread& __rhs) noexcept { __lhs.swap(__rhs); }
0110 
0111   // [thread.jthread.static], static members
0112   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static unsigned int hardware_concurrency() noexcept {
0113     return thread::hardware_concurrency();
0114   }
0115 
0116 private:
0117   template <class _Fun, class... _Args>
0118   _LIBCPP_HIDE_FROM_ABI static thread __init_thread(const stop_source& __ss, _Fun&& __fun, _Args&&... __args) {
0119     if constexpr (is_invocable_v<decay_t<_Fun>, stop_token, decay_t<_Args>...>) {
0120       return thread(std::forward<_Fun>(__fun), __ss.get_token(), std::forward<_Args>(__args)...);
0121     } else {
0122       return thread(std::forward<_Fun>(__fun), std::forward<_Args>(__args)...);
0123     }
0124   }
0125 
0126   stop_source __stop_source_;
0127   thread __thread_;
0128 };
0129 
0130 _LIBCPP_END_NAMESPACE_STD
0131 
0132 #endif // _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_THREADS
0133 
0134 _LIBCPP_POP_MACROS
0135 
0136 #endif // _LIBCPP___THREAD_JTHREAD_H