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 + /** Unwind a coroutine chain's segment when one of its coroutines is abandoned.
28   28  
29 - A segment is a run of coroutines started together: the task 29 + A segment is a run of coroutines started together. It is the task
30 - `run_async` launches and everything it awaits, a task `run` starts 30 + `run_async` starts and everything it awaits, a task `run` starts on
31 - on another executor, or one branch of `when_all` or `when_any`. Its 31 + 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.
40 - finishes without delivering a result. An executor that destroys the 40 + The owner then finishes without delivering a result. An executor
41 - posted continuation instead, as a closed one does, unwinds the 41 + that destroys the posted continuation instead, as a closed one does,
42 - segment the same way. 42 + unwinds the 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 +
  49 + @par Example
  50 + @par !example example
  51 +
  52 + @see @ref abandon, @ref abandonable
48   */ 53   */
49   class chain_owner 54   class chain_owner
50   { 55   {
51   std::atomic<bool> abandoned_{false}; 56   std::atomic<bool> abandoned_{false};
52   continuation self_; 57   continuation self_;
53   executor_ref ex_; 58   executor_ref ex_;
54   59  
55   public: 60   public:
56   /** Bind the owner to its coroutine and executor. 61   /** Bind the owner to its coroutine and executor.
57   62  
58   Called by the owner before it starts the segment. 63   Called by the owner before it starts the segment.
59   64  
60   @param self The owner's coroutine, resumed or destroyed to 65   @param self The owner's coroutine, resumed or destroyed to
61   unwind the segment. 66   unwind the segment.
62   67  
63   @param ex The executor the segment runs on. 68   @param ex The executor the segment runs on.
64   */ 69   */
HITCBC 65   106412 void bind(std::coroutine_handle<> self, executor_ref ex) noexcept 70   106471 void bind(std::coroutine_handle<> self, executor_ref ex) noexcept
66   { 71   {
HITCBC 67   106412 self_.h = self; 72   106471 self_.h = self;
HITCBC 68   106412 ex_ = ex; 73   106471 ex_ = ex;
HITCBC 69   106412 } 74   106471 }
70   75  
71 - /// Return true once the segment has been abandoned. 76 + /// Check whether the segment has been abandoned.
HITCBC 72   7811 bool abandoned() const noexcept 77   7894 bool abandoned() const noexcept
73   { 78   {
HITCBC 74   7811 return abandoned_.load(std::memory_order_acquire); 79   7894 return abandoned_.load(std::memory_order_acquire);
75   } 80   }
76   81  
77   /** Unwind the segment on the owner's executor. 82   /** Unwind the segment on the owner's executor.
78   83  
79   Does nothing if the segment was already abandoned. Otherwise 84   Does nothing if the segment was already abandoned. Otherwise
80   posts the owner's continuation; nothing of the owner is touched 85   posts the owner's continuation; nothing of the owner is touched
81   afterward, since the post may unwind and free it at once. 86   afterward, since the post may unwind and free it at once.
82   87  
83   @note A post that throws calls `std::terminate`. 88   @note A post that throws calls `std::terminate`.
84   */ 89   */
HITCBC 85   101378 void abandon() noexcept 90   101405 void abandon() noexcept
86   { 91   {
HITCBC 87   101378 if(abandoned_.exchange(true, std::memory_order_acq_rel)) 92   101405 if(abandoned_.exchange(true, std::memory_order_acq_rel))
HITCBC 88   1 return; 93   1 return;
HITCBC 89   101377 ex_.post(self_); 94   101404 ex_.post(self_);
90   } 95   }
91   }; 96   };
92   97  
93   /** Give up a suspended coroutine for good. 98   /** Give up a suspended coroutine for good.
94   99  
95   Called by the holder of a suspended coroutine that will never resume 100   Called by the holder of a suspended coroutine that will never resume
96   it, such as an execution context, strand or primitive being torn 101   it, such as an execution context, strand or primitive being torn
97 - down. The coroutine's segment is unwound from its owner: on the 102 + down. The owner of the coroutine's segment unwinds it on the owner's
98 - owner's executor, from the top frame down to this coroutine, without 103 + executor. The unwind runs from the top frame down to this coroutine,
99 - any frame delivering a result. Ordinary cancellation does not use 104 + and no frame delivers a result. Ordinary cancellation does not use
100   this; it resumes the coroutine with an error instead. 105   this; it resumes the coroutine with an error instead.
101   106  
102   A continuation with no owner, such as an owner's own continuation, 107   A continuation with no owner, such as an owner's own continuation,
103   is destroyed directly. 108   is destroyed directly.
104   109  
105 - @par Preconditions 110 + After the call, the caller must not touch the coroutine, or anything
106 - `c` refers to a suspended coroutine that the caller holds and that 111 + in its frame, again.
107 - nothing else will resume or abandon. If `c` has no owner, nothing 112 +
108 - awaits the coroutine: destroying its frame alone would leave any 113 + @pre `c` refers to a suspended coroutine that the caller holds and
109 - awaiting frame holding a destroyed one. 114 + that nothing else will resume or abandon. If `c` has no owner,
  115 + nothing awaits the coroutine: destroying its frame alone would leave
  116 + any awaiting frame holding a destroyed one.
110   117  
111   @par Postconditions 118   @par Postconditions
112 - The caller must not touch the coroutine, or anything in its frame, 119 + 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. 120   owner's executor destroys posted work at once.
115   121  
116 - @note The unwind runs on the owner's executor, which may belong to 122 + @note The unwind runs on the owner's executor. That executor may
117 - another execution context than the caller's, and may run after the 123 + belong to another execution context than the caller's, and the
118 - caller's context is destroyed. A chain that holds objects of the 124 + unwind may run after the caller's context is destroyed. A chain that
119 - caller's context must therefore have finished before that context 125 + holds objects of the caller's context must therefore have finished
120 - is torn down. 126 + before that context is torn down.
121   127  
122 - @note Each frame is destroyed from the destructor of the frame 128 + @note Each frame is destroyed by the destructor of the frame that
123 - awaiting it, so the unwind uses stack in proportion to how deeply 129 + awaits it. The unwind therefore uses stack in proportion to how
124 - the chain's coroutines are nested. 130 + deeply the chain's coroutines are nested.
125   131  
126   @param c The continuation of the coroutine given up. 132   @param c The continuation of the coroutine given up.
127   */ 133   */
128   inline void 134   inline void
HITCBC 129   203298 abandon(continuation& c) noexcept 135   203379 abandon(continuation& c) noexcept
130   { 136   {
HITCBC 131   203298 if(c.owner) 137   203379 if(c.owner)
HITCBC 132   101376 c.owner->abandon(); 138   101403 c.owner->abandon();
HITCBC 133   101922 else if(c.h) 139   101976 else if(c.h)
HITCBC 134   101922 c.h.destroy(); 140   101976 c.h.destroy();
HITCBC 135   203298 } 141   203379 }
136   142  
137   /** Await an awaitable on behalf of a segment owner. 143   /** Await an awaitable on behalf of a segment owner.
138   144  
139   An owner, a coroutine that binds a @ref chain_owner and starts a 145   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 146   segment beneath it, waits for that segment by awaiting this. It is
141   resumed either when the awaitable completes or to unwind the 147   resumed either when the awaitable completes or to unwind the
142   segment after an @ref abandon. In the second case the awaitable 148   segment after an @ref abandon. In the second case the awaitable
143 - never completed, so there is no result to read: the result is 149 + 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 150 + empty, or `false` for an awaitable with no result. The owner must
145 - must destroy the awaitable without reading it. 151 + then destroy the awaitable without reading it.
146   152  
147   The awaitable is referenced, not copied, so the caller keeps it 153   The awaitable is referenced, not copied, so the caller keeps it
148   alive until the `co_await` completes. Its `await_resume` must 154   alive until the `co_await` completes. Its `await_resume` must
149   return a move-constructible object or `void`: the result is moved 155   return a move-constructible object or `void`: the result is moved
150   into a `std::optional`, which cannot hold a reference. 156   into a `std::optional`, which cannot hold a reference.
151   157  
152 - @par Preconditions 158 + @pre The awaiting coroutine's @ref await_context names, as its
153 - The awaiting coroutine's @ref await_context names, as its `owner`, 159 + `owner`, the @ref chain_owner bound to that coroutine.
154 - the @ref chain_owner bound to that coroutine.  
155   160  
156   @par Exception Safety 161   @par Exception Safety
157   An exception from the awaitable's `await_ready`, `await_suspend` or 162   An exception from the awaitable's `await_ready`, `await_suspend` or
158   `await_resume` propagates unchanged. 163   `await_resume` propagates unchanged.
159   164  
160   @note Awaited from a promise built on 165   @note Awaited from a promise built on
161   @ref context_awaitable_promise_base, as an owner usually is, an 166   @ref context_awaitable_promise_base, as an owner usually is, an
162   exception from `await_ready` or `await_suspend` calls 167   exception from `await_ready` or `await_suspend` calls
163   `std::terminate` instead. 168   `std::terminate` instead.
164   169  
165   @par Example 170   @par Example
166   @par !example example 171   @par !example example
167   172  
168 - @see chain_owner, abandon 173 + @see @ref chain_owner, @ref abandon
169   */ 174   */
170   template<ContextAwaitable A> 175   template<ContextAwaitable A>
171   class abandonable 176   class abandonable
172   { 177   {
173   A& a_; 178   A& a_;
174   chain_owner* owner_ = nullptr; 179   chain_owner* owner_ = nullptr;
175   180  
176   public: 181   public:
177   /// The awaitable's own `await_resume` result. 182   /// The awaitable's own `await_resume` result.
178   using result_type = decltype(std::declval<A&>().await_resume()); 183   using result_type = decltype(std::declval<A&>().await_resume());
179   184  
180   /** Construct from the awaitable to wait for. 185   /** Construct from the awaitable to wait for.
181   186  
182   @param a The awaitable. It is referenced, not copied. 187   @param a The awaitable. It is referenced, not copied.
183   */ 188   */
HITCBC 184   323 explicit abandonable(A& a) noexcept 189   374 explicit abandonable(A& a) noexcept
HITCBC 185   323 : a_(a) 190   374 : a_(a)
186   { 191   {
HITCBC 187   323 } 192   374 }
188   193  
189   /// Check whether the awaitable is already complete. 194   /// Check whether the awaitable is already complete.
HITCBC 190   323 bool await_ready() 195   374 bool await_ready()
191   { 196   {
HITCBC 192   323 return a_.await_ready(); 197   374 return a_.await_ready();
193   } 198   }
194   199  
195   /** Suspend on the awaitable, remembering the segment owner. 200   /** Suspend on the awaitable, remembering the segment owner.
196   201  
197   @param h The coroutine performing the `co_await`. 202   @param h The coroutine performing the `co_await`.
198   @param env The environment of that coroutine. 203   @param env The environment of that coroutine.
199   204  
200   @return The awaitable's own `await_suspend` result. 205   @return The awaitable's own `await_suspend` result.
201   */ 206   */
202   template<class H> 207   template<class H>
HITCBC 203   322 auto await_suspend(H h, await_context const* env) 208   373 auto await_suspend(H h, await_context const* env)
204   { 209   {
HITCBC 205   322 owner_ = env->owner; 210   373 owner_ = env->owner;
HITCBC 206   322 return a_.await_suspend(h, env); 211   373 return a_.await_suspend(h, env);
207   } 212   }
208   213  
209   /** Return the result, or nothing if the segment was abandoned. 214   /** Return the result, or nothing if the segment was abandoned.
210   215  
211   For an awaitable with no result this returns `bool` instead, 216   For an awaitable with no result this returns `bool` instead,
212   `false` when the segment was unwound. 217   `false` when the segment was unwound.
213   218  
214 - @return The result, empty when the segment was unwound. 219 + @return An engaged optional or `true`; empty or `false` if unwound.
215   */ 220   */
HITCBC 216   317 auto await_resume() 221   368 auto await_resume()
217   { 222   {
HITCBC 218   317 bool const unwinding = owner_ && owner_->abandoned(); 223   368 bool const unwinding = owner_ && owner_->abandoned();
219   if constexpr(std::is_void_v<result_type>) 224   if constexpr(std::is_void_v<result_type>)
220   { 225   {
221   if(unwinding) 226   if(unwinding)
222   return false; 227   return false;
223   a_.await_resume(); 228   a_.await_resume();
224   return true; 229   return true;
225   } 230   }
226   else 231   else
227   { 232   {
HITCBC 228   317 if(unwinding) 233   368 if(unwinding)
HITCBC 229   34 return std::optional<result_type>(); 234   34 return std::optional<result_type>();
HITCBC 230   283 return std::optional<result_type>(a_.await_resume()); 235   334 return std::optional<result_type>(a_.await_resume());
231   } 236   }
232   } 237   }
233   }; 238   };
234   239  
235   } // namespace capy 240   } // namespace capy
236   } // namespace boost 241   } // namespace boost
237   242  
238   #endif 243   #endif