1b77e50e6SHui // -*- C++ -*- 2b77e50e6SHui //===----------------------------------------------------------------------===// 3b77e50e6SHui // 4b77e50e6SHui // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5b77e50e6SHui // See https://llvm.org/LICENSE.txt for license information. 6b77e50e6SHui // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7b77e50e6SHui // 8b77e50e6SHui //===----------------------------------------------------------------------===// 9b77e50e6SHui 10b77e50e6SHui #ifndef _LIBCPP___STOP_TOKEN_STOP_SOURCE_H 11b77e50e6SHui #define _LIBCPP___STOP_TOKEN_STOP_SOURCE_H 12b77e50e6SHui 13b77e50e6SHui #include <__config> 14b77e50e6SHui #include <__stop_token/intrusive_shared_ptr.h> 15b77e50e6SHui #include <__stop_token/stop_state.h> 16b77e50e6SHui #include <__stop_token/stop_token.h> 17b77e50e6SHui #include <__utility/move.h> 18b77e50e6SHui 19b77e50e6SHui #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 20b77e50e6SHui # pragma GCC system_header 21b77e50e6SHui #endif 22b77e50e6SHui 23b77e50e6SHui _LIBCPP_BEGIN_NAMESPACE_STD 24b77e50e6SHui 25*c6f3b7bcSNikolas Klauser #if _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_THREADS 26b77e50e6SHui 27b77e50e6SHui struct nostopstate_t { 28b77e50e6SHui explicit nostopstate_t() = default; 29b77e50e6SHui }; 30b77e50e6SHui 31b77e50e6SHui inline constexpr nostopstate_t nostopstate{}; 32b77e50e6SHui 33b77e50e6SHui class _LIBCPP_AVAILABILITY_SYNC stop_source { 34b77e50e6SHui public: 35b77e50e6SHui _LIBCPP_HIDE_FROM_ABI stop_source() : __state_(new __stop_state()) { __state_->__increment_stop_source_counter(); } 36b77e50e6SHui 37b77e50e6SHui _LIBCPP_HIDE_FROM_ABI explicit stop_source(nostopstate_t) noexcept : __state_(nullptr) {} 38b77e50e6SHui 39b77e50e6SHui _LIBCPP_HIDE_FROM_ABI stop_source(const stop_source& __other) noexcept : __state_(__other.__state_) { 40b77e50e6SHui if (__state_) { 41b77e50e6SHui __state_->__increment_stop_source_counter(); 42b77e50e6SHui } 43b77e50e6SHui } 44b77e50e6SHui 45b77e50e6SHui _LIBCPP_HIDE_FROM_ABI stop_source(stop_source&& __other) noexcept = default; 46b77e50e6SHui 47b77e50e6SHui _LIBCPP_HIDE_FROM_ABI stop_source& operator=(const stop_source& __other) noexcept { 48b77e50e6SHui // increment `__other` first so that we don't hit 0 in case of self-assignment 49b77e50e6SHui if (__other.__state_) { 50b77e50e6SHui __other.__state_->__increment_stop_source_counter(); 51b77e50e6SHui } 52b77e50e6SHui if (__state_) { 53b77e50e6SHui __state_->__decrement_stop_source_counter(); 54b77e50e6SHui } 55b77e50e6SHui __state_ = __other.__state_; 56b77e50e6SHui return *this; 57b77e50e6SHui } 58b77e50e6SHui 59b77e50e6SHui _LIBCPP_HIDE_FROM_ABI stop_source& operator=(stop_source&&) noexcept = default; 60b77e50e6SHui 61b77e50e6SHui _LIBCPP_HIDE_FROM_ABI ~stop_source() { 62b77e50e6SHui if (__state_) { 63b77e50e6SHui __state_->__decrement_stop_source_counter(); 64b77e50e6SHui } 65b77e50e6SHui } 66b77e50e6SHui 67b77e50e6SHui _LIBCPP_HIDE_FROM_ABI void swap(stop_source& __other) noexcept { __state_.swap(__other.__state_); } 68b77e50e6SHui 69b77e50e6SHui [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_token get_token() const noexcept { return stop_token(__state_); } 70b77e50e6SHui 71b77e50e6SHui [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool stop_possible() const noexcept { return __state_ != nullptr; } 72b77e50e6SHui 73b77e50e6SHui [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool stop_requested() const noexcept { 74b77e50e6SHui return __state_ != nullptr && __state_->__stop_requested(); 75b77e50e6SHui } 76b77e50e6SHui 77b77e50e6SHui _LIBCPP_HIDE_FROM_ABI bool request_stop() noexcept { return __state_ && __state_->__request_stop(); } 78b77e50e6SHui 79b77e50e6SHui [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend bool operator==(const stop_source&, const stop_source&) noexcept = default; 80b77e50e6SHui 81b77e50e6SHui _LIBCPP_HIDE_FROM_ABI friend void swap(stop_source& __lhs, stop_source& __rhs) noexcept { __lhs.swap(__rhs); } 82b77e50e6SHui 83b77e50e6SHui private: 84b77e50e6SHui __intrusive_shared_ptr<__stop_state> __state_; 85b77e50e6SHui }; 86b77e50e6SHui 87*c6f3b7bcSNikolas Klauser #endif // _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_THREADS 88b77e50e6SHui 89b77e50e6SHui _LIBCPP_END_NAMESPACE_STD 90b77e50e6SHui 9199174842SLouis Dionne #endif // _LIBCPP___STOP_TOKEN_STOP_SOURCE_H 92