100.00% Lines (80/80) 100.00% Functions (28/28)
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/io_awaitable.hpp> 16   #include <boost/capy/concept/io_awaitable.hpp>
17   #include <boost/capy/ex/io_awaitable_promise_base.hpp> 17   #include <boost/capy/ex/io_awaitable_promise_base.hpp>
18   #include <boost/capy/ex/io_env.hpp> 18   #include <boost/capy/ex/io_env.hpp>
19   #include <boost/capy/ex/frame_allocator.hpp> 19   #include <boost/capy/ex/frame_allocator.hpp>
20   #include <boost/capy/detail/await_suspend_helper.hpp> 20   #include <boost/capy/detail/await_suspend_helper.hpp>
21   #include <boost/capy/io_result.hpp> 21   #include <boost/capy/io_result.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   870 void return_value(T value) 40   870 void return_value(T value)
41   { 41   {
HITCBC 42   870 result_ = std::move(value); 42   870 result_ = std::move(value);
HITCBC 43   870 } 43   870 }
44   44  
HITCBC 45   273 T&& result() noexcept 45   273 T&& result() noexcept
46   { 46   {
HITCBC 47   273 return std::move(*result_); 47   273 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   1251 void return_void() 54   1271 void return_void()
55   { 55   {
HITCBC 56   1251 } 56   1271 }
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. See @ref quitter for a task 88   awaits, or by reading the token itself. See @ref quitter for a task
89   that stops its own body. 89   that stops its own body.
90   90  
91   @par Await-returns 91   @par Await-returns
92   The value the body passed to `co_return`, moved out of the task, or 92   The value the body passed to `co_return`, moved out of the task, or
93   nothing when `T` is `void`. 93   nothing when `T` is `void`.
94   94  
95   If the body exits via an unhandled exception, that exception is 95   If the body exits via an unhandled exception, that exception is
96   rethrown instead. 96   rethrown instead.
97   97  
98   @par Await-postcondition 98   @par Await-postcondition
99   The task's coroutine has run to completion and is suspended at its 99   The task's coroutine has run to completion and is suspended at its
100   final suspend point. The task still owns the frame, but not the 100   final suspend point. The task still owns the frame, but not the
101   result: the await moves it out, so a task must not be awaited twice. 101   result: the await moves it out, so a task must not be awaited twice.
102   102  
103   @par Thread Safety 103   @par Thread Safety
104   Distinct objects: Safe. 104   Distinct objects: Safe.
105   Shared objects: Unsafe. 105   Shared objects: Unsafe.
106   106  
107   @par Example 107   @par Example
108   108  
109 - @code 109 + @par !example example
110 - task<int> compute_value()  
111 - {  
112 - auto [ec, n] = co_await stream.read_some( buf );  
113 - if( ec )  
114 - co_return 0;  
115 - co_return process( buf, n );  
116 - }  
117 - task<> run_session( tcp_socket sock )  
118 - {  
119 - int result = co_await compute_value();  
120 - // ...  
121 - }  
122 - @endcode  
123   110  
124   111  
125   @tparam T The result type. Use `task<>` for `task<void>`. 112   @tparam T The result type. Use `task<>` for `task<void>`.
126   113  
127   @see IoRunnable, IoAwaitable, run, run_async 114   @see IoRunnable, IoAwaitable, run, run_async
128   */ 115   */
129   template<typename T = void> 116   template<typename T = void>
130   struct [[nodiscard]] BOOST_CAPY_CORO_AWAIT_ELIDABLE 117   struct [[nodiscard]] BOOST_CAPY_CORO_AWAIT_ELIDABLE
131   task 118   task
132   { 119   {
133   /** Stores `task<T>`'s result and joins the I/O awaitable protocol via `io_awaitable_promise_base`. 120   /** Stores `task<T>`'s result and joins the I/O awaitable protocol via `io_awaitable_promise_base`.
134   121  
135   This is the promise object the compiler associates with a 122   This is the promise object the compiler associates with a
136   `task<T>` coroutine. It satisfies the coroutine promise 123   `task<T>` coroutine. It satisfies the coroutine promise
137   requirements and participates in the I/O awaitable protocol via 124   requirements and participates in the I/O awaitable protocol via
138   @ref io_awaitable_promise_base. It is part of the coroutine 125   @ref io_awaitable_promise_base. It is part of the coroutine
139   machinery and is not intended to be used directly by callers. 126   machinery and is not intended to be used directly by callers.
140   127  
141   Result storage and `return_value`/`return_void` are provided by 128   Result storage and `return_value`/`return_void` are provided by
142   `detail::task_return_base<T>`. 129   `detail::task_return_base<T>`.
143   130  
144   @see io_awaitable_promise_base, IoRunnable 131   @see io_awaitable_promise_base, IoRunnable
145   */ 132   */
146   struct promise_type 133   struct promise_type
147   : io_awaitable_promise_base<promise_type> 134   : io_awaitable_promise_base<promise_type>
148   , detail::task_return_base<T> 135   , detail::task_return_base<T>
149   { 136   {
150   private: 137   private:
151   friend task; 138   friend task;
152   union { std::exception_ptr ep_; }; 139   union { std::exception_ptr ep_; };
153   bool has_ep_; 140   bool has_ep_;
154   141  
155   public: 142   public:
156   /// Construct the promise with no stored exception. 143   /// Construct the promise with no stored exception.
HITCBC 157   2760 promise_type() noexcept 144   2766 promise_type() noexcept
HITCBC 158   2760 : has_ep_(false) 145   2766 : has_ep_(false)
159   { 146   {
HITCBC 160   2760 } 147   2766 }
161   148  
162   /// Destroy the promise, releasing any stored exception. 149   /// Destroy the promise, releasing any stored exception.
HITCBC 163   2760 ~promise_type() 150   2766 ~promise_type()
164   { 151   {
HITCBC 165   2760 if(has_ep_) 152   2766 if(has_ep_)
HITCBC 166   489 ep_.~exception_ptr(); 153   489 ep_.~exception_ptr();
HITCBC 167   2760 } 154   2766 }
168   155  
169   /** Return the exception captured by the coroutine body, if any. 156   /** Return the exception captured by the coroutine body, if any.
170   157  
171   @return The stored exception, or a null `std::exception_ptr` 158   @return The stored exception, or a null `std::exception_ptr`
172   if the coroutine did not exit via an unhandled exception. 159   if the coroutine did not exit via an unhandled exception.
173   */ 160   */
HITCBC 174   2151 std::exception_ptr exception() const noexcept 161   2171 std::exception_ptr exception() const noexcept
175   { 162   {
HITCBC 176   2151 if(has_ep_) 163   2171 if(has_ep_)
HITCBC 177   730 return ep_; 164   730 return ep_;
HITCBC 178   1421 return {}; 165   1441 return {};
179   } 166   }
180   167  
181   /** Return the owning `task` for this coroutine. 168   /** Return the owning `task` for this coroutine.
182   169  
183   Called by the compiler to produce the object returned to the 170   Called by the compiler to produce the object returned to the
184   caller when the coroutine is created. 171   caller when the coroutine is created.
185   172  
186   @return A `task` owning the coroutine frame. 173   @return A `task` owning the coroutine frame.
187   */ 174   */
HITCBC 188   2760 task get_return_object() 175   2766 task get_return_object()
189   { 176   {
HITCBC 190   2760 return task{std::coroutine_handle<promise_type>::from_promise(*this)}; 177   2766 return task{std::coroutine_handle<promise_type>::from_promise(*this)};
191   } 178   }
192   179  
193   /** Return the initial-suspend awaiter. 180   /** Return the initial-suspend awaiter.
194   181  
195   The coroutine always suspends at the initial suspend point, 182   The coroutine always suspends at the initial suspend point,
196   so the body does not start until the task is awaited. When the 183   so the body does not start until the task is awaited. When the
197   body is resumed, the awaiter restores the thread-local frame 184   body is resumed, the awaiter restores the thread-local frame
198   allocator from the stored environment. 185   allocator from the stored environment.
199   186  
200   @return An awaiter that suspends unconditionally. 187   @return An awaiter that suspends unconditionally.
201   */ 188   */
HITCBC 202   2760 auto initial_suspend() noexcept 189   2766 auto initial_suspend() noexcept
203   { 190   {
204   struct awaiter 191   struct awaiter
205   { 192   {
206   promise_type* p_; 193   promise_type* p_;
207   194  
HITCBC 208   2760 bool await_ready() const noexcept 195   2766 bool await_ready() const noexcept
209   { 196   {
HITCBC 210   2760 return false; 197   2766 return false;
211   } 198   }
212   199  
HITCBC 213   2760 void await_suspend(std::coroutine_handle<>) const noexcept 200   2766 void await_suspend(std::coroutine_handle<>) const noexcept
214   { 201   {
HITCBC 215   2760 } 202   2766 }
216   203  
HITCBC 217   2756 void await_resume() const noexcept 204   2762 void await_resume() const noexcept
218   { 205   {
219   // Restore TLS when body starts executing 206   // Restore TLS when body starts executing
HITCBC 220   2756 set_current_frame_allocator(p_->environment()->frame_allocator); 207   2762 set_current_frame_allocator(p_->environment()->frame_allocator);
HITCBC 221   2756 } 208   2762 }
222   }; 209   };
HITCBC 223   2760 return awaiter{this}; 210   2766 return awaiter{this};
224   } 211   }
225   212  
226   /** Return the final-suspend awaiter. 213   /** Return the final-suspend awaiter.
227   214  
228   The coroutine always suspends at the final suspend point. The 215   The coroutine always suspends at the final suspend point. The
229   awaiter's `await_suspend` performs symmetric transfer to the 216   awaiter's `await_suspend` performs symmetric transfer to the
230   stored continuation (consuming it), resuming the awaiting 217   stored continuation (consuming it), resuming the awaiting
231   coroutine. 218   coroutine.
232   219  
233   @return An awaiter that suspends and transfers to the 220   @return An awaiter that suspends and transfers to the
234   continuation. 221   continuation.
235   */ 222   */
HITCBC 236   2610 auto final_suspend() noexcept 223   2630 auto final_suspend() noexcept
237   { 224   {
238   struct awaiter 225   struct awaiter
239   { 226   {
240   promise_type* p_; 227   promise_type* p_;
241   228  
HITCBC 242   2610 bool await_ready() const noexcept 229   2630 bool await_ready() const noexcept
243   { 230   {
HITCBC 244   2610 return false; 231   2630 return false;
245   } 232   }
246   233  
HITCBC 247   2610 std::coroutine_handle<> await_suspend(std::coroutine_handle<>) const noexcept 234   2630 std::coroutine_handle<> await_suspend(std::coroutine_handle<>) const noexcept
248   { 235   {
HITCBC 249   2610 return p_->continuation(); 236   2630 return p_->continuation();
250   } 237   }
251   238  
252   void await_resume() const noexcept {} // LCOV_EXCL_LINE final_suspend awaiter, never resumed 239   void await_resume() const noexcept {} // LCOV_EXCL_LINE final_suspend awaiter, never resumed
253   }; 240   };
HITCBC 254   2610 return awaiter{this}; 241   2630 return awaiter{this};
255   } 242   }
256   243  
257   /** Capture the in-flight exception from the coroutine body. 244   /** Capture the in-flight exception from the coroutine body.
258   245  
259   Called by the compiler when the coroutine body exits via an 246   Called by the compiler when the coroutine body exits via an
260   unhandled exception. The captured exception is rethrown when 247   unhandled exception. The captured exception is rethrown when
261   the task is awaited. 248   the task is awaited.
262   */ 249   */
HITCBC 263   489 void unhandled_exception() noexcept 250   489 void unhandled_exception() noexcept
264   { 251   {
HITCBC 265   489 new (&ep_) std::exception_ptr(std::current_exception()); 252   489 new (&ep_) std::exception_ptr(std::current_exception());
HITCBC 266   489 has_ep_ = true; 253   489 has_ep_ = true;
HITCBC 267   489 } 254   489 }
268   255  
269   /** Awaiter wrapping a nested `co_await` of an @ref IoAwaitable. 256   /** Awaiter wrapping a nested `co_await` of an @ref IoAwaitable.
270   257  
271   Forwards the environment to the inner awaitable's 258   Forwards the environment to the inner awaitable's
272   environment-taking `await_suspend` and restores the 259   environment-taking `await_suspend` and restores the
273   thread-local frame allocator before the body resumes. 260   thread-local frame allocator before the body resumes.
274   261  
275   @tparam Awaitable The awaitable being transformed. 262   @tparam Awaitable The awaitable being transformed.
276   */ 263   */
277   template<class Awaitable> 264   template<class Awaitable>
278   struct transform_awaiter 265   struct transform_awaiter
279   { 266   {
280   /// The wrapped awaitable, decayed and stored by value. 267   /// The wrapped awaitable, decayed and stored by value.
281   std::decay_t<Awaitable> a_; 268   std::decay_t<Awaitable> a_;
282   269  
283   /// The promise of the coroutine performing the `co_await`. 270   /// The promise of the coroutine performing the `co_await`.
284   promise_type* p_; 271   promise_type* p_;
285   272  
286   /** Report whether the wrapped awaitable is already complete. 273   /** Report whether the wrapped awaitable is already complete.
287   274  
288   @return The wrapped awaitable's own `await_ready` result: 275   @return The wrapped awaitable's own `await_ready` result:
289   `true` if no suspension is needed. 276   `true` if no suspension is needed.
290   */ 277   */
HITCBC 291   2871 bool await_ready() noexcept 278   2877 bool await_ready() noexcept
292   { 279   {
HITCBC 293   2871 return a_.await_ready(); 280   2877 return a_.await_ready();
294   } 281   }
295   282  
296   /** Restore the frame allocator, then resume the wrapped 283   /** Restore the frame allocator, then resume the wrapped
297   awaitable. 284   awaitable.
298   285  
299   Reinstalls the thread-local frame allocator from the stored 286   Reinstalls the thread-local frame allocator from the stored
300   environment before the body continues. This is needed 287   environment before the body continues. This is needed
301   because the resumption may arrive on a different thread 288   because the resumption may arrive on a different thread
302   than the one that suspended. 289   than the one that suspended.
303   290  
304   @return The wrapped awaitable's await-result, forwarded 291   @return The wrapped awaitable's await-result, forwarded
305   unchanged. 292   unchanged.
306   */ 293   */
HITCBC 307   2725 decltype(auto) await_resume() 294   2745 decltype(auto) await_resume()
308   { 295   {
309   // Restore TLS before body resumes 296   // Restore TLS before body resumes
HITCBC 310   2725 set_current_frame_allocator(p_->environment()->frame_allocator); 297   2745 set_current_frame_allocator(p_->environment()->frame_allocator);
HITCBC 311   2725 return a_.await_resume(); 298   2745 return a_.await_resume();
312   } 299   }
313   300  
314   /** Suspend by calling the wrapped awaitable with the 301   /** Suspend by calling the wrapped awaitable with the
315   environment. 302   environment.
316   303  
317   This is the plain `await_suspend` the compiler calls for the 304   This is the plain `await_suspend` the compiler calls for the
318   nested `co_await`. It forwards to the wrapped awaitable's 305   nested `co_await`. It forwards to the wrapped awaitable's
319   @ref IoAwaitable overload, supplying the promise's stored 306   @ref IoAwaitable overload, supplying the promise's stored
320   environment as the second argument. It then hands back 307   environment as the second argument. It then hands back
321   that call's result unchanged, so the wrapped awaitable's 308   that call's result unchanged, so the wrapped awaitable's
322   suspension decision, whatever form it takes, is preserved. 309   suspension decision, whatever form it takes, is preserved.
323   310  
324   @param h The coroutine performing the `co_await`. 311   @param h The coroutine performing the `co_await`.
325   312  
326   @return Whatever the wrapped awaitable's `await_suspend` 313   @return Whatever the wrapped awaitable's `await_suspend`
327   returns. When that is a `std::coroutine_handle<>`, the 314   returns. When that is a `std::coroutine_handle<>`, the
328   handle is routed through `detail::symmetric_transfer`. 315   handle is routed through `detail::symmetric_transfer`.
329   On MSVC that helper resumes the handle on the current 316   On MSVC that helper resumes the handle on the current
330   stack, and this function returns `void`, so the awaiting 317   stack, and this function returns `void`, so the awaiting
331   coroutine suspends unconditionally. On every other 318   coroutine suspends unconditionally. On every other
332   compiler the handle is returned unchanged for symmetric 319   compiler the handle is returned unchanged for symmetric
333   transfer. 320   transfer.
334   */ 321   */
335   template<class Promise> 322   template<class Promise>
HITCBC 336   2250 auto await_suspend(std::coroutine_handle<Promise> h) noexcept 323   2253 auto await_suspend(std::coroutine_handle<Promise> h) noexcept
337   { 324   {
338   using R = decltype(a_.await_suspend(h, p_->environment())); 325   using R = decltype(a_.await_suspend(h, p_->environment()));
339   if constexpr (std::is_same_v<R, std::coroutine_handle<>>) 326   if constexpr (std::is_same_v<R, std::coroutine_handle<>>)
HITCBC 340   1250 return detail::symmetric_transfer(a_.await_suspend(h, p_->environment())); 327   1253 return detail::symmetric_transfer(a_.await_suspend(h, p_->environment()));
341   else 328   else
HITCBC 342   1000 return a_.await_suspend(h, p_->environment()); 329   1000 return a_.await_suspend(h, p_->environment());
343   } 330   }
344   }; 331   };
345   332  
346   /** Transform a nested awaitable before `co_await`. 333   /** Transform a nested awaitable before `co_await`.
347   334  
348   Wraps an @ref IoAwaitable in a @ref transform_awaiter so the 335   Wraps an @ref IoAwaitable in a @ref transform_awaiter so the
349   coroutine's environment is propagated into it. A diagnostic 336   coroutine's environment is propagated into it. A diagnostic
350   is emitted if the awaitable does not satisfy @ref IoAwaitable. 337   is emitted if the awaitable does not satisfy @ref IoAwaitable.
351   338  
352   @param a The awaitable expression from `co_await a`. 339   @param a The awaitable expression from `co_await a`.
353   340  
354   @return A @ref transform_awaiter wrapping `a`. 341   @return A @ref transform_awaiter wrapping `a`.
355   */ 342   */
356   template<class Awaitable> 343   template<class Awaitable>
HITCBC 357   2871 auto transform_awaitable(Awaitable&& a) 344   2877 auto transform_awaitable(Awaitable&& a)
358   { 345   {
359   using A = std::decay_t<Awaitable>; 346   using A = std::decay_t<Awaitable>;
360   if constexpr (IoAwaitable<A>) 347   if constexpr (IoAwaitable<A>)
361   { 348   {
362   return transform_awaiter<Awaitable>{ 349   return transform_awaiter<Awaitable>{
HITCBC 363   4396 std::forward<Awaitable>(a), this}; 350   4408 std::forward<Awaitable>(a), this};
364   } 351   }
365   else 352   else
366   { 353   {
367   static_assert(sizeof(A) == 0, "requires IoAwaitable"); 354   static_assert(sizeof(A) == 0, "requires IoAwaitable");
368   } 355   }
HITCBC 369   1525 } 356   1531 }
370   }; 357   };
371   358  
372   /** Handle to the owned coroutine frame. 359   /** Handle to the owned coroutine frame.
373   360  
374   Null when the task is empty (for example after a move or after 361   Null when the task is empty (for example after a move or after
375   @ref release). Prefer @ref handle to read this; the member is 362   @ref release). Prefer @ref handle to read this; the member is
376   public for use by the coroutine machinery. 363   public for use by the coroutine machinery.
377   */ 364   */
378   std::coroutine_handle<promise_type> h_; 365   std::coroutine_handle<promise_type> h_;
379   366  
380   /// Destroy the task and its coroutine frame if owned. 367   /// Destroy the task and its coroutine frame if owned.
HITCBC 381   5850 ~task() 368   5856 ~task()
382   { 369   {
HITCBC 383   5850 if(h_) 370   5856 if(h_)
HITCBC 384   767 h_.destroy(); 371   767 h_.destroy();
HITCBC 385   5850 } 372   5856 }
386   373  
387   /** Report whether the awaited task is already complete. 374   /** Report whether the awaited task is already complete.
388   375  
389   Always returns `false`; a task is lazy and has not started when 376   Always returns `false`; a task is lazy and has not started when
390   it is awaited, so the awaiting coroutine always suspends. 377   it is awaited, so the awaiting coroutine always suspends.
391   378  
392   @return `false`. 379   @return `false`.
393   */ 380   */
HITCBC 394   764 bool await_ready() const noexcept 381   764 bool await_ready() const noexcept
395   { 382   {
HITCBC 396   764 return false; 383   764 return false;
397   } 384   }
398   385  
399   /** Return the task's result, rethrowing any captured exception. 386   /** Return the task's result, rethrowing any captured exception.
400   387  
401   If the coroutine body exited via an unhandled exception, that 388   If the coroutine body exited via an unhandled exception, that
402   exception is rethrown here. Otherwise the result is returned by 389   exception is rethrown here. Otherwise the result is returned by
403   move (for `task<T>`) or nothing is returned (for `task<void>`). 390   move (for `task<T>`) or nothing is returned (for `task<void>`).
404   391  
405   @return The result value for non-void `T`; otherwise `void`. 392   @return The result value for non-void `T`; otherwise `void`.
406   393  
407   @throws The exception captured by the coroutine body, if any. 394   @throws The exception captured by the coroutine body, if any.
408   395  
409   @note Discarding an `io_result` silently drops the error 396   @note Discarding an `io_result` silently drops the error
410   code, so that overload is marked `[[nodiscard]]`. 397   code, so that overload is marked `[[nodiscard]]`.
411   */ 398   */
HITCBC 412   552 [[nodiscard]] auto await_resume() 399   552 [[nodiscard]] auto await_resume()
413   requires detail::is_io_result_v<T> 400   requires detail::is_io_result_v<T>
414   { 401   {
HITCBC 415   552 if(h_.promise().has_ep_) 402   552 if(h_.promise().has_ep_)
HITCBC 416   105 std::rethrow_exception(h_.promise().ep_); 403   105 std::rethrow_exception(h_.promise().ep_);
HITCBC 417   447 return std::move(*h_.promise().result_); 404   447 return std::move(*h_.promise().result_);
418   } 405   }
419   406  
HITCBC 420   211 auto await_resume() 407   211 auto await_resume()
421   requires (! detail::is_io_result_v<T>) 408   requires (! detail::is_io_result_v<T>)
422   { 409   {
HITCBC 423   211 if(h_.promise().has_ep_) 410   211 if(h_.promise().has_ep_)
HITCBC 424   18 std::rethrow_exception(h_.promise().ep_); 411   18 std::rethrow_exception(h_.promise().ep_);
425   if constexpr (! std::is_void_v<T>) 412   if constexpr (! std::is_void_v<T>)
HITCBC 426   148 return std::move(*h_.promise().result_); 413   148 return std::move(*h_.promise().result_);
427   else 414   else
HITCBC 428   45 return; 415   45 return;
429   } 416   }
430   417  
431   /** Start the task with the awaiting coroutine's context. 418   /** Start the task with the awaiting coroutine's context.
432   419  
433   Stores `cont` as the continuation to resume on completion. 420   Stores `cont` as the continuation to resume on completion.
434   Stores `env` as the execution environment propagated to nested 421   Stores `env` as the execution environment propagated to nested
435   `co_await` expressions. Then transfers control into the task's 422   `co_await` expressions. Then transfers control into the task's
436   coroutine body via the returned handle. 423   coroutine body via the returned handle.
437   424  
438   @param cont The awaiting coroutine to resume when the task 425   @param cont The awaiting coroutine to resume when the task
439   completes. 426   completes.
440   427  
441   @param env The execution environment (executor, stop token, and 428   @param env The execution environment (executor, stop token, and
442   frame allocator). It must outlive the task. 429   frame allocator). It must outlive the task.
443   430  
444   @return The task's coroutine handle, for symmetric transfer. 431   @return The task's coroutine handle, for symmetric transfer.
445   */ 432   */
HITCBC 446   683 std::coroutine_handle<> await_suspend(std::coroutine_handle<> cont, io_env const* env) 433   683 std::coroutine_handle<> await_suspend(std::coroutine_handle<> cont, io_env const* env)
447   { 434   {
HITCBC 448   683 h_.promise().set_continuation(cont); 435   683 h_.promise().set_continuation(cont);
HITCBC 449   683 h_.promise().set_environment(env); 436   683 h_.promise().set_environment(env);
HITCBC 450   683 return h_; 437   683 return h_;
451   } 438   }
452   439  
453   /** Return the coroutine handle. 440   /** Return the coroutine handle.
454   441  
455   @note Do not call `destroy()` on the returned handle while the 442   @note Do not call `destroy()` on the returned handle while the
456   task is being awaited. The task's lifetime is normally managed 443   task is being awaited. The task's lifetime is normally managed
457   by `run_async`, `run`, or the awaiting parent. Manually 444   by `run_async`, `run`, or the awaiting parent. Manually
458   destroying a suspended task that another coroutine is awaiting 445   destroying a suspended task that another coroutine is awaiting
459   produces undefined behavior. For cooperative cancellation, use 446   produces undefined behavior. For cooperative cancellation, use
460   `std::stop_token`. 447   `std::stop_token`.
461   448  
462   @return The coroutine handle. 449   @return The coroutine handle.
463   */ 450   */
HITCBC 464   2076 std::coroutine_handle<promise_type> handle() const noexcept 451   2082 std::coroutine_handle<promise_type> handle() const noexcept
465   { 452   {
HITCBC 466   2076 return h_; 453   2082 return h_;
467   } 454   }
468   455  
469   /** Release ownership of the coroutine frame. 456   /** Release ownership of the coroutine frame.
470   457  
471   After calling this, destroying the task does not destroy the 458   After calling this, destroying the task does not destroy the
472   coroutine frame. The caller becomes responsible for the frame's 459   coroutine frame. The caller becomes responsible for the frame's
473   lifetime. 460   lifetime.
474   461  
475   @note The caller may call `destroy()` on the released handle 462   @note The caller may call `destroy()` on the released handle
476   only when the task has not started or has fully completed. 463   only when the task has not started or has fully completed.
477   Destroying a suspended task that is being awaited produces 464   Destroying a suspended task that is being awaited produces
478   undefined behavior. 465   undefined behavior.
479   466  
480   @par Postconditions 467   @par Postconditions
481   `handle()` returns a null handle. Callers needing the 468   `handle()` returns a null handle. Callers needing the
482   original handle must save it, via @ref handle, before 469   original handle must save it, via @ref handle, before
483   calling this. 470   calling this.
484   */ 471   */
HITCBC 485   1993 void release() noexcept 472   1999 void release() noexcept
486   { 473   {
HITCBC 487   1993 h_ = nullptr; 474   1999 h_ = nullptr;
HITCBC 488   1993 } 475   1999 }
489   476  
490   /** Copy construction is disabled; a task uniquely owns its frame. 477   /** Copy construction is disabled; a task uniquely owns its frame.
491   478  
492   @param other The task that would be copied. 479   @param other The task that would be copied.
493   */ 480   */
494   task(task const& other) = delete; 481   task(task const& other) = delete;
495   482  
496   /** Copy assignment is disabled; a task uniquely owns its frame. 483   /** Copy assignment is disabled; a task uniquely owns its frame.
497   484  
498   @param other The task that would be assigned from. 485   @param other The task that would be assigned from.
499   486  
500   @return A reference to `*this`. 487   @return A reference to `*this`.
501   */ 488   */
502   task& operator=(task const& other) = delete; 489   task& operator=(task const& other) = delete;
503   490  
504   /** Construct by moving, transferring ownership of the frame. 491   /** Construct by moving, transferring ownership of the frame.
505   492  
506   @par Postconditions 493   @par Postconditions
507   `other` is empty and must not be awaited. 494   `other` is empty and must not be awaited.
508   495  
509   @param other The task to move from. 496   @param other The task to move from.
510   */ 497   */
HITCBC 511   3090 task(task&& other) noexcept 498   3090 task(task&& other) noexcept
HITCBC 512   3090 : h_(std::exchange(other.h_, nullptr)) 499   3090 : h_(std::exchange(other.h_, nullptr))
513   { 500   {
HITCBC 514   3090 } 501   3090 }
515   502  
516   /** Assign by moving, transferring ownership of the frame. 503   /** Assign by moving, transferring ownership of the frame.
517   504  
518   If this task already owns a coroutine frame, that frame is 505   If this task already owns a coroutine frame, that frame is
519   destroyed first. Self-assignment is a no-op. 506   destroyed first. Self-assignment is a no-op.
520   507  
521   @par Postconditions 508   @par Postconditions
522   `other` is empty and must not be awaited. 509   `other` is empty and must not be awaited.
523   510  
524   @param other The task to move from. 511   @param other The task to move from.
525   512  
526   @return A reference to `*this`. 513   @return A reference to `*this`.
527   */ 514   */
528   task& operator=(task&& other) noexcept 515   task& operator=(task&& other) noexcept
529   { 516   {
530   if(this != &other) 517   if(this != &other)
531   { 518   {
532   if(h_) 519   if(h_)
533   h_.destroy(); 520   h_.destroy();
534   h_ = std::exchange(other.h_, nullptr); 521   h_ = std::exchange(other.h_, nullptr);
535   } 522   }
536   return *this; 523   return *this;
537   } 524   }
538   525  
539   private: 526   private:
HITCBC 540   2760 explicit task(std::coroutine_handle<promise_type> h) 527   2766 explicit task(std::coroutine_handle<promise_type> h)
HITCBC 541   2760 : h_(h) 528   2766 : h_(h)
542   { 529   {
HITCBC 543   2760 } 530   2766 }
544   }; 531   };
545   532  
546   } // namespace capy 533   } // namespace capy
547   } // namespace boost 534   } // namespace boost
548   535  
549   #endif 536   #endif