100.00% Lines (29/29)
100.00% Functions (8/8)
| TLA | Baseline | Branch | ||||||
|---|---|---|---|---|---|---|---|---|
| Line | Hits | Code | Line | Hits | Code | |||
| 1 | // | 1 | // | |||||
| 2 | // Copyright (c) 2026 Steve Gerbino | 2 | // Copyright (c) 2026 Steve Gerbino | |||||
| 3 | // | 3 | // | |||||
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |||||
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||||
| 6 | // | 6 | // | |||||
| 7 | // Official repository: https://github.com/cppalliance/capy | 7 | // Official repository: https://github.com/cppalliance/capy | |||||
| 8 | // | 8 | // | |||||
| 9 | 9 | |||||||
| 10 | #ifndef BOOST_CAPY_EX_ABANDON_HPP | 10 | #ifndef BOOST_CAPY_EX_ABANDON_HPP | |||||
| 11 | #define BOOST_CAPY_EX_ABANDON_HPP | 11 | #define BOOST_CAPY_EX_ABANDON_HPP | |||||
| 12 | 12 | |||||||
| 13 | #include <boost/capy/detail/config.hpp> | 13 | #include <boost/capy/detail/config.hpp> | |||||
| 14 | #include <boost/capy/concept/context_awaitable.hpp> | 14 | #include <boost/capy/concept/context_awaitable.hpp> | |||||
| 15 | #include <boost/capy/continuation.hpp> | 15 | #include <boost/capy/continuation.hpp> | |||||
| 16 | #include <boost/capy/ex/await_context.hpp> | 16 | #include <boost/capy/ex/await_context.hpp> | |||||
| 17 | #include <boost/capy/ex/executor_ref.hpp> | 17 | #include <boost/capy/ex/executor_ref.hpp> | |||||
| 18 | 18 | |||||||
| 19 | #include <atomic> | 19 | #include <atomic> | |||||
| 20 | #include <coroutine> | 20 | #include <coroutine> | |||||
| 21 | #include <optional> | 21 | #include <optional> | |||||
| 22 | #include <type_traits> | 22 | #include <type_traits> | |||||
| 23 | 23 | |||||||
| 24 | namespace boost { | 24 | namespace boost { | |||||
| 25 | namespace capy { | 25 | namespace capy { | |||||
| 26 | 26 | |||||||
| 27 | /** The owner of one segment of a coroutine chain. | 27 | /** The owner of one segment of a coroutine chain. | |||||
| 28 | 28 | |||||||
| 29 | A segment is a run of coroutines started together: the task | 29 | A segment is a run of coroutines started together: the task | |||||
| 30 | `run_async` launches and everything it awaits, a task `run` starts | 30 | `run_async` launches and everything it awaits, a task `run` starts | |||||
| 31 | on another executor, or one branch of `when_all` or `when_any`. Its | 31 | on another executor, or one branch of `when_all` or `when_any`. Its | |||||
| 32 | owner is the coroutine that started it, suspended above the | 32 | owner is the coroutine that started it, suspended above the | |||||
| 33 | segment's top frame until that frame completes. Every coroutine in | 33 | segment's top frame until that frame completes. Every coroutine in | |||||
| 34 | the segment reaches the owner through its @ref await_context. | 34 | the segment reaches the owner through its @ref await_context. | |||||
| 35 | 35 | |||||||
| 36 | When the holder of a suspended coroutine in the segment gives it up | 36 | When the holder of a suspended coroutine in the segment gives it up | |||||
| 37 | with @ref abandon, the owner posts its own continuation to its | 37 | with @ref abandon, the owner posts its own continuation to its | |||||
| 38 | executor. There it unwinds the segment from the top: it destroys | 38 | executor. There it unwinds the segment from the top: it destroys | |||||
| 39 | the frame it awaits, which destroys the frames below it in turn, and | 39 | the frame it awaits, which destroys the frames below it in turn, and | |||||
| 40 | finishes without delivering a result. An executor that destroys the | 40 | finishes without delivering a result. An executor that destroys the | |||||
| 41 | posted continuation instead, as a closed one does, unwinds the | 41 | posted continuation instead, as a closed one does, unwinds the | |||||
| 42 | segment the same way. | 42 | segment the same way. | |||||
| 43 | 43 | |||||||
| 44 | @par Thread Safety | 44 | @par Thread Safety | |||||
| 45 | @ref abandon may be called from any thread. Only one holder can | 45 | @ref abandon may be called from any thread. Only one holder can | |||||
| 46 | abandon a segment, since it is suspended at one point; a repeated | 46 | abandon a segment, since it is suspended at one point; a repeated | |||||
| 47 | call before the unwind runs does nothing. | 47 | call before the unwind runs does nothing. | |||||
| 48 | */ | 48 | */ | |||||
| 49 | class chain_owner | 49 | class chain_owner | |||||
| 50 | { | 50 | { | |||||
| 51 | std::atomic<bool> abandoned_{false}; | 51 | std::atomic<bool> abandoned_{false}; | |||||
| 52 | continuation self_; | 52 | continuation self_; | |||||
| 53 | executor_ref ex_; | 53 | executor_ref ex_; | |||||
| 54 | 54 | |||||||
| 55 | public: | 55 | public: | |||||
| 56 | /** Bind the owner to its coroutine and executor. | 56 | /** Bind the owner to its coroutine and executor. | |||||
| 57 | 57 | |||||||
| 58 | Called by the owner before it starts the segment. | 58 | Called by the owner before it starts the segment. | |||||
| 59 | 59 | |||||||
| 60 | @param self The owner's coroutine, resumed or destroyed to | 60 | @param self The owner's coroutine, resumed or destroyed to | |||||
| 61 | unwind the segment. | 61 | unwind the segment. | |||||
| 62 | 62 | |||||||
| 63 | @param ex The executor the segment runs on. | 63 | @param ex The executor the segment runs on. | |||||
| 64 | */ | 64 | */ | |||||
| HITCBC | 65 | 106416 | void bind(std::coroutine_handle<> self, executor_ref ex) noexcept | 65 | 106421 | void bind(std::coroutine_handle<> self, executor_ref ex) noexcept | ||
| 66 | { | 66 | { | |||||
| HITCBC | 67 | 106416 | self_.h = self; | 67 | 106421 | self_.h = self; | ||
| HITCBC | 68 | 106416 | ex_ = ex; | 68 | 106421 | ex_ = ex; | ||
| HITCBC | 69 | 106416 | } | 69 | 106421 | } | ||
| 70 | 70 | |||||||
| 71 | /// Return true once the segment has been abandoned. | 71 | /// Return true once the segment has been abandoned. | |||||
| HITCBC | 72 | 7816 | bool abandoned() const noexcept | 72 | 7827 | bool abandoned() const noexcept | ||
| 73 | { | 73 | { | |||||
| HITCBC | 74 | 7816 | return abandoned_.load(std::memory_order_acquire); | 74 | 7827 | return abandoned_.load(std::memory_order_acquire); | ||
| 75 | } | 75 | } | |||||
| 76 | 76 | |||||||
| 77 | /** Unwind the segment on the owner's executor. | 77 | /** Unwind the segment on the owner's executor. | |||||
| 78 | 78 | |||||||
| 79 | Does nothing if the segment was already abandoned. Otherwise | 79 | Does nothing if the segment was already abandoned. Otherwise | |||||
| 80 | posts the owner's continuation; nothing of the owner is touched | 80 | posts the owner's continuation; nothing of the owner is touched | |||||
| 81 | afterward, since the post may unwind and free it at once. | 81 | afterward, since the post may unwind and free it at once. | |||||
| 82 | 82 | |||||||
| 83 | @note A post that throws calls `std::terminate`. | 83 | @note A post that throws calls `std::terminate`. | |||||
| 84 | */ | 84 | */ | |||||
| HITCBC | 85 | 101377 | void abandon() noexcept | 85 | 101371 | void abandon() noexcept | ||
| 86 | { | 86 | { | |||||
| HITCBC | 87 | 101377 | if(abandoned_.exchange(true, std::memory_order_acq_rel)) | 87 | 101371 | if(abandoned_.exchange(true, std::memory_order_acq_rel)) | ||
| HITCBC | 88 | 1 | return; | 88 | 1 | return; | ||
| HITCBC | 89 | 101376 | ex_.post(self_); | 89 | 101370 | ex_.post(self_); | ||
| 90 | } | 90 | } | |||||
| 91 | }; | 91 | }; | |||||
| 92 | 92 | |||||||
| 93 | /** Give up a suspended coroutine for good. | 93 | /** Give up a suspended coroutine for good. | |||||
| 94 | 94 | |||||||
| 95 | Called by the holder of a suspended coroutine that will never resume | 95 | Called by the holder of a suspended coroutine that will never resume | |||||
| 96 | it, such as an execution context, strand or primitive being torn | 96 | it, such as an execution context, strand or primitive being torn | |||||
| 97 | down. The coroutine's segment is unwound from its owner: on the | 97 | down. The coroutine's segment is unwound from its owner: on the | |||||
| 98 | owner's executor, from the top frame down to this coroutine, without | 98 | owner's executor, from the top frame down to this coroutine, without | |||||
| 99 | any frame delivering a result. Ordinary cancellation does not use | 99 | any frame delivering a result. Ordinary cancellation does not use | |||||
| 100 | this; it resumes the coroutine with an error instead. | 100 | this; it resumes the coroutine with an error instead. | |||||
| 101 | 101 | |||||||
| 102 | A continuation with no owner, such as an owner's own continuation, | 102 | A continuation with no owner, such as an owner's own continuation, | |||||
| 103 | is destroyed directly. | 103 | is destroyed directly. | |||||
| 104 | 104 | |||||||
| 105 | @par Preconditions | 105 | @par Preconditions | |||||
| 106 | `c` refers to a suspended coroutine that the caller holds and that | 106 | `c` refers to a suspended coroutine that the caller holds and that | |||||
| 107 | nothing else will resume or abandon. If `c` has no owner, nothing | 107 | nothing else will resume or abandon. If `c` has no owner, nothing | |||||
| 108 | awaits the coroutine: destroying its frame alone would leave any | 108 | awaits the coroutine: destroying its frame alone would leave any | |||||
| 109 | awaiting frame holding a destroyed one. | 109 | awaiting frame holding a destroyed one. | |||||
| 110 | 110 | |||||||
| 111 | @par Postconditions | 111 | @par Postconditions | |||||
| 112 | The caller must not touch the coroutine, or anything in its frame, | 112 | The caller must not touch the coroutine, or anything in its frame, | |||||
| 113 | again. The frames may already be destroyed when this returns, if the | 113 | again. The frames may already be destroyed when this returns, if the | |||||
| 114 | owner's executor destroys posted work at once. | 114 | owner's executor destroys posted work at once. | |||||
| 115 | 115 | |||||||
| 116 | @note The unwind runs on the owner's executor, which may belong to | 116 | @note The unwind runs on the owner's executor, which may belong to | |||||
| 117 | another execution context than the caller's, and may run after the | 117 | another execution context than the caller's, and may run after the | |||||
| 118 | caller's context is destroyed. A chain that holds objects of the | 118 | caller's context is destroyed. A chain that holds objects of the | |||||
| 119 | caller's context must therefore have finished before that context | 119 | caller's context must therefore have finished before that context | |||||
| 120 | is torn down. | 120 | is torn down. | |||||
| 121 | 121 | |||||||
| 122 | @note Each frame is destroyed from the destructor of the frame | 122 | @note Each frame is destroyed from the destructor of the frame | |||||
| 123 | awaiting it, so the unwind uses stack in proportion to how deeply | 123 | awaiting it, so the unwind uses stack in proportion to how deeply | |||||
| 124 | the chain's coroutines are nested. | 124 | the chain's coroutines are nested. | |||||
| 125 | 125 | |||||||
| 126 | @param c The continuation of the coroutine given up. | 126 | @param c The continuation of the coroutine given up. | |||||
| 127 | */ | 127 | */ | |||||
| 128 | inline void | 128 | inline void | |||||
| HITCBC | 129 | 203395 | abandon(continuation& c) noexcept | 129 | 203632 | abandon(continuation& c) noexcept | ||
| 130 | { | 130 | { | |||||
| HITCBC | 131 | 203395 | if(c.owner) | 131 | 203632 | if(c.owner) | ||
| HITCBC | 132 | 101375 | c.owner->abandon(); | 132 | 101369 | c.owner->abandon(); | ||
| HITCBC | 133 | 102020 | else if(c.h) | 133 | 102263 | else if(c.h) | ||
| HITCBC | 134 | 102020 | c.h.destroy(); | 134 | 102263 | c.h.destroy(); | ||
| HITCBC | 135 | 203395 | } | 135 | 203632 | } | ||
| 136 | 136 | |||||||
| 137 | /** Await an awaitable on behalf of a segment owner. | 137 | /** Await an awaitable on behalf of a segment owner. | |||||
| 138 | 138 | |||||||
| 139 | An owner, a coroutine that binds a @ref chain_owner and starts a | 139 | An owner, a coroutine that binds a @ref chain_owner and starts a | |||||
| 140 | segment beneath it, waits for that segment by awaiting this. It is | 140 | segment beneath it, waits for that segment by awaiting this. It is | |||||
| 141 | resumed either when the awaitable completes or to unwind the | 141 | resumed either when the awaitable completes or to unwind the | |||||
| 142 | segment after an @ref abandon. In the second case the awaitable | 142 | segment after an @ref abandon. In the second case the awaitable | |||||
| 143 | never completed, so there is no result to read: the result is | 143 | never completed, so there is no result to read: the result is | |||||
| 144 | empty, or `false` for an awaitable with no result, and the owner | 144 | empty, or `false` for an awaitable with no result, and the owner | |||||
| 145 | must destroy the awaitable without reading it. | 145 | must destroy the awaitable without reading it. | |||||
| 146 | 146 | |||||||
| 147 | The awaitable is referenced, not copied, so the caller keeps it | 147 | The awaitable is referenced, not copied, so the caller keeps it | |||||
| 148 | alive until the `co_await` completes. Its `await_resume` must | 148 | alive until the `co_await` completes. Its `await_resume` must | |||||
| 149 | return a move-constructible object or `void`: the result is moved | 149 | return a move-constructible object or `void`: the result is moved | |||||
| 150 | into a `std::optional`, which cannot hold a reference. | 150 | into a `std::optional`, which cannot hold a reference. | |||||
| 151 | 151 | |||||||
| 152 | @par Preconditions | 152 | @par Preconditions | |||||
| 153 | The awaiting coroutine's @ref await_context names, as its `owner`, | 153 | The awaiting coroutine's @ref await_context names, as its `owner`, | |||||
| 154 | the @ref chain_owner bound to that coroutine. | 154 | the @ref chain_owner bound to that coroutine. | |||||
| 155 | 155 | |||||||
| 156 | @par Exception Safety | 156 | @par Exception Safety | |||||
| 157 | An exception from the awaitable's `await_ready`, `await_suspend` or | 157 | An exception from the awaitable's `await_ready`, `await_suspend` or | |||||
| 158 | `await_resume` propagates unchanged. | 158 | `await_resume` propagates unchanged. | |||||
| 159 | 159 | |||||||
| 160 | @note Awaited from a promise built on | 160 | @note Awaited from a promise built on | |||||
| 161 | @ref context_awaitable_promise_base, as an owner usually is, an | 161 | @ref context_awaitable_promise_base, as an owner usually is, an | |||||
| 162 | exception from `await_ready` or `await_suspend` calls | 162 | exception from `await_ready` or `await_suspend` calls | |||||
| 163 | `std::terminate` instead. | 163 | `std::terminate` instead. | |||||
| 164 | 164 | |||||||
| 165 | @par Example | 165 | @par Example | |||||
| 166 | @par !example example | 166 | @par !example example | |||||
| 167 | 167 | |||||||
| 168 | @see chain_owner, abandon | 168 | @see chain_owner, abandon | |||||
| 169 | */ | 169 | */ | |||||
| 170 | template<ContextAwaitable A> | 170 | template<ContextAwaitable A> | |||||
| 171 | class abandonable | 171 | class abandonable | |||||
| 172 | { | 172 | { | |||||
| 173 | A& a_; | 173 | A& a_; | |||||
| 174 | chain_owner* owner_ = nullptr; | 174 | chain_owner* owner_ = nullptr; | |||||
| 175 | 175 | |||||||
| 176 | public: | 176 | public: | |||||
| 177 | /// The awaitable's own `await_resume` result. | 177 | /// The awaitable's own `await_resume` result. | |||||
| 178 | using result_type = decltype(std::declval<A&>().await_resume()); | 178 | using result_type = decltype(std::declval<A&>().await_resume()); | |||||
| 179 | 179 | |||||||
| 180 | /** Construct from the awaitable to wait for. | 180 | /** Construct from the awaitable to wait for. | |||||
| 181 | 181 | |||||||
| 182 | @param a The awaitable. It is referenced, not copied. | 182 | @param a The awaitable. It is referenced, not copied. | |||||
| 183 | */ | 183 | */ | |||||
| HITCBC | 184 | 323 | explicit abandonable(A& a) noexcept | 184 | 323 | explicit abandonable(A& a) noexcept | ||
| HITCBC | 185 | 323 | : a_(a) | 185 | 323 | : a_(a) | ||
| 186 | { | 186 | { | |||||
| HITCBC | 187 | 323 | } | 187 | 323 | } | ||
| 188 | 188 | |||||||
| 189 | /// Check whether the awaitable is already complete. | 189 | /// Check whether the awaitable is already complete. | |||||
| HITCBC | 190 | 323 | bool await_ready() | 190 | 323 | bool await_ready() | ||
| 191 | { | 191 | { | |||||
| HITCBC | 192 | 323 | return a_.await_ready(); | 192 | 323 | return a_.await_ready(); | ||
| 193 | } | 193 | } | |||||
| 194 | 194 | |||||||
| 195 | /** Suspend on the awaitable, remembering the segment owner. | 195 | /** Suspend on the awaitable, remembering the segment owner. | |||||
| 196 | 196 | |||||||
| 197 | @param h The coroutine performing the `co_await`. | 197 | @param h The coroutine performing the `co_await`. | |||||
| 198 | @param env The environment of that coroutine. | 198 | @param env The environment of that coroutine. | |||||
| 199 | 199 | |||||||
| 200 | @return The awaitable's own `await_suspend` result. | 200 | @return The awaitable's own `await_suspend` result. | |||||
| 201 | */ | 201 | */ | |||||
| 202 | template<class H> | 202 | template<class H> | |||||
| HITCBC | 203 | 322 | auto await_suspend(H h, await_context const* env) | 203 | 322 | auto await_suspend(H h, await_context const* env) | ||
| 204 | { | 204 | { | |||||
| HITCBC | 205 | 322 | owner_ = env->owner; | 205 | 322 | owner_ = env->owner; | ||
| HITCBC | 206 | 322 | return a_.await_suspend(h, env); | 206 | 322 | return a_.await_suspend(h, env); | ||
| 207 | } | 207 | } | |||||
| 208 | 208 | |||||||
| 209 | /** Return the result, or nothing if the segment was abandoned. | 209 | /** Return the result, or nothing if the segment was abandoned. | |||||
| 210 | 210 | |||||||
| 211 | For an awaitable with no result this returns `bool` instead, | 211 | For an awaitable with no result this returns `bool` instead, | |||||
| 212 | `false` when the segment was unwound. | 212 | `false` when the segment was unwound. | |||||
| 213 | 213 | |||||||
| 214 | @return The result, empty when the segment was unwound. | 214 | @return The result, empty when the segment was unwound. | |||||
| 215 | */ | 215 | */ | |||||
| HITCBC | 216 | 317 | auto await_resume() | 216 | 317 | auto await_resume() | ||
| 217 | { | 217 | { | |||||
| HITCBC | 218 | 317 | bool const unwinding = owner_ && owner_->abandoned(); | 218 | 317 | bool const unwinding = owner_ && owner_->abandoned(); | ||
| 219 | if constexpr(std::is_void_v<result_type>) | 219 | if constexpr(std::is_void_v<result_type>) | |||||
| 220 | { | 220 | { | |||||
| 221 | if(unwinding) | 221 | if(unwinding) | |||||
| 222 | return false; | 222 | return false; | |||||
| 223 | a_.await_resume(); | 223 | a_.await_resume(); | |||||
| 224 | return true; | 224 | return true; | |||||
| 225 | } | 225 | } | |||||
| 226 | else | 226 | else | |||||
| 227 | { | 227 | { | |||||
| HITCBC | 228 | 317 | if(unwinding) | 228 | 317 | if(unwinding) | ||
| HITCBC | 229 | 34 | return std::optional<result_type>(); | 229 | 34 | return std::optional<result_type>(); | ||
| HITCBC | 230 | 283 | return std::optional<result_type>(a_.await_resume()); | 230 | 283 | return std::optional<result_type>(a_.await_resume()); | ||
| 231 | } | 231 | } | |||||
| 232 | } | 232 | } | |||||
| 233 | }; | 233 | }; | |||||
| 234 | 234 | |||||||
| 235 | } // namespace capy | 235 | } // namespace capy | |||||
| 236 | } // namespace boost | 236 | } // namespace boost | |||||
| 237 | 237 | |||||||
| 238 | #endif | 238 | #endif | |||||