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