xref: /freebsd/contrib/llvm-project/libcxx/include/__stop_token/stop_source.h (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___STOP_TOKEN_STOP_SOURCE_H
11 #define _LIBCPP___STOP_TOKEN_STOP_SOURCE_H
12 
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>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #  pragma GCC system_header
22 #endif
23 
24 _LIBCPP_BEGIN_NAMESPACE_STD
25 
26 #if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
27 
28 struct nostopstate_t {
29   explicit nostopstate_t() = default;
30 };
31 
32 inline constexpr nostopstate_t nostopstate{};
33 
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(); }
37 
38   _LIBCPP_HIDE_FROM_ABI explicit stop_source(nostopstate_t) noexcept : __state_(nullptr) {}
39 
40   _LIBCPP_HIDE_FROM_ABI stop_source(const stop_source& __other) noexcept : __state_(__other.__state_) {
41     if (__state_) {
42       __state_->__increment_stop_source_counter();
43     }
44   }
45 
46   _LIBCPP_HIDE_FROM_ABI stop_source(stop_source&& __other) noexcept = default;
47 
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();
52     }
53     if (__state_) {
54       __state_->__decrement_stop_source_counter();
55     }
56     __state_ = __other.__state_;
57     return *this;
58   }
59 
60   _LIBCPP_HIDE_FROM_ABI stop_source& operator=(stop_source&&) noexcept = default;
61 
62   _LIBCPP_HIDE_FROM_ABI ~stop_source() {
63     if (__state_) {
64       __state_->__decrement_stop_source_counter();
65     }
66   }
67 
68   _LIBCPP_HIDE_FROM_ABI void swap(stop_source& __other) noexcept { __state_.swap(__other.__state_); }
69 
70   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_token get_token() const noexcept { return stop_token(__state_); }
71 
72   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool stop_possible() const noexcept { return __state_ != nullptr; }
73 
74   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool stop_requested() const noexcept {
75     return __state_ != nullptr && __state_->__stop_requested();
76   }
77 
78   _LIBCPP_HIDE_FROM_ABI bool request_stop() noexcept { return __state_ && __state_->__request_stop(); }
79 
80   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend bool operator==(const stop_source&, const stop_source&) noexcept = default;
81 
82   _LIBCPP_HIDE_FROM_ABI friend void swap(stop_source& __lhs, stop_source& __rhs) noexcept { __lhs.swap(__rhs); }
83 
84 private:
85   __intrusive_shared_ptr<__stop_state> __state_;
86 };
87 
88 #endif // _LIBCPP_STD_VER >= 20
89 
90 _LIBCPP_END_NAMESPACE_STD
91 
92 #endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
93