TLA Line data Source code
1 : //
2 : // Copyright (c) 2026 Steve Gerbino
3 : //
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)
6 : //
7 : // Official repository: https://github.com/cppalliance/capy
8 : //
9 :
10 : #ifndef BOOST_CAPY_DETAIL_CONTEXT_AWAITER_HPP
11 : #define BOOST_CAPY_DETAIL_CONTEXT_AWAITER_HPP
12 :
13 : #include <boost/capy/detail/config.hpp>
14 : #include <boost/capy/detail/await_suspend_helper.hpp>
15 : #include <boost/capy/concept/context_awaitable.hpp>
16 : #include <boost/capy/ex/await_context.hpp>
17 : #include <boost/capy/ex/frame_allocator.hpp>
18 :
19 : #include <coroutine>
20 : #include <type_traits>
21 : #include <utility>
22 :
23 : namespace boost {
24 : namespace capy {
25 : namespace detail {
26 :
27 : /** Pass a coroutine's environment to an awaitable it awaits.
28 :
29 : The compiler calls `await_suspend` with the suspending coroutine's
30 : handle only, but a @ref ContextAwaitable also needs that
31 : coroutine's @ref await_context. A promise supplies it by returning
32 : this from `await_transform`: the awaiter forwards the environment
33 : to the wrapped awaitable's `await_suspend(h, env)`, and reinstalls
34 : the environment's frame allocator before the coroutine continues,
35 : since it may resume on another thread.
36 :
37 : @ref context_awaitable_promise_base does this for every
38 : @ref ContextAwaitable by default, and `task` uses it the same way.
39 :
40 : The awaiter also applies the workarounds Capy needs for compilers
41 : that miscompile symmetric transfer, so code using it does not have
42 : to.
43 :
44 : The awaitable is moved or copied into the awaiter, which awaits
45 : its own copy. The environment is read from the promise each time
46 : the awaiter suspends and resumes, so it is the one in place at that
47 : moment.
48 :
49 : @par Exception Safety
50 : An exception from the wrapped awaitable's `await_ready` or
51 : `await_suspend` calls `std::terminate`, as it does in a @ref task.
52 : A promise that reports such an exception as the coroutine's
53 : failure, as `when_all` does, needs its own adapter.
54 : */
55 : template<ContextAwaitable A, class P>
56 : struct context_awaiter
57 : {
58 : // An aggregate on purpose: with a user-provided constructor, clang's
59 : // static analyzer reports the promise's environment as uninitialized.
60 :
61 : /// The awaitable, stored by value.
62 : A a_;
63 :
64 : /// The awaiting coroutine's promise.
65 : P* p_;
66 :
67 : /** Check whether the awaitable is already complete.
68 :
69 : Where the symmetric-transfer workaround is on, this always
70 : returns `false` and @ref await_suspend asks the awaitable
71 : instead.
72 :
73 : @note An exception from the awaitable's `await_ready` calls
74 : `std::terminate`.
75 :
76 : @return `true` if the coroutine need not suspend.
77 : */
78 HIT 108242 : bool await_ready() noexcept
79 : {
80 : #if BOOST_CAPY_SYMMETRIC_TRANSFER_WORKAROUND
81 : return false;
82 : #else
83 108242 : return a_.await_ready();
84 : #endif
85 : }
86 :
87 : /** Suspend by calling the awaitable with the environment.
88 :
89 : Where the symmetric-transfer workaround is on, the result is a
90 : `bool` instead of the awaitable's own suspension decision.
91 :
92 : @note An exception from the awaitable's `await_suspend` calls
93 : `std::terminate`.
94 :
95 : @param h The coroutine performing the `co_await`.
96 :
97 : @return The awaitable's own suspension decision.
98 : */
99 : template<class Promise>
100 107619 : auto await_suspend(std::coroutine_handle<Promise> h) noexcept
101 : {
102 107619 : await_context const* const env = p_->environment();
103 : #if BOOST_CAPY_SYMMETRIC_TRANSFER_WORKAROUND
104 : return detail::await_ready_then_suspend(a_, h, env);
105 : #else
106 : using R = decltype(a_.await_suspend(h, env));
107 : if constexpr (std::is_same_v<R, std::coroutine_handle<>>)
108 106578 : return detail::symmetric_transfer(a_.await_suspend(h, env));
109 : else
110 1041 : return a_.await_suspend(h, env);
111 : #endif
112 : }
113 :
114 : /** Reinstall the frame allocator and return the awaitable's result.
115 :
116 : @return The awaitable's `await_resume` result, unchanged.
117 : */
118 5620 : decltype(auto) await_resume()
119 : {
120 5620 : set_current_frame_allocator(p_->environment()->frame_allocator);
121 5620 : return a_.await_resume();
122 : }
123 : };
124 :
125 : } // namespace detail
126 : } // namespace capy
127 : } // namespace boost
128 :
129 : #endif
|