100.00% Lines (84/84)
100.00% Functions (15/15)
| TLA | Baseline | Branch | ||||||
|---|---|---|---|---|---|---|---|---|
| Line | Hits | Code | Line | Hits | Code | |||
| 1 | // | 1 | // | |||||
| 2 | // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) | 2 | // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) | |||||
| 3 | // Copyright (c) 2026 Michael Vandeberg | 3 | // Copyright (c) 2026 Michael Vandeberg | |||||
| 4 | // | 4 | // | |||||
| 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |||||
| 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||||
| 7 | // | 7 | // | |||||
| 8 | // Official repository: https://github.com/cppalliance/capy | 8 | // Official repository: https://github.com/cppalliance/capy | |||||
| 9 | // | 9 | // | |||||
| 10 | 10 | |||||||
| 11 | #ifndef BOOST_CAPY_ASYNC_EVENT_HPP | 11 | #ifndef BOOST_CAPY_ASYNC_EVENT_HPP | |||||
| 12 | #define BOOST_CAPY_ASYNC_EVENT_HPP | 12 | #define BOOST_CAPY_ASYNC_EVENT_HPP | |||||
| 13 | 13 | |||||||
| 14 | #include <boost/capy/detail/config.hpp> | 14 | #include <boost/capy/detail/config.hpp> | |||||
| 15 | #include <boost/capy/detail/intrusive.hpp> | 15 | #include <boost/capy/detail/intrusive.hpp> | |||||
| 16 | #include <boost/capy/continuation.hpp> | 16 | #include <boost/capy/continuation.hpp> | |||||
| 17 | #include <boost/capy/concept/executor.hpp> | 17 | #include <boost/capy/concept/executor.hpp> | |||||
| 18 | #include <boost/capy/error.hpp> | 18 | #include <boost/capy/error.hpp> | |||||
| 19 | #include <boost/capy/ex/await_context.hpp> | 19 | #include <boost/capy/ex/await_context.hpp> | |||||
| 20 | #include <boost/capy/results.hpp> | 20 | #include <boost/capy/results.hpp> | |||||
| 21 | 21 | |||||||
| 22 | #include <stop_token> | 22 | #include <stop_token> | |||||
| 23 | 23 | |||||||
| 24 | #include <atomic> | 24 | #include <atomic> | |||||
| 25 | #include <coroutine> | 25 | #include <coroutine> | |||||
| 26 | #include <new> | 26 | #include <new> | |||||
| 27 | #include <utility> | 27 | #include <utility> | |||||
| 28 | 28 | |||||||
| 29 | /* async_event implementation notes | 29 | /* async_event implementation notes | |||||
| 30 | ================================= | 30 | ================================= | |||||
| 31 | 31 | |||||||
| 32 | Same cancellation pattern as async_mutex (see that file for the | 32 | Same cancellation pattern as async_mutex (see that file for the | |||||
| 33 | full discussion on claimed_, stop_cb lifetime, member ordering, | 33 | full discussion on claimed_, stop_cb lifetime, member ordering, | |||||
| 34 | and threading assumptions). | 34 | and threading assumptions). | |||||
| 35 | 35 | |||||||
| 36 | Key difference: set() wakes ALL waiters (broadcast), not one. | 36 | Key difference: set() wakes ALL waiters (broadcast), not one. | |||||
| 37 | It pops every waiter from the list and posts the ones it | 37 | It pops every waiter from the list and posts the ones it | |||||
| 38 | claims. Waiters already claimed by a stop callback are skipped. | 38 | claims. Waiters already claimed by a stop callback are skipped. | |||||
| 39 | 39 | |||||||
| 40 | Because set() pops all waiters, a canceled waiter may have been | 40 | Because set() pops all waiters, a canceled waiter may have been | |||||
| 41 | removed from the list by set() before its await_resume runs. Each | 41 | removed from the list by set() before its await_resume runs. Each | |||||
| 42 | waiter therefore records the list it is linked into, if any: the | 42 | waiter therefore records the list it is linked into, if any: the | |||||
| 43 | event's, or set()'s own while set() posts the waiters it claimed. | 43 | event's, or set()'s own while set() posts the waiters it claimed. | |||||
| 44 | A waiter destroyed or canceled while linked unlinks itself from | 44 | A waiter destroyed or canceled while linked unlinks itself from | |||||
| 45 | that list, so a post that tears down one claimed waiter, and with | 45 | that list, so a post that tears down one claimed waiter, and with | |||||
| 46 | it another, never leaves set() holding a freed node. | 46 | it another, never leaves set() holding a freed node. | |||||
| 47 | + | |||||||
| 48 | + | Only the post is subject to the claim race. A losing stop | ||||||
| 49 | + | callback does nothing, but set() unlinks every waiter it pops | ||||||
| 50 | + | whether it claims it or not. That is why await_resume unlinks a | ||||||
| 51 | + | canceled waiter only when it is still linked. | ||||||
| 47 | */ | 52 | */ | |||||
| 48 | 53 | |||||||
| 49 | namespace boost { | 54 | namespace boost { | |||||
| 50 | namespace capy { | 55 | namespace capy { | |||||
| 51 | 56 | |||||||
| 52 | /** Queues coroutines in `wait()` and resumes all of them when `set()` is called. | 57 | /** Queues coroutines in `wait()` and resumes all of them when `set()` is called. | |||||
| 53 | 58 | |||||||
| 54 | This event provides a way to notify multiple coroutines that some | 59 | This event provides a way to notify multiple coroutines that some | |||||
| 55 | condition has occurred. When a coroutine awaits an unset event, it | 60 | condition has occurred. When a coroutine awaits an unset event, it | |||||
| 56 | suspends and is added to a wait queue. When the event is set, all | 61 | suspends and is added to a wait queue. When the event is set, all | |||||
| 57 | waiting coroutines are resumed. | 62 | waiting coroutines are resumed. | |||||
| 58 | 63 | |||||||
| 59 | @par Cancellation | 64 | @par Cancellation | |||||
| 60 | 65 | |||||||
| 61 | When a coroutine is suspended waiting for the event and its stop | 66 | When a coroutine is suspended waiting for the event and its stop | |||||
| 62 | token is triggered, the waiter completes with `error::canceled` | 67 | token is triggered, the waiter completes with `error::canceled` | |||||
| 63 | instead of waiting for `set()`. | 68 | instead of waiting for `set()`. | |||||
| 64 | 69 | |||||||
| 65 | - | Cancellation only applies while the coroutine is suspended in the | 70 | + | If the event is already set, `wait()` completes successfully | |||
| 66 | - | wait queue. If the event is already set when `wait()` is called, | 71 | + | without consulting the stop token. Otherwise a pending stop request | |||
| 67 | - | the wait completes immediately even if the stop token is already | 72 | + | completes the wait with `error::canceled`. When a stop request races | |||
| 68 | - | signaled. | 73 | + | with `set()`, whichever reaches the waiter first decides the result. | |||
| 69 | 74 | |||||||
| 70 | @par Zero Allocation | 75 | @par Zero Allocation | |||||
| 71 | 76 | |||||||
| 72 | No heap allocation occurs for wait operations. | 77 | No heap allocation occurs for wait operations. | |||||
| 73 | 78 | |||||||
| 74 | @par Thread Safety | 79 | @par Thread Safety | |||||
| 75 | 80 | |||||||
| 76 | Distinct objects: Safe.@n | 81 | Distinct objects: Safe.@n | |||||
| 77 | Shared objects: Unsafe. | 82 | Shared objects: Unsafe. | |||||
| 78 | 83 | |||||||
| 79 | The event operations are designed for single-threaded use on one | 84 | The event operations are designed for single-threaded use on one | |||||
| 80 | executor. The stop callback may fire from any thread. | 85 | executor. The stop callback may fire from any thread. | |||||
| 81 | 86 | |||||||
| 82 | This type is non-copyable and non-movable because suspended | 87 | This type is non-copyable and non-movable because suspended | |||||
| 83 | waiters hold intrusive pointers into the event's internal list. | 88 | waiters hold intrusive pointers into the event's internal list. | |||||
| 84 | 89 | |||||||
| 85 | @par Example | 90 | @par Example | |||||
| 86 | @par !example example | 91 | @par !example example | |||||
| 87 | 92 | |||||||
| 88 | */ | 93 | */ | |||||
| 89 | class async_event | 94 | class async_event | |||||
| 90 | { | 95 | { | |||||
| 91 | public: | 96 | public: | |||||
| 92 | class wait_awaiter; | 97 | class wait_awaiter; | |||||
| 93 | 98 | |||||||
| 94 | private: | 99 | private: | |||||
| 95 | bool set_ = false; | 100 | bool set_ = false; | |||||
| 96 | detail::intrusive_list<wait_awaiter> waiters_; | 101 | detail::intrusive_list<wait_awaiter> waiters_; | |||||
| 97 | 102 | |||||||
| 98 | // Unlink every waiter and move the ones this call claims into into. | 103 | // Unlink every waiter and move the ones this call claims into into. | |||||
| 99 | void claim_all(detail::intrusive_list<wait_awaiter>& into) noexcept; | 104 | void claim_all(detail::intrusive_list<wait_awaiter>& into) noexcept; | |||||
| 100 | 105 | |||||||
| 101 | public: | 106 | public: | |||||
| 102 | /** Suspends the caller until `set()` runs, or resumes it with `error::canceled` on a stop request. | 107 | /** Suspends the caller until `set()` runs, or resumes it with `error::canceled` on a stop request. | |||||
| 103 | */ | 108 | */ | |||||
| 104 | class wait_awaiter | 109 | class wait_awaiter | |||||
| 105 | : public detail::intrusive_list<wait_awaiter>::node | 110 | : public detail::intrusive_list<wait_awaiter>::node | |||||
| 106 | { | 111 | { | |||||
| 107 | friend class async_event; | 112 | friend class async_event; | |||||
| 108 | 113 | |||||||
| 109 | async_event* e_; | 114 | async_event* e_; | |||||
| 110 | continuation cont_; | 115 | continuation cont_; | |||||
| 111 | executor_ref ex_; | 116 | executor_ref ex_; | |||||
| 112 | 117 | |||||||
| 113 | // Declared before stop_cb_buf_: the callback | 118 | // Declared before stop_cb_buf_: the callback | |||||
| 114 | // accesses these members, so they must still be | 119 | // accesses these members, so they must still be | |||||
| 115 | // alive if the stop_cb_ destructor blocks. | 120 | // alive if the stop_cb_ destructor blocks. | |||||
| 116 | std::atomic<bool> claimed_{false}; | 121 | std::atomic<bool> claimed_{false}; | |||||
| 117 | bool canceled_ = false; | 122 | bool canceled_ = false; | |||||
| 118 | bool active_ = false; | 123 | bool active_ = false; | |||||
| 119 | detail::intrusive_list<wait_awaiter>* list_ = nullptr; | 124 | detail::intrusive_list<wait_awaiter>* list_ = nullptr; | |||||
| 120 | 125 | |||||||
| 121 | struct cancel_fn | 126 | struct cancel_fn | |||||
| 122 | { | 127 | { | |||||
| 123 | wait_awaiter* self_; | 128 | wait_awaiter* self_; | |||||
| 124 | 129 | |||||||
| HITCBC | 125 | 9 | void operator()() const noexcept | 130 | 9 | void operator()() const noexcept | ||
| 126 | { | 131 | { | |||||
| HITCBC | 127 | 9 | if(!self_->claimed_.exchange( | 132 | 9 | if(!self_->claimed_.exchange( | ||
| 128 | true, std::memory_order_acq_rel)) | 133 | true, std::memory_order_acq_rel)) | |||||
| 129 | { | 134 | { | |||||
| HITCBC | 130 | 8 | self_->canceled_ = true; | 135 | 8 | self_->canceled_ = true; | ||
| HITCBC | 131 | 8 | self_->ex_.post(self_->cont_); | 136 | 8 | self_->ex_.post(self_->cont_); | ||
| 132 | } | 137 | } | |||||
| HITCBC | 133 | 9 | } | 138 | 9 | } | ||
| 134 | }; | 139 | }; | |||||
| 135 | 140 | |||||||
| 136 | using stop_cb_t = | 141 | using stop_cb_t = | |||||
| 137 | std::stop_callback<cancel_fn>; | 142 | std::stop_callback<cancel_fn>; | |||||
| 138 | 143 | |||||||
| 139 | // Aligned storage for stop_cb_t. Declared last: | 144 | // Aligned storage for stop_cb_t. Declared last: | |||||
| 140 | // its destructor may block while the callback | 145 | // its destructor may block while the callback | |||||
| 141 | // accesses the members above. | 146 | // accesses the members above. | |||||
| 142 | BOOST_CAPY_MSVC_WARNING_PUSH | 147 | BOOST_CAPY_MSVC_WARNING_PUSH | |||||
| 143 | BOOST_CAPY_MSVC_WARNING_DISABLE(4324) // padded due to alignas | 148 | BOOST_CAPY_MSVC_WARNING_DISABLE(4324) // padded due to alignas | |||||
| 144 | alignas(stop_cb_t) | 149 | alignas(stop_cb_t) | |||||
| 145 | unsigned char stop_cb_buf_[sizeof(stop_cb_t)]; | 150 | unsigned char stop_cb_buf_[sizeof(stop_cb_t)]; | |||||
| 146 | BOOST_CAPY_MSVC_WARNING_POP | 151 | BOOST_CAPY_MSVC_WARNING_POP | |||||
| 147 | 152 | |||||||
| HITCBC | 148 | 93 | stop_cb_t& stop_cb_() noexcept | 153 | 93 | stop_cb_t& stop_cb_() noexcept | ||
| 149 | { | 154 | { | |||||
| 150 | return *reinterpret_cast<stop_cb_t*>( | 155 | return *reinterpret_cast<stop_cb_t*>( | |||||
| HITCBC | 151 | 93 | stop_cb_buf_); | 156 | 93 | stop_cb_buf_); | ||
| 152 | } | 157 | } | |||||
| 153 | 158 | |||||||
| 154 | public: | 159 | public: | |||||
| 155 | /** Destroy the awaiter, leaving the event unable to reach it. | 160 | /** Destroy the awaiter, leaving the event unable to reach it. | |||||
| 156 | 161 | |||||||
| 157 | Destroys the stop callback if one is registered, and unlinks | 162 | Destroys the stop callback if one is registered, and unlinks | |||||
| 158 | the awaiter from the event's wait queue if it is still linked. | 163 | the awaiter from the event's wait queue if it is still linked. | |||||
| 159 | Both are necessary when the coroutine frame is torn down while | 164 | Both are necessary when the coroutine frame is torn down while | |||||
| 160 | suspended, so that neither `set()` nor the stop callback can | 165 | suspended, so that neither `set()` nor the stop callback can | |||||
| 161 | reach a destroyed awaiter. | 166 | reach a destroyed awaiter. | |||||
| 162 | */ | 167 | */ | |||||
| HITCBC | 163 | 200 | ~wait_awaiter() | 168 | 200 | ~wait_awaiter() | ||
| 164 | { | 169 | { | |||||
| HITCBC | 165 | 200 | if(active_) | 170 | 200 | if(active_) | ||
| HITCBC | 166 | 41 | stop_cb_().~stop_cb_t(); | 171 | 41 | stop_cb_().~stop_cb_t(); | ||
| HITCBC | 167 | 200 | if(list_) | 172 | 200 | if(list_) | ||
| HITCBC | 168 | 5 | list_->remove(this); | 173 | 5 | list_->remove(this); | ||
| HITCBC | 169 | 200 | } | 174 | 200 | } | ||
| 170 | 175 | |||||||
| 171 | /** Construct an awaiter for the given event. | 176 | /** Construct an awaiter for the given event. | |||||
| 172 | 177 | |||||||
| 173 | @param e The event to wait on. It must outlive the awaiter. | 178 | @param e The event to wait on. It must outlive the awaiter. | |||||
| 174 | */ | 179 | */ | |||||
| HITCBC | 175 | 100 | explicit wait_awaiter(async_event* e) noexcept | 180 | 100 | explicit wait_awaiter(async_event* e) noexcept | ||
| HITCBC | 176 | 100 | : e_(e) | 181 | 100 | : e_(e) | ||
| 177 | { | 182 | { | |||||
| HITCBC | 178 | 100 | } | 183 | 100 | } | ||
| 179 | 184 | |||||||
| 180 | /** Construct by moving. | 185 | /** Construct by moving. | |||||
| 181 | 186 | |||||||
| 182 | The moved-from awaiter is left inert: its destructor no longer | 187 | The moved-from awaiter is left inert: its destructor no longer | |||||
| 183 | destroys the stop callback and no longer unlinks from the | 188 | destroys the stop callback and no longer unlinks from the | |||||
| 184 | event's wait queue. | 189 | event's wait queue. | |||||
| 185 | 190 | |||||||
| 191 | + | @pre The source has not been suspended (its `await_suspend` | ||||||
| 192 | + | has not enqueued it). | ||||||
| 193 | + | |||||||
| 186 | @param o The awaiter to move from. | 194 | @param o The awaiter to move from. | |||||
| 187 | */ | 195 | */ | |||||
| HITCBC | 188 | 100 | wait_awaiter(wait_awaiter&& o) noexcept | 196 | 100 | wait_awaiter(wait_awaiter&& o) noexcept | ||
| HITCBC | 189 | 200 | : e_(o.e_) | 197 | 200 | : e_(o.e_) | ||
| HITCBC | 190 | 100 | , cont_(o.cont_) | 198 | 100 | , cont_(o.cont_) | ||
| HITCBC | 191 | 100 | , ex_(o.ex_) | 199 | 100 | , ex_(o.ex_) | ||
| HITCBC | 192 | 100 | , claimed_(o.claimed_.load( | 200 | 100 | , claimed_(o.claimed_.load( | ||
| 193 | std::memory_order_relaxed)) | 201 | std::memory_order_relaxed)) | |||||
| HITCBC | 194 | 100 | , canceled_(o.canceled_) | 202 | 100 | , canceled_(o.canceled_) | ||
| HITCBC | 195 | 100 | , active_(std::exchange(o.active_, false)) | 203 | 100 | , active_(std::exchange(o.active_, false)) | ||
| HITCBC | 196 | 200 | , list_(std::exchange(o.list_, nullptr)) | 204 | 200 | , list_(std::exchange(o.list_, nullptr)) | ||
| 197 | { | 205 | { | |||||
| HITCBC | 198 | 100 | } | 206 | 100 | } | ||
| 199 | 207 | |||||||
| 200 | - | /** Copy construction is disabled; a waiter is linked into the | 208 | + | /** Deleted: a waiter is linked into the event's queue by address. | |||
| 201 | - | event's wait queue by address. | ||||||
| 202 | 209 | |||||||
| 203 | @param other The awaiter that would be copied. | 210 | @param other The awaiter that would be copied. | |||||
| 204 | */ | 211 | */ | |||||
| 205 | wait_awaiter(wait_awaiter const& other) = delete; | 212 | wait_awaiter(wait_awaiter const& other) = delete; | |||||
| 206 | 213 | |||||||
| 207 | - | /** Copy assignment is disabled; a waiter is linked into the | 214 | + | /** Deleted: a waiter is linked into the event's queue by address. | |||
| 208 | - | event's wait queue by address. | ||||||
| 209 | 215 | |||||||
| 210 | @param other The awaiter that would be assigned from. | 216 | @param other The awaiter that would be assigned from. | |||||
| 211 | 217 | |||||||
| 212 | @return A reference to `*this`. | 218 | @return A reference to `*this`. | |||||
| 213 | */ | 219 | */ | |||||
| 214 | wait_awaiter& operator=(wait_awaiter const& other) = delete; | 220 | wait_awaiter& operator=(wait_awaiter const& other) = delete; | |||||
| 215 | 221 | |||||||
| 216 | - | /** Move assignment is disabled; a waiter is linked into the | 222 | + | /** Deleted: a waiter is linked into the event's queue by address. | |||
| 217 | - | event's wait queue by address. | ||||||
| 218 | 223 | |||||||
| 219 | @param other The awaiter that would be moved from. | 224 | @param other The awaiter that would be moved from. | |||||
| 220 | 225 | |||||||
| 221 | @return A reference to `*this`. | 226 | @return A reference to `*this`. | |||||
| 222 | */ | 227 | */ | |||||
| 223 | wait_awaiter& operator=(wait_awaiter&& other) = delete; | 228 | wait_awaiter& operator=(wait_awaiter&& other) = delete; | |||||
| 224 | 229 | |||||||
| 225 | - | /** Report whether the event is already set. | 230 | + | /** Check whether the event is already set. | |||
| 226 | 231 | |||||||
| 227 | - | @return `true` if the event is set, in which case the awaiting | 232 | + | If the event is set, the awaiting coroutine does not suspend. | |||
| 228 | - | coroutine does not suspend; otherwise `false`. | 233 | + | ||||
| 234 | + | @return `true` if the event is set; otherwise `false`. | ||||||
| 229 | */ | 235 | */ | |||||
| HITCBC | 230 | 100 | bool await_ready() const noexcept | 236 | 100 | bool await_ready() const noexcept | ||
| 231 | { | 237 | { | |||||
| HITCBC | 232 | 100 | return e_->set_; | 238 | 100 | return e_->set_; | ||
| 233 | } | 239 | } | |||||
| 234 | 240 | |||||||
| 235 | /** Enqueue the awaiting coroutine until the event is set. | 241 | /** Enqueue the awaiting coroutine until the event is set. | |||||
| 236 | 242 | |||||||
| 237 | This is the @ref ContextAwaitable overload of `await_suspend`. | 243 | This is the @ref ContextAwaitable overload of `await_suspend`. | |||||
| 238 | 244 | |||||||
| 239 | If a stop request is already pending on `env->stop_token`, the | 245 | If a stop request is already pending on `env->stop_token`, the | |||||
| 240 | awaiter records the cancellation and does not enqueue. | 246 | awaiter records the cancellation and does not enqueue. | |||||
| 241 | 247 | |||||||
| 242 | Otherwise it stores `h` and `env->executor`, links itself into | 248 | Otherwise it stores `h` and `env->executor`, links itself into | |||||
| 243 | the event's wait queue, and registers a stop callback on | 249 | the event's wait queue, and registers a stop callback on | |||||
| 244 | - | `env->stop_token`. Exactly one of `set()` and that callback posts | 250 | + | `env->stop_token`. Exactly one of `set()`, that callback, and | |||
| 245 | - | `h` through the stored executor, whichever claims the waiter | 251 | + | the event's destructor posts `h` through the stored executor. | |||
| 246 | - | first. Only the post is subject to that race. A losing stop | 252 | + | Whichever claims the waiter first does so. | |||
| 247 | - | callback does nothing at all, but `set()` unlinks every waiter it | ||||||
| 248 | - | pops whether it claims it or not. That is why @ref await_resume | ||||||
| 249 | - | unlinks a canceled waiter only when it is still linked. | ||||||
| 250 | 253 | |||||||
| 251 | @param h The awaiting coroutine, resumed when the event is set | 254 | @param h The awaiting coroutine, resumed when the event is set | |||||
| 252 | or the wait is canceled. | 255 | or the wait is canceled. | |||||
| 253 | 256 | |||||||
| 254 | @param env The execution environment. Its executor posts the | 257 | @param env The execution environment. Its executor posts the | |||||
| 255 | resumption and its stop token is watched for the duration of | 258 | resumption and its stop token is watched for the duration of | |||||
| 256 | the wait. It must outlive the wait. | 259 | the wait. It must outlive the wait. | |||||
| 257 | 260 | |||||||
| 258 | - | @return `h` if a stop request was already pending, which | 261 | + | A returned `h` resumes the awaiting coroutine at once without | |||
| 259 | - | resumes the awaiting coroutine immediately without enqueuing | 262 | + | enqueuing it. A returned `std::noop_coroutine()` leaves the | |||
| 260 | - | it. Otherwise `std::noop_coroutine()`, which leaves the | ||||||
| 261 | coroutine suspended and returns control to the resumer. | 263 | coroutine suspended and returns control to the resumer. | |||||
| 264 | + | |||||||
| 265 | + | @return `h` if stop was already requested, else `noop_coroutine()`. | ||||||
| 262 | */ | 266 | */ | |||||
| 263 | std::coroutine_handle<> | 267 | std::coroutine_handle<> | |||||
| HITCBC | 264 | 95 | await_suspend( | 268 | 95 | await_suspend( | ||
| 265 | std::coroutine_handle<> h, | 269 | std::coroutine_handle<> h, | |||||
| 266 | await_context const* env) noexcept | 270 | await_context const* env) noexcept | |||||
| 267 | { | 271 | { | |||||
| HITCBC | 268 | 95 | if(env->stop_token.stop_requested()) | 272 | 95 | if(env->stop_token.stop_requested()) | ||
| 269 | { | 273 | { | |||||
| HITCBC | 270 | 2 | canceled_ = true; | 274 | 2 | canceled_ = true; | ||
| HITCBC | 271 | 2 | return h; | 275 | 2 | return h; | ||
| 272 | } | 276 | } | |||||
| HITCBC | 273 | 93 | cont_.h = h; | 277 | 93 | cont_.h = h; | ||
| HITCBC | 274 | 93 | cont_.owner = env->owner; | 278 | 93 | cont_.owner = env->owner; | ||
| HITCBC | 275 | 93 | ex_ = env->executor; | 279 | 93 | ex_ = env->executor; | ||
| HITCBC | 276 | 93 | e_->waiters_.push_back(this); | 280 | 93 | e_->waiters_.push_back(this); | ||
| HITCBC | 277 | 93 | list_ = &e_->waiters_; | 281 | 93 | list_ = &e_->waiters_; | ||
| HITCBC | 278 | 279 | ::new(stop_cb_buf_) stop_cb_t( | 282 | 279 | ::new(stop_cb_buf_) stop_cb_t( | ||
| HITCBC | 279 | 93 | env->stop_token, cancel_fn{this}); | 283 | 93 | env->stop_token, cancel_fn{this}); | ||
| HITCBC | 280 | 93 | active_ = true; | 284 | 93 | active_ = true; | ||
| HITCBC | 281 | 93 | return std::noop_coroutine(); | 285 | 93 | return std::noop_coroutine(); | ||
| 282 | } | 286 | } | |||||
| 283 | 287 | |||||||
| 284 | /** Complete the wait and report the outcome. | 288 | /** Complete the wait and report the outcome. | |||||
| 285 | 289 | |||||||
| 286 | - | Destroys the stop callback if one is registered. If the wait | 290 | + | Destroys the stop callback if one is registered, and leaves | |||
| 287 | - | was canceled while still linked into the event's wait queue, | 291 | + | the awaiter unlinked from the event's wait queue. | |||
| 288 | - | unlinks it. `set()` pops every waiter, so a canceled waiter may | ||||||
| 289 | - | or may not still be linked when it resumes. | ||||||
| 290 | 292 | |||||||
| 291 | - | @return An empty `results<>` if the event was set, or one | 293 | + | @return Empty on set, else `error::canceled` (stop or destruction). | |||
| 292 | - | holding `error::canceled` if the stop token fired first. | ||||||
| 293 | */ | 294 | */ | |||||
| HITCBC | 294 | 57 | [[nodiscard]] results<> await_resume() noexcept | 295 | 57 | [[nodiscard]] results<> await_resume() noexcept | ||
| 295 | { | 296 | { | |||||
| HITCBC | 296 | 57 | if(active_) | 297 | 57 | if(active_) | ||
| 297 | { | 298 | { | |||||
| HITCBC | 298 | 52 | stop_cb_().~stop_cb_t(); | 299 | 52 | stop_cb_().~stop_cb_t(); | ||
| HITCBC | 299 | 52 | active_ = false; | 300 | 52 | active_ = false; | ||
| 300 | } | 301 | } | |||||
| HITCBC | 301 | 57 | if(canceled_) | 302 | 57 | if(canceled_) | ||
| 302 | { | 303 | { | |||||
| HITCBC | 303 | 38 | if(list_) | 304 | 38 | if(list_) | ||
| 304 | { | 305 | { | |||||
| HITCBC | 305 | 3 | list_->remove(this); | 306 | 3 | list_->remove(this); | ||
| HITCBC | 306 | 3 | list_ = nullptr; | 307 | 3 | list_ = nullptr; | ||
| 307 | } | 308 | } | |||||
| HITCBC | 308 | 76 | return {make_error_code( | 309 | 76 | return {make_error_code( | ||
| HITCBC | 309 | 38 | error::canceled)}; | 310 | 38 | error::canceled)}; | ||
| 310 | } | 311 | } | |||||
| HITCBC | 311 | 19 | return {{}}; | 312 | 19 | return {{}}; | ||
| 312 | } | 313 | } | |||||
| 313 | }; | 314 | }; | |||||
| 314 | 315 | |||||||
| 315 | /// Construct an unset event. | 316 | /// Construct an unset event. | |||||
| HITCBC | 316 | 2 | async_event() = default; | 317 | 2 | async_event() = default; | ||
| 317 | 318 | |||||||
| 318 | - | /** Copy construction is disabled; suspended waiters point into the | 319 | + | /** Deleted: suspended waiters point into the event's wait queue. | |||
| 319 | - | event's wait queue. | ||||||
| 320 | 320 | |||||||
| 321 | @param other The event that would be copied. | 321 | @param other The event that would be copied. | |||||
| 322 | */ | 322 | */ | |||||
| 323 | async_event(async_event const& other) = delete; | 323 | async_event(async_event const& other) = delete; | |||||
| 324 | 324 | |||||||
| 325 | - | /** Copy assignment is disabled; suspended waiters point into the | 325 | + | /** Deleted: suspended waiters point into the event's wait queue. | |||
| 326 | - | event's wait queue. | ||||||
| 327 | 326 | |||||||
| 328 | @param other The event that would be assigned from. | 327 | @param other The event that would be assigned from. | |||||
| 329 | 328 | |||||||
| 330 | @return A reference to `*this`. | 329 | @return A reference to `*this`. | |||||
| 331 | */ | 330 | */ | |||||
| 332 | async_event& operator=(async_event const& other) = delete; | 331 | async_event& operator=(async_event const& other) = delete; | |||||
| 333 | 332 | |||||||
| 334 | - | /** Move construction is disabled; suspended waiters point into the | 333 | + | /** Deleted: suspended waiters point into the event's wait queue. | |||
| 335 | - | event's wait queue. | ||||||
| 336 | 334 | |||||||
| 337 | @param other The event that would be moved from. | 335 | @param other The event that would be moved from. | |||||
| 338 | */ | 336 | */ | |||||
| 339 | async_event(async_event&& other) = delete; | 337 | async_event(async_event&& other) = delete; | |||||
| 340 | 338 | |||||||
| 341 | - | /** Move assignment is disabled; suspended waiters point into the | 339 | + | /** Deleted: suspended waiters point into the event's wait queue. | |||
| 342 | - | event's wait queue. | ||||||
| 343 | 340 | |||||||
| 344 | @param other The event that would be moved from. | 341 | @param other The event that would be moved from. | |||||
| 345 | 342 | |||||||
| 346 | @return A reference to `*this`. | 343 | @return A reference to `*this`. | |||||
| 347 | */ | 344 | */ | |||||
| 348 | async_event& operator=(async_event&& other) = delete; | 345 | async_event& operator=(async_event&& other) = delete; | |||||
| 349 | 346 | |||||||
| 350 | /** Destroy the event, canceling every coroutine still waiting. | 347 | /** Destroy the event, canceling every coroutine still waiting. | |||||
| 351 | 348 | |||||||
| 352 | Each waiter is posted to its executor to resume with | 349 | Each waiter is posted to its executor to resume with | |||||
| 353 | `error::canceled`, as if its stop token had been triggered. An | 350 | `error::canceled`, as if its stop token had been triggered. An | |||||
| 354 | executor that can no longer run it abandons it instead, possibly | 351 | executor that can no longer run it abandons it instead, possibly | |||||
| 355 | before this destructor returns. Either way the waiter no longer | 352 | before this destructor returns. Either way the waiter no longer | |||||
| 356 | refers to the event. | 353 | refers to the event. | |||||
| 357 | */ | 354 | */ | |||||
| HITCBC | 358 | 30 | ~async_event() | 355 | 30 | ~async_event() | ||
| 359 | { | 356 | { | |||||
| HITCBC | 360 | 97 | while(auto* w = waiters_.pop_front()) | 357 | 97 | while(auto* w = waiters_.pop_front()) | ||
| 361 | { | 358 | { | |||||
| HITCBC | 362 | 67 | w->list_ = nullptr; | 359 | 67 | w->list_ = nullptr; | ||
| HITCBC | 363 | 67 | if(!w->claimed_.exchange(true, std::memory_order_acq_rel)) | 360 | 67 | if(!w->claimed_.exchange(true, std::memory_order_acq_rel)) | ||
| 364 | { | 361 | { | |||||
| HITCBC | 365 | 66 | w->canceled_ = true; | 362 | 66 | w->canceled_ = true; | ||
| HITCBC | 366 | 66 | w->ex_.post(w->cont_); | 363 | 66 | w->ex_.post(w->cont_); | ||
| 367 | } | 364 | } | |||||
| HITCBC | 368 | 67 | } | 365 | 67 | } | ||
| HITCBC | 369 | 30 | } | 366 | 30 | } | ||
| 370 | 367 | |||||||
| 371 | - | /** Returns an awaiter that waits until the event is set. | 368 | + | /** Return an awaiter that waits until the event is set. | |||
| 372 | 369 | |||||||
| 373 | If the event is already set, completes immediately. | 370 | If the event is already set, completes immediately. | |||||
| 374 | 371 | |||||||
| 375 | @return An awaitable that await-returns `(error_code)`. | 372 | @return An awaitable that await-returns `(error_code)`. | |||||
| 376 | */ | 373 | */ | |||||
| HITCBC | 377 | 100 | wait_awaiter wait() noexcept | 374 | 100 | wait_awaiter wait() noexcept | ||
| 378 | { | 375 | { | |||||
| HITCBC | 379 | 100 | return wait_awaiter{this}; | 376 | 100 | return wait_awaiter{this}; | ||
| 380 | } | 377 | } | |||||
| 381 | 378 | |||||||
| 382 | - | /** Resumes every waiting coroutine and marks the event set for later `wait()` calls. | 379 | + | /** Post every waiting coroutine to its executor and mark the event set. | |||
| 383 | 380 | |||||||
| 384 | - | All waiting coroutines are resumed. Canceled waiters | 381 | + | Each waiting coroutine is posted to its executor. Canceled waiters | |||
| 385 | are skipped. Subsequent calls to wait() complete | 382 | are skipped. Subsequent calls to wait() complete | |||||
| 386 | immediately until clear() is called. | 383 | immediately until clear() is called. | |||||
| 387 | 384 | |||||||
| 388 | @note If posting a waiter's resumption throws, `std::terminate` | 385 | @note If posting a waiter's resumption throws, `std::terminate` | |||||
| 389 | is called. | 386 | is called. | |||||
| 390 | */ | 387 | */ | |||||
| HITCBC | 391 | 20 | void set() noexcept | 388 | 20 | void set() noexcept | ||
| 392 | { | 389 | { | |||||
| HITCBC | 393 | 20 | set_ = true; | 390 | 20 | set_ = true; | ||
| 394 | // A post can destroy a waiter on the spot, and with it whatever | 391 | // A post can destroy a waiter on the spot, and with it whatever | |||||
| 395 | // owns this event; nothing below touches *this. | 392 | // owns this event; nothing below touches *this. | |||||
| HITCBC | 396 | 20 | detail::intrusive_list<wait_awaiter> claimed; | 393 | 20 | detail::intrusive_list<wait_awaiter> claimed; | ||
| HITCBC | 397 | 20 | claim_all(claimed); | 394 | 20 | claim_all(claimed); | ||
| HITCBC | 398 | 38 | while(auto* w = claimed.pop_front()) | 395 | 38 | while(auto* w = claimed.pop_front()) | ||
| 399 | { | 396 | { | |||||
| HITCBC | 400 | 18 | w->list_ = nullptr; | 397 | 18 | w->list_ = nullptr; | ||
| HITCBC | 401 | 18 | w->ex_.post(w->cont_); | 398 | 18 | w->ex_.post(w->cont_); | ||
| HITCBC | 402 | 18 | } | 399 | 18 | } | ||
| HITCBC | 403 | 20 | } | 400 | 20 | } | ||
| 404 | 401 | |||||||
| 405 | - | /** Clears the event. | 402 | + | /** Clear the event. | |||
| 406 | 403 | |||||||
| 407 | Subsequent calls to wait() suspend until | 404 | Subsequent calls to wait() suspend until | |||||
| 408 | set() is called again. | 405 | set() is called again. | |||||
| 409 | */ | 406 | */ | |||||
| HITCBC | 410 | 2 | void clear() noexcept | 407 | 2 | void clear() noexcept | ||
| 411 | { | 408 | { | |||||
| HITCBC | 412 | 2 | set_ = false; | 409 | 2 | set_ = false; | ||
| HITCBC | 413 | 2 | } | 410 | 2 | } | ||
| 414 | 411 | |||||||
| 415 | - | /** Returns true if the event is currently set. | 412 | + | /** Check whether the event is currently set. | |||
| 416 | 413 | |||||||
| 417 | @return `true` if the event is set; otherwise `false`. | 414 | @return `true` if the event is set; otherwise `false`. | |||||
| 418 | */ | 415 | */ | |||||
| HITCBC | 419 | 9 | bool is_set() const noexcept | 416 | 9 | bool is_set() const noexcept | ||
| 420 | { | 417 | { | |||||
| HITCBC | 421 | 9 | return set_; | 418 | 9 | return set_; | ||
| 422 | } | 419 | } | |||||
| 423 | }; | 420 | }; | |||||
| 424 | 421 | |||||||
| 425 | inline void | 422 | inline void | |||||
| HITCBC | 426 | 20 | async_event::claim_all( | 423 | 20 | async_event::claim_all( | ||
| 427 | detail::intrusive_list<wait_awaiter>& into) noexcept | 424 | detail::intrusive_list<wait_awaiter>& into) noexcept | |||||
| 428 | { | 425 | { | |||||
| HITCBC | 429 | 38 | while(auto* w = waiters_.pop_front()) | 426 | 38 | while(auto* w = waiters_.pop_front()) | ||
| 430 | { | 427 | { | |||||
| HITCBC | 431 | 18 | w->list_ = nullptr; | 428 | 18 | w->list_ = nullptr; | ||
| HITCBC | 432 | 18 | if(!w->claimed_.exchange(true, std::memory_order_acq_rel)) | 429 | 18 | if(!w->claimed_.exchange(true, std::memory_order_acq_rel)) | ||
| 433 | { | 430 | { | |||||
| HITCBC | 434 | 18 | into.push_back(w); | 431 | 18 | into.push_back(w); | ||
| HITCBC | 435 | 18 | w->list_ = &into; | 432 | 18 | w->list_ = &into; | ||
| 436 | } | 433 | } | |||||
| HITCBC | 437 | 18 | } | 434 | 18 | } | ||
| HITCBC | 438 | 20 | } | 435 | 20 | } | ||
| 439 | 436 | |||||||
| 440 | } // namespace capy | 437 | } // namespace capy | |||||
| 441 | } // namespace boost | 438 | } // namespace boost | |||||
| 442 | 439 | |||||||
| 443 | #endif | 440 | #endif | |||||