|
1 | 1 | // include/beman/execution26/detail/finite_inplace_stop_source.hpp -*-C++-*- |
2 | | -// ---------------------------------------------------------------------------- |
3 | 2 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
4 | | -// ---------------------------------------------------------------------------- |
5 | 3 |
|
6 | 4 | #ifndef INCLUDED_INCLUDE_BEMAN_EXECUTION26_DETAIL_FINITE_INPLACE_STOP_SOURCE |
7 | 5 | #define INCLUDED_INCLUDE_BEMAN_EXECUTION26_DETAIL_FINITE_INPLACE_STOP_SOURCE |
8 | 6 |
|
| 7 | +#include <algorithm> |
| 8 | +#include <array> |
| 9 | +#include <atomic> |
| 10 | +#include <exception> |
| 11 | +#include <thread> |
| 12 | +#include <cstddef> |
| 13 | + |
| 14 | +// ---------------------------------------------------------------------------- |
| 15 | + |
| 16 | +namespace beman::execution26::detail { |
| 17 | + struct finite_inplace_stop_callback_base { |
| 18 | + virtual auto notify() -> void = 0; |
| 19 | + }; |
| 20 | + |
| 21 | + struct finite_inplace_stop_marker_t |
| 22 | + : ::beman::execution26::detail::finite_inplace_stop_callback_base |
| 23 | + { |
| 24 | + auto notify() -> void override {} |
| 25 | + }; |
| 26 | + inline constinit finite_inplace_stop_marker_t finite_inplace_stop_stopping{}; |
| 27 | + inline constinit finite_inplace_stop_marker_t finite_inplace_stop_stopped{}; |
| 28 | +} |
| 29 | + |
| 30 | +namespace beman::execution26 { |
| 31 | + template <::std::size_t> |
| 32 | + class finite_inplace_stop_source; |
| 33 | + template <::std::size_t N, ::std::size_t I> |
| 34 | + requires(I < N) |
| 35 | + class finite_inplace_stop_token; |
| 36 | + template <::std::size_t N, ::std::size_t I, typename> |
| 37 | + requires(I < N) |
| 38 | + class finite_inplace_stop_callback; |
| 39 | + |
| 40 | + template <::std::size_t N, ::std::size_t I, typename Callback> |
| 41 | + finite_inplace_stop_callback(finite_inplace_stop_token<N, I>, Callback) |
| 42 | + -> finite_inplace_stop_callback<N, I, Callback>; |
| 43 | +} |
| 44 | + |
| 45 | +// ---------------------------------------------------------------------------- |
| 46 | + |
| 47 | +template <::std::size_t N, ::std::size_t I> |
| 48 | + requires(I < N) |
| 49 | +class beman::execution26::finite_inplace_stop_token |
| 50 | +{ |
| 51 | +public: |
| 52 | + template <typename Callback> |
| 53 | + using callback_type = ::beman::execution26::finite_inplace_stop_callback<N, I, Callback>; |
| 54 | + |
| 55 | + constexpr finite_inplace_stop_token() noexcept = default; |
| 56 | + |
| 57 | + auto operator== (finite_inplace_stop_token const&) const noexcept -> bool = default; |
| 58 | + auto stop_possible() const noexcept -> bool { return this->source != nullptr; } |
| 59 | + auto stop_requested() const noexcept -> bool; |
| 60 | + auto swap(finite_inplace_stop_token&) noexcept -> void; |
| 61 | + |
| 62 | +private: |
| 63 | + friend class ::beman::execution26::finite_inplace_stop_source<N>; |
| 64 | + template <std::size_t NN, std::size_t II, typename> |
| 65 | + requires(II < NN) |
| 66 | + friend class ::beman::execution26::finite_inplace_stop_callback; |
| 67 | + |
| 68 | + finite_inplace_stop_token(::beman::execution26::finite_inplace_stop_source<N>* source) |
| 69 | + : source(source) |
| 70 | + { |
| 71 | + } |
| 72 | + ::beman::execution26::finite_inplace_stop_source<N>* source{}; |
| 73 | +}; |
| 74 | + |
| 75 | +// ---------------------------------------------------------------------------- |
| 76 | + |
| 77 | +template <std::size_t N> |
| 78 | +class beman::execution26::finite_inplace_stop_source |
| 79 | +{ |
| 80 | +public: |
| 81 | + constexpr finite_inplace_stop_source() noexcept; |
| 82 | + finite_inplace_stop_source(finite_inplace_stop_source&&) = delete; |
| 83 | + finite_inplace_stop_source(finite_inplace_stop_source const&) = delete; |
| 84 | + ~finite_inplace_stop_source() = default; |
| 85 | + auto operator=(finite_inplace_stop_source&&) -> finite_inplace_stop_source& = delete; |
| 86 | + auto operator=(finite_inplace_stop_source const&) -> finite_inplace_stop_source& = delete; |
| 87 | + |
| 88 | + template <std::size_t I = N == 1? 0u: ~0u> |
| 89 | + requires (I < N) |
| 90 | + constexpr auto get_token() const noexcept |
| 91 | + -> beman::execution26::finite_inplace_stop_token<N, I> |
| 92 | + { |
| 93 | + return {const_cast<finite_inplace_stop_source*>(this)}; |
| 94 | + } |
| 95 | + static constexpr auto stop_possible() noexcept -> bool { return true; } |
| 96 | + auto stop_requested() const noexcept -> bool; |
| 97 | + auto request_stop() noexcept -> bool; |
| 98 | + |
| 99 | +private: |
| 100 | + template <::std::size_t NN, std::size_t II, typename> |
| 101 | + requires(II < NN) |
| 102 | + friend class ::beman::execution26::finite_inplace_stop_callback; |
| 103 | + |
| 104 | + ::std::atomic<bool> requested{false}; |
| 105 | + ::std::atomic<::std::thread::id> id; |
| 106 | + ::std::array<::std::atomic<::beman::execution26::detail::finite_inplace_stop_callback_base*>, N> callbacks{}; |
| 107 | +}; |
| 108 | + |
| 109 | +// ---------------------------------------------------------------------------- |
| 110 | + |
| 111 | +template <std::size_t N, std::size_t I, typename Callback> |
| 112 | + requires(I < N) |
| 113 | +class beman::execution26::finite_inplace_stop_callback |
| 114 | + : beman::execution26::detail::finite_inplace_stop_callback_base |
| 115 | +{ |
| 116 | +public: |
| 117 | + template <typename Initializer> |
| 118 | + finite_inplace_stop_callback(::beman::execution26::finite_inplace_stop_token<N, I> token, Initializer&& init) |
| 119 | + : callback(init) |
| 120 | + , source(token.source) |
| 121 | + { |
| 122 | + ::beman::execution26::detail::finite_inplace_stop_callback_base* expected{}; |
| 123 | + |
| 124 | + if (this->source && not this->source->callbacks[I].compare_exchange_strong(expected, this)) { |
| 125 | + if (expected == &::beman::execution26::detail::finite_inplace_stop_stopping |
| 126 | + || expected == &::beman::execution26::detail::finite_inplace_stop_stopped) { |
| 127 | + this->source = nullptr; |
| 128 | + this->callback(); |
| 129 | + } |
| 130 | + else { |
| 131 | + ::std::terminate(); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + finite_inplace_stop_callback(finite_inplace_stop_callback&&) = delete; |
| 137 | + finite_inplace_stop_callback(finite_inplace_stop_callback const&) = delete; |
| 138 | + ~finite_inplace_stop_callback() |
| 139 | + { |
| 140 | + auto* const source{this->source}; |
| 141 | + if (source) { |
| 142 | + auto result{source->callbacks[I].exchange(nullptr)}; |
| 143 | + if (result == &::beman::execution26::detail::finite_inplace_stop_stopping |
| 144 | + && source->id != ::std::this_thread::get_id()) { |
| 145 | + source->callbacks[I].wait(&::beman::execution26::detail::finite_inplace_stop_stopping); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + auto operator=(finite_inplace_stop_callback&&) -> finite_inplace_stop_callback = delete; |
| 150 | + auto operator=(finite_inplace_stop_callback const&) -> finite_inplace_stop_callback = delete; |
| 151 | + |
| 152 | +private: |
| 153 | + auto notify() -> void override { |
| 154 | + this->callback(); |
| 155 | + } |
| 156 | + |
| 157 | + Callback callback; |
| 158 | + ::beman::execution26::finite_inplace_stop_source<N>* source; |
| 159 | +}; |
| 160 | + |
| 161 | +// ---------------------------------------------------------------------------- |
| 162 | + |
| 163 | +template <::std::size_t N, ::std::size_t I> |
| 164 | + requires(I < N) |
| 165 | +auto beman::execution26::finite_inplace_stop_token<N, I>::stop_requested() const noexcept -> bool |
| 166 | +{ |
| 167 | + return this->source && this->source->stop_requested(); |
| 168 | +} |
| 169 | + |
| 170 | +template <::std::size_t N, ::std::size_t I> |
| 171 | + requires(I < N) |
| 172 | +auto beman::execution26::finite_inplace_stop_token<N, I>::swap(finite_inplace_stop_token& other) noexcept -> void |
| 173 | +{ |
| 174 | + ::std::swap(this->source, other.source); |
| 175 | +} |
9 | 176 | // ---------------------------------------------------------------------------- |
10 | 177 |
|
11 | | -namespace nstd { |
12 | | - namespace xxx { |
| 178 | +template <::std::size_t N> |
| 179 | +constexpr beman::execution26::finite_inplace_stop_source<N>::finite_inplace_stop_source() noexcept |
| 180 | +{ |
| 181 | +} |
| 182 | + |
| 183 | +template <::std::size_t N> |
| 184 | +auto beman::execution26::finite_inplace_stop_source<N>::stop_requested() const noexcept -> bool |
| 185 | +{ |
| 186 | + return this->requested; |
| 187 | +} |
| 188 | + |
| 189 | +template <::std::size_t N> |
| 190 | +auto beman::execution26::finite_inplace_stop_source<N>::request_stop() noexcept -> bool |
| 191 | +{ |
| 192 | + if (this->requested.exchange(true)) |
| 193 | + return false; |
| 194 | + this->id = ::std::this_thread::get_id(); |
| 195 | + for (auto& cb: this->callbacks) { |
| 196 | + auto current{cb.exchange(&::beman::execution26::detail::finite_inplace_stop_stopping)}; |
| 197 | + if (current != nullptr) { |
| 198 | + current->notify(); |
| 199 | + } |
| 200 | + cb = &::beman::execution26::detail::finite_inplace_stop_stopped; |
| 201 | + cb.notify_one(); |
13 | 202 | } |
| 203 | + return true; |
14 | 204 | } |
15 | 205 |
|
16 | 206 | // ---------------------------------------------------------------------------- |
17 | 207 |
|
| 208 | + |
18 | 209 | #endif |
0 commit comments