100.00% Lines (64/64) 100.00% Functions (16/16)
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_TEST_RUN_BLOCKING_HPP 11   #ifndef BOOST_CAPY_TEST_RUN_BLOCKING_HPP
12   #define BOOST_CAPY_TEST_RUN_BLOCKING_HPP 12   #define BOOST_CAPY_TEST_RUN_BLOCKING_HPP
13   13  
14   #include <boost/capy/detail/config.hpp> 14   #include <boost/capy/detail/config.hpp>
15   #include <boost/capy/concept/execution_context.hpp> 15   #include <boost/capy/concept/execution_context.hpp>
16   #include <boost/capy/concept/executor.hpp> 16   #include <boost/capy/concept/executor.hpp>
17   #include <boost/capy/error.hpp> 17   #include <boost/capy/error.hpp>
18   #include <boost/capy/ex/run_async.hpp> 18   #include <boost/capy/ex/run_async.hpp>
19   19  
20   #include <coroutine> 20   #include <coroutine>
21   #include <exception> 21   #include <exception>
22   #include <stop_token> 22   #include <stop_token>
23   #include <system_error> 23   #include <system_error>
24   #include <type_traits> 24   #include <type_traits>
25   #include <utility> 25   #include <utility>
26   26  
27   namespace boost { 27   namespace boost {
28   namespace capy { 28   namespace capy {
29   namespace test { 29   namespace test {
30   30  
31   class blocking_context; 31   class blocking_context;
32   32  
33   /** Dispatches work inline for symmetric transfer, or enqueues it into the owning `blocking_context`. 33   /** Dispatches work inline for symmetric transfer, or enqueues it into the owning `blocking_context`.
34   34  
35   This executor is used internally by @ref run_blocking to 35   This executor is used internally by @ref run_blocking to
36   execute coroutine tasks on the calling thread. Work submitted 36   execute coroutine tasks on the calling thread. Work submitted
37   via `dispatch()` from within the context's event loop is 37   via `dispatch()` from within the context's event loop is
38   returned for symmetric transfer; from elsewhere it is queued. Work 38   returned for symmetric transfer; from elsewhere it is queued. Work
39   submitted via `post()` is enqueued and processed by the 39   submitted via `post()` is enqueued and processed by the
40   @ref blocking_context event loop. 40   @ref blocking_context event loop.
41   41  
42   Users do not construct this type directly. It is obtained 42   Users do not construct this type directly. It is obtained
43   from @ref blocking_context::get_executor. 43   from @ref blocking_context::get_executor.
44   44  
45   @par Thread Safety 45   @par Thread Safety
46   All member functions are safe to call from any thread. 46   All member functions are safe to call from any thread.
47   47  
48   @see @ref blocking_context, @ref run_blocking 48   @see @ref blocking_context, @ref run_blocking
49   */ 49   */
50   struct BOOST_CAPY_DECL blocking_executor 50   struct BOOST_CAPY_DECL blocking_executor
51   { 51   {
52   /** Construct from a context pointer. 52   /** Construct from a context pointer.
53   53  
54   @param ctx The owning execution context. 54   @param ctx The owning execution context.
55   */ 55   */
HITCBC 56   1117 explicit blocking_executor( 56   1118 explicit blocking_executor(
57   blocking_context* ctx) noexcept 57   blocking_context* ctx) noexcept
HITCBC 58   1117 : ctx_(ctx) 58   1118 : ctx_(ctx)
59   { 59   {
HITCBC 60   1117 } 60   1118 }
61   61  
62   /** Compare two blocking executors for equality. 62   /** Compare two blocking executors for equality.
63   63  
64   Two executors are equal if they share the same context. 64   Two executors are equal if they share the same context.
65   65  
66   @param other The executor to compare against. 66   @param other The executor to compare against.
67   67  
68   @return `true` if both executors share the same context. 68   @return `true` if both executors share the same context.
69   */ 69   */
70   bool 70   bool
71   operator==(blocking_executor const& other) const noexcept; 71   operator==(blocking_executor const& other) const noexcept;
72   72  
73   /** Return the associated execution context. 73   /** Return the associated execution context.
74   74  
75   @return A reference to the owning `blocking_context`. 75   @return A reference to the owning `blocking_context`.
76   */ 76   */
77   blocking_context& 77   blocking_context&
78   context() const noexcept; 78   context() const noexcept;
79   79  
80   /// Called when work is submitted (no-op). 80   /// Called when work is submitted (no-op).
81   void on_work_started() const noexcept; 81   void on_work_started() const noexcept;
82   82  
83   /// Called when work completes (no-op). 83   /// Called when work completes (no-op).
84   void on_work_finished() const noexcept; 84   void on_work_finished() const noexcept;
85   85  
86   /** Dispatch work, running it inline when already in the loop. 86   /** Dispatch work, running it inline when already in the loop.
87   87  
88   Called from within the context's event loop, returns the 88   Called from within the context's event loop, returns the
89   handle for the caller to resume by symmetric transfer. 89   handle for the caller to resume by symmetric transfer.
90   Called from anywhere else, including while the context is 90   Called from anywhere else, including while the context is
91   shutting down, queues the continuation as @ref post does. 91   shutting down, queues the continuation as @ref post does.
92   92  
93   @param c The continuation to execute. 93   @param c The continuation to execute.
94   94  
95   @return `c.h` for symmetric transfer, or 95   @return `c.h` for symmetric transfer, or
96   `std::noop_coroutine()` if the continuation was queued. 96   `std::noop_coroutine()` if the continuation was queued.
97   */ 97   */
98   std::coroutine_handle<> 98   std::coroutine_handle<>
99   dispatch(continuation& c) const; 99   dispatch(continuation& c) const;
100   100  
101   /** Post work for deferred execution. 101   /** Post work for deferred execution.
102   102  
103   Enqueues the coroutine handle into the context's work 103   Enqueues the coroutine handle into the context's work
104   queue. The handle is resumed when the blocking event 104   queue. The handle is resumed when the blocking event
105   loop processes it. 105   loop processes it.
106   106  
107   @param c The continuation to enqueue. 107   @param c The continuation to enqueue.
108   */ 108   */
109   void 109   void
110   post(continuation& c) const; 110   post(continuation& c) const;
111   111  
112   private: 112   private:
113   blocking_context* ctx_; 113   blocking_context* ctx_;
114   }; 114   };
115   115  
116   /** Runs a work queue and event loop on the calling thread until the task completes. 116   /** Runs a work queue and event loop on the calling thread until the task completes.
117   117  
118   Provides a work queue and event loop that runs on the 118   Provides a work queue and event loop that runs on the
119   calling thread. Coroutines dispatched through the 119   calling thread. Coroutines dispatched through the
120   associated @ref blocking_executor have their `post()` 120   associated @ref blocking_executor have their `post()`
121   calls enqueued and processed by @ref run, which blocks 121   calls enqueued and processed by @ref run, which blocks
122 - until @ref signal_done is called. 122 + until @ref signal_done or @ref signal_abandoned is called.
123   123  
124   This context is created internally by @ref run_blocking. 124   This context is created internally by @ref run_blocking.
125   Users do not interact with it directly. 125   Users do not interact with it directly.
126   126  
127   Coroutines still queued when the context is destroyed are 127   Coroutines still queued when the context is destroyed are
128   passed to @ref abandon rather than resumed. 128   passed to @ref abandon rather than resumed.
129   129  
130   @par Thread Safety 130   @par Thread Safety
131   The event loop runs on the thread that calls `run()`. 131   The event loop runs on the thread that calls `run()`.
132   `signal_done()`, `signal_abandoned()`, and `enqueue()` are safe 132   `signal_done()`, `signal_abandoned()`, and `enqueue()` are safe
133   to call from any thread. 133   to call from any thread.
134   134  
135   @see @ref blocking_executor, @ref run_blocking 135   @see @ref blocking_executor, @ref run_blocking
136   */ 136   */
137   class BOOST_CAPY_DECL blocking_context 137   class BOOST_CAPY_DECL blocking_context
138   : public execution_context 138   : public execution_context
139   { 139   {
140   struct impl; 140   struct impl;
141   impl* impl_; 141   impl* impl_;
142   142  
143   public: 143   public:
144   /// Names `blocking_executor` as the type `get_executor()` returns. 144   /// Names `blocking_executor` as the type `get_executor()` returns.
145   using executor_type = blocking_executor; 145   using executor_type = blocking_executor;
146   146  
147   /** Construct a blocking context. 147   /** Construct a blocking context.
148   148  
149   Allocates the internal work queue and 149   Allocates the internal work queue and
150   synchronization state. 150   synchronization state.
151   */ 151   */
152   blocking_context(); 152   blocking_context();
153   153  
154 - /** Destroy the blocking context. */ 154 + /// Destroy the blocking context.
155   ~blocking_context(); 155   ~blocking_context();
156   156  
157   /** Return an executor bound to this context. 157   /** Return an executor bound to this context.
158   158  
159   @return A `blocking_executor` that enqueues work 159   @return A `blocking_executor` that enqueues work
160   into this context's queue. 160   into this context's queue.
161   */ 161   */
162   blocking_executor 162   blocking_executor
163   get_executor() noexcept; 163   get_executor() noexcept;
164   164  
165   /** Signal that the task has completed. 165   /** Signal that the task has completed.
166   166  
167   Wakes the event loop so that @ref run returns. 167   Wakes the event loop so that @ref run returns.
168   */ 168   */
169   void 169   void
170   signal_done() noexcept; 170   signal_done() noexcept;
171   171  
172   /** Signal that the task has completed with an error. 172   /** Signal that the task has completed with an error.
173   173  
174   Stores the exception and wakes the event loop 174   Stores the exception and wakes the event loop
175   so that @ref run rethrows it. 175   so that @ref run rethrows it.
176   176  
177   @param ep The exception to propagate. 177   @param ep The exception to propagate.
178   */ 178   */
179   void 179   void
180   signal_done(std::exception_ptr ep) noexcept; 180   signal_done(std::exception_ptr ep) noexcept;
181   181  
182   /** Signal that the task was abandoned without a result. 182   /** Signal that the task was abandoned without a result.
183   183  
184   Wakes the event loop so that @ref run throws 184   Wakes the event loop so that @ref run throws
185   `std::system_error` with `error::canceled`. 185   `std::system_error` with `error::canceled`.
186   */ 186   */
187   void 187   void
188   signal_abandoned() noexcept; 188   signal_abandoned() noexcept;
189   189  
190   /** Run the event loop until done. 190   /** Run the event loop until done.
191   191  
192   Blocks the calling thread, processing posted 192   Blocks the calling thread, processing posted
193 - coroutine handles until @ref signal_done is called. 193 + coroutine handles until @ref signal_done or
  194 + @ref signal_abandoned is called.
194   After draining remaining work, rethrows any stored 195   After draining remaining work, rethrows any stored
195   exception. 196   exception.
196   197  
197   @par Exception Safety 198   @par Exception Safety
198   Basic guarantee. If the completed task stored an 199   Basic guarantee. If the completed task stored an
199   exception via `signal_done(ep)`, it is rethrown. 200   exception via `signal_done(ep)`, it is rethrown.
200   */ 201   */
201   void 202   void
202   run(); 203   run();
203   204  
204   /** Enqueue a continuation for processing. 205   /** Enqueue a continuation for processing.
205   206  
206   A copy is queued, so the owner it carries reaches @ref abandon 207   A copy is queued, so the owner it carries reaches @ref abandon
207   if the context is destroyed first. 208   if the context is destroyed first.
208   209  
209   @param c The continuation to enqueue. 210   @param c The continuation to enqueue.
210   */ 211   */
211   void 212   void
212   enqueue(continuation const& c); 213   enqueue(continuation const& c);
213   }; 214   };
214   215  
215 - /** Wrapper that signals completion after invoking the handler. 216 + /** Forward a task's outcome to its handlers, then signal the blocking context.
216   217  
217 - Forwards invocations to the contained handler_pair, then 218 + Forwards invocations to the contained handlers, then
218   signals the `blocking_context` so that its event loop 219   signals the `blocking_context` so that its event loop
219 - unblocks. Exceptions thrown by the handler are captured 220 + unblocks. Exceptions thrown by the handlers are captured
220   and stored for later rethrow. 221   and stored for later rethrow.
221 - @tparam H1 The success handler type.  
222 - @tparam H2 The error handler type.  
223 -  
224   222  
225   @par Thread Safety 223   @par Thread Safety
226   Safe to invoke from any thread. 224   Safe to invoke from any thread.
227   225  
  226 + @tparam H1 The success handler type.
  227 + @tparam H2 The error handler type.
  228 +
228   @see @ref run_blocking, @ref blocking_context 229   @see @ref run_blocking, @ref blocking_context
229   */ 230   */
230   template<class H1, class H2> 231   template<class H1, class H2>
231   struct blocking_handler_wrapper 232   struct blocking_handler_wrapper
232   { 233   {
233   /// The context signalled once the handler returns. 234   /// The context signalled once the handler returns.
234   blocking_context* ctx_; 235   blocking_context* ctx_;
235   236  
236   /// The success and error handlers to forward to. 237   /// The success and error handlers to forward to.
237   detail::handler_pair<H1, H2> handlers_; 238   detail::handler_pair<H1, H2> handlers_;
238   239  
239   /** Destroy the wrapper. 240   /** Destroy the wrapper.
240   241  
241   A wrapper destroyed without being invoked belongs to an 242   A wrapper destroyed without being invoked belongs to an
242   abandoned chain, which delivers no result. The context is 243   abandoned chain, which delivers no result. The context is
243   signalled with `error::canceled`, so its loop neither waits 244   signalled with `error::canceled`, so its loop neither waits
244   forever nor reports success. 245   forever nor reports success.
245   */ 246   */
HITCBC 246   5520 ~blocking_handler_wrapper() 247   5525 ~blocking_handler_wrapper()
247   { 248   {
HITCBC 248   5520 if(ctx_) 249   5525 if(ctx_)
HITCBC 249   1 ctx_->signal_abandoned(); 250   1 ctx_->signal_abandoned();
HITCBC 250   5520 } 251   5525 }
251   252  
252   /** Construct from a context and handlers. 253   /** Construct from a context and handlers.
253   254  
254   @param ctx The context to signal. 255   @param ctx The context to signal.
255   256  
256   @param handlers The handlers to forward to. 257   @param handlers The handlers to forward to.
257   */ 258   */
HITCBC 258   1104 blocking_handler_wrapper( 259   1105 blocking_handler_wrapper(
259   blocking_context* ctx, 260   blocking_context* ctx,
260   detail::handler_pair<H1, H2> handlers) 261   detail::handler_pair<H1, H2> handlers)
HITCBC 261   1104 : ctx_(ctx) 262   1105 : ctx_(ctx)
HITCBC 262   1104 , handlers_(std::move(handlers)) 263   1105 , handlers_(std::move(handlers))
263   { 264   {
HITCBC 264   1104 } 265   1105 }
265   266  
266   /** Construct by moving, leaving `other` unable to signal. 267   /** Construct by moving, leaving `other` unable to signal.
267   268  
268   @param other The wrapper to move from. 269   @param other The wrapper to move from.
269   */ 270   */
HITCBC 270   4416 blocking_handler_wrapper(blocking_handler_wrapper&& other) noexcept 271   4420 blocking_handler_wrapper(blocking_handler_wrapper&& other) noexcept
HITCBC 271   4416 : ctx_(std::exchange(other.ctx_, nullptr)) 272   4420 : ctx_(std::exchange(other.ctx_, nullptr))
HITCBC 272   4416 , handlers_(std::move(other.handlers_)) 273   4420 , handlers_(std::move(other.handlers_))
273   { 274   {
HITCBC 274   4416 } 275   4420 }
275   276  
276   /** Invoke the handler with a non-void result. 277   /** Invoke the handler with a non-void result.
277   278  
278   @param v The result value to forward to the handler. 279   @param v The result value to forward to the handler.
279   */ 280   */
280   template<class T> 281   template<class T>
HITCBC 281   48 void operator()(T&& v) 282   48 void operator()(T&& v)
282   { 283   {
283   try 284   try
284   { 285   {
HITCBC 285   48 handlers_(std::forward<T>(v)); 286   48 handlers_(std::forward<T>(v));
286   } 287   }
287   catch(...) 288   catch(...)
288   { 289   {
289   std::exchange(ctx_, nullptr)->signal_done( 290   std::exchange(ctx_, nullptr)->signal_done(
290   std::current_exception()); 291   std::current_exception());
291   return; 292   return;
292   } 293   }
HITCBC 293   48 std::exchange(ctx_, nullptr)->signal_done(); 294   48 std::exchange(ctx_, nullptr)->signal_done();
294   } 295   }
295   296  
296   /** Invoke the handler for a void result. */ 297   /** Invoke the handler for a void result. */
HITCBC 297   719 void operator()() 298   720 void operator()()
298   { 299   {
299   try 300   try
300   { 301   {
HITCBC 301   719 handlers_(); 302   720 handlers_();
302   } 303   }
303   catch(...) 304   catch(...)
304   { 305   {
305   std::exchange(ctx_, nullptr)->signal_done( 306   std::exchange(ctx_, nullptr)->signal_done(
306   std::current_exception()); 307   std::current_exception());
307   return; 308   return;
308   } 309   }
HITCBC 309   719 std::exchange(ctx_, nullptr)->signal_done(); 310   720 std::exchange(ctx_, nullptr)->signal_done();
310   } 311   }
311   312  
312   /** Invoke the handler with an exception. 313   /** Invoke the handler with an exception.
313   314  
314   @param ep The exception to forward to the error handler. 315   @param ep The exception to forward to the error handler.
315   */ 316   */
HITCBC 316   336 void operator()(std::exception_ptr ep) 317   336 void operator()(std::exception_ptr ep)
317   { 318   {
318   try 319   try
319   { 320   {
HITCBC 320   669 handlers_(ep); 321   669 handlers_(ep);
321   } 322   }
HITCBC 322   333 catch(...) 323   333 catch(...)
323   { 324   {
HITCBC 324   333 std::exchange(ctx_, nullptr)->signal_done( 325   333 std::exchange(ctx_, nullptr)->signal_done(
325   std::current_exception()); 326   std::current_exception());
HITCBC 326   333 return; 327   333 return;
327   } 328   }
HITCBC 328   3 std::exchange(ctx_, nullptr)->signal_done(); 329   3 std::exchange(ctx_, nullptr)->signal_done();
329   } 330   }
330   }; 331   };
331   332  
332   /** Starts a `blocking_context`, runs the task on it, and pumps the event loop until it completes. 333   /** Starts a `blocking_context`, runs the task on it, and pumps the event loop until it completes.
333   334  
334   Holds the handlers and optional stop token. When invoked 335   Holds the handlers and optional stop token. When invoked
335   with a task, creates a @ref blocking_context, starts 336   with a task, creates a @ref blocking_context, starts
336   the task via `run_async`, and pumps the event loop until 337   the task via `run_async`, and pumps the event loop until
337   the task completes. 338   the task completes.
338   339  
339   The rvalue ref-qualifier on `operator()` restricts invocation 340   The rvalue ref-qualifier on `operator()` restricts invocation
340   to rvalues, so `run_blocking(h)(task)` is the supported spelling. 341   to rvalues, so `run_blocking(h)(task)` is the supported spelling.
341   `operator()` moves `h1_` out of the wrapper, and `h2_` too unless 342   `operator()` moves `h1_` out of the wrapper, and `h2_` too unless
342   `H2` is `default_handler`. The stop token is copied, not moved. 343   `H2` is `default_handler`. The stop token is copied, not moved.
343   The wrapper is single-use regardless. A stored wrapper needs an 344   The wrapper is single-use regardless. A stored wrapper needs an
344   explicit `std::move` to invoke: 345   explicit `std::move` to invoke:
345   `auto w = run_blocking(h); std::move(w)(task);`. That explicit 346   `auto w = run_blocking(h); std::move(w)(task);`. That explicit
346   `std::move` surfaces the single-use hazard that a bare `w(task)` 347   `std::move` surfaces the single-use hazard that a bare `w(task)`
347   on an lvalue would otherwise hide. 348   on an lvalue would otherwise hide.
348   349  
349 - @tparam H1 The success handler type. 350 + @pre `H1` and `H2` are nothrow move constructible.
350 - @tparam H2 The error handler type.  
351   351  
352   @par Thread Safety 352   @par Thread Safety
353   The wrapper itself should only be used from one thread. 353   The wrapper itself should only be used from one thread.
354   The calling thread blocks until the task completes. 354   The calling thread blocks until the task completes.
355   355  
356   @par Example 356   @par Example
357   @code 357   @code
  358 + task<int> compute();
  359 +
358   int result = 0; 360   int result = 0;
359 - run_blocking([&](int v) { result = v; })(my_task()); 361 + run_blocking([&](int v) { result = v; })(compute());
360   @endcode 362   @endcode
361   363  
  364 + @tparam H1 The success handler type.
  365 + @tparam H2 The error handler type.
  366 +
362   @see @ref run_blocking, @ref run_async 367   @see @ref run_blocking, @ref run_async
363   */ 368   */
364   template<class H1, class H2> 369   template<class H1, class H2>
365   class [[nodiscard]] run_blocking_wrapper 370   class [[nodiscard]] run_blocking_wrapper
366   { 371   {
367   std::stop_token st_; 372   std::stop_token st_;
368   H1 h1_; 373   H1 h1_;
369   H2 h2_; 374   H2 h2_;
370   375  
371   public: 376   public:
372   /** Construct wrapper with stop token and handlers. 377   /** Construct wrapper with stop token and handlers.
373   378  
374   @param st The stop token for cooperative cancellation. 379   @param st The stop token for cooperative cancellation.
375   @param h1 The success handler. 380   @param h1 The success handler.
376   @param h2 The error handler. 381   @param h2 The error handler.
377   */ 382   */
HITCBC 378   1104 run_blocking_wrapper( 383   1105 run_blocking_wrapper(
379   std::stop_token st, 384   std::stop_token st,
380   H1 h1, 385   H1 h1,
381   H2 h2) 386   H2 h2)
HITCBC 382   1104 : st_(std::move(st)) 387   1105 : st_(std::move(st))
HITCBC 383   1104 , h1_(std::move(h1)) 388   1105 , h1_(std::move(h1))
HITCBC 384   1104 , h2_(std::move(h2)) 389   1105 , h2_(std::move(h2))
385   { 390   {
HITCBC 386   1104 } 391   1105 }
387   392  
388   /** Copy construction is disabled; the wrapper is single-use. 393   /** Copy construction is disabled; the wrapper is single-use.
389   394  
390   @param other The wrapper that would be copied. 395   @param other The wrapper that would be copied.
391   */ 396   */
392   run_blocking_wrapper(run_blocking_wrapper const& other) = delete; 397   run_blocking_wrapper(run_blocking_wrapper const& other) = delete;
393   398  
394   /** Move construction is disabled; the wrapper is single-use. 399   /** Move construction is disabled; the wrapper is single-use.
395   400  
396   @param other The wrapper that would be moved from. 401   @param other The wrapper that would be moved from.
397   */ 402   */
398   run_blocking_wrapper(run_blocking_wrapper&& other) = delete; 403   run_blocking_wrapper(run_blocking_wrapper&& other) = delete;
399   404  
400   /** Copy assignment is disabled; the wrapper is single-use. 405   /** Copy assignment is disabled; the wrapper is single-use.
401   406  
402   @param other The wrapper that would be assigned from. 407   @param other The wrapper that would be assigned from.
403   408  
404   @return A reference to `*this`. 409   @return A reference to `*this`.
405   */ 410   */
406   run_blocking_wrapper& operator=(run_blocking_wrapper const& other) = delete; 411   run_blocking_wrapper& operator=(run_blocking_wrapper const& other) = delete;
407   412  
408   /** Move assignment is disabled; the wrapper is single-use. 413   /** Move assignment is disabled; the wrapper is single-use.
409   414  
410   @param other The wrapper that would be moved from. 415   @param other The wrapper that would be moved from.
411   416  
412   @return A reference to `*this`. 417   @return A reference to `*this`.
413   */ 418   */
414   run_blocking_wrapper& operator=(run_blocking_wrapper&& other) = delete; 419   run_blocking_wrapper& operator=(run_blocking_wrapper&& other) = delete;
415   420  
416   /** Start the task and block until completion. 421   /** Start the task and block until completion.
417   422  
418   Creates a blocking_context with a single-threaded 423   Creates a blocking_context with a single-threaded
419   event loop, starts the task via `run_async`, then 424   event loop, starts the task via `run_async`, then
420   pumps the loop until the task completes or throws. 425   pumps the loop until the task completes or throws.
421   426  
422   @throws std::system_error with `error::canceled` if the task 427   @throws std::system_error with `error::canceled` if the task
423   is abandoned before it completes. 428   is abandoned before it completes.
424   429  
425 - @tparam Task The ContextRunnable type. 430 + @throws Any exception the task exits with that no error
  431 + handler consumes, or any exception a handler throws.
426   432  
427   @param t The task to execute. 433   @param t The task to execute.
428   */ 434   */
429   template<ContextRunnable Task> 435   template<ContextRunnable Task>
430   void 436   void
HITCBC 431   1104 operator()(Task t) && 437   1105 operator()(Task t) &&
432   { 438   {
HITCBC 433   1104 blocking_context ctx; 439   1105 blocking_context ctx;
434   440  
HITCBC 435   2208 auto make_handlers = [&]() { 441   2210 auto make_handlers = [&]() {
436   if constexpr( 442   if constexpr(
437   std::is_same_v<H2, detail::default_handler>) 443   std::is_same_v<H2, detail::default_handler>)
438   return detail::handler_pair<H1, H2>{ 444   return detail::handler_pair<H1, H2>{
HITCBC 439   1099 std::move(h1_)}; 445   1100 std::move(h1_)};
440   else 446   else
441   return detail::handler_pair<H1, H2>{ 447   return detail::handler_pair<H1, H2>{
HITCBC 442   5 std::move(h1_), std::move(h2_)}; 448   5 std::move(h1_), std::move(h2_)};
443   }; 449   };
444   450  
HITCBC 445   1104 run_async( 451   1105 run_async(
446   ctx.get_executor(), 452   ctx.get_executor(),
HITCBC 447   1104 st_, 453   1105 st_,
448   blocking_handler_wrapper<H1, H2>{ 454   blocking_handler_wrapper<H1, H2>{
449   &ctx, make_handlers()} 455   &ctx, make_handlers()}
HITCBC 450   1104 )(std::move(t)); 456   1105 )(std::move(t));
451   457  
HITCBC 452   1104 ctx.run(); 458   1105 ctx.run();
HITCBC 453   1104 } 459   1105 }
454   }; 460   };
455   461  
456 - /** Block until task completes and discard result. 462 + /** Create a wrapper that runs a task to completion and discards its result.
457   463  
458 - Executes a lazy task on a single-threaded event loop 464 + Invoking the returned wrapper with a task executes it on a
459 - and blocks the calling thread until the task completes 465 + single-threaded event loop. The call blocks the calling thread
460 - or throws. 466 + until the task completes or throws.
461   467  
462   @par Exception Safety 468   @par Exception Safety
463   Basic guarantee. If the task throws, the exception is 469   Basic guarantee. If the task throws, the exception is
464   rethrown to the caller. 470   rethrown to the caller.
  471 + If the task is abandoned before it completes, the call throws
  472 + `std::system_error` with `error::canceled`.
465   473  
466   @par Example 474   @par Example
467   @par !example example_2 475   @par !example example_2
468   476  
469   477  
470   @return A wrapper that accepts a task for blocking execution. 478   @return A wrapper that accepts a task for blocking execution.
471   479  
472   @see @ref run_async 480   @see @ref run_async
473   */ 481   */
474   [[nodiscard]] inline auto 482   [[nodiscard]] inline auto
HITCBC 475   1045 run_blocking() 483   1046 run_blocking()
476   { 484   {
477   return run_blocking_wrapper< 485   return run_blocking_wrapper<
478   detail::default_handler, 486   detail::default_handler,
479   detail::default_handler>( 487   detail::default_handler>(
HITCBC 480   2090 std::stop_token{}, 488   2092 std::stop_token{},
481   detail::default_handler{}, 489   detail::default_handler{},
HITCBC 482   1045 detail::default_handler{}); 490   1046 detail::default_handler{});
483   } 491   }
484   492  
485 - /** Block until task completes and invoke handler with result. 493 + /** Create a wrapper that runs a task to completion and passes its result to a handler.
486   494  
487 - Executes a lazy task on a single-threaded event loop 495 + Invoking the returned wrapper with a task executes it on a
488 - and blocks until completion. The handler `h1` is called 496 + single-threaded event loop and blocks until completion. The
489 - with the result on success. If `h1` is also invocable 497 + handler `h1` is called with the result on success. If `h1` is
490 - with `std::exception_ptr`, it handles exceptions too. 498 + also invocable with `std::exception_ptr`, it handles exceptions
491 - Otherwise, exceptions are rethrown. 499 + too. Otherwise, exceptions are rethrown.
  500 +
  501 + @pre `H1` is nothrow move constructible.
492   502  
493   @par Exception Safety 503   @par Exception Safety
494   Basic guarantee. Exceptions from the task are passed 504   Basic guarantee. Exceptions from the task are passed
495   to `h1` if it accepts `std::exception_ptr`, otherwise 505   to `h1` if it accepts `std::exception_ptr`, otherwise
496   rethrown. 506   rethrown.
  507 + If the task is abandoned before it completes, the call throws
  508 + `std::system_error` with `error::canceled`.
497   509  
498   @par Example 510   @par Example
499   @par !example example_3 511   @par !example example_3
500   512  
501   513  
502   @param h1 Handler invoked with the result on success, 514   @param h1 Handler invoked with the result on success,
503   and optionally with `std::exception_ptr` on failure. 515   and optionally with `std::exception_ptr` on failure.
504   516  
505   @return A wrapper that accepts a task for blocking execution. 517   @return A wrapper that accepts a task for blocking execution.
506   518  
507   @see @ref run_async 519   @see @ref run_async
508   */ 520   */
509   template<class H1> 521   template<class H1>
510   [[nodiscard]] auto 522   [[nodiscard]] auto
HITCBC 511   44 run_blocking(H1 h1) 523   44 run_blocking(H1 h1)
512   { 524   {
513   return run_blocking_wrapper< 525   return run_blocking_wrapper<
514   H1, 526   H1,
515   detail::default_handler>( 527   detail::default_handler>(
HITCBC 516   88 std::stop_token{}, 528   88 std::stop_token{},
HITCBC 517   44 std::move(h1), 529   44 std::move(h1),
HITCBC 518   44 detail::default_handler{}); 530   44 detail::default_handler{});
519   } 531   }
520   532  
521 - /** Block until task completes with separate handlers. 533 + /** Create a wrapper that runs a task to completion with separate handlers.
522   534  
523 - Executes a lazy task on a single-threaded event loop 535 + Invoking the returned wrapper with a task executes it on a
524 - and blocks until completion. The handler `h1` is called 536 + single-threaded event loop and blocks until completion. The
525 - on success, `h2` on failure. 537 + handler `h1` is called on success, `h2` on failure.
  538 +
  539 + @pre `H1` and `H2` are nothrow move constructible.
526   540  
527   @par Exception Safety 541   @par Exception Safety
528   Basic guarantee. Exceptions from the task are passed 542   Basic guarantee. Exceptions from the task are passed
529   to `h2`. 543   to `h2`.
  544 + If the task is abandoned before it completes, the call throws
  545 + `std::system_error` with `error::canceled`.
530   546  
531   @par Example 547   @par Example
532   @par !example example_1 548   @par !example example_1
533   549  
534   550  
535   @param h1 Handler invoked with the result on success. 551   @param h1 Handler invoked with the result on success.
536   @param h2 Handler invoked with the exception on failure. 552   @param h2 Handler invoked with the exception on failure.
537   553  
538   @return A wrapper that accepts a task for blocking execution. 554   @return A wrapper that accepts a task for blocking execution.
539   555  
540   @see @ref run_async 556   @see @ref run_async
541   */ 557   */
542   template<class H1, class H2> 558   template<class H1, class H2>
543   [[nodiscard]] auto 559   [[nodiscard]] auto
HITCBC 544   4 run_blocking(H1 h1, H2 h2) 560   4 run_blocking(H1 h1, H2 h2)
545   { 561   {
546   return run_blocking_wrapper< 562   return run_blocking_wrapper<
547   H1, 563   H1,
548   H2>( 564   H2>(
HITCBC 549   8 std::stop_token{}, 565   8 std::stop_token{},
HITCBC 550   4 std::move(h1), 566   4 std::move(h1),
HITCBC 551   8 std::move(h2)); 567   8 std::move(h2));
552   } 568   }
553   569  
554 - /** Block until task completes with stop token support. 570 + /** Create a wrapper that runs a task to completion under a stop token.
555   571  
556 - Executes a lazy task on a single-threaded event loop 572 + Invoking the returned wrapper with a task executes it on a
557 - with the given stop token and blocks until completion. 573 + single-threaded event loop with the given stop token. The call
  574 + blocks until completion.
558   575  
559   @par Exception Safety 576   @par Exception Safety
560   Basic guarantee. If the task throws, the exception is 577   Basic guarantee. If the task throws, the exception is
561   rethrown to the caller. 578   rethrown to the caller.
  579 + If the task is abandoned before it completes, the call throws
  580 + `std::system_error` with `error::canceled`.
562   581  
563   @param st The stop token for cooperative cancellation. 582   @param st The stop token for cooperative cancellation.
564   583  
565   @return A wrapper that accepts a task for blocking execution. 584   @return A wrapper that accepts a task for blocking execution.
566   585  
567   @see @ref run_async 586   @see @ref run_async
568   */ 587   */
569   [[nodiscard]] inline auto 588   [[nodiscard]] inline auto
HITCBC 570   7 run_blocking(std::stop_token st) 589   7 run_blocking(std::stop_token st)
571   { 590   {
572   return run_blocking_wrapper< 591   return run_blocking_wrapper<
573   detail::default_handler, 592   detail::default_handler,
574   detail::default_handler>( 593   detail::default_handler>(
HITCBC 575   7 std::move(st), 594   7 std::move(st),
576   detail::default_handler{}, 595   detail::default_handler{},
HITCBC 577   7 detail::default_handler{}); 596   7 detail::default_handler{});
578   } 597   }
579   598  
580 - /** Block until task completes with stop token and handler. 599 + /** Create a wrapper that runs a task to completion under a stop token, passing its result to a handler.
  600 +
  601 + Invoking the returned wrapper with a task executes it on a
  602 + single-threaded event loop with the given stop token, and
  603 + blocks until completion. The handler `h1` is called with the
  604 + result on success. If `h1` is also invocable with
  605 + `std::exception_ptr`, it handles exceptions too. Otherwise,
  606 + exceptions are rethrown.
  607 +
  608 + @pre `H1` is nothrow move constructible.
  609 +
  610 + @par Exception Safety
  611 + Basic guarantee. Exceptions from the task are passed
  612 + to `h1` if it accepts `std::exception_ptr`, otherwise
  613 + rethrown.
  614 + If the task is abandoned before it completes, the call throws
  615 + `std::system_error` with `error::canceled`.
581   616  
582   @param st The stop token for cooperative cancellation. 617   @param st The stop token for cooperative cancellation.
583   @param h1 Handler invoked with the result on success. 618   @param h1 Handler invoked with the result on success.
584   619  
585   @return A wrapper that accepts a task for blocking execution. 620   @return A wrapper that accepts a task for blocking execution.
586   621  
587   @see @ref run_async 622   @see @ref run_async
588   */ 623   */
589   template<class H1> 624   template<class H1>
590   [[nodiscard]] auto 625   [[nodiscard]] auto
HITCBC 591   3 run_blocking(std::stop_token st, H1 h1) 626   3 run_blocking(std::stop_token st, H1 h1)
592   { 627   {
593   return run_blocking_wrapper< 628   return run_blocking_wrapper<
594   H1, 629   H1,
595   detail::default_handler>( 630   detail::default_handler>(
HITCBC 596   3 std::move(st), 631   3 std::move(st),
HITCBC 597   3 std::move(h1), 632   3 std::move(h1),
HITCBC 598   3 detail::default_handler{}); 633   3 detail::default_handler{});
599   } 634   }
600   635  
601 - /** Block until task completes with stop token and handlers. 636 + /** Create a wrapper that runs a task to completion under a stop token with separate handlers.
  637 +
  638 + Invoking the returned wrapper with a task executes it on a
  639 + single-threaded event loop with the given stop token, and
  640 + blocks until completion. The handler `h1` is called on
  641 + success, `h2` on failure.
  642 +
  643 + @pre `H1` and `H2` are nothrow move constructible.
  644 +
  645 + @par Exception Safety
  646 + Basic guarantee. Exceptions from the task are passed
  647 + to `h2`.
  648 + If the task is abandoned before it completes, the call throws
  649 + `std::system_error` with `error::canceled`.
602   650  
603   @param st The stop token for cooperative cancellation. 651   @param st The stop token for cooperative cancellation.
604   @param h1 Handler invoked with the result on success. 652   @param h1 Handler invoked with the result on success.
605   @param h2 Handler invoked with the exception on failure. 653   @param h2 Handler invoked with the exception on failure.
606   654  
607   @return A wrapper that accepts a task for blocking execution. 655   @return A wrapper that accepts a task for blocking execution.
608   656  
609   @see @ref run_async 657   @see @ref run_async
610   */ 658   */
611   template<class H1, class H2> 659   template<class H1, class H2>
612   [[nodiscard]] auto 660   [[nodiscard]] auto
HITCBC 613   1 run_blocking(std::stop_token st, H1 h1, H2 h2) 661   1 run_blocking(std::stop_token st, H1 h1, H2 h2)
614   { 662   {
615   return run_blocking_wrapper< 663   return run_blocking_wrapper<
616   H1, 664   H1,
617   H2>( 665   H2>(
HITCBC 618   1 std::move(st), 666   1 std::move(st),
HITCBC 619   1 std::move(h1), 667   1 std::move(h1),
HITCBC 620   2 std::move(h2)); 668   2 std::move(h2));
621   } 669   }
622   670  
623   } // namespace test 671   } // namespace test
624   } // namespace capy 672   } // namespace capy
625   } // namespace boost 673   } // namespace boost
626   674  
627   #endif 675   #endif