100.00% Lines (46/46) 100.00% Functions (17/17)
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_EXECUTOR_REF_HPP 11   #ifndef BOOST_CAPY_EXECUTOR_REF_HPP
12   #define BOOST_CAPY_EXECUTOR_REF_HPP 12   #define BOOST_CAPY_EXECUTOR_REF_HPP
13   13  
14   #include <boost/capy/detail/config.hpp> 14   #include <boost/capy/detail/config.hpp>
15   #include <boost/capy/detail/type_id.hpp> 15   #include <boost/capy/detail/type_id.hpp>
16   #include <boost/capy/continuation.hpp> 16   #include <boost/capy/continuation.hpp>
17   #include <concepts> 17   #include <concepts>
18   #include <coroutine> 18   #include <coroutine>
19   #include <type_traits> 19   #include <type_traits>
20   #include <utility> 20   #include <utility>
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   26  
27   namespace detail { 27   namespace detail {
28   28  
29   /** Virtual function table for type-erased executor operations. */ 29   /** Virtual function table for type-erased executor operations. */
30   struct executor_vtable 30   struct executor_vtable
31   { 31   {
32   execution_context& (*context)(void const*) noexcept; 32   execution_context& (*context)(void const*) noexcept;
33   void (*on_work_started)(void const*) noexcept; 33   void (*on_work_started)(void const*) noexcept;
34   void (*on_work_finished)(void const*) noexcept; 34   void (*on_work_finished)(void const*) noexcept;
35   void (*post)(void const*, continuation&); 35   void (*post)(void const*, continuation&);
36   std::coroutine_handle<> (*dispatch)(void const*, continuation&); 36   std::coroutine_handle<> (*dispatch)(void const*, continuation&);
37   bool (*equals)(void const*, void const*) noexcept; 37   bool (*equals)(void const*, void const*) noexcept;
38   detail::type_info const* type_id; 38   detail::type_info const* type_id;
39   }; 39   };
40   40  
41   /** Vtable instance for a specific executor type. */ 41   /** Vtable instance for a specific executor type. */
42   template<class Ex> 42   template<class Ex>
43   inline constexpr executor_vtable vtable_for = { 43   inline constexpr executor_vtable vtable_for = {
44   // context 44   // context
HITCBC 45   1 [](void const* p) noexcept -> execution_context& { 45   1 [](void const* p) noexcept -> execution_context& {
HITCBC 46   1 return const_cast<Ex*>(static_cast<Ex const*>(p))->context(); 46   1 return const_cast<Ex*>(static_cast<Ex const*>(p))->context();
47   }, 47   },
48   // on_work_started 48   // on_work_started
HITCBC 49   176 [](void const* p) noexcept { 49   176 [](void const* p) noexcept {
HITCBC 50   88 const_cast<Ex*>(static_cast<Ex const*>(p))->on_work_started(); 50   88 const_cast<Ex*>(static_cast<Ex const*>(p))->on_work_started();
51   }, 51   },
52   // on_work_finished 52   // on_work_finished
HITCBC 53   176 [](void const* p) noexcept { 53   176 [](void const* p) noexcept {
HITCBC 54   88 const_cast<Ex*>(static_cast<Ex const*>(p))->on_work_finished(); 54   88 const_cast<Ex*>(static_cast<Ex const*>(p))->on_work_finished();
55   }, 55   },
56   // post 56   // post
HITCBC 57   443603 [](void const* p, continuation& c) { 57   447107 [](void const* p, continuation& c) {
HITCBC 58   221805 static_cast<Ex const*>(p)->post(c); 58   223557 static_cast<Ex const*>(p)->post(c);
59   }, 59   },
60   // dispatch 60   // dispatch
HITCBC 61   125 [](void const* p, continuation& c) -> std::coroutine_handle<> { 61   142 [](void const* p, continuation& c) -> std::coroutine_handle<> {
HITCBC 62   125 return static_cast<Ex const*>(p)->dispatch(c); 62   142 return static_cast<Ex const*>(p)->dispatch(c);
63   }, 63   },
64   // equals 64   // equals
HITCBC 65   1 [](void const* a, void const* b) noexcept -> bool { 65   1 [](void const* a, void const* b) noexcept -> bool {
HITCBC 66   1 return *static_cast<Ex const*>(a) == *static_cast<Ex const*>(b); 66   1 return *static_cast<Ex const*>(a) == *static_cast<Ex const*>(b);
67   }, 67   },
68   // type_id 68   // type_id
69   &detail::type_id<Ex>() 69   &detail::type_id<Ex>()
70   }; 70   };
71   71  
72   } // detail 72   } // detail
73   73  
74   /** Forwards `dispatch`/`post`/`context` calls through a non-owning, type-erased executor pointer. 74   /** Forwards `dispatch`/`post`/`context` calls through a non-owning, type-erased executor pointer.
75   75  
76   This class provides type erasure for any executor type, enabling 76   This class provides type erasure for any executor type, enabling
77   runtime polymorphism without virtual functions or allocation. 77   runtime polymorphism without virtual functions or allocation.
78   It stores a pointer to the original executor and a pointer to a 78   It stores a pointer to the original executor and a pointer to a
79   static vtable. Executors of different types are therefore stored 79   static vtable. Executors of different types are therefore stored
80   uniformly, while satisfying the full `Executor` concept. 80   uniformly, while satisfying the full `Executor` concept.
81   81  
82   @par Reference Semantics 82   @par Reference Semantics
83   This class has reference semantics: it does not allocate or own 83   This class has reference semantics: it does not allocate or own
84   the wrapped executor. Copy operations copy the internal 84   the wrapped executor. Copy operations copy the internal
85   pointers. The caller must ensure the referenced executor outlives 85   pointers. The caller must ensure the referenced executor outlives
86   all `executor_ref` instances that wrap it. 86   all `executor_ref` instances that wrap it.
87   87  
88   @par Thread Safety 88   @par Thread Safety
89   The `executor_ref` itself is not thread-safe for concurrent 89   The `executor_ref` itself is not thread-safe for concurrent
90   modification, but its executor operations are safe to call 90   modification, but its executor operations are safe to call
91   concurrently if the underlying executor supports it. 91   concurrently if the underlying executor supports it.
92   92  
93   @par Executor Concept 93   @par Executor Concept
94   This class satisfies the `Executor` concept, making it usable 94   This class satisfies the `Executor` concept, making it usable
95   anywhere a concrete executor is expected. 95   anywhere a concrete executor is expected.
96   96  
97   @par Example 97   @par Example
98   @par !example example 98   @par !example example
99   99  
100   100  
101   @see @ref any_executor, @ref Executor 101   @see @ref any_executor, @ref Executor
102   */ 102   */
103   class executor_ref 103   class executor_ref
104   { 104   {
105   void const* ex_ = nullptr; 105   void const* ex_ = nullptr;
106   detail::executor_vtable const* vt_ = nullptr; 106   detail::executor_vtable const* vt_ = nullptr;
107   107  
108   public: 108   public:
109   /** Construct a default instance. 109   /** Construct a default instance.
110   110  
111   Constructs an empty `executor_ref`. `operator bool()`, 111   Constructs an empty `executor_ref`. `operator bool()`,
112   `operator==()`, and `target()` report the empty state; 112   `operator==()`, and `target()` report the empty state;
113   `context()`, `on_work_started()`, `on_work_finished()`, 113   `context()`, `on_work_started()`, `on_work_finished()`,
114   `dispatch()`, and `post()` are undefined behavior until an 114   `dispatch()`, and `post()` are undefined behavior until an
115   executor is assigned. 115   executor is assigned.
116   */ 116   */
HITCBC 117   317544 executor_ref() = default; 117   317774 executor_ref() = default;
118   118  
119   /** Construct a copy. 119   /** Construct a copy.
120   120  
121 - This enables the same-executor optimization when passing  
122 - executor_ref through coroutine chains.  
123   Copies the internal pointers, preserving identity. 121   Copies the internal pointers, preserving identity.
124   122  
125   @param other The reference to copy. 123   @param other The reference to copy.
126   */ 124   */
127   executor_ref(executor_ref const& other) = default; 125   executor_ref(executor_ref const& other) = default;
128   126  
129 - /** Copy assignment operator. 127 + /** Rebind to the executor `other` refers to.
130   128  
131   @param other The reference to copy. 129   @param other The reference to copy.
132   130  
133   @return A reference to `*this`. 131   @return A reference to `*this`.
134   */ 132   */
135   executor_ref& operator=(executor_ref const& other) = default; 133   executor_ref& operator=(executor_ref const& other) = default;
136   134  
137 - /** Constructs from any executor type. 135 + /** Construct from any executor type.
138   136  
139   Captures a reference to the given executor and stores a pointer 137   Captures a reference to the given executor and stores a pointer
140   to the type-specific vtable. The executor must remain valid for 138   to the type-specific vtable. The executor must remain valid for
141   the lifetime of this `executor_ref` instance. 139   the lifetime of this `executor_ref` instance.
142   140  
  141 + @note The constructor accepts a temporary executor. Binding to a
  142 + temporary leaves a dangling reference once the full expression
  143 + ends.
  144 +
143   @param ex The executor to wrap. Must satisfy the `Executor` 145   @param ex The executor to wrap. Must satisfy the `Executor`
144   concept. A pointer to this object is stored 146   concept. A pointer to this object is stored
145   internally; the executor must outlive this wrapper. 147   internally; the executor must outlive this wrapper.
146   */ 148   */
147   #if defined(__GNUC__) && !defined(__clang__) 149   #if defined(__GNUC__) && !defined(__clang__)
148   // GCC constraint satisfaction caching bug workaround 150   // GCC constraint satisfaction caching bug workaround
149   template<class Ex, 151   template<class Ex,
150   std::enable_if_t<!std::is_same_v< 152   std::enable_if_t<!std::is_same_v<
151   std::decay_t<Ex>, executor_ref>, int> = 0> 153   std::decay_t<Ex>, executor_ref>, int> = 0>
152   #else 154   #else
153   template<class Ex> 155   template<class Ex>
154   requires (!std::same_as<std::decay_t<Ex>, executor_ref>) 156   requires (!std::same_as<std::decay_t<Ex>, executor_ref>)
155   #endif 157   #endif
HITCBC 156   543672 executor_ref(Ex const& ex) noexcept 158   543688 executor_ref(Ex const& ex) noexcept
HITCBC 157   543672 : ex_(&ex) 159   543688 : ex_(&ex)
HITCBC 158   543672 , vt_(&detail::vtable_for<Ex>) 160   543688 , vt_(&detail::vtable_for<Ex>)
159   { 161   {
HITCBC 160   543672 } 162   543688 }
161   163  
162 - /** Returns true if this instance holds a valid executor. 164 + /** Check whether this instance holds a valid executor.
163   165  
164   @return `true` if constructed with an executor, `false` if 166   @return `true` if constructed with an executor, `false` if
165   default-constructed. 167   default-constructed.
166   */ 168   */
HITCBC 167   6 explicit operator bool() const noexcept 169   6 explicit operator bool() const noexcept
168   { 170   {
HITCBC 169   6 return ex_ != nullptr; 171   6 return ex_ != nullptr;
170   } 172   }
171   173  
172 - /** Returns a reference to the associated execution context. 174 + /** Return a reference to the associated execution context.
173 -  
174 - @return A reference to the execution context.  
175   175  
176   @pre This instance was constructed with a valid executor. 176   @pre This instance was constructed with a valid executor.
  177 +
  178 + @return A reference to the execution context.
177   */ 179   */
HITCBC 178   1 execution_context& context() const noexcept 180   1 execution_context& context() const noexcept
179   { 181   {
HITCBC 180   1 return vt_->context(ex_); 182   1 return vt_->context(ex_);
181   } 183   }
182   184  
183 - /** Informs the executor that work is beginning. 185 + /** Inform the executor that work is beginning.
184   186  
185   Must be paired with a subsequent call to `on_work_finished()`. 187   Must be paired with a subsequent call to `on_work_finished()`.
186   188  
187   @pre This instance was constructed with a valid executor. 189   @pre This instance was constructed with a valid executor.
188   */ 190   */
HITCBC 189   88 void on_work_started() const noexcept 191   88 void on_work_started() const noexcept
190   { 192   {
HITCBC 191   88 vt_->on_work_started(ex_); 193   88 vt_->on_work_started(ex_);
HITCBC 192   88 } 194   88 }
193   195  
194 - /** Informs the executor that work has completed. 196 + /** Inform the executor that work has completed.
195   197  
196   @pre A preceding call to `on_work_started()` was made. 198   @pre A preceding call to `on_work_started()` was made.
197   @pre This instance was constructed with a valid executor. 199   @pre This instance was constructed with a valid executor.
198   */ 200   */
HITCBC 199   88 void on_work_finished() const noexcept 201   88 void on_work_finished() const noexcept
200   { 202   {
HITCBC 201   88 vt_->on_work_finished(ex_); 203   88 vt_->on_work_finished(ex_);
HITCBC 202   88 } 204   88 }
203   205  
204 - /** Dispatches a continuation through the wrapped executor. 206 + /** Dispatch a continuation through the wrapped executor.
205   207  
206 - Returns a handle for symmetric transfer. If running in the 208 + Forwards to the wrapped executor's `dispatch`. That executor
207 - executor's thread, returns `c.h`. Otherwise, posts the 209 + returns `c.h` to resume inline, or posts `c` and returns
208 - continuation for later execution and returns  
209   `std::noop_coroutine()`. 210   `std::noop_coroutine()`.
210   211  
  212 + @pre This instance was constructed with a valid executor.
  213 +
  214 + @par Exception Safety
  215 + Propagates any exception from the wrapped executor.
  216 +
211   @param c The continuation to dispatch for resumption. 217   @param c The continuation to dispatch for resumption.
212   Must remain at a stable address until dequeued. 218   Must remain at a stable address until dequeued.
213   219  
214 -  
215 - @pre This instance was constructed with a valid executor.  
216   @return A handle for symmetric transfer or `std::noop_coroutine()`. 220   @return A handle for symmetric transfer or `std::noop_coroutine()`.
217   */ 221   */
HITCBC 218   125 std::coroutine_handle<> dispatch(continuation& c) const 222   142 std::coroutine_handle<> dispatch(continuation& c) const
219   { 223   {
HITCBC 220   125 return vt_->dispatch(ex_, c); 224   142 return vt_->dispatch(ex_, c);
221   } 225   }
222   226  
223 - /** Posts a continuation to the wrapped executor. 227 + /** Post a continuation to the wrapped executor.
224   228  
225   Posts the continuation to the executor for later execution 229   Posts the continuation to the executor for later execution
226   and returns. The caller should transfer to `std::noop_coroutine()` 230   and returns. The caller should transfer to `std::noop_coroutine()`
227   after calling this. 231   after calling this.
228   232  
  233 + @pre This instance was constructed with a valid executor.
  234 +
  235 + @par Exception Safety
  236 + Propagates any exception from the wrapped executor.
  237 +
229   @param c The continuation to post for resumption. 238   @param c The continuation to post for resumption.
230 -  
231 - @pre This instance was constructed with a valid executor.  
232   Must remain at a stable address until dequeued. 239   Must remain at a stable address until dequeued.
233   */ 240   */
HITCBC 234   221805 void post(continuation& c) const 241   223557 void post(continuation& c) const
235   { 242   {
HITCBC 236   221805 vt_->post(ex_, c); 243   223557 vt_->post(ex_, c);
HITCBC 237   221798 } 244   223550 }
238   245  
239 - /** Compares two executor references for equality. 246 + /** Compare two executor references for equality.
240   247  
241   Two `executor_ref` instances are equal if they wrap 248   Two `executor_ref` instances are equal if they wrap
242 - executors of the same type that compare equal. 249 + executors of the same type that compare equal. Two empty
  250 + references are equal, and so are two references to the same
  251 + executor object.
243   252  
244   @param other The executor reference to compare against. 253   @param other The executor reference to compare against.
245   254  
246 - @return `true` if both wrap equal executors of the same type. 255 + @return `true` if both are empty or wrap equal executors of the same type.
247   */ 256   */
HITCBC 248   7 bool operator==(executor_ref const& other) const noexcept 257   7 bool operator==(executor_ref const& other) const noexcept
249   { 258   {
HITCBC 250   7 if (ex_ == other.ex_) 259   7 if (ex_ == other.ex_)
HITCBC 251   5 return true; 260   5 return true;
HITCBC 252   2 if (vt_ != other.vt_) 261   2 if (vt_ != other.vt_)
HITCBC 253   1 return false; 262   1 return false;
HITCBC 254   1 return vt_->equals(ex_, other.ex_); 263   1 return vt_->equals(ex_, other.ex_);
255   } 264   }
256   265  
257   /** Return a pointer to the wrapped executor if it matches 266   /** Return a pointer to the wrapped executor if it matches
258   the requested type. 267   the requested type.
259   268  
260   Performs a type check against the stored executor and 269   Performs a type check against the stored executor and
261   returns a typed pointer when the types match, or 270   returns a typed pointer when the types match, or
262   `nullptr` otherwise. Analogous to 271   `nullptr` otherwise. Analogous to
263   `std::any_cast< Executor >( &a )`. 272   `std::any_cast< Executor >( &a )`.
264   273  
265   @tparam Executor The executor type to retrieve. 274   @tparam Executor The executor type to retrieve.
266   275  
267   @return A pointer to the underlying executor, or 276   @return A pointer to the underlying executor, or
268   `nullptr` if this reference is empty or the type 277   `nullptr` if this reference is empty or the type
269   does not match. 278   does not match.
270   */ 279   */
271   template< typename Executor > 280   template< typename Executor >
HITCBC 272   3 const Executor* target() const noexcept 281   3 const Executor* target() const noexcept
273   { 282   {
HITCBC 274   3 if ( vt_ && *vt_->type_id == detail::type_id< Executor >() ) 283   3 if ( vt_ && *vt_->type_id == detail::type_id< Executor >() )
HITCBC 275   1 return static_cast< Executor const* >( ex_ ); 284   1 return static_cast< Executor const* >( ex_ );
HITCBC 276   2 return nullptr; 285   2 return nullptr;
277   } 286   }
278   287  
279   /// @copydoc target() const 288   /// @copydoc target() const
280   template< typename Executor> 289   template< typename Executor>
HITCBC 281   5 Executor* target() noexcept 290   5 Executor* target() noexcept
282   { 291   {
HITCBC 283   5 if ( vt_ && *vt_->type_id == detail::type_id< Executor >() ) 292   5 if ( vt_ && *vt_->type_id == detail::type_id< Executor >() )
284   return const_cast< Executor* >( 293   return const_cast< Executor* >(
HITCBC 285   2 static_cast< Executor const* >( ex_ )); 294   2 static_cast< Executor const* >( ex_ ));
HITCBC 286   3 return nullptr; 295   3 return nullptr;
287   } 296   }
288   }; 297   };
289   298  
290   } // capy 299   } // capy
291   } // boost 300   } // boost
292   301  
293   #endif 302   #endif