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