LCOV - code coverage report
Current view: top level - capy/ex - run_async.hpp (source / functions) Coverage Total Hit Missed
Test: coverage_remapped.info Lines: 100.0 % 210 210
Test Date: 2026-11-25 23:19:07 Functions: 97.1 % 5344 5188 156

           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_RUN_ASYNC_HPP
      12                 : #define BOOST_CAPY_RUN_ASYNC_HPP
      13                 : 
      14                 : #include <boost/capy/detail/config.hpp>
      15                 : #include <boost/capy/detail/run.hpp>
      16                 : #include <boost/capy/detail/run_callbacks.hpp>
      17                 : #include <boost/capy/concept/executor.hpp>
      18                 : #include <boost/capy/concept/context_runnable.hpp>
      19                 : #include <boost/capy/ex/abandon.hpp>
      20                 : #include <boost/capy/ex/execution_context.hpp>
      21                 : #include <boost/capy/ex/frame_allocator.hpp>
      22                 : #include <boost/capy/ex/await_context.hpp>
      23                 : #include <boost/capy/ex/recycling_memory_resource.hpp>
      24                 : #include <boost/capy/ex/work_guard.hpp>
      25                 : 
      26                 : #include <coroutine>
      27                 : #include <cstring>
      28                 : #include <exception>
      29                 : #include <memory_resource>
      30                 : #include <new>
      31                 : #include <stop_token>
      32                 : #include <type_traits>
      33                 : 
      34                 : namespace boost {
      35                 : namespace capy {
      36                 : namespace detail {
      37                 : 
      38                 : /** Match types usable as `run_async` completion handlers.
      39                 : 
      40                 :     Excludes the types meaningful to the other `run_async` parameters.
      41                 :     A stop token, memory resource pointer, or allocator argument
      42                 :     therefore selects its dedicated overload by conversion. It does not
      43                 :     deduce as an exact-match handler.
      44                 : */
      45                 : template<class H>
      46                 : concept RunAsyncHandler =
      47                 :     !std::is_convertible_v<H, std::pmr::memory_resource*> &&
      48                 :     !std::is_convertible_v<H, std::stop_token> &&
      49                 :     !Allocator<H>;
      50                 : 
      51                 : /// Function pointer type for type-erased frame deallocation.
      52                 : using dealloc_fn = void(*)(void*, std::size_t);
      53                 : 
      54                 : /// Type-erased deallocator implementation for trampoline frames.
      55                 : template<class Alloc>
      56 HIT        2326 : void dealloc_impl(void* raw, std::size_t total)
      57                 : {
      58                 :     static_assert(std::is_same_v<typename Alloc::value_type, frame_unit>);
      59            2326 :     auto* a = std::launder(reinterpret_cast<Alloc*>(
      60            2326 :         static_cast<char*>(raw) + total - sizeof(Alloc)));
      61            2326 :     Alloc ua(std::move(*a));
      62              17 :     a->~Alloc();
      63            2326 :     ua.deallocate(static_cast<frame_unit*>(raw), frame_units(total));
      64            2326 : }
      65                 : 
      66                 : /// Awaiter to access the promise from within the coroutine.
      67                 : template<class Promise>
      68                 : struct get_promise_awaiter
      69                 : {
      70                 :     Promise* p_ = nullptr;
      71                 : 
      72            5782 :     bool await_ready() const noexcept { return false; }
      73                 : 
      74            5782 :     bool await_suspend(std::coroutine_handle<Promise> h) noexcept
      75                 :     {
      76            5782 :         p_ = &h.promise();
      77            5782 :         return false;
      78                 :     }
      79                 : 
      80            5782 :     Promise& await_resume() const noexcept
      81                 :     {
      82            5782 :         return *p_;
      83                 :     }
      84                 : };
      85                 : 
      86                 : /** Internal run_async_trampoline coroutine for run_async.
      87                 : 
      88                 :     The run_async_trampoline is allocated BEFORE the task (via C++17 postfix evaluation
      89                 :     order) and serves as the task's continuation. When the task final_suspends,
      90                 :     control returns to the run_async_trampoline which then invokes the appropriate handler.
      91                 : 
      92                 :     For value-type allocators, the run_async_trampoline stores a frame_memory_resource
      93                 :     that wraps the allocator. For memory_resource*, it stores the pointer directly.
      94                 : 
      95                 :     @tparam Ex The executor type.
      96                 :     @tparam Handlers The handler type (default_handler or handler_pair).
      97                 :     @tparam Alloc The allocator type (value type or memory_resource*).
      98                 : */
      99                 : template<class Ex, class Handlers, class Alloc>
     100                 : struct run_async_trampoline
     101                 : {
     102                 :     using invoke_fn = void(*)(void*, Handlers&);
     103                 : 
     104                 :     struct promise_type
     105                 :     {
     106                 :         work_guard<Ex> wg_;
     107                 :         Handlers handlers_;
     108                 :         frame_memory_resource<Alloc> resource_;
     109                 :         await_context env_;
     110                 :         invoke_fn invoke_ = nullptr;
     111                 :         void* task_promise_ = nullptr;
     112                 :         // task_h_: raw handle for frame_guard cleanup in make_trampoline.
     113                 :         // task_cont_: continuation wrapping the same handle for executor dispatch.
     114                 :         // Both must reference the same coroutine and be kept in sync.
     115                 :         std::coroutine_handle<> task_h_;
     116                 :         continuation task_cont_;
     117                 :         // Owns the task's segment: abandoning it resumes this
     118                 :         // trampoline, or has it destroyed, to unwind the task.
     119                 :         chain_owner owner_;
     120                 : 
     121            2326 :         ~promise_type()
     122                 :         {
     123                 :             // Still owned only when a closed executor destroyed this
     124                 :             // trampoline instead of resuming it to unwind the task.
     125            2326 :             if(task_h_)
     126               7 :                 task_h_.destroy();
     127            2326 :         }
     128                 : 
     129            2326 :         promise_type(Ex& ex, Handlers& h, Alloc& a) noexcept
     130            2326 :             : wg_(std::move(ex))
     131            2326 :             , handlers_(std::move(h))
     132            2326 :             , resource_(std::move(a))
     133                 :         {
     134            2326 :         }
     135                 : 
     136                 :         // Allocates whole frame units: a standard allocator takes no
     137                 :         // alignment argument, and one rebound to std::byte may place
     138                 :         // this frame at byte alignment.
     139                 :         using unit_alloc = typename std::allocator_traits<Alloc>
     140                 :             ::template rebind_alloc<frame_unit>;
     141                 : 
     142                 :         static_assert(
     143                 :             alignof(unit_alloc) <= frame_alignment,
     144                 :             "Alloc must not be over-aligned");
     145                 : 
     146                 :         static constexpr std::size_t
     147           13956 :         round_up(std::size_t n, std::size_t align) noexcept
     148                 :         {
     149           13956 :             return (n + align - 1) & ~(align - 1);
     150                 :         }
     151                 : 
     152                 :         // The block holds the frame, then the deallocator, then the
     153                 :         // allocator copy, each at its own alignment. The copy comes
     154                 :         // last, where dealloc_impl looks for it.
     155                 :         static constexpr std::size_t
     156            9304 :         fn_offset(std::size_t size) noexcept
     157                 :         {
     158            9304 :             return round_up(size, alignof(dealloc_fn));
     159                 :         }
     160                 : 
     161                 :         static constexpr std::size_t
     162            4652 :         block_size(std::size_t size) noexcept
     163                 :         {
     164            4652 :             return round_up(
     165            4652 :                 fn_offset(size) + sizeof(dealloc_fn),
     166            4652 :                 alignof(unit_alloc)) + sizeof(unit_alloc);
     167                 :         }
     168                 : 
     169            2326 :         static void* operator new(
     170                 :             std::size_t size, Ex const&, Handlers const&, Alloc a)
     171                 :         {
     172            2326 :             auto const total = block_size(size);
     173                 : 
     174              17 :             unit_alloc ua(std::move(a));
     175            2326 :             void* raw = ua.allocate(frame_units(total));
     176                 : 
     177            2326 :             auto* fn_loc = reinterpret_cast<dealloc_fn*>(
     178            2326 :                 static_cast<char*>(raw) + fn_offset(size));
     179            2326 :             *fn_loc = &dealloc_impl<unit_alloc>;
     180                 : 
     181            2341 :             new (static_cast<char*>(raw) + total - sizeof(unit_alloc))
     182            2326 :                 unit_alloc(std::move(ua));
     183                 : 
     184            6944 :             return raw;
     185              14 :         }
     186                 : 
     187            2326 :         static void operator delete(void* ptr, std::size_t size)
     188                 :         {
     189            2326 :             auto* fn = reinterpret_cast<dealloc_fn*>(
     190            2326 :                 static_cast<char*>(ptr) + fn_offset(size));
     191            2326 :             (*fn)(ptr, block_size(size));
     192            2326 :         }
     193                 : 
     194            4652 :         std::pmr::memory_resource* get_resource() noexcept
     195                 :         {
     196            4652 :             return &resource_;
     197                 :         }
     198                 : 
     199            2326 :         run_async_trampoline get_return_object() noexcept
     200                 :         {
     201                 :             return run_async_trampoline{
     202            2326 :                 std::coroutine_handle<promise_type>::from_promise(*this)};
     203                 :         }
     204                 : 
     205            2326 :         std::suspend_always initial_suspend() noexcept
     206                 :         {
     207            2326 :             return {};
     208                 :         }
     209                 : 
     210            2319 :         std::suspend_never final_suspend() noexcept
     211                 :         {
     212            2319 :             return {};
     213                 :         }
     214                 : 
     215            2319 :         void return_void() noexcept
     216                 :         {
     217            2319 :         }
     218                 : 
     219                 :         // An exception reaches here only by escaping a handler: a handler
     220                 :         // that threw, or the default handler rethrowing an otherwise
     221                 :         // unhandled task exception. It has no owner to receive it: fail
     222                 :         // fast.
     223                 :         void unhandled_exception() noexcept { std::terminate(); } // LCOV_EXCL_LINE
     224                 :     };
     225                 : 
     226                 :     std::coroutine_handle<promise_type> h_;
     227                 : 
     228                 :     template<ContextRunnable Task>
     229            2306 :     static void invoke_impl(void* p, Handlers& h)
     230                 :     {
     231                 :         using R = decltype(std::declval<Task&>().await_resume());
     232            2306 :         auto& promise = *static_cast<typename Task::promise_type*>(p);
     233            2306 :         if(promise.exception())
     234               1 :             h(promise.exception());
     235                 :         else if constexpr(std::is_void_v<R>)
     236            2302 :             h();
     237                 :         else
     238               3 :             h(std::move(promise.result()));
     239            2306 :     }
     240                 : };
     241                 : 
     242                 : /** Specialization for memory_resource* - stores pointer directly.
     243                 : 
     244                 :     This avoids double indirection when the user passes a memory_resource*.
     245                 : */
     246                 : template<class Ex, class Handlers>
     247                 : struct run_async_trampoline<Ex, Handlers, std::pmr::memory_resource*>
     248                 : {
     249                 :     using invoke_fn = void(*)(void*, Handlers&);
     250                 : 
     251                 :     struct promise_type
     252                 :     {
     253                 :         work_guard<Ex> wg_;
     254                 :         Handlers handlers_;
     255                 :         std::pmr::memory_resource* mr_;
     256                 :         await_context env_;
     257                 :         invoke_fn invoke_ = nullptr;
     258                 :         void* task_promise_ = nullptr;
     259                 :         // task_h_: raw handle for frame_guard cleanup in make_trampoline.
     260                 :         // task_cont_: continuation wrapping the same handle for executor dispatch.
     261                 :         // Both must reference the same coroutine and be kept in sync.
     262                 :         std::coroutine_handle<> task_h_;
     263                 :         continuation task_cont_;
     264                 :         // Owns the task's segment: abandoning it resumes this
     265                 :         // trampoline, or has it destroyed, to unwind the task.
     266                 :         chain_owner owner_;
     267                 : 
     268          103672 :         ~promise_type()
     269                 :         {
     270                 :             // Still owned only when a closed executor destroyed this
     271                 :             // trampoline instead of resuming it to unwind the task.
     272          103672 :             if(task_h_)
     273          100208 :                 task_h_.destroy();
     274          103672 :         }
     275                 : 
     276          103672 :         promise_type(
     277                 :             Ex& ex, Handlers& h, std::pmr::memory_resource* mr) noexcept
     278          103672 :             : wg_(std::move(ex))
     279          103672 :             , handlers_(std::move(h))
     280          103672 :             , mr_(mr)
     281                 :         {
     282          103672 :         }
     283                 : 
     284          103672 :         static void* operator new(
     285                 :             std::size_t size, Ex const&, Handlers const&,
     286                 :             std::pmr::memory_resource* mr)
     287                 :         {
     288          103672 :             auto total = size + sizeof(mr);
     289          103672 :             void* raw = mr->allocate(total, frame_alignment);
     290          103672 :             std::memcpy(static_cast<char*>(raw) + size, &mr, sizeof(mr));
     291          103672 :             return raw;
     292                 :         }
     293                 : 
     294          103672 :         static void operator delete(void* ptr, std::size_t size)
     295                 :         {
     296                 :             std::pmr::memory_resource* mr;
     297          103672 :             std::memcpy(&mr, static_cast<char*>(ptr) + size, sizeof(mr));
     298          103672 :             auto total = size + sizeof(mr);
     299          103672 :             mr->deallocate(ptr, total, frame_alignment);
     300          103672 :         }
     301                 : 
     302          207343 :         std::pmr::memory_resource* get_resource() noexcept
     303                 :         {
     304          207343 :             return mr_;
     305                 :         }
     306                 : 
     307          103672 :         run_async_trampoline get_return_object() noexcept
     308                 :         {
     309                 :             return run_async_trampoline{
     310          103672 :                 std::coroutine_handle<promise_type>::from_promise(*this)};
     311                 :         }
     312                 : 
     313          103672 :         std::suspend_always initial_suspend() noexcept
     314                 :         {
     315          103672 :             return {};
     316                 :         }
     317                 : 
     318            3463 :         std::suspend_never final_suspend() noexcept
     319                 :         {
     320            3463 :             return {};
     321                 :         }
     322                 : 
     323            3463 :         void return_void() noexcept
     324                 :         {
     325            3463 :         }
     326                 : 
     327                 :         // See primary template: an escaping handler exception is fatal.
     328                 :         void unhandled_exception() noexcept { std::terminate(); } // LCOV_EXCL_LINE
     329                 :     };
     330                 : 
     331                 :     std::coroutine_handle<promise_type> h_;
     332                 : 
     333                 :     template<ContextRunnable Task>
     334            2335 :     static void invoke_impl(void* p, Handlers& h)
     335                 :     {
     336                 :         using R = decltype(std::declval<Task&>().await_resume());
     337            2335 :         auto& promise = *static_cast<typename Task::promise_type*>(p);
     338            2335 :         if(promise.exception())
     339             363 :             h(promise.exception());
     340                 :         else if constexpr(std::is_void_v<R>)
     341            1689 :             h();
     342                 :         else
     343             283 :             h(std::move(promise.result()));
     344            2335 :     }
     345                 : };
     346                 : 
     347                 : /// Coroutine body for run_async_trampoline - invokes handlers then destroys task.
     348                 : template<class Ex, class Handlers, class Alloc>
     349                 : run_async_trampoline<Ex, Handlers, Alloc>
     350          105998 : make_trampoline(Ex, Handlers, Alloc)
     351                 : {
     352                 :     // promise_type ctor steals the parameters
     353                 :     auto& p = co_await get_promise_awaiter<
     354                 :         typename run_async_trampoline<Ex, Handlers, Alloc>::promise_type>{};
     355                 : 
     356                 :     // Guard ensures the task frame is destroyed even when invoke_
     357                 :     // throws (e.g. default_handler rethrows an unhandled exception).
     358                 :     struct frame_guard
     359                 :     {
     360                 :         std::coroutine_handle<>& h;
     361            5782 :         ~frame_guard() { std::exchange(h, nullptr).destroy(); }
     362                 :     } guard{p.task_h_};
     363                 : 
     364                 :     // Resumed with the task unfinished: the segment was abandoned, and
     365                 :     // destroying the task unwinds it without calling a handler.
     366                 :     if(!p.owner_.abandoned())
     367                 :         p.invoke_(p.task_promise_, p.handlers_);
     368          216614 : }
     369                 : 
     370                 : } // namespace detail
     371                 : 
     372                 : /** Installs the frame allocator, then starts the task on the executor when called once.
     373                 : 
     374                 :     This wrapper holds the start state: the executor, stop token, and
     375                 :     completion handlers. The start state is allocated when the wrapper
     376                 :     is constructed, before the task argument is evaluated (C++17
     377                 :     postfix evaluation order).
     378                 : 
     379                 :     The rvalue ref-qualifier on `operator()` ensures the wrapper can only
     380                 :     be used as a temporary, preventing misuse that would violate LIFO ordering.
     381                 : 
     382                 :     @par Thread Safety
     383                 :     The wrapper itself should only be used from one thread. The
     384                 :     completion handlers may be invoked from any thread where the
     385                 :     executor schedules work.
     386                 : 
     387                 :     @warning **Always construct the task as the direct argument of the
     388                 :     two-call expression `run_async(ex)(task)`.** The wrapper's constructor
     389                 :     installs the frame allocator in thread-local storage. The task's
     390                 :     `operator new` reads that thread-local state. Each of the following
     391                 :     splits allocates the task's coroutine frame under the wrong
     392                 :     allocator, silently, with no compile error.
     393                 :     @li *Preconstructed task.* Passing a task constructed earlier, as
     394                 :         in `auto t = my_task(); run_async(ex)(std::move(t));`. A
     395                 :         moved-from local, or a task returned from an earlier statement,
     396                 :         is the same case. The frame exists before the allocator is
     397                 :         installed.
     398                 :     @li *Wrapper function.* Forwarding the task through a helper that
     399                 :         itself performs the two-call pattern constructs the task as an
     400                 :         argument to the helper. It is therefore constructed before the
     401                 :         helper's body runs, and so before `run_async` runs. An example is
     402                 :         `submit(ex, my_task())`, where `submit` calls
     403                 :         `run_async(ex)(std::forward<Task>(t))` internally.
     404                 : 
     405                 :     Storing the wrapper itself (`auto w = run_async(ex);`) compiles,
     406                 :     because guaranteed copy elision constructs `w` in place. Calling it
     407                 :     as an lvalue, `w(my_task())`, does not compile.
     408                 :     `std::move(w)(my_task())` compiles. It allocates from the right
     409                 :     resource only if nothing changes the thread-local frame allocator
     410                 :     in between. Creating or destroying another wrapper changes it, as
     411                 :     does any `co_await` or task start.
     412                 : 
     413                 :     See the Frame Allocators guide
     414                 :     (`doc/modules/ROOT/pages/4.coroutines/4h.allocators.adoc`) for the full
     415                 :     C++17-evaluation-order rationale behind this constraint.
     416                 : 
     417                 :     @par Example
     418                 :     @par !example example
     419                 : 
     420                 : 
     421                 :     @tparam Ex The executor type satisfying the `Executor` concept.
     422                 :     @tparam Handlers The handler type (default_handler or handler_pair).
     423                 :     @tparam Alloc The allocator type (value type or memory_resource*).
     424                 : 
     425                 :     @see @ref run_async
     426                 : */
     427                 : template<Executor Ex, class Handlers, class Alloc>
     428                 : class [[nodiscard]] run_async_wrapper
     429                 : {
     430                 :     detail::run_async_trampoline<Ex, Handlers, Alloc> tr_;
     431                 :     std::stop_token st_;
     432                 :     std::pmr::memory_resource* saved_tls_;
     433                 : 
     434                 : public:
     435                 :     /** Construct the wrapper and install the frame allocator.
     436                 : 
     437                 :         Builds the start state and saves the current thread-local frame
     438                 :         allocator. Then installs the start state's resource as the new
     439                 :         thread-local allocator. The task frame, evaluated as the argument
     440                 :         to @ref operator(), is therefore allocated from that resource.
     441                 : 
     442                 :         @note When `Alloc` is not `std::pmr::memory_resource*` it must be
     443                 :         nothrow move constructible (enforced by a `static_assert`), which
     444                 :         is what allows this constructor to be `noexcept`. If allocating
     445                 :         the start state throws, `std::terminate` is called.
     446                 : 
     447                 :         @param ex The executor on which the task runs.
     448                 :         @param st The stop token for cooperative cancellation.
     449                 :         @param h The completion handlers.
     450                 :         @param a The allocator for frame allocation.
     451                 :     */
     452          105998 :     run_async_wrapper(
     453                 :         Ex ex,
     454                 :         std::stop_token st,
     455                 :         Handlers h,
     456                 :         Alloc a) noexcept
     457          107126 :         : tr_(detail::make_trampoline<Ex, Handlers, Alloc>(
     458          109435 :             std::move(ex), std::move(h), std::move(a)))
     459          105998 :         , st_(std::move(st))
     460          105998 :         , saved_tls_(get_current_frame_allocator())
     461                 :     {
     462                 :         if constexpr (!std::is_same_v<Alloc, std::pmr::memory_resource*>)
     463                 :         {
     464                 :             static_assert(
     465                 :                 std::is_nothrow_move_constructible_v<Alloc>,
     466                 :                 "Allocator must be nothrow move constructible");
     467                 :         }
     468                 :         // Set TLS before task argument is evaluated
     469          105998 :         set_current_frame_allocator(tr_.h_.promise().get_resource());
     470          105998 :     }
     471                 : 
     472                 :     /** Restore the previously installed frame allocator.
     473                 : 
     474                 :         Resets the thread-local frame allocator to the value saved at
     475                 :         construction. A stale pointer to the start state's resource
     476                 :         therefore does not outlive the execution context that owns it.
     477                 :         If no task was started, for example because evaluating the task
     478                 :         argument threw, the unused start state is destroyed, releasing
     479                 :         the executor's work.
     480                 :     */
     481          105998 :     ~run_async_wrapper()
     482                 :     {
     483          105998 :         if(tr_.h_)
     484               2 :             tr_.h_.destroy();
     485          105998 :         set_current_frame_allocator(saved_tls_);
     486          105998 :     }
     487                 : 
     488                 :     // Non-copyable, non-movable (must be used immediately)
     489                 : 
     490                 :     /** Copy construction is disabled; the wrapper must be used immediately.
     491                 : 
     492                 :         @param other The wrapper that would be copied.
     493                 :     */
     494                 :     run_async_wrapper(run_async_wrapper const& other) = delete;
     495                 : 
     496                 :     /** Move construction is disabled; the wrapper must be used immediately.
     497                 : 
     498                 :         @param other The wrapper that would be moved from.
     499                 :     */
     500                 :     run_async_wrapper(run_async_wrapper&& other) = delete;
     501                 : 
     502                 :     /** Copy assignment is disabled; the wrapper must be used immediately.
     503                 : 
     504                 :         @param other The wrapper that would be assigned from.
     505                 : 
     506                 :         @return A reference to `*this`.
     507                 :     */
     508                 :     run_async_wrapper& operator=(run_async_wrapper const& other) = delete;
     509                 : 
     510                 :     /** Move assignment is disabled; the wrapper must be used immediately.
     511                 : 
     512                 :         @param other The wrapper that would be moved from.
     513                 : 
     514                 :         @return A reference to `*this`.
     515                 :     */
     516                 :     run_async_wrapper& operator=(run_async_wrapper&& other) = delete;
     517                 : 
     518                 :     /** Start the task for execution.
     519                 : 
     520                 :         This operator accepts a task and starts it on the executor.
     521                 :         The rvalue ref-qualifier ensures the wrapper is consumed, enforcing
     522                 :         correct LIFO destruction order.
     523                 : 
     524                 :         The `await_context` constructed for the task is owned by the start
     525                 :         state and is guaranteed to outlive the task and all awaitables
     526                 :         in its chain. Awaitables may store `await_context const*` without concern
     527                 :         for dangling references.
     528                 : 
     529                 :         @throws Any exception thrown by the executor's `dispatch`. The
     530                 :             task is then destroyed without running, and the executor's
     531                 :             work is released.
     532                 : 
     533                 :         @param t The task to execute. Ownership passes to the wrapper's
     534                 :                  start state, which destroys the task after it completes.
     535                 :     */
     536                 :     template<ContextRunnable Task>
     537          105997 :     void operator()(Task t) &&
     538                 :     {
     539          105997 :         auto task_h = t.handle();
     540          105997 :         auto& task_promise = task_h.promise();
     541          105997 :         t.release();
     542                 : 
     543          105997 :         auto& p = tr_.h_.promise();
     544                 : 
     545                 :         // Inject Task-specific invoke function
     546          105997 :         p.invoke_ = detail::run_async_trampoline<Ex, Handlers, Alloc>::template invoke_impl<Task>;
     547          105997 :         p.task_promise_ = &task_promise;
     548          105997 :         p.task_h_ = task_h;
     549                 : 
     550                 :         // Setup task's continuation to return to run_async_trampoline
     551          105997 :         task_promise.set_continuation(tr_.h_);
     552          105997 :         p.owner_.bind(tr_.h_, p.wg_.executor());
     553          211994 :         p.env_ = {p.wg_.executor(), st_, p.get_resource(), &p.owner_};
     554          105997 :         task_promise.set_environment(&p.env_);
     555                 : 
     556                 :         // Start task through executor.
     557                 :         // safe_resume is not needed here: TLS is already saved in the
     558                 :         // constructor (saved_tls_) and restored in the destructor.
     559          105997 :         p.task_cont_.h = task_h;
     560          105997 :         p.task_cont_.owner = &p.owner_;
     561                 :         // If dispatch throws, the destructor destroys the trampoline
     562                 :         // and the unstarted task with it.
     563          105997 :         auto next = p.wg_.executor().dispatch(p.task_cont_);
     564                 :         // From here the trampoline destroys itself when the task ends.
     565          105996 :         tr_.h_ = nullptr;
     566          105996 :         next.resume();
     567          211993 :     }
     568                 : };
     569                 : 
     570                 : // Executor only (uses the execution context's frame allocator)
     571                 : 
     572                 : /** Bind an executor to produce a launcher. Invoke the launcher with a task to start it.
     573                 : 
     574                 :     Use this to start execution of a `task<T>` that was created lazily.
     575                 :     The returned wrapper must be immediately invoked with the task;
     576                 :     storing the wrapper and calling it later violates LIFO ordering.
     577                 : 
     578                 :     Allocates coroutine frames from the execution context's frame
     579                 :     allocator (see @ref execution_context::get_frame_allocator).
     580                 : 
     581                 :     With no completion handlers, the result is discarded. An unhandled
     582                 :     exception thrown by the task calls `std::terminate`. To catch it
     583                 :     instead, pass an error handler that receives it as an
     584                 :     `exception_ptr`, or `co_await` the work inside a coroutine.
     585                 : 
     586                 :     Construct the task as the direct argument of the two-call expression
     587                 :     `run_async(ex)(task)`.
     588                 : 
     589                 :     @par Thread Safety
     590                 :     The wrapper itself should only be used from one thread.
     591                 : 
     592                 :     @par Example
     593                 :     @par !example example_1
     594                 : 
     595                 : 
     596                 :     @param ex The executor to execute the task on.
     597                 : 
     598                 :     @return A wrapper that accepts a `task<T>` for immediate execution.
     599                 : 
     600                 :     @see @ref task
     601                 :     @see @ref Executor
     602                 :     @see @ref run_async_wrapper
     603                 : */
     604                 : template<Executor Ex>
     605                 : [[nodiscard]] auto
     606          101433 : run_async(Ex ex)
     607                 : {
     608          101433 :     auto* mr = ex.context().get_frame_allocator();
     609                 :     return run_async_wrapper<Ex, detail::default_handler, std::pmr::memory_resource*>(
     610          101433 :         std::move(ex),
     611          202866 :         std::stop_token{},
     612                 :         detail::default_handler{},
     613          101433 :         mr);
     614                 : }
     615                 : 
     616                 : /** Bind an executor and a result handler to produce a launcher. Invoke the launcher with a task to start it.
     617                 : 
     618                 :     Allocates coroutine frames from the execution context's frame
     619                 :     allocator (see @ref execution_context::get_frame_allocator).
     620                 : 
     621                 :     The completion handler `h1` is called with the task's result on
     622                 :     success. If `h1` is also invocable with `std::exception_ptr`, it
     623                 :     handles exceptions too. Otherwise, an unhandled exception calls
     624                 :     `std::terminate`.
     625                 : 
     626                 :     If the task is abandoned before it completes, as a context's
     627                 :     teardown does, no completion handler is called. The handlers are
     628                 :     destroyed with it.
     629                 : 
     630                 :     `H1` must be nothrow move constructible; this is checked at
     631                 :     compile time. A completion handler that throws calls
     632                 :     `std::terminate`.
     633                 : 
     634                 :     Construct the task as the direct argument of the two-call expression
     635                 :     `run_async(ex)(task)`.
     636                 : 
     637                 :     @par Thread Safety
     638                 :     The wrapper itself should only be used from one thread. The
     639                 :     completion handlers may be invoked from any thread where the
     640                 :     executor schedules work.
     641                 : 
     642                 :     @par Example
     643                 :     @par !example example_2
     644                 : 
     645                 : 
     646                 :     @param ex The executor to execute the task on.
     647                 :     @param h1 The completion handler for the result and, optionally,
     648                 :         the exception.
     649                 : 
     650                 :     @return A wrapper that accepts a `task<T>` for immediate execution.
     651                 : 
     652                 :     @see @ref task
     653                 :     @see @ref Executor
     654                 :     @see @ref run_async_wrapper
     655                 : */
     656                 : template<Executor Ex, class H1>
     657                 :     requires detail::RunAsyncHandler<H1>
     658                 : [[nodiscard]] auto
     659             115 : run_async(Ex ex, H1 h1)
     660                 : {
     661             115 :     auto* mr = ex.context().get_frame_allocator();
     662                 :     return run_async_wrapper<Ex, detail::handler_pair<H1, detail::default_handler>, std::pmr::memory_resource*>(
     663             115 :         std::move(ex),
     664             122 :         std::stop_token{},
     665             108 :         detail::handler_pair<H1, detail::default_handler>{std::move(h1)},
     666             223 :         mr);
     667                 : }
     668                 : 
     669                 : /** Bind an executor and separate result and error handlers to produce a launcher. Invoke the launcher with a task to start it.
     670                 : 
     671                 :     Allocates coroutine frames from the execution context's frame
     672                 :     allocator (see @ref execution_context::get_frame_allocator).
     673                 : 
     674                 :     The completion handler `h1` is called with the task's result on
     675                 :     success, and `h2` is called with the `std::exception_ptr` on
     676                 :     failure.
     677                 : 
     678                 :     If the task is abandoned before it completes, as a context's
     679                 :     teardown does, no completion handler is called. The handlers are
     680                 :     destroyed with it.
     681                 : 
     682                 :     `H1` and `H2` must be nothrow move constructible; this is checked at
     683                 :     compile time. A completion handler that throws calls
     684                 :     `std::terminate`.
     685                 : 
     686                 :     Construct the task as the direct argument of the two-call expression
     687                 :     `run_async(ex)(task)`.
     688                 : 
     689                 :     @par Thread Safety
     690                 :     The wrapper itself should only be used from one thread. The
     691                 :     completion handlers may be invoked from any thread where the
     692                 :     executor schedules work.
     693                 : 
     694                 :     @par Example
     695                 :     @par !example example_3
     696                 : 
     697                 : 
     698                 :     @param ex The executor to execute the task on.
     699                 :     @param h1 The completion handler to invoke with the result on success.
     700                 :     @param h2 The completion handler to invoke with the exception on failure.
     701                 : 
     702                 :     @return A wrapper that accepts a `task<T>` for immediate execution.
     703                 : 
     704                 :     @see @ref task
     705                 :     @see @ref Executor
     706                 :     @see @ref run_async_wrapper
     707                 : */
     708                 : template<Executor Ex, class H1, class H2>
     709                 :     requires (detail::RunAsyncHandler<H1> && detail::RunAsyncHandler<H2>)
     710                 : [[nodiscard]] auto
     711             110 : run_async(Ex ex, H1 h1, H2 h2)
     712                 : {
     713             110 :     auto* mr = ex.context().get_frame_allocator();
     714                 :     return run_async_wrapper<Ex, detail::handler_pair<H1, H2>, std::pmr::memory_resource*>(
     715             110 :         std::move(ex),
     716             113 :         std::stop_token{},
     717             107 :         detail::handler_pair<H1, H2>{std::move(h1), std::move(h2)},
     718             217 :         mr);
     719               3 : }
     720                 : 
     721                 : // Ex + stop_token
     722                 : 
     723                 : /** Bind an executor and a stop token to produce a launcher. Invoke the launcher with a task to start it.
     724                 : 
     725                 :     The stop token is propagated to the task for cooperative
     726                 :     cancellation.
     727                 : 
     728                 :     Allocates coroutine frames from the execution context's frame
     729                 :     allocator (see @ref execution_context::get_frame_allocator).
     730                 : 
     731                 :     With no completion handlers, the result is discarded and an
     732                 :     unhandled exception calls `std::terminate`.
     733                 : 
     734                 :     Construct the task as the direct argument of the two-call expression
     735                 :     `run_async(ex)(task)`.
     736                 : 
     737                 :     @par Thread Safety
     738                 :     The wrapper itself should only be used from one thread.
     739                 : 
     740                 :     @par Example
     741                 :     @par !example example_4
     742                 : 
     743                 : 
     744                 :     @param ex The executor to execute the task on.
     745                 :     @param st The stop token for cooperative cancellation.
     746                 : 
     747                 :     @return A wrapper that accepts a `task<T>` for immediate execution.
     748                 : 
     749                 :     @see @ref task
     750                 :     @see @ref Executor
     751                 :     @see @ref run_async_wrapper
     752                 : */
     753                 : template<Executor Ex>
     754                 : [[nodiscard]] auto
     755             375 : run_async(Ex ex, std::stop_token st)
     756                 : {
     757             375 :     auto* mr = ex.context().get_frame_allocator();
     758                 :     return run_async_wrapper<Ex, detail::default_handler, std::pmr::memory_resource*>(
     759             375 :         std::move(ex),
     760             375 :         std::move(st),
     761                 :         detail::default_handler{},
     762             750 :         mr);
     763                 : }
     764                 : 
     765                 : /** Bind an executor, a stop token, and a result handler to produce a launcher. Invoke the launcher with a task to start it.
     766                 : 
     767                 :     The stop token is propagated to the task for cooperative
     768                 :     cancellation.
     769                 : 
     770                 :     Allocates coroutine frames from the execution context's frame
     771                 :     allocator (see @ref execution_context::get_frame_allocator).
     772                 : 
     773                 :     The completion handler `h1` is called with the task's result on
     774                 :     success. If `h1` is also invocable with `std::exception_ptr`, it
     775                 :     handles exceptions too. Otherwise, an unhandled exception calls
     776                 :     `std::terminate`.
     777                 : 
     778                 :     If the task is abandoned before it completes, as a context's
     779                 :     teardown does, no completion handler is called. The handlers are
     780                 :     destroyed with it.
     781                 : 
     782                 :     `H1` must be nothrow move constructible; this is checked at
     783                 :     compile time. A completion handler that throws calls
     784                 :     `std::terminate`.
     785                 : 
     786                 :     Construct the task as the direct argument of the two-call expression
     787                 :     `run_async(ex)(task)`.
     788                 : 
     789                 :     @par Thread Safety
     790                 :     The wrapper itself should only be used from one thread. The
     791                 :     completion handlers may be invoked from any thread where the
     792                 :     executor schedules work.
     793                 : 
     794                 :     @param ex The executor to execute the task on.
     795                 :     @param st The stop token for cooperative cancellation.
     796                 :     @param h1 The completion handler for the result and, optionally,
     797                 :         the exception.
     798                 : 
     799                 :     @return A wrapper that accepts a `task<T>` for immediate execution.
     800                 : 
     801                 :     @see @ref task
     802                 :     @see @ref Executor
     803                 :     @see @ref run_async_wrapper
     804                 : */
     805                 : template<Executor Ex, class H1>
     806                 :     requires detail::RunAsyncHandler<H1>
     807                 : [[nodiscard]] auto
     808            1611 : run_async(Ex ex, std::stop_token st, H1 h1)
     809                 : {
     810            1611 :     auto* mr = ex.context().get_frame_allocator();
     811                 :     return run_async_wrapper<Ex, detail::handler_pair<H1, detail::default_handler>, std::pmr::memory_resource*>(
     812            1611 :         std::move(ex),
     813            1611 :         std::move(st),
     814            1611 :         detail::handler_pair<H1, detail::default_handler>{std::move(h1)},
     815            3222 :         mr);
     816            1105 : }
     817                 : 
     818                 : /** Bind an executor, a stop token, and separate result and error handlers to produce a launcher. Invoke the launcher with a task to start it.
     819                 : 
     820                 :     The stop token is propagated to the task for cooperative
     821                 :     cancellation.
     822                 : 
     823                 :     Allocates coroutine frames from the execution context's frame
     824                 :     allocator (see @ref execution_context::get_frame_allocator).
     825                 : 
     826                 :     The completion handler `h1` is called with the task's result on
     827                 :     success, and `h2` is called with the `std::exception_ptr` on
     828                 :     failure.
     829                 : 
     830                 :     If the task is abandoned before it completes, as a context's
     831                 :     teardown does, no completion handler is called. The handlers are
     832                 :     destroyed with it.
     833                 : 
     834                 :     `H1` and `H2` must be nothrow move constructible; this is checked at
     835                 :     compile time. A completion handler that throws calls
     836                 :     `std::terminate`.
     837                 : 
     838                 :     Construct the task as the direct argument of the two-call expression
     839                 :     `run_async(ex)(task)`.
     840                 : 
     841                 :     @par Thread Safety
     842                 :     The wrapper itself should only be used from one thread. The
     843                 :     completion handlers may be invoked from any thread where the
     844                 :     executor schedules work.
     845                 : 
     846                 :     @param ex The executor to execute the task on.
     847                 :     @param st The stop token for cooperative cancellation.
     848                 :     @param h1 The completion handler to invoke with the result on success.
     849                 :     @param h2 The completion handler to invoke with the exception on failure.
     850                 : 
     851                 :     @return A wrapper that accepts a `task<T>` for immediate execution.
     852                 : 
     853                 :     @see @ref task
     854                 :     @see @ref Executor
     855                 :     @see @ref run_async_wrapper
     856                 : */
     857                 : template<Executor Ex, class H1, class H2>
     858                 :     requires (detail::RunAsyncHandler<H1> && detail::RunAsyncHandler<H2>)
     859                 : [[nodiscard]] auto
     860               6 : run_async(Ex ex, std::stop_token st, H1 h1, H2 h2)
     861                 : {
     862               6 :     auto* mr = ex.context().get_frame_allocator();
     863                 :     return run_async_wrapper<Ex, detail::handler_pair<H1, H2>, std::pmr::memory_resource*>(
     864               6 :         std::move(ex),
     865               6 :         std::move(st),
     866               6 :         detail::handler_pair<H1, H2>{std::move(h1), std::move(h2)},
     867              12 :         mr);
     868                 : }
     869                 : 
     870                 : // Ex + memory_resource*
     871                 : 
     872                 : /** Bind an executor and a memory resource to produce a launcher. Invoke the launcher with a task to start it.
     873                 : 
     874                 :     Coroutine frames are allocated from `mr`.
     875                 : 
     876                 :     With no completion handlers, the result is discarded and an
     877                 :     unhandled exception calls `std::terminate`.
     878                 : 
     879                 :     Construct the task as the direct argument of the two-call expression
     880                 :     `run_async(ex)(task)`.
     881                 : 
     882                 :     @pre `mr` is not null and outlives every task started through the
     883                 :     returned wrapper.
     884                 : 
     885                 :     @par Thread Safety
     886                 :     The wrapper itself should only be used from one thread.
     887                 : 
     888                 :     @param ex The executor to execute the task on.
     889                 :     @param mr The memory resource for frame allocation.
     890                 : 
     891                 :     @return A wrapper that accepts a `task<T>` for immediate execution.
     892                 : 
     893                 :     @see @ref task
     894                 :     @see @ref Executor
     895                 :     @see @ref run_async_wrapper
     896                 : */
     897                 : template<Executor Ex>
     898                 : [[nodiscard]] auto
     899              16 : run_async(Ex ex, std::pmr::memory_resource* mr)
     900                 : {
     901                 :     return run_async_wrapper<Ex, detail::default_handler, std::pmr::memory_resource*>(
     902              16 :         std::move(ex),
     903              32 :         std::stop_token{},
     904                 :         detail::default_handler{},
     905              16 :         mr);
     906                 : }
     907                 : 
     908                 : /** Bind an executor, a memory resource, and a result handler to produce a launcher. Invoke the launcher with a task to start it.
     909                 : 
     910                 :     Coroutine frames are allocated from `mr`.
     911                 : 
     912                 :     The completion handler `h1` is called with the task's result on
     913                 :     success. If `h1` is also invocable with `std::exception_ptr`, it
     914                 :     handles exceptions too. Otherwise, an unhandled exception calls
     915                 :     `std::terminate`.
     916                 : 
     917                 :     If the task is abandoned before it completes, as a context's
     918                 :     teardown does, no completion handler is called. The handlers are
     919                 :     destroyed with it.
     920                 : 
     921                 :     `H1` must be nothrow move constructible; this is checked at
     922                 :     compile time. A completion handler that throws calls
     923                 :     `std::terminate`.
     924                 : 
     925                 :     Construct the task as the direct argument of the two-call expression
     926                 :     `run_async(ex)(task)`.
     927                 : 
     928                 :     @pre `mr` is not null and outlives every task started through the
     929                 :     returned wrapper.
     930                 : 
     931                 :     @par Thread Safety
     932                 :     The wrapper itself should only be used from one thread. The
     933                 :     completion handlers may be invoked from any thread where the
     934                 :     executor schedules work.
     935                 : 
     936                 :     @param ex The executor to execute the task on.
     937                 :     @param mr The memory resource for frame allocation.
     938                 :     @param h1 The completion handler for the result and, optionally,
     939                 :         the exception.
     940                 : 
     941                 :     @return A wrapper that accepts a `task<T>` for immediate execution.
     942                 : 
     943                 :     @see @ref task
     944                 :     @see @ref Executor
     945                 :     @see @ref run_async_wrapper
     946                 : */
     947                 : template<Executor Ex, class H1>
     948                 : [[nodiscard]] auto
     949               2 : run_async(Ex ex, std::pmr::memory_resource* mr, H1 h1)
     950                 : {
     951                 :     return run_async_wrapper<Ex, detail::handler_pair<H1, detail::default_handler>, std::pmr::memory_resource*>(
     952               2 :         std::move(ex),
     953               2 :         std::stop_token{},
     954               2 :         detail::handler_pair<H1, detail::default_handler>{std::move(h1)},
     955               4 :         mr);
     956                 : }
     957                 : 
     958                 : /** Bind an executor, a memory resource, and separate result and error handlers to produce a launcher. Invoke the launcher with a task to start it.
     959                 : 
     960                 :     Coroutine frames are allocated from `mr`.
     961                 : 
     962                 :     The completion handler `h1` is called with the task's result on
     963                 :     success, and `h2` is called with the `std::exception_ptr` on
     964                 :     failure.
     965                 : 
     966                 :     If the task is abandoned before it completes, as a context's
     967                 :     teardown does, no completion handler is called. The handlers are
     968                 :     destroyed with it.
     969                 : 
     970                 :     `H1` and `H2` must be nothrow move constructible; this is checked at
     971                 :     compile time. A completion handler that throws calls
     972                 :     `std::terminate`.
     973                 : 
     974                 :     Construct the task as the direct argument of the two-call expression
     975                 :     `run_async(ex)(task)`.
     976                 : 
     977                 :     @pre `mr` is not null and outlives every task started through the
     978                 :     returned wrapper.
     979                 : 
     980                 :     @par Thread Safety
     981                 :     The wrapper itself should only be used from one thread. The
     982                 :     completion handlers may be invoked from any thread where the
     983                 :     executor schedules work.
     984                 : 
     985                 :     @param ex The executor to execute the task on.
     986                 :     @param mr The memory resource for frame allocation.
     987                 :     @param h1 The completion handler to invoke with the result on success.
     988                 :     @param h2 The completion handler to invoke with the exception on failure.
     989                 : 
     990                 :     @return A wrapper that accepts a `task<T>` for immediate execution.
     991                 : 
     992                 :     @see @ref task
     993                 :     @see @ref Executor
     994                 :     @see @ref run_async_wrapper
     995                 : */
     996                 : template<Executor Ex, class H1, class H2>
     997                 : [[nodiscard]] auto
     998               2 : run_async(Ex ex, std::pmr::memory_resource* mr, H1 h1, H2 h2)
     999                 : {
    1000                 :     return run_async_wrapper<Ex, detail::handler_pair<H1, H2>, std::pmr::memory_resource*>(
    1001               2 :         std::move(ex),
    1002               2 :         std::stop_token{},
    1003               2 :         detail::handler_pair<H1, H2>{std::move(h1), std::move(h2)},
    1004               4 :         mr);
    1005               2 : }
    1006                 : 
    1007                 : // Ex + stop_token + memory_resource*
    1008                 : 
    1009                 : /** Bind an executor, a stop token, and a memory resource to produce a launcher. Invoke the launcher with a task to start it.
    1010                 : 
    1011                 :     The stop token is propagated to the task for cooperative
    1012                 :     cancellation.
    1013                 : 
    1014                 :     Coroutine frames are allocated from `mr`.
    1015                 : 
    1016                 :     With no completion handlers, the result is discarded and an
    1017                 :     unhandled exception calls `std::terminate`.
    1018                 : 
    1019                 :     Construct the task as the direct argument of the two-call expression
    1020                 :     `run_async(ex)(task)`.
    1021                 : 
    1022                 :     @pre `mr` is not null and outlives every task started through the
    1023                 :     returned wrapper.
    1024                 : 
    1025                 :     @par Thread Safety
    1026                 :     The wrapper itself should only be used from one thread.
    1027                 : 
    1028                 :     @param ex The executor to execute the task on.
    1029                 :     @param st The stop token for cooperative cancellation.
    1030                 :     @param mr The memory resource for frame allocation.
    1031                 : 
    1032                 :     @return A wrapper that accepts a `task<T>` for immediate execution.
    1033                 : 
    1034                 :     @see @ref task
    1035                 :     @see @ref Executor
    1036                 :     @see @ref run_async_wrapper
    1037                 : */
    1038                 : template<Executor Ex>
    1039                 : [[nodiscard]] auto
    1040               1 : run_async(Ex ex, std::stop_token st, std::pmr::memory_resource* mr)
    1041                 : {
    1042                 :     return run_async_wrapper<Ex, detail::default_handler, std::pmr::memory_resource*>(
    1043               1 :         std::move(ex),
    1044               1 :         std::move(st),
    1045                 :         detail::default_handler{},
    1046               2 :         mr);
    1047                 : }
    1048                 : 
    1049                 : /** Bind an executor, a stop token, a memory resource, and a result handler to produce a launcher. Invoke the launcher with a task to start it.
    1050                 : 
    1051                 :     The stop token is propagated to the task for cooperative
    1052                 :     cancellation.
    1053                 : 
    1054                 :     Coroutine frames are allocated from `mr`.
    1055                 : 
    1056                 :     The completion handler `h1` is called with the task's result on
    1057                 :     success. If `h1` is also invocable with `std::exception_ptr`, it
    1058                 :     handles exceptions too. Otherwise, an unhandled exception calls
    1059                 :     `std::terminate`.
    1060                 : 
    1061                 :     If the task is abandoned before it completes, as a context's
    1062                 :     teardown does, no completion handler is called. The handlers are
    1063                 :     destroyed with it.
    1064                 : 
    1065                 :     `H1` must be nothrow move constructible; this is checked at
    1066                 :     compile time. A completion handler that throws calls
    1067                 :     `std::terminate`.
    1068                 : 
    1069                 :     Construct the task as the direct argument of the two-call expression
    1070                 :     `run_async(ex)(task)`.
    1071                 : 
    1072                 :     @pre `mr` is not null and outlives every task started through the
    1073                 :     returned wrapper.
    1074                 : 
    1075                 :     @par Thread Safety
    1076                 :     The wrapper itself should only be used from one thread. The
    1077                 :     completion handlers may be invoked from any thread where the
    1078                 :     executor schedules work.
    1079                 : 
    1080                 :     @param ex The executor to execute the task on.
    1081                 :     @param st The stop token for cooperative cancellation.
    1082                 :     @param mr The memory resource for frame allocation.
    1083                 :     @param h1 The completion handler for the result and, optionally,
    1084                 :         the exception.
    1085                 : 
    1086                 :     @return A wrapper that accepts a `task<T>` for immediate execution.
    1087                 : 
    1088                 :     @see @ref task
    1089                 :     @see @ref Executor
    1090                 :     @see @ref run_async_wrapper
    1091                 : */
    1092                 : template<Executor Ex, class H1>
    1093                 : [[nodiscard]] auto
    1094                 : run_async(Ex ex, std::stop_token st, std::pmr::memory_resource* mr, H1 h1)
    1095                 : {
    1096                 :     return run_async_wrapper<Ex, detail::handler_pair<H1, detail::default_handler>, std::pmr::memory_resource*>(
    1097                 :         std::move(ex),
    1098                 :         std::move(st),
    1099                 :         detail::handler_pair<H1, detail::default_handler>{std::move(h1)},
    1100                 :         mr);
    1101                 : }
    1102                 : 
    1103                 : /** Bind an executor, a stop token, a memory resource, and separate result and error handlers to produce a launcher. Invoke the launcher with a task to start it.
    1104                 : 
    1105                 :     The stop token is propagated to the task for cooperative
    1106                 :     cancellation.
    1107                 : 
    1108                 :     Coroutine frames are allocated from `mr`.
    1109                 : 
    1110                 :     The completion handler `h1` is called with the task's result on
    1111                 :     success, and `h2` is called with the `std::exception_ptr` on
    1112                 :     failure.
    1113                 : 
    1114                 :     If the task is abandoned before it completes, as a context's
    1115                 :     teardown does, no completion handler is called. The handlers are
    1116                 :     destroyed with it.
    1117                 : 
    1118                 :     `H1` and `H2` must be nothrow move constructible; this is checked at
    1119                 :     compile time. A completion handler that throws calls
    1120                 :     `std::terminate`.
    1121                 : 
    1122                 :     Construct the task as the direct argument of the two-call expression
    1123                 :     `run_async(ex)(task)`.
    1124                 : 
    1125                 :     @pre `mr` is not null and outlives every task started through the
    1126                 :     returned wrapper.
    1127                 : 
    1128                 :     @par Thread Safety
    1129                 :     The wrapper itself should only be used from one thread. The
    1130                 :     completion handlers may be invoked from any thread where the
    1131                 :     executor schedules work.
    1132                 : 
    1133                 :     @param ex The executor to execute the task on.
    1134                 :     @param st The stop token for cooperative cancellation.
    1135                 :     @param mr The memory resource for frame allocation.
    1136                 :     @param h1 The completion handler to invoke with the result on success.
    1137                 :     @param h2 The completion handler to invoke with the exception on failure.
    1138                 : 
    1139                 :     @return A wrapper that accepts a `task<T>` for immediate execution.
    1140                 : 
    1141                 :     @see @ref task
    1142                 :     @see @ref Executor
    1143                 :     @see @ref run_async_wrapper
    1144                 : */
    1145                 : template<Executor Ex, class H1, class H2>
    1146                 : [[nodiscard]] auto
    1147               1 : run_async(Ex ex, std::stop_token st, std::pmr::memory_resource* mr, H1 h1, H2 h2)
    1148                 : {
    1149                 :     return run_async_wrapper<Ex, detail::handler_pair<H1, H2>, std::pmr::memory_resource*>(
    1150               1 :         std::move(ex),
    1151               1 :         std::move(st),
    1152                 :         detail::handler_pair<H1, H2>{std::move(h1), std::move(h2)},
    1153               2 :         mr);
    1154                 : }
    1155                 : 
    1156                 : // Ex + standard Allocator (value type)
    1157                 : 
    1158                 : /** Bind an executor and an allocator to produce a launcher. Invoke the launcher with a task to start it.
    1159                 : 
    1160                 :     Coroutine frames are allocated from a copy of `alloc`, which is
    1161                 :     kept alive until every frame allocated from it is freed.
    1162                 : 
    1163                 :     With no completion handlers, the result is discarded and an
    1164                 :     unhandled exception calls `std::terminate`.
    1165                 : 
    1166                 :     Construct the task as the direct argument of the two-call expression
    1167                 :     `run_async(ex)(task)`.
    1168                 : 
    1169                 :     @par Thread Safety
    1170                 :     The wrapper itself should only be used from one thread.
    1171                 : 
    1172                 :     @param ex The executor to execute the task on.
    1173                 :     @param alloc The allocator for frame allocation (copied and stored).
    1174                 : 
    1175                 :     @return A wrapper that accepts a `task<T>` for immediate execution.
    1176                 : 
    1177                 :     @see @ref task
    1178                 :     @see @ref Executor
    1179                 :     @see @ref run_async_wrapper
    1180                 : */
    1181                 : template<Executor Ex, detail::Allocator Alloc>
    1182                 : [[nodiscard]] auto
    1183             318 : run_async(Ex ex, Alloc alloc)
    1184                 : {
    1185                 :     return run_async_wrapper<Ex, detail::default_handler, Alloc>(
    1186             318 :         std::move(ex),
    1187             635 :         std::stop_token{},
    1188                 :         detail::default_handler{},
    1189             952 :         std::move(alloc));
    1190                 : }
    1191                 : 
    1192                 : /** Bind an executor, an allocator, and a result handler to produce a launcher. Invoke the launcher with a task to start it.
    1193                 : 
    1194                 :     Coroutine frames are allocated from a copy of `alloc`, which is
    1195                 :     kept alive until every frame allocated from it is freed.
    1196                 : 
    1197                 :     The completion handler `h1` is called with the task's result on
    1198                 :     success. If `h1` is also invocable with `std::exception_ptr`, it
    1199                 :     handles exceptions too. Otherwise, an unhandled exception calls
    1200                 :     `std::terminate`.
    1201                 : 
    1202                 :     If the task is abandoned before it completes, as a context's
    1203                 :     teardown does, no completion handler is called. The handlers are
    1204                 :     destroyed with it.
    1205                 : 
    1206                 :     `H1` must be nothrow move constructible; this is checked at
    1207                 :     compile time. A completion handler that throws calls
    1208                 :     `std::terminate`.
    1209                 : 
    1210                 :     Construct the task as the direct argument of the two-call expression
    1211                 :     `run_async(ex)(task)`.
    1212                 : 
    1213                 :     @par Thread Safety
    1214                 :     The wrapper itself should only be used from one thread. The
    1215                 :     completion handlers may be invoked from any thread where the
    1216                 :     executor schedules work.
    1217                 : 
    1218                 :     @param ex The executor to execute the task on.
    1219                 :     @param alloc The allocator for frame allocation (copied and stored).
    1220                 :     @param h1 The completion handler for the result and, optionally,
    1221                 :         the exception.
    1222                 : 
    1223                 :     @return A wrapper that accepts a `task<T>` for immediate execution.
    1224                 : 
    1225                 :     @see @ref task
    1226                 :     @see @ref Executor
    1227                 :     @see @ref run_async_wrapper
    1228                 : */
    1229                 : template<Executor Ex, detail::Allocator Alloc, class H1>
    1230                 : [[nodiscard]] auto
    1231               3 : run_async(Ex ex, Alloc alloc, H1 h1)
    1232                 : {
    1233                 :     return run_async_wrapper<Ex, detail::handler_pair<H1, detail::default_handler>, Alloc>(
    1234               3 :         std::move(ex),
    1235               3 :         std::stop_token{},
    1236               3 :         detail::handler_pair<H1, detail::default_handler>{std::move(h1)},
    1237              11 :         std::move(alloc));
    1238                 : }
    1239                 : 
    1240                 : /** Bind an executor, an allocator, and separate result and error handlers to produce a launcher. Invoke the launcher with a task to start it.
    1241                 : 
    1242                 :     Coroutine frames are allocated from a copy of `alloc`, which is
    1243                 :     kept alive until every frame allocated from it is freed.
    1244                 : 
    1245                 :     The completion handler `h1` is called with the task's result on
    1246                 :     success, and `h2` is called with the `std::exception_ptr` on
    1247                 :     failure.
    1248                 : 
    1249                 :     If the task is abandoned before it completes, as a context's
    1250                 :     teardown does, no completion handler is called. The handlers are
    1251                 :     destroyed with it.
    1252                 : 
    1253                 :     `H1` and `H2` must be nothrow move constructible; this is checked at
    1254                 :     compile time. A completion handler that throws calls
    1255                 :     `std::terminate`.
    1256                 : 
    1257                 :     Construct the task as the direct argument of the two-call expression
    1258                 :     `run_async(ex)(task)`.
    1259                 : 
    1260                 :     @par Thread Safety
    1261                 :     The wrapper itself should only be used from one thread. The
    1262                 :     completion handlers may be invoked from any thread where the
    1263                 :     executor schedules work.
    1264                 : 
    1265                 :     @param ex The executor to execute the task on.
    1266                 :     @param alloc The allocator for frame allocation (copied and stored).
    1267                 :     @param h1 The completion handler to invoke with the result on success.
    1268                 :     @param h2 The completion handler to invoke with the exception on failure.
    1269                 : 
    1270                 :     @return A wrapper that accepts a `task<T>` for immediate execution.
    1271                 : 
    1272                 :     @see @ref task
    1273                 :     @see @ref Executor
    1274                 :     @see @ref run_async_wrapper
    1275                 : */
    1276                 : template<Executor Ex, detail::Allocator Alloc, class H1, class H2>
    1277                 : [[nodiscard]] auto
    1278               4 : run_async(Ex ex, Alloc alloc, H1 h1, H2 h2)
    1279                 : {
    1280                 :     return run_async_wrapper<Ex, detail::handler_pair<H1, H2>, Alloc>(
    1281               4 :         std::move(ex),
    1282               4 :         std::stop_token{},
    1283               4 :         detail::handler_pair<H1, H2>{std::move(h1), std::move(h2)},
    1284              13 :         std::move(alloc));
    1285               3 : }
    1286                 : 
    1287                 : // Ex + stop_token + standard Allocator
    1288                 : 
    1289                 : /** Bind an executor, a stop token, and an allocator to produce a launcher. Invoke the launcher with a task to start it.
    1290                 : 
    1291                 :     The stop token is propagated to the task for cooperative
    1292                 :     cancellation.
    1293                 : 
    1294                 :     Coroutine frames are allocated from a copy of `alloc`, which is
    1295                 :     kept alive until every frame allocated from it is freed.
    1296                 : 
    1297                 :     With no completion handlers, the result is discarded and an
    1298                 :     unhandled exception calls `std::terminate`.
    1299                 : 
    1300                 :     Construct the task as the direct argument of the two-call expression
    1301                 :     `run_async(ex)(task)`.
    1302                 : 
    1303                 :     @par Thread Safety
    1304                 :     The wrapper itself should only be used from one thread.
    1305                 : 
    1306                 :     @param ex The executor to execute the task on.
    1307                 :     @param st The stop token for cooperative cancellation.
    1308                 :     @param alloc The allocator for frame allocation (copied and stored).
    1309                 : 
    1310                 :     @return A wrapper that accepts a `task<T>` for immediate execution.
    1311                 : 
    1312                 :     @see @ref task
    1313                 :     @see @ref Executor
    1314                 :     @see @ref run_async_wrapper
    1315                 : */
    1316                 : template<Executor Ex, detail::Allocator Alloc>
    1317                 : [[nodiscard]] auto
    1318            2001 : run_async(Ex ex, std::stop_token st, Alloc alloc)
    1319                 : {
    1320                 :     return run_async_wrapper<Ex, detail::default_handler, Alloc>(
    1321            2001 :         std::move(ex),
    1322            2001 :         std::move(st),
    1323                 :         detail::default_handler{},
    1324            6003 :         std::move(alloc));
    1325                 : }
    1326                 : 
    1327                 : /** Bind an executor, a stop token, an allocator, and a result handler to produce a launcher. Invoke the launcher with a task to start it.
    1328                 : 
    1329                 :     The stop token is propagated to the task for cooperative
    1330                 :     cancellation.
    1331                 : 
    1332                 :     Coroutine frames are allocated from a copy of `alloc`, which is
    1333                 :     kept alive until every frame allocated from it is freed.
    1334                 : 
    1335                 :     The completion handler `h1` is called with the task's result on
    1336                 :     success. If `h1` is also invocable with `std::exception_ptr`, it
    1337                 :     handles exceptions too. Otherwise, an unhandled exception calls
    1338                 :     `std::terminate`.
    1339                 : 
    1340                 :     If the task is abandoned before it completes, as a context's
    1341                 :     teardown does, no completion handler is called. The handlers are
    1342                 :     destroyed with it.
    1343                 : 
    1344                 :     `H1` must be nothrow move constructible; this is checked at
    1345                 :     compile time. A completion handler that throws calls
    1346                 :     `std::terminate`.
    1347                 : 
    1348                 :     Construct the task as the direct argument of the two-call expression
    1349                 :     `run_async(ex)(task)`.
    1350                 : 
    1351                 :     @par Thread Safety
    1352                 :     The wrapper itself should only be used from one thread. The
    1353                 :     completion handlers may be invoked from any thread where the
    1354                 :     executor schedules work.
    1355                 : 
    1356                 :     @param ex The executor to execute the task on.
    1357                 :     @param st The stop token for cooperative cancellation.
    1358                 :     @param alloc The allocator for frame allocation (copied and stored).
    1359                 :     @param h1 The completion handler for the result and, optionally,
    1360                 :         the exception.
    1361                 : 
    1362                 :     @return A wrapper that accepts a `task<T>` for immediate execution.
    1363                 : 
    1364                 :     @see @ref task
    1365                 :     @see @ref Executor
    1366                 :     @see @ref run_async_wrapper
    1367                 : */
    1368                 : template<Executor Ex, detail::Allocator Alloc, class H1>
    1369                 : [[nodiscard]] auto
    1370                 : run_async(Ex ex, std::stop_token st, Alloc alloc, H1 h1)
    1371                 : {
    1372                 :     return run_async_wrapper<Ex, detail::handler_pair<H1, detail::default_handler>, Alloc>(
    1373                 :         std::move(ex),
    1374                 :         std::move(st),
    1375                 :         detail::handler_pair<H1, detail::default_handler>{std::move(h1)},
    1376                 :         std::move(alloc));
    1377                 : }
    1378                 : 
    1379                 : /** Bind an executor, a stop token, an allocator, and separate result and error handlers to produce a launcher. Invoke the launcher with a task to start it.
    1380                 : 
    1381                 :     The stop token is propagated to the task for cooperative
    1382                 :     cancellation.
    1383                 : 
    1384                 :     Coroutine frames are allocated from a copy of `alloc`, which is
    1385                 :     kept alive until every frame allocated from it is freed.
    1386                 : 
    1387                 :     The completion handler `h1` is called with the task's result on
    1388                 :     success, and `h2` is called with the `std::exception_ptr` on
    1389                 :     failure.
    1390                 : 
    1391                 :     If the task is abandoned before it completes, as a context's
    1392                 :     teardown does, no completion handler is called. The handlers are
    1393                 :     destroyed with it.
    1394                 : 
    1395                 :     `H1` and `H2` must be nothrow move constructible; this is checked at
    1396                 :     compile time. A completion handler that throws calls
    1397                 :     `std::terminate`.
    1398                 : 
    1399                 :     Construct the task as the direct argument of the two-call expression
    1400                 :     `run_async(ex)(task)`.
    1401                 : 
    1402                 :     @par Thread Safety
    1403                 :     The wrapper itself should only be used from one thread. The
    1404                 :     completion handlers may be invoked from any thread where the
    1405                 :     executor schedules work.
    1406                 : 
    1407                 :     @param ex The executor to execute the task on.
    1408                 :     @param st The stop token for cooperative cancellation.
    1409                 :     @param alloc The allocator for frame allocation (copied and stored).
    1410                 :     @param h1 The completion handler to invoke with the result on success.
    1411                 :     @param h2 The completion handler to invoke with the exception on failure.
    1412                 : 
    1413                 :     @return A wrapper that accepts a `task<T>` for immediate execution.
    1414                 : 
    1415                 :     @see @ref task
    1416                 :     @see @ref Executor
    1417                 :     @see @ref run_async_wrapper
    1418                 : */
    1419                 : template<Executor Ex, detail::Allocator Alloc, class H1, class H2>
    1420                 : [[nodiscard]] auto
    1421                 : run_async(Ex ex, std::stop_token st, Alloc alloc, H1 h1, H2 h2)
    1422                 : {
    1423                 :     return run_async_wrapper<Ex, detail::handler_pair<H1, H2>, Alloc>(
    1424                 :         std::move(ex),
    1425                 :         std::move(st),
    1426                 :         detail::handler_pair<H1, H2>{std::move(h1), std::move(h2)},
    1427                 :         std::move(alloc));
    1428                 : }
    1429                 : 
    1430                 : } // namespace capy
    1431                 : } // namespace boost
    1432                 : 
    1433                 : #endif
        

Generated by: LCOV version 2.3