Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / include / __stop_token / stop_source.h
blob1080069cf3b8bea40190a3eb60afcff48f062d3a
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___STOP_TOKEN_STOP_SOURCE_H
11 #define _LIBCPP___STOP_TOKEN_STOP_SOURCE_H
13 #include <__availability>
14 #include <__config>
15 #include <__stop_token/intrusive_shared_ptr.h>
16 #include <__stop_token/stop_state.h>
17 #include <__stop_token/stop_token.h>
18 #include <__utility/move.h>
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 # pragma GCC system_header
22 #endif
24 _LIBCPP_BEGIN_NAMESPACE_STD
26 #if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN) && !defined(_LIBCPP_HAS_NO_THREADS)
28 struct nostopstate_t {
29 explicit nostopstate_t() = default;
32 inline constexpr nostopstate_t nostopstate{};
34 class _LIBCPP_AVAILABILITY_SYNC stop_source {
35 public:
36 _LIBCPP_HIDE_FROM_ABI stop_source() : __state_(new __stop_state()) { __state_->__increment_stop_source_counter(); }
38 _LIBCPP_HIDE_FROM_ABI explicit stop_source(nostopstate_t) noexcept : __state_(nullptr) {}
40 _LIBCPP_HIDE_FROM_ABI stop_source(const stop_source& __other) noexcept : __state_(__other.__state_) {
41 if (__state_) {
42 __state_->__increment_stop_source_counter();
46 _LIBCPP_HIDE_FROM_ABI stop_source(stop_source&& __other) noexcept = default;
48 _LIBCPP_HIDE_FROM_ABI stop_source& operator=(const stop_source& __other) noexcept {
49 // increment `__other` first so that we don't hit 0 in case of self-assignment
50 if (__other.__state_) {
51 __other.__state_->__increment_stop_source_counter();
53 if (__state_) {
54 __state_->__decrement_stop_source_counter();
56 __state_ = __other.__state_;
57 return *this;
60 _LIBCPP_HIDE_FROM_ABI stop_source& operator=(stop_source&&) noexcept = default;
62 _LIBCPP_HIDE_FROM_ABI ~stop_source() {
63 if (__state_) {
64 __state_->__decrement_stop_source_counter();
68 _LIBCPP_HIDE_FROM_ABI void swap(stop_source& __other) noexcept { __state_.swap(__other.__state_); }
70 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_token get_token() const noexcept { return stop_token(__state_); }
72 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool stop_possible() const noexcept { return __state_ != nullptr; }
74 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool stop_requested() const noexcept {
75 return __state_ != nullptr && __state_->__stop_requested();
78 _LIBCPP_HIDE_FROM_ABI bool request_stop() noexcept { return __state_ && __state_->__request_stop(); }
80 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend bool operator==(const stop_source&, const stop_source&) noexcept = default;
82 _LIBCPP_HIDE_FROM_ABI friend void swap(stop_source& __lhs, stop_source& __rhs) noexcept { __lhs.swap(__rhs); }
84 private:
85 __intrusive_shared_ptr<__stop_state> __state_;
88 #endif // _LIBCPP_STD_VER >= 20
90 _LIBCPP_END_NAMESPACE_STD
92 #endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN) && !defined(_LIBCPP_HAS_NO_THREADS)