100.00% Lines (87/87)
100.00% Functions (27/27)
| 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_TASK_HPP | 11 | #ifndef BOOST_CAPY_TASK_HPP | |||||
| 12 | #define BOOST_CAPY_TASK_HPP | 12 | #define BOOST_CAPY_TASK_HPP | |||||
| 13 | 13 | |||||||
| 14 | #include <boost/capy/detail/config.hpp> | 14 | #include <boost/capy/detail/config.hpp> | |||||
| 15 | #include <boost/capy/concept/executor.hpp> | 15 | #include <boost/capy/concept/executor.hpp> | |||||
| 16 | #include <boost/capy/concept/context_awaitable.hpp> | 16 | #include <boost/capy/concept/context_awaitable.hpp> | |||||
| 17 | #include <boost/capy/ex/context_awaitable_promise_base.hpp> | 17 | #include <boost/capy/ex/context_awaitable_promise_base.hpp> | |||||
| 18 | #include <boost/capy/ex/await_context.hpp> | 18 | #include <boost/capy/ex/await_context.hpp> | |||||
| 19 | #include <boost/capy/detail/context_awaiter.hpp> | 19 | #include <boost/capy/detail/context_awaiter.hpp> | |||||
| 20 | #include <boost/capy/ex/frame_allocator.hpp> | 20 | #include <boost/capy/ex/frame_allocator.hpp> | |||||
| 21 | #include <boost/capy/results.hpp> | 21 | #include <boost/capy/results.hpp> | |||||
| 22 | 22 | |||||||
| 23 | #include <exception> | 23 | #include <exception> | |||||
| 24 | #include <optional> | 24 | #include <optional> | |||||
| 25 | #include <type_traits> | 25 | #include <type_traits> | |||||
| 26 | #include <utility> | 26 | #include <utility> | |||||
| 27 | #include <variant> | 27 | #include <variant> | |||||
| 28 | 28 | |||||||
| 29 | namespace boost { | 29 | namespace boost { | |||||
| 30 | namespace capy { | 30 | namespace capy { | |||||
| 31 | 31 | |||||||
| 32 | namespace detail { | 32 | namespace detail { | |||||
| 33 | 33 | |||||||
| 34 | // Helper base for result storage and return_void/return_value | 34 | // Helper base for result storage and return_void/return_value | |||||
| 35 | template<typename T> | 35 | template<typename T> | |||||
| 36 | struct task_return_base | 36 | struct task_return_base | |||||
| 37 | { | 37 | { | |||||
| 38 | std::optional<T> result_; | 38 | std::optional<T> result_; | |||||
| 39 | 39 | |||||||
| HITCBC | 40 | 901 | void return_value(T value) | 40 | 901 | void return_value(T value) | ||
| 41 | { | 41 | { | |||||
| HITCBC | 42 | 901 | result_ = std::move(value); | 42 | 901 | result_ = std::move(value); | ||
| HITCBC | 43 | 901 | } | 43 | 901 | } | ||
| 44 | 44 | |||||||
| HITCBC | 45 | 284 | T&& result() noexcept | 45 | 284 | T&& result() noexcept | ||
| 46 | { | 46 | { | |||||
| HITCBC | 47 | 284 | return std::move(*result_); | 47 | 284 | return std::move(*result_); | ||
| 48 | } | 48 | } | |||||
| 49 | }; | 49 | }; | |||||
| 50 | 50 | |||||||
| 51 | template<> | 51 | template<> | |||||
| 52 | struct task_return_base<void> | 52 | struct task_return_base<void> | |||||
| 53 | { | 53 | { | |||||
| HITCBC | 54 | 4149 | void return_void() | 54 | 4160 | void return_void() | ||
| 55 | { | 55 | { | |||||
| HITCBC | 56 | 4149 | } | 56 | 4160 | } | ||
| 57 | }; | 57 | }; | |||||
| 58 | 58 | |||||||
| 59 | } // namespace detail | 59 | } // namespace detail | |||||
| 60 | 60 | |||||||
| 61 | /** Defers a coroutine body until awaited, then runs it inline on the caller's thread. | 61 | /** Defers a coroutine body until awaited, then runs it inline on the caller's thread. | |||||
| 62 | 62 | |||||||
| 63 | Use `task<T>` as the return type for coroutines that perform I/O | 63 | Use `task<T>` as the return type for coroutines that perform I/O | |||||
| 64 | and return a value of type `T`. The coroutine body does not start | 64 | and return a value of type `T`. The coroutine body does not start | |||||
| 65 | executing until the task is awaited, enabling efficient composition | 65 | executing until the task is awaited, enabling efficient composition | |||||
| 66 | without unnecessary eager execution. | 66 | without unnecessary eager execution. | |||||
| 67 | 67 | |||||||
| 68 | The task participates in the I/O awaitable protocol: when awaited, | 68 | The task participates in the I/O awaitable protocol: when awaited, | |||||
| 69 | it receives the caller's executor and stop token, propagating them | 69 | it receives the caller's executor and stop token, propagating them | |||||
| 70 | to nested `co_await` expressions. This enables cancellation and | 70 | to nested `co_await` expressions. This enables cancellation and | |||||
| 71 | proper completion dispatch across executor boundaries. | 71 | proper completion dispatch across executor boundaries. | |||||
| 72 | 72 | |||||||
| 73 | @par Await-effects | 73 | @par Await-effects | |||||
| 74 | 74 | |||||||
| 75 | Let `t` be a `task<T>`. `co_await t` always suspends the awaiting | 75 | Let `t` be a `task<T>`. `co_await t` always suspends the awaiting | |||||
| 76 | coroutine, then transfers control directly into the task's coroutine | 76 | coroutine, then transfers control directly into the task's coroutine | |||||
| 77 | body on the current thread; no executor operation is posted. The task | 77 | body on the current thread; no executor operation is posted. The task | |||||
| 78 | records the caller's environment (executor, stop token, and frame | 78 | records the caller's environment (executor, stop token, and frame | |||||
| 79 | allocator) by pointer rather than copying it. It propagates that | 79 | allocator) by pointer rather than copying it. It propagates that | |||||
| 80 | environment to every `co_await` inside the body. | 80 | environment to every `co_await` inside the body. | |||||
| 81 | 81 | |||||||
| 82 | The body runs until it returns or exits via an exception. Control | 82 | The body runs until it returns or exits via an exception. Control | |||||
| 83 | then transfers directly back to the awaiting coroutine, again | 83 | then transfers directly back to the awaiting coroutine, again | |||||
| 84 | without an executor operation. | 84 | without an executor operation. | |||||
| 85 | 85 | |||||||
| 86 | `task` never inspects the stop token; it only propagates it. A task | 86 | `task` never inspects the stop token; it only propagates it. A task | |||||
| 87 | body observes a stop request through the results of the operations it | 87 | body observes a stop request through the results of the operations it | |||||
| 88 | awaits, or by reading the token itself. | 88 | awaits, or by reading the token itself. | |||||
| 89 | 89 | |||||||
| 90 | @par Await-returns | 90 | @par Await-returns | |||||
| 91 | The value the body passed to `co_return`, moved out of the task, or | 91 | The value the body passed to `co_return`, moved out of the task, or | |||||
| 92 | nothing when `T` is `void`. | 92 | nothing when `T` is `void`. | |||||
| 93 | 93 | |||||||
| 94 | If the body exits via an unhandled exception, that exception is | 94 | If the body exits via an unhandled exception, that exception is | |||||
| 95 | rethrown instead. | 95 | rethrown instead. | |||||
| 96 | 96 | |||||||
| 97 | @par Await-postcondition | 97 | @par Await-postcondition | |||||
| 98 | The task's coroutine has run to completion and is suspended at its | 98 | The task's coroutine has run to completion and is suspended at its | |||||
| 99 | final suspend point. The task still owns the frame, but not the | 99 | final suspend point. The task still owns the frame, but not the | |||||
| 100 | result: the await moves it out, so a task must not be awaited twice. | 100 | result: the await moves it out, so a task must not be awaited twice. | |||||
| 101 | 101 | |||||||
| 102 | @par Thread Safety | 102 | @par Thread Safety | |||||
| 103 | Distinct objects: Safe. | 103 | Distinct objects: Safe. | |||||
| 104 | Shared objects: Unsafe. | 104 | Shared objects: Unsafe. | |||||
| 105 | 105 | |||||||
| 106 | @par Example | 106 | @par Example | |||||
| 107 | 107 | |||||||
| 108 | @par !example example | 108 | @par !example example | |||||
| 109 | 109 | |||||||
| 110 | 110 | |||||||
| 111 | @tparam T The result type. Use `task<>` for `task<void>`. | 111 | @tparam T The result type. Use `task<>` for `task<void>`. | |||||
| 112 | 112 | |||||||
| 113 | @see @ref ContextRunnable, @ref ContextAwaitable, @ref run, @ref run_async | 113 | @see @ref ContextRunnable, @ref ContextAwaitable, @ref run, @ref run_async | |||||
| 114 | */ | 114 | */ | |||||
| 115 | template<typename T = void> | 115 | template<typename T = void> | |||||
| 116 | struct [[nodiscard]] BOOST_CAPY_CORO_AWAIT_ELIDABLE | 116 | struct [[nodiscard]] BOOST_CAPY_CORO_AWAIT_ELIDABLE | |||||
| 117 | task | 117 | task | |||||
| 118 | { | 118 | { | |||||
| 119 | /** Stores `task<T>`'s result and joins the I/O awaitable protocol via `context_awaitable_promise_base`. | 119 | /** Stores `task<T>`'s result and joins the I/O awaitable protocol via `context_awaitable_promise_base`. | |||||
| 120 | 120 | |||||||
| 121 | This is the promise object the compiler associates with a | 121 | This is the promise object the compiler associates with a | |||||
| 122 | `task<T>` coroutine. It satisfies the coroutine promise | 122 | `task<T>` coroutine. It satisfies the coroutine promise | |||||
| 123 | requirements and participates in the I/O awaitable protocol via | 123 | requirements and participates in the I/O awaitable protocol via | |||||
| 124 | @ref context_awaitable_promise_base. It is part of the coroutine | 124 | @ref context_awaitable_promise_base. It is part of the coroutine | |||||
| 125 | machinery and is not intended to be used directly by callers. | 125 | machinery and is not intended to be used directly by callers. | |||||
| 126 | 126 | |||||||
| 127 | Result storage and `return_value`/`return_void` are provided by | 127 | Result storage and `return_value`/`return_void` are provided by | |||||
| 128 | `detail::task_return_base<T>`. | 128 | `detail::task_return_base<T>`. | |||||
| 129 | 129 | |||||||
| 130 | @see @ref context_awaitable_promise_base, @ref ContextRunnable | 130 | @see @ref context_awaitable_promise_base, @ref ContextRunnable | |||||
| 131 | */ | 131 | */ | |||||
| 132 | struct promise_type | 132 | struct promise_type | |||||
| 133 | : context_awaitable_promise_base<promise_type> | 133 | : context_awaitable_promise_base<promise_type> | |||||
| 134 | , detail::task_return_base<T> | 134 | , detail::task_return_base<T> | |||||
| 135 | { | 135 | { | |||||
| 136 | private: | 136 | private: | |||||
| 137 | friend task; | 137 | friend task; | |||||
| 138 | union { std::exception_ptr ep_; }; | 138 | union { std::exception_ptr ep_; }; | |||||
| 139 | bool has_ep_; | 139 | bool has_ep_; | |||||
| 140 | 140 | |||||||
| 141 | public: | 141 | public: | |||||
| 142 | /// Construct the promise with no stored exception. | 142 | /// Construct the promise with no stored exception. | |||||
| HITCBC | 143 | 109251 | promise_type() noexcept | 143 | 109256 | promise_type() noexcept | ||
| HITCBC | 144 | 109251 | : has_ep_(false) | 144 | 109256 | : has_ep_(false) | ||
| 145 | { | 145 | { | |||||
| HITCBC | 146 | 109251 | } | 146 | 109256 | } | ||
| 147 | 147 | |||||||
| 148 | /// Destroy the promise, releasing any stored exception. | 148 | /// Destroy the promise, releasing any stored exception. | |||||
| HITCBC | 149 | 109251 | ~promise_type() | 149 | 109256 | ~promise_type() | ||
| 150 | { | 150 | { | |||||
| HITCBC | 151 | 109251 | if(has_ep_) | 151 | 109256 | if(has_ep_) | ||
| HITCBC | 152 | 489 | ep_.~exception_ptr(); | 152 | 489 | ep_.~exception_ptr(); | ||
| HITCBC | 153 | 109251 | } | 153 | 109256 | } | ||
| 154 | 154 | |||||||
| 155 | /** Return the exception captured by the coroutine body, if any. | 155 | /** Return the exception captured by the coroutine body, if any. | |||||
| 156 | 156 | |||||||
| 157 | @return The stored exception, or a null `std::exception_ptr` | 157 | @return The stored exception, or a null `std::exception_ptr` | |||||
| 158 | if the coroutine did not exit via an unhandled exception. | 158 | if the coroutine did not exit via an unhandled exception. | |||||
| 159 | */ | 159 | */ | |||||
| HITCBC | 160 | 5030 | std::exception_ptr exception() const noexcept | 160 | 5041 | std::exception_ptr exception() const noexcept | ||
| 161 | { | 161 | { | |||||
| HITCBC | 162 | 5030 | if(has_ep_) | 162 | 5041 | if(has_ep_) | ||
| HITCBC | 163 | 732 | return ep_; | 163 | 732 | return ep_; | ||
| HITCBC | 164 | 4298 | return {}; | 164 | 4309 | return {}; | ||
| 165 | } | 165 | } | |||||
| 166 | 166 | |||||||
| 167 | /** Return the owning `task` for this coroutine. | 167 | /** Return the owning `task` for this coroutine. | |||||
| 168 | 168 | |||||||
| 169 | Called by the compiler to produce the object returned to the | 169 | Called by the compiler to produce the object returned to the | |||||
| 170 | caller when the coroutine is created. | 170 | caller when the coroutine is created. | |||||
| 171 | 171 | |||||||
| 172 | @return A `task` owning the coroutine frame. | 172 | @return A `task` owning the coroutine frame. | |||||
| 173 | */ | 173 | */ | |||||
| HITCBC | 174 | 109251 | task get_return_object() | 174 | 109256 | task get_return_object() | ||
| 175 | { | 175 | { | |||||
| HITCBC | 176 | 109251 | return task{std::coroutine_handle<promise_type>::from_promise(*this)}; | 176 | 109256 | return task{std::coroutine_handle<promise_type>::from_promise(*this)}; | ||
| 177 | } | 177 | } | |||||
| 178 | 178 | |||||||
| 179 | /** Return the initial-suspend awaiter. | 179 | /** Return the initial-suspend awaiter. | |||||
| 180 | 180 | |||||||
| 181 | The coroutine always suspends at the initial suspend point, | 181 | The coroutine always suspends at the initial suspend point, | |||||
| 182 | so the body does not start until the task is awaited. When the | 182 | so the body does not start until the task is awaited. When the | |||||
| 183 | body is resumed, the awaiter restores the thread-local frame | 183 | body is resumed, the awaiter restores the thread-local frame | |||||
| 184 | allocator from the stored environment. | 184 | allocator from the stored environment. | |||||
| 185 | 185 | |||||||
| 186 | @return An awaiter that suspends unconditionally. | 186 | @return An awaiter that suspends unconditionally. | |||||
| 187 | */ | 187 | */ | |||||
| HITCBC | 188 | 109251 | auto initial_suspend() noexcept | 188 | 109256 | auto initial_suspend() noexcept | ||
| 189 | { | 189 | { | |||||
| 190 | struct awaiter | 190 | struct awaiter | |||||
| 191 | { | 191 | { | |||||
| 192 | promise_type* p_; | 192 | promise_type* p_; | |||||
| 193 | 193 | |||||||
| HITCBC | 194 | 109251 | bool await_ready() const noexcept | 194 | 109256 | bool await_ready() const noexcept | ||
| 195 | { | 195 | { | |||||
| HITCBC | 196 | 109251 | return false; | 196 | 109256 | return false; | ||
| 197 | } | 197 | } | |||||
| 198 | 198 | |||||||
| HITCBC | 199 | 109251 | void await_suspend(std::coroutine_handle<>) const noexcept | 199 | 109256 | void await_suspend(std::coroutine_handle<>) const noexcept | ||
| 200 | { | 200 | { | |||||
| HITCBC | 201 | 109251 | } | 201 | 109256 | } | ||
| 202 | 202 | |||||||
| HITCBC | 203 | 108168 | void await_resume() const noexcept | 203 | 108173 | void await_resume() const noexcept | ||
| 204 | { | 204 | { | |||||
| 205 | // Restore TLS when body starts executing | 205 | // Restore TLS when body starts executing | |||||
| HITCBC | 206 | 108168 | set_current_frame_allocator(p_->environment()->frame_allocator); | 206 | 108173 | set_current_frame_allocator(p_->environment()->frame_allocator); | ||
| HITCBC | 207 | 108168 | } | 207 | 108173 | } | ||
| 208 | }; | 208 | }; | |||||
| HITCBC | 209 | 109251 | return awaiter{this}; | 209 | 109256 | return awaiter{this}; | ||
| 210 | } | 210 | } | |||||
| 211 | 211 | |||||||
| 212 | /** Return the final-suspend awaiter. | 212 | /** Return the final-suspend awaiter. | |||||
| 213 | 213 | |||||||
| 214 | The coroutine always suspends at the final suspend point. The | 214 | The coroutine always suspends at the final suspend point. The | |||||
| 215 | awaiter's `await_suspend` performs symmetric transfer to the | 215 | awaiter's `await_suspend` performs symmetric transfer to the | |||||
| 216 | stored continuation (consuming it), resuming the awaiting | 216 | stored continuation (consuming it), resuming the awaiting | |||||
| 217 | coroutine. | 217 | coroutine. | |||||
| 218 | 218 | |||||||
| 219 | @return An awaiter that suspends and transfers to the | 219 | @return An awaiter that suspends and transfers to the | |||||
| 220 | continuation. | 220 | continuation. | |||||
| 221 | */ | 221 | */ | |||||
| HITCBC | 222 | 5539 | auto final_suspend() noexcept | 222 | 5550 | auto final_suspend() noexcept | ||
| 223 | { | 223 | { | |||||
| 224 | struct awaiter | 224 | struct awaiter | |||||
| 225 | { | 225 | { | |||||
| 226 | promise_type* p_; | 226 | promise_type* p_; | |||||
| 227 | 227 | |||||||
| HITCBC | 228 | 5539 | bool await_ready() const noexcept | 228 | 5550 | bool await_ready() const noexcept | ||
| 229 | { | 229 | { | |||||
| HITCBC | 230 | 5539 | return false; | 230 | 5550 | return false; | ||
| 231 | } | 231 | } | |||||
| 232 | 232 | |||||||
| HITCBC | 233 | 5539 | std::coroutine_handle<> await_suspend(std::coroutine_handle<>) const noexcept | 233 | 5550 | std::coroutine_handle<> await_suspend(std::coroutine_handle<>) const noexcept | ||
| 234 | { | 234 | { | |||||
| HITCBC | 235 | 5539 | return p_->continuation(); | 235 | 5550 | return p_->continuation(); | ||
| 236 | } | 236 | } | |||||
| 237 | 237 | |||||||
| 238 | void await_resume() const noexcept {} // LCOV_EXCL_LINE final_suspend awaiter, never resumed | 238 | void await_resume() const noexcept {} // LCOV_EXCL_LINE final_suspend awaiter, never resumed | |||||
| 239 | }; | 239 | }; | |||||
| HITCBC | 240 | 5539 | return awaiter{this}; | 240 | 5550 | return awaiter{this}; | ||
| 241 | } | 241 | } | |||||
| 242 | 242 | |||||||
| 243 | /** Capture the in-flight exception from the coroutine body. | 243 | /** Capture the in-flight exception from the coroutine body. | |||||
| 244 | 244 | |||||||
| 245 | Called by the compiler when the coroutine body exits via an | 245 | Called by the compiler when the coroutine body exits via an | |||||
| 246 | unhandled exception. The captured exception is rethrown when | 246 | unhandled exception. The captured exception is rethrown when | |||||
| 247 | the task is awaited. | 247 | the task is awaited. | |||||
| 248 | */ | 248 | */ | |||||
| HITCBC | 249 | 489 | void unhandled_exception() noexcept | 249 | 489 | void unhandled_exception() noexcept | ||
| 250 | { | 250 | { | |||||
| HITCBC | 251 | 489 | new (&ep_) std::exception_ptr(std::current_exception()); | 251 | 489 | new (&ep_) std::exception_ptr(std::current_exception()); | ||
| HITCBC | 252 | 489 | has_ep_ = true; | 252 | 489 | has_ep_ = true; | ||
| HITCBC | 253 | 489 | } | 253 | 489 | } | ||
| 254 | 254 | |||||||
| 255 | /** Transform a nested awaitable before `co_await`. | 255 | /** Transform a nested awaitable before `co_await`. | |||||
| 256 | 256 | |||||||
| 257 | Wraps an @ref ContextAwaitable in an awaiter so the | 257 | Wraps an @ref ContextAwaitable in an awaiter so the | |||||
| 258 | coroutine's environment is propagated into it. A diagnostic | 258 | coroutine's environment is propagated into it. A diagnostic | |||||
| 259 | is emitted if the awaitable does not satisfy @ref ContextAwaitable. | 259 | is emitted if the awaitable does not satisfy @ref ContextAwaitable. | |||||
| 260 | 260 | |||||||
| 261 | @param a The awaitable expression from `co_await a`. | 261 | @param a The awaitable expression from `co_await a`. | |||||
| 262 | 262 | |||||||
| 263 | @return The wrapped awaitable. | 263 | @return The wrapped awaitable. | |||||
| 264 | */ | 264 | */ | |||||
| 265 | template<class Awaitable> | 265 | template<class Awaitable> | |||||
| HITCBC | 266 | 108232 | auto transform_awaitable(Awaitable&& a) | 266 | 108237 | auto transform_awaitable(Awaitable&& a) | ||
| 267 | { | 267 | { | |||||
| 268 | using A = std::decay_t<Awaitable>; | 268 | using A = std::decay_t<Awaitable>; | |||||
| 269 | if constexpr (ContextAwaitable<A>) | 269 | if constexpr (ContextAwaitable<A>) | |||||
| 270 | { | 270 | { | |||||
| 271 | return detail::context_awaiter<A, promise_type>{ | 271 | return detail::context_awaiter<A, promise_type>{ | |||||
| HITCBC | 272 | 213943 | std::forward<Awaitable>(a), this}; | 272 | 213953 | std::forward<Awaitable>(a), this}; | ||
| 273 | } | 273 | } | |||||
| 274 | else | 274 | else | |||||
| 275 | { | 275 | { | |||||
| 276 | static_assert(ContextAwaitable<A>, "requires ContextAwaitable"); | 276 | static_assert(ContextAwaitable<A>, "requires ContextAwaitable"); | |||||
| 277 | } | 277 | } | |||||
| HITCBC | 278 | 105711 | } | 278 | 105716 | } | ||
| 279 | }; | 279 | }; | |||||
| 280 | 280 | |||||||
| 281 | /** Handle to the owned coroutine frame. | 281 | /** Handle to the owned coroutine frame. | |||||
| 282 | 282 | |||||||
| 283 | Null when the task is empty (for example after a move or after | 283 | Null when the task is empty (for example after a move or after | |||||
| 284 | @ref release). Prefer @ref handle to read this; the member is | 284 | @ref release). Prefer @ref handle to read this; the member is | |||||
| 285 | public for use by the coroutine machinery. | 285 | public for use by the coroutine machinery. | |||||
| 286 | */ | 286 | */ | |||||
| 287 | std::coroutine_handle<promise_type> h_; | 287 | std::coroutine_handle<promise_type> h_; | |||||
| 288 | 288 | |||||||
| 289 | /** Destroy the task and its coroutine frame if owned. | 289 | /** Destroy the task and its coroutine frame if owned. | |||||
| 290 | 290 | |||||||
| 291 | @par Preconditions | 291 | @par Preconditions | |||||
| 292 | The frame is not lent: no parent is suspended awaiting it, | 292 | The frame is not lent: no parent is suspended awaiting it, | |||||
| 293 | unless its chain is being unwound after an @ref abandon. | 293 | unless its chain is being unwound after an @ref abandon. | |||||
| 294 | Destroying a lent frame otherwise calls `std::terminate`; end | 294 | Destroying a lent frame otherwise calls `std::terminate`; end | |||||
| 295 | a running chain by requesting stop, or by destroying its | 295 | a running chain by requesting stop, or by destroying its | |||||
| 296 | execution context. | 296 | execution context. | |||||
| 297 | */ | 297 | */ | |||||
| HITCBC | 298 | 113331 | ~task() | 298 | 113336 | ~task() | ||
| 299 | { | 299 | { | |||||
| HITCBC | 300 | 113331 | destroy_frame(); | 300 | 113336 | destroy_frame(); | ||
| HITCBC | 301 | 113331 | } | 301 | 113336 | } | ||
| 302 | 302 | |||||||
| 303 | /** Report whether the awaited task is already complete. | 303 | /** Report whether the awaited task is already complete. | |||||
| 304 | 304 | |||||||
| 305 | Always returns `false`; a task is lazy and has not started when | 305 | Always returns `false`; a task is lazy and has not started when | |||||
| 306 | it is awaited, so the awaiting coroutine always suspends. | 306 | it is awaited, so the awaiting coroutine always suspends. | |||||
| 307 | 307 | |||||||
| 308 | @return `false`. | 308 | @return `false`. | |||||
| 309 | */ | 309 | */ | |||||
| HITCBC | 310 | 2112 | bool await_ready() const noexcept | 310 | 2112 | bool await_ready() const noexcept | ||
| 311 | { | 311 | { | |||||
| HITCBC | 312 | 2112 | return false; | 312 | 2112 | return false; | ||
| 313 | } | 313 | } | |||||
| 314 | 314 | |||||||
| 315 | /** Return the task's result, rethrowing any captured exception. | 315 | /** Return the task's result, rethrowing any captured exception. | |||||
| 316 | 316 | |||||||
| 317 | If the coroutine body exited via an unhandled exception, that | 317 | If the coroutine body exited via an unhandled exception, that | |||||
| 318 | exception is rethrown here. Otherwise the result is returned by | 318 | exception is rethrown here. Otherwise the result is returned by | |||||
| 319 | move (for `task<T>`) or nothing is returned (for `task<void>`). | 319 | move (for `task<T>`) or nothing is returned (for `task<void>`). | |||||
| 320 | 320 | |||||||
| 321 | @return The result value for non-void `T`; otherwise `void`. | 321 | @return The result value for non-void `T`; otherwise `void`. | |||||
| 322 | 322 | |||||||
| 323 | @throws The exception captured by the coroutine body, if any. | 323 | @throws The exception captured by the coroutine body, if any. | |||||
| 324 | 324 | |||||||
| 325 | @note Discarding a `results` silently drops the error | 325 | @note Discarding a `results` silently drops the error | |||||
| 326 | code, so that overload is marked `[[nodiscard]]`. | 326 | code, so that overload is marked `[[nodiscard]]`. | |||||
| 327 | */ | 327 | */ | |||||
| HITCBC | 328 | 568 | [[nodiscard]] auto await_resume() | 328 | 568 | [[nodiscard]] auto await_resume() | ||
| 329 | requires detail::is_results_v<T> | 329 | requires detail::is_results_v<T> | |||||
| 330 | { | 330 | { | |||||
| HITCBC | 331 | 568 | lent_ = false; | 331 | 568 | lent_ = false; | ||
| HITCBC | 332 | 568 | if(h_.promise().has_ep_) | 332 | 568 | if(h_.promise().has_ep_) | ||
| HITCBC | 333 | 105 | std::rethrow_exception(h_.promise().ep_); | 333 | 105 | std::rethrow_exception(h_.promise().ep_); | ||
| HITCBC | 334 | 463 | return std::move(*h_.promise().result_); | 334 | 463 | return std::move(*h_.promise().result_); | ||
| 335 | } | 335 | } | |||||
| 336 | 336 | |||||||
| HITCBC | 337 | 244 | auto await_resume() | 337 | 244 | auto await_resume() | ||
| 338 | requires (! detail::is_results_v<T>) | 338 | requires (! detail::is_results_v<T>) | |||||
| 339 | { | 339 | { | |||||
| HITCBC | 340 | 244 | lent_ = false; | 340 | 244 | lent_ = false; | ||
| HITCBC | 341 | 244 | if(h_.promise().has_ep_) | 341 | 244 | if(h_.promise().has_ep_) | ||
| HITCBC | 342 | 17 | std::rethrow_exception(h_.promise().ep_); | 342 | 17 | std::rethrow_exception(h_.promise().ep_); | ||
| 343 | if constexpr (! std::is_void_v<T>) | 343 | if constexpr (! std::is_void_v<T>) | |||||
| HITCBC | 344 | 152 | return std::move(*h_.promise().result_); | 344 | 152 | return std::move(*h_.promise().result_); | ||
| 345 | else | 345 | else | |||||
| HITCBC | 346 | 75 | return; | 346 | 75 | return; | ||
| 347 | } | 347 | } | |||||
| 348 | 348 | |||||||
| 349 | /** Start the task with the awaiting coroutine's context. | 349 | /** Start the task with the awaiting coroutine's context. | |||||
| 350 | 350 | |||||||
| 351 | Stores `cont` as the continuation to resume on completion. | 351 | Stores `cont` as the continuation to resume on completion. | |||||
| 352 | Stores `env` as the execution environment propagated to nested | 352 | Stores `env` as the execution environment propagated to nested | |||||
| 353 | `co_await` expressions. Then transfers control into the task's | 353 | `co_await` expressions. Then transfers control into the task's | |||||
| 354 | coroutine body via the returned handle. | 354 | coroutine body via the returned handle. | |||||
| 355 | 355 | |||||||
| 356 | @param cont The awaiting coroutine to resume when the task | 356 | @param cont The awaiting coroutine to resume when the task | |||||
| 357 | completes. | 357 | completes. | |||||
| 358 | 358 | |||||||
| 359 | @param env The execution environment (executor, stop token, and | 359 | @param env The execution environment (executor, stop token, and | |||||
| 360 | frame allocator). It must outlive the task. | 360 | frame allocator). It must outlive the task. | |||||
| 361 | 361 | |||||||
| 362 | @return The task's coroutine handle, for symmetric transfer. | 362 | @return The task's coroutine handle, for symmetric transfer. | |||||
| 363 | */ | 363 | */ | |||||
| HITCBC | 364 | 2009 | std::coroutine_handle<> await_suspend(std::coroutine_handle<> cont, await_context const* env) | 364 | 2009 | std::coroutine_handle<> await_suspend(std::coroutine_handle<> cont, await_context const* env) | ||
| 365 | { | 365 | { | |||||
| HITCBC | 366 | 2009 | h_.promise().set_continuation(cont); | 366 | 2009 | h_.promise().set_continuation(cont); | ||
| HITCBC | 367 | 2009 | h_.promise().set_environment(env); | 367 | 2009 | h_.promise().set_environment(env); | ||
| HITCBC | 368 | 2009 | lent_ = true; | 368 | 2009 | lent_ = true; | ||
| HITCBC | 369 | 2009 | return h_; | 369 | 2009 | return h_; | ||
| 370 | } | 370 | } | |||||
| 371 | 371 | |||||||
| 372 | /** Return the coroutine handle. | 372 | /** Return the coroutine handle. | |||||
| 373 | 373 | |||||||
| 374 | @note Do not call `destroy()` on the returned handle while the | 374 | @note Do not call `destroy()` on the returned handle while the | |||||
| 375 | task is being awaited: the awaiting frame still holds it and | 375 | task is being awaited: the awaiting frame still holds it and | |||||
| 376 | destroys it again, which has undefined behavior. A holder that | 376 | destroys it again, which has undefined behavior. A holder that | |||||
| 377 | gives the coroutine up passes its continuation to @ref abandon | 377 | gives the coroutine up passes its continuation to @ref abandon | |||||
| 378 | instead. (Destroying or | 378 | instead. (Destroying or | |||||
| 379 | move-assigning over the task object itself while its frame is | 379 | move-assigning over the task object itself while its frame is | |||||
| 380 | lent is a different operation, and calls `std::terminate`.) | 380 | lent is a different operation, and calls `std::terminate`.) | |||||
| 381 | The task's lifetime is normally managed by `run_async`, `run`, | 381 | The task's lifetime is normally managed by `run_async`, `run`, | |||||
| 382 | or the awaiting parent. For cooperative cancellation, use | 382 | or the awaiting parent. For cooperative cancellation, use | |||||
| 383 | `std::stop_token`. | 383 | `std::stop_token`. | |||||
| 384 | 384 | |||||||
| 385 | @note The operation, queue or primitive holding a suspended | 385 | @note The operation, queue or primitive holding a suspended | |||||
| 386 | coroutine gives it up with @ref abandon, through the | 386 | coroutine gives it up with @ref abandon, through the | |||||
| 387 | continuation it keeps, never by destroying a handle. | 387 | continuation it keeps, never by destroying a handle. | |||||
| 388 | `handle()` belongs to the owning task object. | 388 | `handle()` belongs to the owning task object. | |||||
| 389 | 389 | |||||||
| 390 | @return The coroutine handle. | 390 | @return The coroutine handle. | |||||
| 391 | */ | 391 | */ | |||||
| HITCBC | 392 | 107271 | std::coroutine_handle<promise_type> handle() const noexcept | 392 | 107276 | std::coroutine_handle<promise_type> handle() const noexcept | ||
| 393 | { | 393 | { | |||||
| HITCBC | 394 | 107271 | return h_; | 394 | 107276 | return h_; | ||
| 395 | } | 395 | } | |||||
| 396 | 396 | |||||||
| 397 | /** Release ownership of the coroutine frame. | 397 | /** Release ownership of the coroutine frame. | |||||
| 398 | 398 | |||||||
| 399 | After calling this, destroying the task does not destroy the | 399 | After calling this, destroying the task does not destroy the | |||||
| 400 | coroutine frame. The caller becomes responsible for the frame's | 400 | coroutine frame. The caller becomes responsible for the frame's | |||||
| 401 | lifetime. | 401 | lifetime. | |||||
| 402 | 402 | |||||||
| 403 | @note The caller may call `destroy()` on the released handle | 403 | @note The caller may call `destroy()` on the released handle | |||||
| 404 | only when the task has not started or has fully completed. | 404 | only when the task has not started or has fully completed. | |||||
| 405 | Destroying the released handle of a task that is being | 405 | Destroying the released handle of a task that is being | |||||
| 406 | awaited is not supported and has undefined behavior. | 406 | awaited is not supported and has undefined behavior. | |||||
| 407 | 407 | |||||||
| 408 | @par Postconditions | 408 | @par Postconditions | |||||
| 409 | `handle()` returns a null handle. Callers needing the | 409 | `handle()` returns a null handle. Callers needing the | |||||
| 410 | original handle must save it, via @ref handle, before | 410 | original handle must save it, via @ref handle, before | |||||
| 411 | calling this. | 411 | calling this. | |||||
| 412 | */ | 412 | */ | |||||
| HITCBC | 413 | 107128 | void release() noexcept | 413 | 107133 | void release() noexcept | ||
| 414 | { | 414 | { | |||||
| HITCBC | 415 | 107128 | h_ = nullptr; | 415 | 107133 | h_ = nullptr; | ||
| HITCBC | 416 | 107128 | } | 416 | 107133 | } | ||
| 417 | 417 | |||||||
| 418 | /** Copy construction is disabled; a task uniquely owns its frame. | 418 | /** Copy construction is disabled; a task uniquely owns its frame. | |||||
| 419 | 419 | |||||||
| 420 | @param other The task that would be copied. | 420 | @param other The task that would be copied. | |||||
| 421 | */ | 421 | */ | |||||
| 422 | task(task const& other) = delete; | 422 | task(task const& other) = delete; | |||||
| 423 | 423 | |||||||
| 424 | /** Copy assignment is disabled; a task uniquely owns its frame. | 424 | /** Copy assignment is disabled; a task uniquely owns its frame. | |||||
| 425 | 425 | |||||||
| 426 | @param other The task that would be assigned from. | 426 | @param other The task that would be assigned from. | |||||
| 427 | 427 | |||||||
| 428 | @return A reference to `*this`. | 428 | @return A reference to `*this`. | |||||
| 429 | */ | 429 | */ | |||||
| 430 | task& operator=(task const& other) = delete; | 430 | task& operator=(task const& other) = delete; | |||||
| 431 | 431 | |||||||
| 432 | /** Construct by moving, transferring ownership of the frame. | 432 | /** Construct by moving, transferring ownership of the frame. | |||||
| 433 | 433 | |||||||
| 434 | @par Postconditions | 434 | @par Postconditions | |||||
| 435 | `other` is empty and must not be awaited. | 435 | `other` is empty and must not be awaited. | |||||
| 436 | 436 | |||||||
| 437 | @param other The task to move from. | 437 | @param other The task to move from. | |||||
| 438 | */ | 438 | */ | |||||
| HITCBC | 439 | 4080 | task(task&& other) noexcept | 439 | 4080 | task(task&& other) noexcept | ||
| HITCBC | 440 | 4080 | : h_(std::exchange(other.h_, nullptr)) | 440 | 4080 | : h_(std::exchange(other.h_, nullptr)) | ||
| 441 | { | 441 | { | |||||
| HITCBC | 442 | 4080 | } | 442 | 4080 | } | ||
| 443 | 443 | |||||||
| 444 | /** Assign by moving, transferring ownership of the frame. | 444 | /** Assign by moving, transferring ownership of the frame. | |||||
| 445 | 445 | |||||||
| 446 | If this task already owns a coroutine frame, that frame is | 446 | If this task already owns a coroutine frame, that frame is | |||||
| 447 | destroyed first. Self-assignment is a no-op. | 447 | destroyed first. Self-assignment is a no-op. | |||||
| 448 | 448 | |||||||
| 449 | @par Preconditions | 449 | @par Preconditions | |||||
| 450 | The frame being replaced is not lent: no parent is suspended | 450 | The frame being replaced is not lent: no parent is suspended | |||||
| 451 | awaiting it. Replacing a lent frame calls `std::terminate`. | 451 | awaiting it. Replacing a lent frame calls `std::terminate`. | |||||
| 452 | 452 | |||||||
| 453 | @par Postconditions | 453 | @par Postconditions | |||||
| 454 | `other` is empty and must not be awaited. | 454 | `other` is empty and must not be awaited. | |||||
| 455 | 455 | |||||||
| 456 | @param other The task to move from. | 456 | @param other The task to move from. | |||||
| 457 | 457 | |||||||
| 458 | @return A reference to `*this`. | 458 | @return A reference to `*this`. | |||||
| 459 | */ | 459 | */ | |||||
| HITCBC | 460 | 1 | task& operator=(task&& other) noexcept | 460 | 1 | task& operator=(task&& other) noexcept | ||
| 461 | { | 461 | { | |||||
| HITCBC | 462 | 1 | if(this != &other) | 462 | 1 | if(this != &other) | ||
| 463 | { | 463 | { | |||||
| HITCBC | 464 | 1 | destroy_frame(); | 464 | 1 | destroy_frame(); | ||
| HITCBC | 465 | 1 | h_ = std::exchange(other.h_, nullptr); | 465 | 1 | h_ = std::exchange(other.h_, nullptr); | ||
| 466 | } | 466 | } | |||||
| HITCBC | 467 | 1 | return *this; | 467 | 1 | return *this; | ||
| 468 | } | 468 | } | |||||
| 469 | 469 | |||||||
| 470 | private: | 470 | private: | |||||
| 471 | // Set while a parent is suspended awaiting this task. | 471 | // Set while a parent is suspended awaiting this task. | |||||
| 472 | bool lent_ = false; | 472 | bool lent_ = false; | |||||
| 473 | 473 | |||||||
| HITCBC | 474 | 109251 | explicit task(std::coroutine_handle<promise_type> h) | 474 | 109256 | explicit task(std::coroutine_handle<promise_type> h) | ||
| HITCBC | 475 | 109251 | : h_(h) | 475 | 109256 | : h_(h) | ||
| 476 | { | 476 | { | |||||
| HITCBC | 477 | 109251 | } | 477 | 109256 | } | ||
| 478 | 478 | |||||||
| HITCBC | 479 | 113332 | void destroy_frame() noexcept | 479 | 113337 | void destroy_frame() noexcept | ||
| 480 | { | 480 | { | |||||
| HITCBC | 481 | 113332 | if(!h_) | 481 | 113337 | if(!h_) | ||
| HITCBC | 482 | 111209 | return; | 482 | 111214 | return; | ||
| HITCBC | 483 | 2123 | if(lent_ && !h_.done()) | 483 | 2123 | if(lent_ && !h_.done()) | ||
| 484 | { | 484 | { | |||||
| 485 | // Only the unwind of an abandoned chain destroys a frame | 485 | // Only the unwind of an abandoned chain destroys a frame | |||||
| 486 | // that is still awaited. | 486 | // that is still awaited. | |||||
| HITCBC | 487 | 1278 | auto* owner = h_.promise().environment()->owner; | 487 | 1278 | auto* owner = h_.promise().environment()->owner; | ||
| HITCBC | 488 | 1278 | if(!owner || !owner->abandoned()) | 488 | 1278 | if(!owner || !owner->abandoned()) | ||
| 489 | std::terminate(); // LCOV_EXCL_LINE reached only in a death test's child process | 489 | std::terminate(); // LCOV_EXCL_LINE reached only in a death test's child process | |||||
| 490 | } | 490 | } | |||||
| HITCBC | 491 | 2123 | std::exchange(h_, nullptr).destroy(); | 491 | 2123 | std::exchange(h_, nullptr).destroy(); | ||
| HITCBC | 492 | 2123 | lent_ = false; | 492 | 2123 | lent_ = false; | ||
| 493 | } | 493 | } | |||||
| 494 | }; | 494 | }; | |||||
| 495 | 495 | |||||||
| 496 | } // namespace capy | 496 | } // namespace capy | |||||
| 497 | } // namespace boost | 497 | } // namespace boost | |||||
| 498 | 498 | |||||||
| 499 | #endif | 499 | #endif | |||||