106c3fb27SDimitry Andric // -*- C++ -*- 206c3fb27SDimitry Andric //===----------------------------------------------------------------------===// 306c3fb27SDimitry Andric // 406c3fb27SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 506c3fb27SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 606c3fb27SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 706c3fb27SDimitry Andric // 806c3fb27SDimitry Andric //===----------------------------------------------------------------------===// 906c3fb27SDimitry Andric 1006c3fb27SDimitry Andric #ifndef _LIBCPP___STOP_TOKEN_STOP_SOURCE_H 1106c3fb27SDimitry Andric #define _LIBCPP___STOP_TOKEN_STOP_SOURCE_H 1206c3fb27SDimitry Andric 1306c3fb27SDimitry Andric #include <__config> 1406c3fb27SDimitry Andric #include <__stop_token/intrusive_shared_ptr.h> 1506c3fb27SDimitry Andric #include <__stop_token/stop_state.h> 1606c3fb27SDimitry Andric #include <__stop_token/stop_token.h> 1706c3fb27SDimitry Andric #include <__utility/move.h> 1806c3fb27SDimitry Andric 1906c3fb27SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 2006c3fb27SDimitry Andric # pragma GCC system_header 2106c3fb27SDimitry Andric #endif 2206c3fb27SDimitry Andric 2306c3fb27SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 2406c3fb27SDimitry Andric 25*5f757f3fSDimitry Andric #if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN) && !defined(_LIBCPP_HAS_NO_THREADS) 2606c3fb27SDimitry Andric 2706c3fb27SDimitry Andric struct nostopstate_t { 2806c3fb27SDimitry Andric explicit nostopstate_t() = default; 2906c3fb27SDimitry Andric }; 3006c3fb27SDimitry Andric 3106c3fb27SDimitry Andric inline constexpr nostopstate_t nostopstate{}; 3206c3fb27SDimitry Andric 3306c3fb27SDimitry Andric class _LIBCPP_AVAILABILITY_SYNC stop_source { 3406c3fb27SDimitry Andric public: 3506c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI stop_source() : __state_(new __stop_state()) { __state_->__increment_stop_source_counter(); } 3606c3fb27SDimitry Andric 3706c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit stop_source(nostopstate_t) noexcept : __state_(nullptr) {} 3806c3fb27SDimitry Andric 3906c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI stop_source(const stop_source& __other) noexcept : __state_(__other.__state_) { 4006c3fb27SDimitry Andric if (__state_) { 4106c3fb27SDimitry Andric __state_->__increment_stop_source_counter(); 4206c3fb27SDimitry Andric } 4306c3fb27SDimitry Andric } 4406c3fb27SDimitry Andric 4506c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI stop_source(stop_source&& __other) noexcept = default; 4606c3fb27SDimitry Andric 4706c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI stop_source& operator=(const stop_source& __other) noexcept { 4806c3fb27SDimitry Andric // increment `__other` first so that we don't hit 0 in case of self-assignment 4906c3fb27SDimitry Andric if (__other.__state_) { 5006c3fb27SDimitry Andric __other.__state_->__increment_stop_source_counter(); 5106c3fb27SDimitry Andric } 5206c3fb27SDimitry Andric if (__state_) { 5306c3fb27SDimitry Andric __state_->__decrement_stop_source_counter(); 5406c3fb27SDimitry Andric } 5506c3fb27SDimitry Andric __state_ = __other.__state_; 5606c3fb27SDimitry Andric return *this; 5706c3fb27SDimitry Andric } 5806c3fb27SDimitry Andric 5906c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI stop_source& operator=(stop_source&&) noexcept = default; 6006c3fb27SDimitry Andric 6106c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI ~stop_source() { 6206c3fb27SDimitry Andric if (__state_) { 6306c3fb27SDimitry Andric __state_->__decrement_stop_source_counter(); 6406c3fb27SDimitry Andric } 6506c3fb27SDimitry Andric } 6606c3fb27SDimitry Andric 6706c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI void swap(stop_source& __other) noexcept { __state_.swap(__other.__state_); } 6806c3fb27SDimitry Andric 6906c3fb27SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_token get_token() const noexcept { return stop_token(__state_); } 7006c3fb27SDimitry Andric 7106c3fb27SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool stop_possible() const noexcept { return __state_ != nullptr; } 7206c3fb27SDimitry Andric 7306c3fb27SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool stop_requested() const noexcept { 7406c3fb27SDimitry Andric return __state_ != nullptr && __state_->__stop_requested(); 7506c3fb27SDimitry Andric } 7606c3fb27SDimitry Andric 7706c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI bool request_stop() noexcept { return __state_ && __state_->__request_stop(); } 7806c3fb27SDimitry Andric 7906c3fb27SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend bool operator==(const stop_source&, const stop_source&) noexcept = default; 8006c3fb27SDimitry Andric 8106c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI friend void swap(stop_source& __lhs, stop_source& __rhs) noexcept { __lhs.swap(__rhs); } 8206c3fb27SDimitry Andric 8306c3fb27SDimitry Andric private: 8406c3fb27SDimitry Andric __intrusive_shared_ptr<__stop_state> __state_; 8506c3fb27SDimitry Andric }; 8606c3fb27SDimitry Andric 8706c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20 8806c3fb27SDimitry Andric 8906c3fb27SDimitry Andric _LIBCPP_END_NAMESPACE_STD 9006c3fb27SDimitry Andric 91*5f757f3fSDimitry Andric #endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN) && !defined(_LIBCPP_HAS_NO_THREADS) 92