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 <__config> |
14 | #include <__stop_token/intrusive_shared_ptr.h> |
15 | #include <__stop_token/stop_state.h> |
16 | #include <__stop_token/stop_token.h> |
17 | #include <__utility/move.h> |
18 | |
19 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
20 | # pragma GCC system_header |
21 | #endif |
22 | |
23 | _LIBCPP_BEGIN_NAMESPACE_STD |
24 | |
25 | #if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN) && !defined(_LIBCPP_HAS_NO_THREADS) |
26 | |
27 | struct nostopstate_t { |
28 | explicit nostopstate_t() = default; |
29 | }; |
30 | |
31 | inline constexpr nostopstate_t nostopstate{}; |
32 | |
33 | class _LIBCPP_AVAILABILITY_SYNC stop_source { |
34 | public: |
35 | _LIBCPP_HIDE_FROM_ABI stop_source() : __state_(new __stop_state()) { __state_->__increment_stop_source_counter(); } |
36 | |
37 | _LIBCPP_HIDE_FROM_ABI explicit stop_source(nostopstate_t) noexcept : __state_(nullptr) {} |
38 | |
39 | _LIBCPP_HIDE_FROM_ABI stop_source(const stop_source& __other) noexcept : __state_(__other.__state_) { |
40 | if (__state_) { |
41 | __state_->__increment_stop_source_counter(); |
42 | } |
43 | } |
44 | |
45 | _LIBCPP_HIDE_FROM_ABI stop_source(stop_source&& __other) noexcept = default; |
46 | |
47 | _LIBCPP_HIDE_FROM_ABI stop_source& operator=(const stop_source& __other) noexcept { |
48 | // increment `__other` first so that we don't hit 0 in case of self-assignment |
49 | if (__other.__state_) { |
50 | __other.__state_->__increment_stop_source_counter(); |
51 | } |
52 | if (__state_) { |
53 | __state_->__decrement_stop_source_counter(); |
54 | } |
55 | __state_ = __other.__state_; |
56 | return *this; |
57 | } |
58 | |
59 | _LIBCPP_HIDE_FROM_ABI stop_source& operator=(stop_source&&) noexcept = default; |
60 | |
61 | _LIBCPP_HIDE_FROM_ABI ~stop_source() { |
62 | if (__state_) { |
63 | __state_->__decrement_stop_source_counter(); |
64 | } |
65 | } |
66 | |
67 | _LIBCPP_HIDE_FROM_ABI void swap(stop_source& __other) noexcept { __state_.swap(__other.__state_); } |
68 | |
69 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_token get_token() const noexcept { return stop_token(__state_); } |
70 | |
71 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool stop_possible() const noexcept { return __state_ != nullptr; } |
72 | |
73 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool stop_requested() const noexcept { |
74 | return __state_ != nullptr && __state_->__stop_requested(); |
75 | } |
76 | |
77 | _LIBCPP_HIDE_FROM_ABI bool request_stop() noexcept { return __state_ && __state_->__request_stop(); } |
78 | |
79 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend bool operator==(const stop_source&, const stop_source&) noexcept = default; |
80 | |
81 | _LIBCPP_HIDE_FROM_ABI friend void swap(stop_source& __lhs, stop_source& __rhs) noexcept { __lhs.swap(__rhs); } |
82 | |
83 | private: |
84 | __intrusive_shared_ptr<__stop_state> __state_; |
85 | }; |
86 | |
87 | #endif // _LIBCPP_STD_VER >= 20 |
88 | |
89 | _LIBCPP_END_NAMESPACE_STD |
90 | |
91 | #endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN) && !defined(_LIBCPP_HAS_NO_THREADS) |
92 | |