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