TLA Line data Source code
1 : //
2 : // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3 : // Copyright (c) 2026 Michael Vandeberg
4 : //
5 : // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 : //
8 : // Official repository: https://github.com/cppalliance/capy
9 : //
10 :
11 : #ifndef BOOST_CAPY_ANY_EXECUTOR_HPP
12 : #define BOOST_CAPY_ANY_EXECUTOR_HPP
13 :
14 : #include <boost/capy/detail/config.hpp>
15 : #include <boost/capy/continuation.hpp>
16 : #include <concepts>
17 : #include <coroutine>
18 : #include <memory>
19 : #include <type_traits>
20 : #include <typeinfo>
21 :
22 : namespace boost {
23 : namespace capy {
24 :
25 : class execution_context;
26 : template<typename> class strand;
27 :
28 : namespace detail {
29 :
30 : template<typename T>
31 : struct is_strand_type : std::false_type {};
32 :
33 : template<typename E>
34 : struct is_strand_type<strand<E>> : std::true_type {};
35 :
36 : } // detail
37 :
38 : /** Forwards `dispatch`/`post`/`context` calls through a shared, type-erased executor pointer.
39 :
40 : This class provides type erasure for any executor type, enabling
41 : runtime polymorphism with automatic memory management via shared
42 : ownership. It stores a shared pointer to a polymorphic wrapper,
43 : allowing executors of different types to be stored uniformly
44 : while satisfying the full `Executor` concept.
45 :
46 : @par Value Semantics
47 :
48 : This class has value semantics with shared ownership. Copy and
49 : move operations are cheap, copying the internal shared
50 : pointer. Multiple `any_executor` instances may share the same
51 : underlying executor. Move operations do not invalidate the
52 : source; there is no moved-from state.
53 :
54 : @par Default State
55 :
56 : A default-constructed `any_executor` holds no executor.
57 : `operator bool()`, `operator==`, and `target_type()` report the
58 : empty state. `context()`, `on_work_started()`, `on_work_finished()`,
59 : `dispatch()`, and `post()` are undefined behavior until an
60 : executor is assigned.
61 :
62 : @par Thread Safety
63 :
64 : The `any_executor` itself is thread-safe for concurrent reads.
65 : Concurrent modification requires external synchronization.
66 : Executor operations are safe to call concurrently if the
67 : underlying executor supports it.
68 :
69 : @par Executor Concept
70 :
71 : This class satisfies the `Executor` concept, making it usable
72 : anywhere a concrete executor is expected.
73 :
74 : @par Example
75 : @code
76 : any_executor exec = ctx.get_executor();
77 : if(exec)
78 : {
79 : auto& context = exec.context();
80 : exec.post(my_coroutine);
81 : }
82 : @endcode
83 :
84 : @see executor_ref, Executor
85 : */
86 : class any_executor
87 : {
88 : struct impl_base;
89 :
90 : std::shared_ptr<impl_base> p_;
91 :
92 : struct impl_base
93 : {
94 HIT 20 : virtual ~impl_base() = default;
95 : virtual execution_context& context() const noexcept = 0;
96 : virtual void on_work_started() const noexcept = 0;
97 : virtual void on_work_finished() const noexcept = 0;
98 : virtual std::coroutine_handle<> dispatch(continuation&) const = 0;
99 : virtual void post(continuation&) const = 0;
100 : virtual bool equals(impl_base const*) const noexcept = 0;
101 : virtual std::type_info const& target_type() const noexcept = 0;
102 : };
103 :
104 : template<class Ex>
105 : struct impl final : impl_base
106 : {
107 : Ex ex_;
108 :
109 : template<class Ex1>
110 20 : explicit impl(Ex1&& ex)
111 20 : : ex_(std::forward<Ex1>(ex))
112 : {
113 20 : }
114 :
115 6 : execution_context& context() const noexcept override
116 : {
117 6 : return const_cast<Ex&>(ex_).context();
118 : }
119 :
120 5 : void on_work_started() const noexcept override
121 : {
122 5 : ex_.on_work_started();
123 5 : }
124 :
125 5 : void on_work_finished() const noexcept override
126 : {
127 5 : ex_.on_work_finished();
128 5 : }
129 :
130 5 : std::coroutine_handle<> dispatch(continuation& c) const override
131 : {
132 5 : return ex_.dispatch(c);
133 : }
134 :
135 16 : void post(continuation& c) const override
136 : {
137 16 : ex_.post(c);
138 16 : }
139 :
140 9 : bool equals(impl_base const* other) const noexcept override
141 : {
142 9 : if(target_type() != other->target_type())
143 1 : return false;
144 8 : return ex_ == static_cast<impl const*>(other)->ex_;
145 : }
146 :
147 19 : std::type_info const& target_type() const noexcept override
148 : {
149 19 : return typeid(Ex);
150 : }
151 : };
152 :
153 : public:
154 : /** Construct a default instance.
155 :
156 : Constructs an empty `any_executor`. `operator bool()` reports
157 : the empty state; `context()`, `on_work_started()`,
158 : `on_work_finished()`, `dispatch()`, and `post()` are undefined
159 : behavior until an executor is assigned.
160 :
161 : @par Postconditions
162 : @li `!*this`
163 : */
164 2 : any_executor() = default;
165 :
166 : /** Construct a copy.
167 :
168 : Creates a new `any_executor` sharing ownership of the
169 : underlying executor with `other`.
170 :
171 : @param other The executor to copy.
172 :
173 : @par Postconditions
174 : @li `*this == other`
175 : */
176 33 : any_executor(any_executor const& other) = default;
177 :
178 : /** Copy assignment operator.
179 :
180 : Shares ownership of the underlying executor with `other`.
181 :
182 : @param other The executor to copy.
183 :
184 : @return A reference to `*this`.
185 :
186 : @par Postconditions
187 : @li `*this == other`
188 : */
189 6 : any_executor& operator=(any_executor const& other) = default;
190 :
191 : /** Constructs from any executor type.
192 :
193 : Allocates storage for a copy of the given executor and
194 : stores it internally. The executor must satisfy the
195 : `Executor` concept.
196 :
197 : @param ex The executor to wrap. A copy is stored internally.
198 :
199 : @par Postconditions
200 : @li `*this` is valid
201 : */
202 : template<class Ex>
203 : requires (
204 : !std::same_as<std::decay_t<Ex>, any_executor> &&
205 : !detail::is_strand_type<std::decay_t<Ex>>::value &&
206 : std::copy_constructible<std::decay_t<Ex>>)
207 20 : any_executor(Ex&& ex)
208 20 : : p_(std::make_shared<impl<std::decay_t<Ex>>>(std::forward<Ex>(ex)))
209 : {
210 20 : }
211 :
212 : /** Returns true if this instance holds a valid executor.
213 :
214 : @return `true` if constructed with an executor, `false` if
215 : default-constructed.
216 : */
217 6 : explicit operator bool() const noexcept
218 : {
219 6 : return p_ != nullptr;
220 : }
221 :
222 : /** Returns a reference to the associated execution context.
223 :
224 : @return A reference to the execution context.
225 :
226 : @pre This instance holds a valid executor.
227 : */
228 6 : execution_context& context() const noexcept
229 : {
230 6 : return p_->context();
231 : }
232 :
233 : /** Informs the executor that work is beginning.
234 :
235 : Must be paired with a subsequent call to `on_work_finished()`.
236 :
237 : @pre This instance holds a valid executor.
238 : */
239 5 : void on_work_started() const noexcept
240 : {
241 5 : p_->on_work_started();
242 5 : }
243 :
244 : /** Informs the executor that work has completed.
245 :
246 : @pre A preceding call to `on_work_started()` was made.
247 : @pre This instance holds a valid executor.
248 : */
249 5 : void on_work_finished() const noexcept
250 : {
251 5 : p_->on_work_finished();
252 5 : }
253 :
254 : /** Dispatches a continuation through the wrapped executor.
255 :
256 : Returns a handle for symmetric transfer. If running in the
257 : executor's thread, returns `c.h`. Otherwise, posts the
258 : continuation for later execution and returns
259 : `std::noop_coroutine()`.
260 :
261 : @param c The continuation to dispatch for resumption.
262 : Must remain at a stable address until dequeued.
263 :
264 : @return A handle for symmetric transfer or `std::noop_coroutine()`.
265 :
266 : @pre This instance holds a valid executor.
267 : */
268 5 : std::coroutine_handle<> dispatch(continuation& c) const
269 : {
270 5 : return p_->dispatch(c);
271 : }
272 :
273 : /** Posts a continuation to the wrapped executor.
274 :
275 : Posts the continuation to the executor for later execution
276 : and returns. The caller should transfer to `std::noop_coroutine()`
277 : after calling this.
278 :
279 : @param c The continuation to post for resumption.
280 : Must remain at a stable address until dequeued.
281 :
282 : @pre This instance holds a valid executor.
283 : */
284 16 : void post(continuation& c) const
285 : {
286 16 : p_->post(c);
287 16 : }
288 :
289 : /** Compares two executor wrappers for equality.
290 :
291 : Two `any_executor` instances are equal if they both hold
292 : executors of the same type that compare equal, or if both
293 : are empty.
294 :
295 : @param other The executor to compare against.
296 :
297 : @return `true` if both wrap equal executors of the same type,
298 : or both are empty.
299 : */
300 11 : bool operator==(any_executor const& other) const noexcept
301 : {
302 11 : if(!p_ && !other.p_)
303 1 : return true;
304 10 : if(!p_ || !other.p_)
305 1 : return false;
306 9 : return p_->equals(other.p_.get());
307 : }
308 :
309 : /** Returns the type_info of the wrapped executor.
310 :
311 : @return The `std::type_info` of the stored executor type,
312 : or `typeid(void)` if empty.
313 : */
314 2 : std::type_info const& target_type() const noexcept
315 : {
316 2 : if(!p_)
317 1 : return typeid(void);
318 1 : return p_->target_type();
319 : }
320 : };
321 :
322 : } // capy
323 : } // boost
324 :
325 : #endif
|