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