Path: blob/main/contrib/llvm-project/libcxx/include/__stop_token/stop_source.h
35236 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___STOP_TOKEN_STOP_SOURCE_H10#define _LIBCPP___STOP_TOKEN_STOP_SOURCE_H1112#include <__config>13#include <__stop_token/intrusive_shared_ptr.h>14#include <__stop_token/stop_state.h>15#include <__stop_token/stop_token.h>16#include <__utility/move.h>1718#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)19# pragma GCC system_header20#endif2122_LIBCPP_BEGIN_NAMESPACE_STD2324#if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN) && !defined(_LIBCPP_HAS_NO_THREADS)2526struct nostopstate_t {27explicit nostopstate_t() = default;28};2930inline constexpr nostopstate_t nostopstate{};3132class _LIBCPP_AVAILABILITY_SYNC stop_source {33public:34_LIBCPP_HIDE_FROM_ABI stop_source() : __state_(new __stop_state()) { __state_->__increment_stop_source_counter(); }3536_LIBCPP_HIDE_FROM_ABI explicit stop_source(nostopstate_t) noexcept : __state_(nullptr) {}3738_LIBCPP_HIDE_FROM_ABI stop_source(const stop_source& __other) noexcept : __state_(__other.__state_) {39if (__state_) {40__state_->__increment_stop_source_counter();41}42}4344_LIBCPP_HIDE_FROM_ABI stop_source(stop_source&& __other) noexcept = default;4546_LIBCPP_HIDE_FROM_ABI stop_source& operator=(const stop_source& __other) noexcept {47// increment `__other` first so that we don't hit 0 in case of self-assignment48if (__other.__state_) {49__other.__state_->__increment_stop_source_counter();50}51if (__state_) {52__state_->__decrement_stop_source_counter();53}54__state_ = __other.__state_;55return *this;56}5758_LIBCPP_HIDE_FROM_ABI stop_source& operator=(stop_source&&) noexcept = default;5960_LIBCPP_HIDE_FROM_ABI ~stop_source() {61if (__state_) {62__state_->__decrement_stop_source_counter();63}64}6566_LIBCPP_HIDE_FROM_ABI void swap(stop_source& __other) noexcept { __state_.swap(__other.__state_); }6768[[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_token get_token() const noexcept { return stop_token(__state_); }6970[[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool stop_possible() const noexcept { return __state_ != nullptr; }7172[[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool stop_requested() const noexcept {73return __state_ != nullptr && __state_->__stop_requested();74}7576_LIBCPP_HIDE_FROM_ABI bool request_stop() noexcept { return __state_ && __state_->__request_stop(); }7778[[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend bool operator==(const stop_source&, const stop_source&) noexcept = default;7980_LIBCPP_HIDE_FROM_ABI friend void swap(stop_source& __lhs, stop_source& __rhs) noexcept { __lhs.swap(__rhs); }8182private:83__intrusive_shared_ptr<__stop_state> __state_;84};8586#endif // _LIBCPP_STD_VER >= 208788_LIBCPP_END_NAMESPACE_STD8990#endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN) && !defined(_LIBCPP_HAS_NO_THREADS)919293