Back to home page

EIC code displayed by LXR

 
 

    


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

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___STOP_TOKEN_STOP_SOURCE_H
0011 #define _LIBCPP___STOP_TOKEN_STOP_SOURCE_H
0012 
0013 #include <__config>
0014 #include <__stop_token/intrusive_shared_ptr.h>
0015 #include <__stop_token/stop_state.h>
0016 #include <__stop_token/stop_token.h>
0017 #include <__utility/move.h>
0018 
0019 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0020 #  pragma GCC system_header
0021 #endif
0022 
0023 _LIBCPP_BEGIN_NAMESPACE_STD
0024 
0025 #if _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_THREADS
0026 
0027 struct nostopstate_t {
0028   explicit nostopstate_t() = default;
0029 };
0030 
0031 inline constexpr nostopstate_t nostopstate{};
0032 
0033 class _LIBCPP_AVAILABILITY_SYNC stop_source {
0034 public:
0035   _LIBCPP_HIDE_FROM_ABI stop_source() : __state_(new __stop_state()) { __state_->__increment_stop_source_counter(); }
0036 
0037   _LIBCPP_HIDE_FROM_ABI explicit stop_source(nostopstate_t) noexcept : __state_(nullptr) {}
0038 
0039   _LIBCPP_HIDE_FROM_ABI stop_source(const stop_source& __other) noexcept : __state_(__other.__state_) {
0040     if (__state_) {
0041       __state_->__increment_stop_source_counter();
0042     }
0043   }
0044 
0045   _LIBCPP_HIDE_FROM_ABI stop_source(stop_source&& __other) noexcept = default;
0046 
0047   _LIBCPP_HIDE_FROM_ABI stop_source& operator=(const stop_source& __other) noexcept {
0048     // increment `__other` first so that we don't hit 0 in case of self-assignment
0049     if (__other.__state_) {
0050       __other.__state_->__increment_stop_source_counter();
0051     }
0052     if (__state_) {
0053       __state_->__decrement_stop_source_counter();
0054     }
0055     __state_ = __other.__state_;
0056     return *this;
0057   }
0058 
0059   _LIBCPP_HIDE_FROM_ABI stop_source& operator=(stop_source&&) noexcept = default;
0060 
0061   _LIBCPP_HIDE_FROM_ABI ~stop_source() {
0062     if (__state_) {
0063       __state_->__decrement_stop_source_counter();
0064     }
0065   }
0066 
0067   _LIBCPP_HIDE_FROM_ABI void swap(stop_source& __other) noexcept { __state_.swap(__other.__state_); }
0068 
0069   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_token get_token() const noexcept { return stop_token(__state_); }
0070 
0071   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool stop_possible() const noexcept { return __state_ != nullptr; }
0072 
0073   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool stop_requested() const noexcept {
0074     return __state_ != nullptr && __state_->__stop_requested();
0075   }
0076 
0077   _LIBCPP_HIDE_FROM_ABI bool request_stop() noexcept { return __state_ && __state_->__request_stop(); }
0078 
0079   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend bool operator==(const stop_source&, const stop_source&) noexcept = default;
0080 
0081   _LIBCPP_HIDE_FROM_ABI friend void swap(stop_source& __lhs, stop_source& __rhs) noexcept { __lhs.swap(__rhs); }
0082 
0083 private:
0084   __intrusive_shared_ptr<__stop_state> __state_;
0085 };
0086 
0087 #endif // _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_THREADS
0088 
0089 _LIBCPP_END_NAMESPACE_STD
0090 
0091 #endif // _LIBCPP___STOP_TOKEN_STOP_SOURCE_H