1  
//
1  
//
2  
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/cppalliance/capy
7  
// Official repository: https://github.com/cppalliance/capy
8  
//
8  
//
9  

9  

10  
#ifndef BOOST_CAPY_RUN_ASYNC_HPP
10  
#ifndef BOOST_CAPY_RUN_ASYNC_HPP
11  
#define BOOST_CAPY_RUN_ASYNC_HPP
11  
#define BOOST_CAPY_RUN_ASYNC_HPP
12  

12  

13  
#include <boost/capy/detail/config.hpp>
13  
#include <boost/capy/detail/config.hpp>
14  
#include <boost/capy/detail/run.hpp>
14  
#include <boost/capy/detail/run.hpp>
15  
#include <boost/capy/detail/run_callbacks.hpp>
15  
#include <boost/capy/detail/run_callbacks.hpp>
16  
#include <boost/capy/concept/executor.hpp>
16  
#include <boost/capy/concept/executor.hpp>
17  
#include <boost/capy/concept/io_runnable.hpp>
17  
#include <boost/capy/concept/io_runnable.hpp>
18  
#include <boost/capy/ex/execution_context.hpp>
18  
#include <boost/capy/ex/execution_context.hpp>
19  
#include <boost/capy/ex/frame_allocator.hpp>
19  
#include <boost/capy/ex/frame_allocator.hpp>
20  
#include <boost/capy/ex/io_env.hpp>
20  
#include <boost/capy/ex/io_env.hpp>
21  
#include <boost/capy/ex/recycling_memory_resource.hpp>
21  
#include <boost/capy/ex/recycling_memory_resource.hpp>
22  
#include <boost/capy/ex/work_guard.hpp>
22  
#include <boost/capy/ex/work_guard.hpp>
23  

23  

24  
#include <algorithm>
24  
#include <algorithm>
25  
#include <coroutine>
25  
#include <coroutine>
26  
#include <cstring>
26  
#include <cstring>
27  
#include <memory_resource>
27  
#include <memory_resource>
28  
#include <new>
28  
#include <new>
29  
#include <stop_token>
29  
#include <stop_token>
30  
#include <type_traits>
30  
#include <type_traits>
31  

31  

32  
namespace boost {
32  
namespace boost {
33  
namespace capy {
33  
namespace capy {
34  
namespace detail {
34  
namespace detail {
35  

35  

36  
/// Function pointer type for type-erased frame deallocation.
36  
/// Function pointer type for type-erased frame deallocation.
37  
using dealloc_fn = void(*)(void*, std::size_t);
37  
using dealloc_fn = void(*)(void*, std::size_t);
38  

38  

39  
/// Type-erased deallocator implementation for trampoline frames.
39  
/// Type-erased deallocator implementation for trampoline frames.
40  
template<class Alloc>
40  
template<class Alloc>
41  
void dealloc_impl(void* raw, std::size_t total)
41  
void dealloc_impl(void* raw, std::size_t total)
42  
{
42  
{
43  
    static_assert(std::is_same_v<typename Alloc::value_type, std::byte>);
43  
    static_assert(std::is_same_v<typename Alloc::value_type, std::byte>);
44  
    auto* a = std::launder(reinterpret_cast<Alloc*>(
44  
    auto* a = std::launder(reinterpret_cast<Alloc*>(
45  
        static_cast<char*>(raw) + total - sizeof(Alloc)));
45  
        static_cast<char*>(raw) + total - sizeof(Alloc)));
46  
    Alloc ba(std::move(*a));
46  
    Alloc ba(std::move(*a));
47  
    a->~Alloc();
47  
    a->~Alloc();
48  
    ba.deallocate(static_cast<std::byte*>(raw), total);
48  
    ba.deallocate(static_cast<std::byte*>(raw), total);
49  
}
49  
}
50  

50  

51  
/// Awaiter to access the promise from within the coroutine.
51  
/// Awaiter to access the promise from within the coroutine.
52  
template<class Promise>
52  
template<class Promise>
53  
struct get_promise_awaiter
53  
struct get_promise_awaiter
54  
{
54  
{
55  
    Promise* p_ = nullptr;
55  
    Promise* p_ = nullptr;
56  

56  

57  
    bool await_ready() const noexcept { return false; }
57  
    bool await_ready() const noexcept { return false; }
58  

58  

59  
    bool await_suspend(std::coroutine_handle<Promise> h) noexcept
59  
    bool await_suspend(std::coroutine_handle<Promise> h) noexcept
60  
    {
60  
    {
61  
        p_ = &h.promise();
61  
        p_ = &h.promise();
62  
        return false;
62  
        return false;
63  
    }
63  
    }
64  

64  

65  
    Promise& await_resume() const noexcept
65  
    Promise& await_resume() const noexcept
66  
    {
66  
    {
67  
        return *p_;
67  
        return *p_;
68  
    }
68  
    }
69  
};
69  
};
70  

70  

71  
/** Internal run_async_trampoline coroutine for run_async.
71  
/** Internal run_async_trampoline coroutine for run_async.
72  

72  

73  
    The run_async_trampoline is allocated BEFORE the task (via C++17 postfix evaluation
73  
    The run_async_trampoline is allocated BEFORE the task (via C++17 postfix evaluation
74  
    order) and serves as the task's continuation. When the task final_suspends,
74  
    order) and serves as the task's continuation. When the task final_suspends,
75  
    control returns to the run_async_trampoline which then invokes the appropriate handler.
75  
    control returns to the run_async_trampoline which then invokes the appropriate handler.
76  

76  

77  
    For value-type allocators, the run_async_trampoline stores a frame_memory_resource
77  
    For value-type allocators, the run_async_trampoline stores a frame_memory_resource
78  
    that wraps the allocator. For memory_resource*, it stores the pointer directly.
78  
    that wraps the allocator. For memory_resource*, it stores the pointer directly.
79  

79  

80  
    @tparam Ex The executor type.
80  
    @tparam Ex The executor type.
81  
    @tparam Handlers The handler type (default_handler or handler_pair).
81  
    @tparam Handlers The handler type (default_handler or handler_pair).
82  
    @tparam Alloc The allocator type (value type or memory_resource*).
82  
    @tparam Alloc The allocator type (value type or memory_resource*).
83  
*/
83  
*/
84  
template<class Ex, class Handlers, class Alloc>
84  
template<class Ex, class Handlers, class Alloc>
85  
struct run_async_trampoline
85  
struct run_async_trampoline
86  
{
86  
{
87  
    using invoke_fn = void(*)(void*, Handlers&);
87  
    using invoke_fn = void(*)(void*, Handlers&);
88  

88  

89  
    struct promise_type
89  
    struct promise_type
90  
    {
90  
    {
91  
        work_guard<Ex> wg_;
91  
        work_guard<Ex> wg_;
92  
        Handlers handlers_;
92  
        Handlers handlers_;
93  
        frame_memory_resource<Alloc> resource_;
93  
        frame_memory_resource<Alloc> resource_;
94  
        io_env env_;
94  
        io_env env_;
95  
        invoke_fn invoke_ = nullptr;
95  
        invoke_fn invoke_ = nullptr;
96  
        void* task_promise_ = nullptr;
96  
        void* task_promise_ = nullptr;
97  
        std::coroutine_handle<> task_h_;
97  
        std::coroutine_handle<> task_h_;
98  

98  

99  
        promise_type(Ex& ex, Handlers& h, Alloc& a) noexcept
99  
        promise_type(Ex& ex, Handlers& h, Alloc& a) noexcept
100  
            : wg_(std::move(ex))
100  
            : wg_(std::move(ex))
101  
            , handlers_(std::move(h))
101  
            , handlers_(std::move(h))
102  
            , resource_(std::move(a))
102  
            , resource_(std::move(a))
103  
        {
103  
        {
104  
        }
104  
        }
105  

105  

106  
        static void* operator new(
106  
        static void* operator new(
107  
            std::size_t size, Ex const&, Handlers const&, Alloc a)
107  
            std::size_t size, Ex const&, Handlers const&, Alloc a)
108  
        {
108  
        {
109  
            using byte_alloc = typename std::allocator_traits<Alloc>
109  
            using byte_alloc = typename std::allocator_traits<Alloc>
110  
                ::template rebind_alloc<std::byte>;
110  
                ::template rebind_alloc<std::byte>;
111  

111  

112  
            constexpr auto footer_align =
112  
            constexpr auto footer_align =
113  
                (std::max)(alignof(dealloc_fn), alignof(Alloc));
113  
                (std::max)(alignof(dealloc_fn), alignof(Alloc));
114  
            auto padded = (size + footer_align - 1) & ~(footer_align - 1);
114  
            auto padded = (size + footer_align - 1) & ~(footer_align - 1);
115  
            auto total = padded + sizeof(dealloc_fn) + sizeof(Alloc);
115  
            auto total = padded + sizeof(dealloc_fn) + sizeof(Alloc);
116  

116  

117  
            byte_alloc ba(std::move(a));
117  
            byte_alloc ba(std::move(a));
118  
            void* raw = ba.allocate(total);
118  
            void* raw = ba.allocate(total);
119  

119  

120  
            auto* fn_loc = reinterpret_cast<dealloc_fn*>(
120  
            auto* fn_loc = reinterpret_cast<dealloc_fn*>(
121  
                static_cast<char*>(raw) + padded);
121  
                static_cast<char*>(raw) + padded);
122  
            *fn_loc = &dealloc_impl<byte_alloc>;
122  
            *fn_loc = &dealloc_impl<byte_alloc>;
123  

123  

124  
            new (fn_loc + 1) byte_alloc(std::move(ba));
124  
            new (fn_loc + 1) byte_alloc(std::move(ba));
125  

125  

126  
            return raw;
126  
            return raw;
127  
        }
127  
        }
128  

128  

129  
        static void operator delete(void* ptr, std::size_t size)
129  
        static void operator delete(void* ptr, std::size_t size)
130  
        {
130  
        {
131  
            constexpr auto footer_align =
131  
            constexpr auto footer_align =
132  
                (std::max)(alignof(dealloc_fn), alignof(Alloc));
132  
                (std::max)(alignof(dealloc_fn), alignof(Alloc));
133  
            auto padded = (size + footer_align - 1) & ~(footer_align - 1);
133  
            auto padded = (size + footer_align - 1) & ~(footer_align - 1);
134  
            auto total = padded + sizeof(dealloc_fn) + sizeof(Alloc);
134  
            auto total = padded + sizeof(dealloc_fn) + sizeof(Alloc);
135  

135  

136  
            auto* fn = reinterpret_cast<dealloc_fn*>(
136  
            auto* fn = reinterpret_cast<dealloc_fn*>(
137  
                static_cast<char*>(ptr) + padded);
137  
                static_cast<char*>(ptr) + padded);
138  
            (*fn)(ptr, total);
138  
            (*fn)(ptr, total);
139  
        }
139  
        }
140  

140  

141  
        std::pmr::memory_resource* get_resource() noexcept
141  
        std::pmr::memory_resource* get_resource() noexcept
142  
        {
142  
        {
143  
            return &resource_;
143  
            return &resource_;
144  
        }
144  
        }
145  

145  

146  
        run_async_trampoline get_return_object() noexcept
146  
        run_async_trampoline get_return_object() noexcept
147  
        {
147  
        {
148  
            return run_async_trampoline{
148  
            return run_async_trampoline{
149  
                std::coroutine_handle<promise_type>::from_promise(*this)};
149  
                std::coroutine_handle<promise_type>::from_promise(*this)};
150  
        }
150  
        }
151  

151  

152  
        std::suspend_always initial_suspend() noexcept
152  
        std::suspend_always initial_suspend() noexcept
153  
        {
153  
        {
154  
            return {};
154  
            return {};
155  
        }
155  
        }
156  

156  

157 -
        std::suspend_never final_suspend() noexcept
157 +
        auto final_suspend() noexcept
158  
        {
158  
        {
159 -
            return {};
159 +
            // Explicitly destroy the frame rather than using suspend_never.
 
160 +
            // MSVC's symmetric transfer trampoline loop can mishandle
 
161 +
            // suspend_never at final_suspend, leading to frame
 
162 +
            // double-destruction and premature work count drain.
 
163 +
            struct destroyer
 
164 +
            {
 
165 +
                bool await_ready() noexcept { return false; }
 
166 +
                void await_suspend(
 
167 +
                    std::coroutine_handle<> h) noexcept
 
168 +
                {
 
169 +
                    h.destroy();
 
170 +
                }
 
171 +
                void await_resume() noexcept {}
 
172 +
            };
 
173 +
            return destroyer{};
160  
        }
174  
        }
161  

175  

162  
        void return_void() noexcept
176  
        void return_void() noexcept
163  
        {
177  
        {
164  
        }
178  
        }
165  

179  

166  
        void unhandled_exception() noexcept
180  
        void unhandled_exception() noexcept
167  
        {
181  
        {
168  
        }
182  
        }
169  
    };
183  
    };
170  

184  

171  
    std::coroutine_handle<promise_type> h_;
185  
    std::coroutine_handle<promise_type> h_;
172  

186  

173  
    template<IoRunnable Task>
187  
    template<IoRunnable Task>
174  
    static void invoke_impl(void* p, Handlers& h)
188  
    static void invoke_impl(void* p, Handlers& h)
175  
    {
189  
    {
176  
        using R = decltype(std::declval<Task&>().await_resume());
190  
        using R = decltype(std::declval<Task&>().await_resume());
177  
        auto& promise = *static_cast<typename Task::promise_type*>(p);
191  
        auto& promise = *static_cast<typename Task::promise_type*>(p);
178  
        if(promise.exception())
192  
        if(promise.exception())
179  
            h(promise.exception());
193  
            h(promise.exception());
180  
        else if constexpr(std::is_void_v<R>)
194  
        else if constexpr(std::is_void_v<R>)
181  
            h();
195  
            h();
182  
        else
196  
        else
183  
            h(std::move(promise.result()));
197  
            h(std::move(promise.result()));
184  
    }
198  
    }
185  
};
199  
};
186  

200  

187  
/** Specialization for memory_resource* - stores pointer directly.
201  
/** Specialization for memory_resource* - stores pointer directly.
188  

202  

189  
    This avoids double indirection when the user passes a memory_resource*.
203  
    This avoids double indirection when the user passes a memory_resource*.
190  
*/
204  
*/
191  
template<class Ex, class Handlers>
205  
template<class Ex, class Handlers>
192  
struct run_async_trampoline<Ex, Handlers, std::pmr::memory_resource*>
206  
struct run_async_trampoline<Ex, Handlers, std::pmr::memory_resource*>
193  
{
207  
{
194  
    using invoke_fn = void(*)(void*, Handlers&);
208  
    using invoke_fn = void(*)(void*, Handlers&);
195  

209  

196  
    struct promise_type
210  
    struct promise_type
197  
    {
211  
    {
198  
        work_guard<Ex> wg_;
212  
        work_guard<Ex> wg_;
199  
        Handlers handlers_;
213  
        Handlers handlers_;
200  
        std::pmr::memory_resource* mr_;
214  
        std::pmr::memory_resource* mr_;
201  
        io_env env_;
215  
        io_env env_;
202  
        invoke_fn invoke_ = nullptr;
216  
        invoke_fn invoke_ = nullptr;
203  
        void* task_promise_ = nullptr;
217  
        void* task_promise_ = nullptr;
204  
        std::coroutine_handle<> task_h_;
218  
        std::coroutine_handle<> task_h_;
205  

219  

206  
        promise_type(
220  
        promise_type(
207  
            Ex& ex, Handlers& h, std::pmr::memory_resource* mr) noexcept
221  
            Ex& ex, Handlers& h, std::pmr::memory_resource* mr) noexcept
208  
            : wg_(std::move(ex))
222  
            : wg_(std::move(ex))
209  
            , handlers_(std::move(h))
223  
            , handlers_(std::move(h))
210  
            , mr_(mr)
224  
            , mr_(mr)
211  
        {
225  
        {
212  
        }
226  
        }
213  

227  

214  
        static void* operator new(
228  
        static void* operator new(
215  
            std::size_t size, Ex const&, Handlers const&,
229  
            std::size_t size, Ex const&, Handlers const&,
216  
            std::pmr::memory_resource* mr)
230  
            std::pmr::memory_resource* mr)
217  
        {
231  
        {
218  
            auto total = size + sizeof(mr);
232  
            auto total = size + sizeof(mr);
219  
            void* raw = mr->allocate(total, alignof(std::max_align_t));
233  
            void* raw = mr->allocate(total, alignof(std::max_align_t));
220  
            std::memcpy(static_cast<char*>(raw) + size, &mr, sizeof(mr));
234  
            std::memcpy(static_cast<char*>(raw) + size, &mr, sizeof(mr));
221  
            return raw;
235  
            return raw;
222  
        }
236  
        }
223  

237  

224  
        static void operator delete(void* ptr, std::size_t size)
238  
        static void operator delete(void* ptr, std::size_t size)
225  
        {
239  
        {
226  
            std::pmr::memory_resource* mr;
240  
            std::pmr::memory_resource* mr;
227  
            std::memcpy(&mr, static_cast<char*>(ptr) + size, sizeof(mr));
241  
            std::memcpy(&mr, static_cast<char*>(ptr) + size, sizeof(mr));
228  
            auto total = size + sizeof(mr);
242  
            auto total = size + sizeof(mr);
229  
            mr->deallocate(ptr, total, alignof(std::max_align_t));
243  
            mr->deallocate(ptr, total, alignof(std::max_align_t));
230  
        }
244  
        }
231  

245  

232  
        std::pmr::memory_resource* get_resource() noexcept
246  
        std::pmr::memory_resource* get_resource() noexcept
233  
        {
247  
        {
234  
            return mr_;
248  
            return mr_;
235  
        }
249  
        }
236  

250  

237  
        run_async_trampoline get_return_object() noexcept
251  
        run_async_trampoline get_return_object() noexcept
238  
        {
252  
        {
239  
            return run_async_trampoline{
253  
            return run_async_trampoline{
240  
                std::coroutine_handle<promise_type>::from_promise(*this)};
254  
                std::coroutine_handle<promise_type>::from_promise(*this)};
241  
        }
255  
        }
242  

256  

243  
        std::suspend_always initial_suspend() noexcept
257  
        std::suspend_always initial_suspend() noexcept
244  
        {
258  
        {
245  
            return {};
259  
            return {};
246  
        }
260  
        }
247  

261  

248 -
        std::suspend_never final_suspend() noexcept
262 +
        auto final_suspend() noexcept
249  
        {
263  
        {
250 -
            return {};
264 +
            struct destroyer
 
265 +
            {
 
266 +
                bool await_ready() noexcept { return false; }
 
267 +
                void await_suspend(
 
268 +
                    std::coroutine_handle<> h) noexcept
 
269 +
                {
 
270 +
                    h.destroy();
 
271 +
                }
 
272 +
                void await_resume() noexcept {}
 
273 +
            };
 
274 +
            return destroyer{};
251  
        }
275  
        }
252  

276  

253  
        void return_void() noexcept
277  
        void return_void() noexcept
254  
        {
278  
        {
255  
        }
279  
        }
256  

280  

257  
        void unhandled_exception() noexcept
281  
        void unhandled_exception() noexcept
258  
        {
282  
        {
259  
        }
283  
        }
260  
    };
284  
    };
261  

285  

262  
    std::coroutine_handle<promise_type> h_;
286  
    std::coroutine_handle<promise_type> h_;
263  

287  

264  
    template<IoRunnable Task>
288  
    template<IoRunnable Task>
265  
    static void invoke_impl(void* p, Handlers& h)
289  
    static void invoke_impl(void* p, Handlers& h)
266  
    {
290  
    {
267  
        using R = decltype(std::declval<Task&>().await_resume());
291  
        using R = decltype(std::declval<Task&>().await_resume());
268  
        auto& promise = *static_cast<typename Task::promise_type*>(p);
292  
        auto& promise = *static_cast<typename Task::promise_type*>(p);
269  
        if(promise.exception())
293  
        if(promise.exception())
270  
            h(promise.exception());
294  
            h(promise.exception());
271  
        else if constexpr(std::is_void_v<R>)
295  
        else if constexpr(std::is_void_v<R>)
272  
            h();
296  
            h();
273  
        else
297  
        else
274  
            h(std::move(promise.result()));
298  
            h(std::move(promise.result()));
275  
    }
299  
    }
276  
};
300  
};
277  

301  

278  
/// Coroutine body for run_async_trampoline - invokes handlers then destroys task.
302  
/// Coroutine body for run_async_trampoline - invokes handlers then destroys task.
279  
template<class Ex, class Handlers, class Alloc>
303  
template<class Ex, class Handlers, class Alloc>
280  
run_async_trampoline<Ex, Handlers, Alloc>
304  
run_async_trampoline<Ex, Handlers, Alloc>
281  
make_trampoline(Ex, Handlers, Alloc)
305  
make_trampoline(Ex, Handlers, Alloc)
282  
{
306  
{
283  
    // promise_type ctor steals the parameters
307  
    // promise_type ctor steals the parameters
284  
    auto& p = co_await get_promise_awaiter<
308  
    auto& p = co_await get_promise_awaiter<
285  
        typename run_async_trampoline<Ex, Handlers, Alloc>::promise_type>{};
309  
        typename run_async_trampoline<Ex, Handlers, Alloc>::promise_type>{};
286  
    
310  
    
287  
    p.invoke_(p.task_promise_, p.handlers_);
311  
    p.invoke_(p.task_promise_, p.handlers_);
288  
    p.task_h_.destroy();
312  
    p.task_h_.destroy();
289  
}
313  
}
290  

314  

291  
} // namespace detail
315  
} // namespace detail
292  

316  

293  
/** Wrapper returned by run_async that accepts a task for execution.
317  
/** Wrapper returned by run_async that accepts a task for execution.
294  

318  

295  
    This wrapper holds the run_async_trampoline coroutine, executor, stop token,
319  
    This wrapper holds the run_async_trampoline coroutine, executor, stop token,
296  
    and handlers. The run_async_trampoline is allocated when the wrapper is constructed
320  
    and handlers. The run_async_trampoline is allocated when the wrapper is constructed
297  
    (before the task due to C++17 postfix evaluation order).
321  
    (before the task due to C++17 postfix evaluation order).
298  

322  

299  
    The rvalue ref-qualifier on `operator()` ensures the wrapper can only
323  
    The rvalue ref-qualifier on `operator()` ensures the wrapper can only
300  
    be used as a temporary, preventing misuse that would violate LIFO ordering.
324  
    be used as a temporary, preventing misuse that would violate LIFO ordering.
301  

325  

302  
    @tparam Ex The executor type satisfying the `Executor` concept.
326  
    @tparam Ex The executor type satisfying the `Executor` concept.
303  
    @tparam Handlers The handler type (default_handler or handler_pair).
327  
    @tparam Handlers The handler type (default_handler or handler_pair).
304  
    @tparam Alloc The allocator type (value type or memory_resource*).
328  
    @tparam Alloc The allocator type (value type or memory_resource*).
305  

329  

306  
    @par Thread Safety
330  
    @par Thread Safety
307  
    The wrapper itself should only be used from one thread. The handlers
331  
    The wrapper itself should only be used from one thread. The handlers
308  
    may be invoked from any thread where the executor schedules work.
332  
    may be invoked from any thread where the executor schedules work.
309  

333  

310  
    @par Example
334  
    @par Example
311  
    @code
335  
    @code
312  
    // Correct usage - wrapper is temporary
336  
    // Correct usage - wrapper is temporary
313  
    run_async(ex)(my_task());
337  
    run_async(ex)(my_task());
314  

338  

315  
    // Compile error - cannot call operator() on lvalue
339  
    // Compile error - cannot call operator() on lvalue
316  
    auto w = run_async(ex);
340  
    auto w = run_async(ex);
317  
    w(my_task());  // Error: operator() requires rvalue
341  
    w(my_task());  // Error: operator() requires rvalue
318  
    @endcode
342  
    @endcode
319  

343  

320  
    @see run_async
344  
    @see run_async
321  
*/
345  
*/
322  
template<Executor Ex, class Handlers, class Alloc>
346  
template<Executor Ex, class Handlers, class Alloc>
323  
class [[nodiscard]] run_async_wrapper
347  
class [[nodiscard]] run_async_wrapper
324  
{
348  
{
325  
    detail::run_async_trampoline<Ex, Handlers, Alloc> tr_;
349  
    detail::run_async_trampoline<Ex, Handlers, Alloc> tr_;
326  
    std::stop_token st_;
350  
    std::stop_token st_;
327  
    std::pmr::memory_resource* saved_tls_;
351  
    std::pmr::memory_resource* saved_tls_;
328  

352  

329  
public:
353  
public:
330  
    /// Construct wrapper with executor, stop token, handlers, and allocator.
354  
    /// Construct wrapper with executor, stop token, handlers, and allocator.
331  
    run_async_wrapper(
355  
    run_async_wrapper(
332  
        Ex ex,
356  
        Ex ex,
333  
        std::stop_token st,
357  
        std::stop_token st,
334  
        Handlers h,
358  
        Handlers h,
335  
        Alloc a) noexcept
359  
        Alloc a) noexcept
336  
        : tr_(detail::make_trampoline<Ex, Handlers, Alloc>(
360  
        : tr_(detail::make_trampoline<Ex, Handlers, Alloc>(
337  
            std::move(ex), std::move(h), std::move(a)))
361  
            std::move(ex), std::move(h), std::move(a)))
338  
        , st_(std::move(st))
362  
        , st_(std::move(st))
339  
        , saved_tls_(get_current_frame_allocator())
363  
        , saved_tls_(get_current_frame_allocator())
340  
    {
364  
    {
341  
        if constexpr (!std::is_same_v<Alloc, std::pmr::memory_resource*>)
365  
        if constexpr (!std::is_same_v<Alloc, std::pmr::memory_resource*>)
342  
        {
366  
        {
343  
            static_assert(
367  
            static_assert(
344  
                std::is_nothrow_move_constructible_v<Alloc>,
368  
                std::is_nothrow_move_constructible_v<Alloc>,
345  
                "Allocator must be nothrow move constructible");
369  
                "Allocator must be nothrow move constructible");
346  
        }
370  
        }
347  
        // Set TLS before task argument is evaluated
371  
        // Set TLS before task argument is evaluated
348  
        set_current_frame_allocator(tr_.h_.promise().get_resource());
372  
        set_current_frame_allocator(tr_.h_.promise().get_resource());
349  
    }
373  
    }
350  

374  

351  
    ~run_async_wrapper()
375  
    ~run_async_wrapper()
352  
    {
376  
    {
353  
        // Restore TLS so stale pointer doesn't outlive
377  
        // Restore TLS so stale pointer doesn't outlive
354  
        // the execution context that owns the resource.
378  
        // the execution context that owns the resource.
355  
        set_current_frame_allocator(saved_tls_);
379  
        set_current_frame_allocator(saved_tls_);
356  
    }
380  
    }
357  

381  

358  
    // Non-copyable, non-movable (must be used immediately)
382  
    // Non-copyable, non-movable (must be used immediately)
359  
    run_async_wrapper(run_async_wrapper const&) = delete;
383  
    run_async_wrapper(run_async_wrapper const&) = delete;
360  
    run_async_wrapper(run_async_wrapper&&) = delete;
384  
    run_async_wrapper(run_async_wrapper&&) = delete;
361  
    run_async_wrapper& operator=(run_async_wrapper const&) = delete;
385  
    run_async_wrapper& operator=(run_async_wrapper const&) = delete;
362  
    run_async_wrapper& operator=(run_async_wrapper&&) = delete;
386  
    run_async_wrapper& operator=(run_async_wrapper&&) = delete;
363  

387  

364  
    /** Launch the task for execution.
388  
    /** Launch the task for execution.
365  

389  

366  
        This operator accepts a task and launches it on the executor.
390  
        This operator accepts a task and launches it on the executor.
367  
        The rvalue ref-qualifier ensures the wrapper is consumed, enforcing
391  
        The rvalue ref-qualifier ensures the wrapper is consumed, enforcing
368  
        correct LIFO destruction order.
392  
        correct LIFO destruction order.
369  

393  

370  
        The `io_env` constructed for the task is owned by the trampoline
394  
        The `io_env` constructed for the task is owned by the trampoline
371  
        coroutine and is guaranteed to outlive the task and all awaitables
395  
        coroutine and is guaranteed to outlive the task and all awaitables
372  
        in its chain. Awaitables may store `io_env const*` without concern
396  
        in its chain. Awaitables may store `io_env const*` without concern
373  
        for dangling references.
397  
        for dangling references.
374  

398  

375  
        @tparam Task The IoRunnable type.
399  
        @tparam Task The IoRunnable type.
376  

400  

377  
        @param t The task to execute. Ownership is transferred to the
401  
        @param t The task to execute. Ownership is transferred to the
378  
                 run_async_trampoline which will destroy it after completion.
402  
                 run_async_trampoline which will destroy it after completion.
379  
    */
403  
    */
380  
    template<IoRunnable Task>
404  
    template<IoRunnable Task>
381  
    void operator()(Task t) &&
405  
    void operator()(Task t) &&
382  
    {
406  
    {
383  
        auto task_h = t.handle();
407  
        auto task_h = t.handle();
384  
        auto& task_promise = task_h.promise();
408  
        auto& task_promise = task_h.promise();
385  
        t.release();
409  
        t.release();
386  

410  

387  
        auto& p = tr_.h_.promise();
411  
        auto& p = tr_.h_.promise();
388  

412  

389  
        // Inject Task-specific invoke function
413  
        // Inject Task-specific invoke function
390  
        p.invoke_ = detail::run_async_trampoline<Ex, Handlers, Alloc>::template invoke_impl<Task>;
414  
        p.invoke_ = detail::run_async_trampoline<Ex, Handlers, Alloc>::template invoke_impl<Task>;
391  
        p.task_promise_ = &task_promise;
415  
        p.task_promise_ = &task_promise;
392  
        p.task_h_ = task_h;
416  
        p.task_h_ = task_h;
393  

417  

394  
        // Setup task's continuation to return to run_async_trampoline
418  
        // Setup task's continuation to return to run_async_trampoline
395  
        task_promise.set_continuation(tr_.h_);
419  
        task_promise.set_continuation(tr_.h_);
396  
        p.env_ = {p.wg_.executor(), st_, p.get_resource()};
420  
        p.env_ = {p.wg_.executor(), st_, p.get_resource()};
397  
        task_promise.set_environment(&p.env_);
421  
        task_promise.set_environment(&p.env_);
398  

422  

399  
        // Start task through executor
423  
        // Start task through executor
400  
        p.wg_.executor().dispatch(task_h).resume();
424  
        p.wg_.executor().dispatch(task_h).resume();
401  
    }
425  
    }
402  
};
426  
};
403  

427  

404  
// Executor only (uses default recycling allocator)
428  
// Executor only (uses default recycling allocator)
405  

429  

406  
/** Asynchronously launch a lazy task on the given executor.
430  
/** Asynchronously launch a lazy task on the given executor.
407  

431  

408  
    Use this to start execution of a `task<T>` that was created lazily.
432  
    Use this to start execution of a `task<T>` that was created lazily.
409  
    The returned wrapper must be immediately invoked with the task;
433  
    The returned wrapper must be immediately invoked with the task;
410  
    storing the wrapper and calling it later violates LIFO ordering.
434  
    storing the wrapper and calling it later violates LIFO ordering.
411  

435  

412  
    Uses the default recycling frame allocator for coroutine frames.
436  
    Uses the default recycling frame allocator for coroutine frames.
413  
    With no handlers, the result is discarded and exceptions are rethrown.
437  
    With no handlers, the result is discarded and exceptions are rethrown.
414  

438  

415  
    @par Thread Safety
439  
    @par Thread Safety
416  
    The wrapper and handlers may be called from any thread where the
440  
    The wrapper and handlers may be called from any thread where the
417  
    executor schedules work.
441  
    executor schedules work.
418  

442  

419  
    @par Example
443  
    @par Example
420  
    @code
444  
    @code
421  
    run_async(ioc.get_executor())(my_task());
445  
    run_async(ioc.get_executor())(my_task());
422  
    @endcode
446  
    @endcode
423  

447  

424  
    @param ex The executor to execute the task on.
448  
    @param ex The executor to execute the task on.
425  

449  

426  
    @return A wrapper that accepts a `task<T>` for immediate execution.
450  
    @return A wrapper that accepts a `task<T>` for immediate execution.
427  

451  

428  
    @see task
452  
    @see task
429  
    @see executor
453  
    @see executor
430  
*/
454  
*/
431  
template<Executor Ex>
455  
template<Executor Ex>
432  
[[nodiscard]] auto
456  
[[nodiscard]] auto
433  
run_async(Ex ex)
457  
run_async(Ex ex)
434  
{
458  
{
435  
    auto* mr = ex.context().get_frame_allocator();
459  
    auto* mr = ex.context().get_frame_allocator();
436  
    return run_async_wrapper<Ex, detail::default_handler, std::pmr::memory_resource*>(
460  
    return run_async_wrapper<Ex, detail::default_handler, std::pmr::memory_resource*>(
437  
        std::move(ex),
461  
        std::move(ex),
438  
        std::stop_token{},
462  
        std::stop_token{},
439  
        detail::default_handler{},
463  
        detail::default_handler{},
440  
        mr);
464  
        mr);
441  
}
465  
}
442  

466  

443  
/** Asynchronously launch a lazy task with a result handler.
467  
/** Asynchronously launch a lazy task with a result handler.
444  

468  

445  
    The handler `h1` is called with the task's result on success. If `h1`
469  
    The handler `h1` is called with the task's result on success. If `h1`
446  
    is also invocable with `std::exception_ptr`, it handles exceptions too.
470  
    is also invocable with `std::exception_ptr`, it handles exceptions too.
447  
    Otherwise, exceptions are rethrown.
471  
    Otherwise, exceptions are rethrown.
448  

472  

449  
    @par Thread Safety
473  
    @par Thread Safety
450  
    The handler may be called from any thread where the executor
474  
    The handler may be called from any thread where the executor
451  
    schedules work.
475  
    schedules work.
452  

476  

453  
    @par Example
477  
    @par Example
454  
    @code
478  
    @code
455  
    // Handler for result only (exceptions rethrown)
479  
    // Handler for result only (exceptions rethrown)
456  
    run_async(ex, [](int result) {
480  
    run_async(ex, [](int result) {
457  
        std::cout << "Got: " << result << "\n";
481  
        std::cout << "Got: " << result << "\n";
458  
    })(compute_value());
482  
    })(compute_value());
459  

483  

460  
    // Overloaded handler for both result and exception
484  
    // Overloaded handler for both result and exception
461  
    run_async(ex, overloaded{
485  
    run_async(ex, overloaded{
462  
        [](int result) { std::cout << "Got: " << result << "\n"; },
486  
        [](int result) { std::cout << "Got: " << result << "\n"; },
463  
        [](std::exception_ptr) { std::cout << "Failed\n"; }
487  
        [](std::exception_ptr) { std::cout << "Failed\n"; }
464  
    })(compute_value());
488  
    })(compute_value());
465  
    @endcode
489  
    @endcode
466  

490  

467  
    @param ex The executor to execute the task on.
491  
    @param ex The executor to execute the task on.
468  
    @param h1 The handler to invoke with the result (and optionally exception).
492  
    @param h1 The handler to invoke with the result (and optionally exception).
469  

493  

470  
    @return A wrapper that accepts a `task<T>` for immediate execution.
494  
    @return A wrapper that accepts a `task<T>` for immediate execution.
471  

495  

472  
    @see task
496  
    @see task
473  
    @see executor
497  
    @see executor
474  
*/
498  
*/
475  
template<Executor Ex, class H1>
499  
template<Executor Ex, class H1>
476  
[[nodiscard]] auto
500  
[[nodiscard]] auto
477  
run_async(Ex ex, H1 h1)
501  
run_async(Ex ex, H1 h1)
478  
{
502  
{
479  
    auto* mr = ex.context().get_frame_allocator();
503  
    auto* mr = ex.context().get_frame_allocator();
480  
    return run_async_wrapper<Ex, detail::handler_pair<H1, detail::default_handler>, std::pmr::memory_resource*>(
504  
    return run_async_wrapper<Ex, detail::handler_pair<H1, detail::default_handler>, std::pmr::memory_resource*>(
481  
        std::move(ex),
505  
        std::move(ex),
482  
        std::stop_token{},
506  
        std::stop_token{},
483  
        detail::handler_pair<H1, detail::default_handler>{std::move(h1)},
507  
        detail::handler_pair<H1, detail::default_handler>{std::move(h1)},
484  
        mr);
508  
        mr);
485  
}
509  
}
486  

510  

487  
/** Asynchronously launch a lazy task with separate result and error handlers.
511  
/** Asynchronously launch a lazy task with separate result and error handlers.
488  

512  

489  
    The handler `h1` is called with the task's result on success.
513  
    The handler `h1` is called with the task's result on success.
490  
    The handler `h2` is called with the exception_ptr on failure.
514  
    The handler `h2` is called with the exception_ptr on failure.
491  

515  

492  
    @par Thread Safety
516  
    @par Thread Safety
493  
    The handlers may be called from any thread where the executor
517  
    The handlers may be called from any thread where the executor
494  
    schedules work.
518  
    schedules work.
495  

519  

496  
    @par Example
520  
    @par Example
497  
    @code
521  
    @code
498  
    run_async(ex,
522  
    run_async(ex,
499  
        [](int result) { std::cout << "Got: " << result << "\n"; },
523  
        [](int result) { std::cout << "Got: " << result << "\n"; },
500  
        [](std::exception_ptr ep) {
524  
        [](std::exception_ptr ep) {
501  
            try { std::rethrow_exception(ep); }
525  
            try { std::rethrow_exception(ep); }
502  
            catch (std::exception const& e) {
526  
            catch (std::exception const& e) {
503  
                std::cout << "Error: " << e.what() << "\n";
527  
                std::cout << "Error: " << e.what() << "\n";
504  
            }
528  
            }
505  
        }
529  
        }
506  
    )(compute_value());
530  
    )(compute_value());
507  
    @endcode
531  
    @endcode
508  

532  

509  
    @param ex The executor to execute the task on.
533  
    @param ex The executor to execute the task on.
510  
    @param h1 The handler to invoke with the result on success.
534  
    @param h1 The handler to invoke with the result on success.
511  
    @param h2 The handler to invoke with the exception on failure.
535  
    @param h2 The handler to invoke with the exception on failure.
512  

536  

513  
    @return A wrapper that accepts a `task<T>` for immediate execution.
537  
    @return A wrapper that accepts a `task<T>` for immediate execution.
514  

538  

515  
    @see task
539  
    @see task
516  
    @see executor
540  
    @see executor
517  
*/
541  
*/
518  
template<Executor Ex, class H1, class H2>
542  
template<Executor Ex, class H1, class H2>
519  
[[nodiscard]] auto
543  
[[nodiscard]] auto
520  
run_async(Ex ex, H1 h1, H2 h2)
544  
run_async(Ex ex, H1 h1, H2 h2)
521  
{
545  
{
522  
    auto* mr = ex.context().get_frame_allocator();
546  
    auto* mr = ex.context().get_frame_allocator();
523  
    return run_async_wrapper<Ex, detail::handler_pair<H1, H2>, std::pmr::memory_resource*>(
547  
    return run_async_wrapper<Ex, detail::handler_pair<H1, H2>, std::pmr::memory_resource*>(
524  
        std::move(ex),
548  
        std::move(ex),
525  
        std::stop_token{},
549  
        std::stop_token{},
526  
        detail::handler_pair<H1, H2>{std::move(h1), std::move(h2)},
550  
        detail::handler_pair<H1, H2>{std::move(h1), std::move(h2)},
527  
        mr);
551  
        mr);
528  
}
552  
}
529  

553  

530  
// Ex + stop_token
554  
// Ex + stop_token
531  

555  

532  
/** Asynchronously launch a lazy task with stop token support.
556  
/** Asynchronously launch a lazy task with stop token support.
533  

557  

534  
    The stop token is propagated to the task, enabling cooperative
558  
    The stop token is propagated to the task, enabling cooperative
535  
    cancellation. With no handlers, the result is discarded and
559  
    cancellation. With no handlers, the result is discarded and
536  
    exceptions are rethrown.
560  
    exceptions are rethrown.
537  

561  

538  
    @par Thread Safety
562  
    @par Thread Safety
539  
    The wrapper may be called from any thread where the executor
563  
    The wrapper may be called from any thread where the executor
540  
    schedules work.
564  
    schedules work.
541  

565  

542  
    @par Example
566  
    @par Example
543  
    @code
567  
    @code
544  
    std::stop_source source;
568  
    std::stop_source source;
545  
    run_async(ex, source.get_token())(cancellable_task());
569  
    run_async(ex, source.get_token())(cancellable_task());
546  
    // Later: source.request_stop();
570  
    // Later: source.request_stop();
547  
    @endcode
571  
    @endcode
548  

572  

549  
    @param ex The executor to execute the task on.
573  
    @param ex The executor to execute the task on.
550  
    @param st The stop token for cooperative cancellation.
574  
    @param st The stop token for cooperative cancellation.
551  

575  

552  
    @return A wrapper that accepts a `task<T>` for immediate execution.
576  
    @return A wrapper that accepts a `task<T>` for immediate execution.
553  

577  

554  
    @see task
578  
    @see task
555  
    @see executor
579  
    @see executor
556  
*/
580  
*/
557  
template<Executor Ex>
581  
template<Executor Ex>
558  
[[nodiscard]] auto
582  
[[nodiscard]] auto
559  
run_async(Ex ex, std::stop_token st)
583  
run_async(Ex ex, std::stop_token st)
560  
{
584  
{
561  
    auto* mr = ex.context().get_frame_allocator();
585  
    auto* mr = ex.context().get_frame_allocator();
562  
    return run_async_wrapper<Ex, detail::default_handler, std::pmr::memory_resource*>(
586  
    return run_async_wrapper<Ex, detail::default_handler, std::pmr::memory_resource*>(
563  
        std::move(ex),
587  
        std::move(ex),
564  
        std::move(st),
588  
        std::move(st),
565  
        detail::default_handler{},
589  
        detail::default_handler{},
566  
        mr);
590  
        mr);
567  
}
591  
}
568  

592  

569  
/** Asynchronously launch a lazy task with stop token and result handler.
593  
/** Asynchronously launch a lazy task with stop token and result handler.
570  

594  

571  
    The stop token is propagated to the task for cooperative cancellation.
595  
    The stop token is propagated to the task for cooperative cancellation.
572  
    The handler `h1` is called with the result on success, and optionally
596  
    The handler `h1` is called with the result on success, and optionally
573  
    with exception_ptr if it accepts that type.
597  
    with exception_ptr if it accepts that type.
574  

598  

575  
    @param ex The executor to execute the task on.
599  
    @param ex The executor to execute the task on.
576  
    @param st The stop token for cooperative cancellation.
600  
    @param st The stop token for cooperative cancellation.
577  
    @param h1 The handler to invoke with the result (and optionally exception).
601  
    @param h1 The handler to invoke with the result (and optionally exception).
578  

602  

579  
    @return A wrapper that accepts a `task<T>` for immediate execution.
603  
    @return A wrapper that accepts a `task<T>` for immediate execution.
580  

604  

581  
    @see task
605  
    @see task
582  
    @see executor
606  
    @see executor
583  
*/
607  
*/
584  
template<Executor Ex, class H1>
608  
template<Executor Ex, class H1>
585  
[[nodiscard]] auto
609  
[[nodiscard]] auto
586  
run_async(Ex ex, std::stop_token st, H1 h1)
610  
run_async(Ex ex, std::stop_token st, H1 h1)
587  
{
611  
{
588  
    auto* mr = ex.context().get_frame_allocator();
612  
    auto* mr = ex.context().get_frame_allocator();
589  
    return run_async_wrapper<Ex, detail::handler_pair<H1, detail::default_handler>, std::pmr::memory_resource*>(
613  
    return run_async_wrapper<Ex, detail::handler_pair<H1, detail::default_handler>, std::pmr::memory_resource*>(
590  
        std::move(ex),
614  
        std::move(ex),
591  
        std::move(st),
615  
        std::move(st),
592  
        detail::handler_pair<H1, detail::default_handler>{std::move(h1)},
616  
        detail::handler_pair<H1, detail::default_handler>{std::move(h1)},
593  
        mr);
617  
        mr);
594  
}
618  
}
595  

619  

596  
/** Asynchronously launch a lazy task with stop token and separate handlers.
620  
/** Asynchronously launch a lazy task with stop token and separate handlers.
597  

621  

598  
    The stop token is propagated to the task for cooperative cancellation.
622  
    The stop token is propagated to the task for cooperative cancellation.
599  
    The handler `h1` is called on success, `h2` on failure.
623  
    The handler `h1` is called on success, `h2` on failure.
600  

624  

601  
    @param ex The executor to execute the task on.
625  
    @param ex The executor to execute the task on.
602  
    @param st The stop token for cooperative cancellation.
626  
    @param st The stop token for cooperative cancellation.
603  
    @param h1 The handler to invoke with the result on success.
627  
    @param h1 The handler to invoke with the result on success.
604  
    @param h2 The handler to invoke with the exception on failure.
628  
    @param h2 The handler to invoke with the exception on failure.
605  

629  

606  
    @return A wrapper that accepts a `task<T>` for immediate execution.
630  
    @return A wrapper that accepts a `task<T>` for immediate execution.
607  

631  

608  
    @see task
632  
    @see task
609  
    @see executor
633  
    @see executor
610  
*/
634  
*/
611  
template<Executor Ex, class H1, class H2>
635  
template<Executor Ex, class H1, class H2>
612  
[[nodiscard]] auto
636  
[[nodiscard]] auto
613  
run_async(Ex ex, std::stop_token st, H1 h1, H2 h2)
637  
run_async(Ex ex, std::stop_token st, H1 h1, H2 h2)
614  
{
638  
{
615  
    auto* mr = ex.context().get_frame_allocator();
639  
    auto* mr = ex.context().get_frame_allocator();
616  
    return run_async_wrapper<Ex, detail::handler_pair<H1, H2>, std::pmr::memory_resource*>(
640  
    return run_async_wrapper<Ex, detail::handler_pair<H1, H2>, std::pmr::memory_resource*>(
617  
        std::move(ex),
641  
        std::move(ex),
618  
        std::move(st),
642  
        std::move(st),
619  
        detail::handler_pair<H1, H2>{std::move(h1), std::move(h2)},
643  
        detail::handler_pair<H1, H2>{std::move(h1), std::move(h2)},
620  
        mr);
644  
        mr);
621  
}
645  
}
622  

646  

623  
// Ex + memory_resource*
647  
// Ex + memory_resource*
624  

648  

625  
/** Asynchronously launch a lazy task with custom memory resource.
649  
/** Asynchronously launch a lazy task with custom memory resource.
626  

650  

627  
    The memory resource is used for coroutine frame allocation. The caller
651  
    The memory resource is used for coroutine frame allocation. The caller
628  
    is responsible for ensuring the memory resource outlives all tasks.
652  
    is responsible for ensuring the memory resource outlives all tasks.
629  

653  

630  
    @param ex The executor to execute the task on.
654  
    @param ex The executor to execute the task on.
631  
    @param mr The memory resource for frame allocation.
655  
    @param mr The memory resource for frame allocation.
632  

656  

633  
    @return A wrapper that accepts a `task<T>` for immediate execution.
657  
    @return A wrapper that accepts a `task<T>` for immediate execution.
634  

658  

635  
    @see task
659  
    @see task
636  
    @see executor
660  
    @see executor
637  
*/
661  
*/
638  
template<Executor Ex>
662  
template<Executor Ex>
639  
[[nodiscard]] auto
663  
[[nodiscard]] auto
640  
run_async(Ex ex, std::pmr::memory_resource* mr)
664  
run_async(Ex ex, std::pmr::memory_resource* mr)
641  
{
665  
{
642  
    return run_async_wrapper<Ex, detail::default_handler, std::pmr::memory_resource*>(
666  
    return run_async_wrapper<Ex, detail::default_handler, std::pmr::memory_resource*>(
643  
        std::move(ex),
667  
        std::move(ex),
644  
        std::stop_token{},
668  
        std::stop_token{},
645  
        detail::default_handler{},
669  
        detail::default_handler{},
646  
        mr);
670  
        mr);
647  
}
671  
}
648  

672  

649  
/** Asynchronously launch a lazy task with memory resource and handler.
673  
/** Asynchronously launch a lazy task with memory resource and handler.
650  

674  

651  
    @param ex The executor to execute the task on.
675  
    @param ex The executor to execute the task on.
652  
    @param mr The memory resource for frame allocation.
676  
    @param mr The memory resource for frame allocation.
653  
    @param h1 The handler to invoke with the result (and optionally exception).
677  
    @param h1 The handler to invoke with the result (and optionally exception).
654  

678  

655  
    @return A wrapper that accepts a `task<T>` for immediate execution.
679  
    @return A wrapper that accepts a `task<T>` for immediate execution.
656  

680  

657  
    @see task
681  
    @see task
658  
    @see executor
682  
    @see executor
659  
*/
683  
*/
660  
template<Executor Ex, class H1>
684  
template<Executor Ex, class H1>
661  
[[nodiscard]] auto
685  
[[nodiscard]] auto
662  
run_async(Ex ex, std::pmr::memory_resource* mr, H1 h1)
686  
run_async(Ex ex, std::pmr::memory_resource* mr, H1 h1)
663  
{
687  
{
664  
    return run_async_wrapper<Ex, detail::handler_pair<H1, detail::default_handler>, std::pmr::memory_resource*>(
688  
    return run_async_wrapper<Ex, detail::handler_pair<H1, detail::default_handler>, std::pmr::memory_resource*>(
665  
        std::move(ex),
689  
        std::move(ex),
666  
        std::stop_token{},
690  
        std::stop_token{},
667  
        detail::handler_pair<H1, detail::default_handler>{std::move(h1)},
691  
        detail::handler_pair<H1, detail::default_handler>{std::move(h1)},
668  
        mr);
692  
        mr);
669  
}
693  
}
670  

694  

671  
/** Asynchronously launch a lazy task with memory resource and handlers.
695  
/** Asynchronously launch a lazy task with memory resource and handlers.
672  

696  

673  
    @param ex The executor to execute the task on.
697  
    @param ex The executor to execute the task on.
674  
    @param mr The memory resource for frame allocation.
698  
    @param mr The memory resource for frame allocation.
675  
    @param h1 The handler to invoke with the result on success.
699  
    @param h1 The handler to invoke with the result on success.
676  
    @param h2 The handler to invoke with the exception on failure.
700  
    @param h2 The handler to invoke with the exception on failure.
677  

701  

678  
    @return A wrapper that accepts a `task<T>` for immediate execution.
702  
    @return A wrapper that accepts a `task<T>` for immediate execution.
679  

703  

680  
    @see task
704  
    @see task
681  
    @see executor
705  
    @see executor
682  
*/
706  
*/
683  
template<Executor Ex, class H1, class H2>
707  
template<Executor Ex, class H1, class H2>
684  
[[nodiscard]] auto
708  
[[nodiscard]] auto
685  
run_async(Ex ex, std::pmr::memory_resource* mr, H1 h1, H2 h2)
709  
run_async(Ex ex, std::pmr::memory_resource* mr, H1 h1, H2 h2)
686  
{
710  
{
687  
    return run_async_wrapper<Ex, detail::handler_pair<H1, H2>, std::pmr::memory_resource*>(
711  
    return run_async_wrapper<Ex, detail::handler_pair<H1, H2>, std::pmr::memory_resource*>(
688  
        std::move(ex),
712  
        std::move(ex),
689  
        std::stop_token{},
713  
        std::stop_token{},
690  
        detail::handler_pair<H1, H2>{std::move(h1), std::move(h2)},
714  
        detail::handler_pair<H1, H2>{std::move(h1), std::move(h2)},
691  
        mr);
715  
        mr);
692  
}
716  
}
693  

717  

694  
// Ex + stop_token + memory_resource*
718  
// Ex + stop_token + memory_resource*
695  

719  

696  
/** Asynchronously launch a lazy task with stop token and memory resource.
720  
/** Asynchronously launch a lazy task with stop token and memory resource.
697  

721  

698  
    @param ex The executor to execute the task on.
722  
    @param ex The executor to execute the task on.
699  
    @param st The stop token for cooperative cancellation.
723  
    @param st The stop token for cooperative cancellation.
700  
    @param mr The memory resource for frame allocation.
724  
    @param mr The memory resource for frame allocation.
701  

725  

702  
    @return A wrapper that accepts a `task<T>` for immediate execution.
726  
    @return A wrapper that accepts a `task<T>` for immediate execution.
703  

727  

704  
    @see task
728  
    @see task
705  
    @see executor
729  
    @see executor
706  
*/
730  
*/
707  
template<Executor Ex>
731  
template<Executor Ex>
708  
[[nodiscard]] auto
732  
[[nodiscard]] auto
709  
run_async(Ex ex, std::stop_token st, std::pmr::memory_resource* mr)
733  
run_async(Ex ex, std::stop_token st, std::pmr::memory_resource* mr)
710  
{
734  
{
711  
    return run_async_wrapper<Ex, detail::default_handler, std::pmr::memory_resource*>(
735  
    return run_async_wrapper<Ex, detail::default_handler, std::pmr::memory_resource*>(
712  
        std::move(ex),
736  
        std::move(ex),
713  
        std::move(st),
737  
        std::move(st),
714  
        detail::default_handler{},
738  
        detail::default_handler{},
715  
        mr);
739  
        mr);
716  
}
740  
}
717  

741  

718  
/** Asynchronously launch a lazy task with stop token, memory resource, and handler.
742  
/** Asynchronously launch a lazy task with stop token, memory resource, and handler.
719  

743  

720  
    @param ex The executor to execute the task on.
744  
    @param ex The executor to execute the task on.
721  
    @param st The stop token for cooperative cancellation.
745  
    @param st The stop token for cooperative cancellation.
722  
    @param mr The memory resource for frame allocation.
746  
    @param mr The memory resource for frame allocation.
723  
    @param h1 The handler to invoke with the result (and optionally exception).
747  
    @param h1 The handler to invoke with the result (and optionally exception).
724  

748  

725  
    @return A wrapper that accepts a `task<T>` for immediate execution.
749  
    @return A wrapper that accepts a `task<T>` for immediate execution.
726  

750  

727  
    @see task
751  
    @see task
728  
    @see executor
752  
    @see executor
729  
*/
753  
*/
730  
template<Executor Ex, class H1>
754  
template<Executor Ex, class H1>
731  
[[nodiscard]] auto
755  
[[nodiscard]] auto
732  
run_async(Ex ex, std::stop_token st, std::pmr::memory_resource* mr, H1 h1)
756  
run_async(Ex ex, std::stop_token st, std::pmr::memory_resource* mr, H1 h1)
733  
{
757  
{
734  
    return run_async_wrapper<Ex, detail::handler_pair<H1, detail::default_handler>, std::pmr::memory_resource*>(
758  
    return run_async_wrapper<Ex, detail::handler_pair<H1, detail::default_handler>, std::pmr::memory_resource*>(
735  
        std::move(ex),
759  
        std::move(ex),
736  
        std::move(st),
760  
        std::move(st),
737  
        detail::handler_pair<H1, detail::default_handler>{std::move(h1)},
761  
        detail::handler_pair<H1, detail::default_handler>{std::move(h1)},
738  
        mr);
762  
        mr);
739  
}
763  
}
740  

764  

741  
/** Asynchronously launch a lazy task with stop token, memory resource, and handlers.
765  
/** Asynchronously launch a lazy task with stop token, memory resource, and handlers.
742  

766  

743  
    @param ex The executor to execute the task on.
767  
    @param ex The executor to execute the task on.
744  
    @param st The stop token for cooperative cancellation.
768  
    @param st The stop token for cooperative cancellation.
745  
    @param mr The memory resource for frame allocation.
769  
    @param mr The memory resource for frame allocation.
746  
    @param h1 The handler to invoke with the result on success.
770  
    @param h1 The handler to invoke with the result on success.
747  
    @param h2 The handler to invoke with the exception on failure.
771  
    @param h2 The handler to invoke with the exception on failure.
748  

772  

749  
    @return A wrapper that accepts a `task<T>` for immediate execution.
773  
    @return A wrapper that accepts a `task<T>` for immediate execution.
750  

774  

751  
    @see task
775  
    @see task
752  
    @see executor
776  
    @see executor
753  
*/
777  
*/
754  
template<Executor Ex, class H1, class H2>
778  
template<Executor Ex, class H1, class H2>
755  
[[nodiscard]] auto
779  
[[nodiscard]] auto
756  
run_async(Ex ex, std::stop_token st, std::pmr::memory_resource* mr, H1 h1, H2 h2)
780  
run_async(Ex ex, std::stop_token st, std::pmr::memory_resource* mr, H1 h1, H2 h2)
757  
{
781  
{
758  
    return run_async_wrapper<Ex, detail::handler_pair<H1, H2>, std::pmr::memory_resource*>(
782  
    return run_async_wrapper<Ex, detail::handler_pair<H1, H2>, std::pmr::memory_resource*>(
759  
        std::move(ex),
783  
        std::move(ex),
760  
        std::move(st),
784  
        std::move(st),
761  
        detail::handler_pair<H1, H2>{std::move(h1), std::move(h2)},
785  
        detail::handler_pair<H1, H2>{std::move(h1), std::move(h2)},
762  
        mr);
786  
        mr);
763  
}
787  
}
764  

788  

765  
// Ex + standard Allocator (value type)
789  
// Ex + standard Allocator (value type)
766  

790  

767  
/** Asynchronously launch a lazy task with custom allocator.
791  
/** Asynchronously launch a lazy task with custom allocator.
768  

792  

769  
    The allocator is wrapped in a frame_memory_resource and stored in the
793  
    The allocator is wrapped in a frame_memory_resource and stored in the
770  
    run_async_trampoline, ensuring it outlives all coroutine frames.
794  
    run_async_trampoline, ensuring it outlives all coroutine frames.
771  

795  

772  
    @param ex The executor to execute the task on.
796  
    @param ex The executor to execute the task on.
773  
    @param alloc The allocator for frame allocation (copied and stored).
797  
    @param alloc The allocator for frame allocation (copied and stored).
774  

798  

775  
    @return A wrapper that accepts a `task<T>` for immediate execution.
799  
    @return A wrapper that accepts a `task<T>` for immediate execution.
776  

800  

777  
    @see task
801  
    @see task
778  
    @see executor
802  
    @see executor
779  
*/
803  
*/
780  
template<Executor Ex, detail::Allocator Alloc>
804  
template<Executor Ex, detail::Allocator Alloc>
781  
[[nodiscard]] auto
805  
[[nodiscard]] auto
782  
run_async(Ex ex, Alloc alloc)
806  
run_async(Ex ex, Alloc alloc)
783  
{
807  
{
784  
    return run_async_wrapper<Ex, detail::default_handler, Alloc>(
808  
    return run_async_wrapper<Ex, detail::default_handler, Alloc>(
785  
        std::move(ex),
809  
        std::move(ex),
786  
        std::stop_token{},
810  
        std::stop_token{},
787  
        detail::default_handler{},
811  
        detail::default_handler{},
788  
        std::move(alloc));
812  
        std::move(alloc));
789  
}
813  
}
790  

814  

791  
/** Asynchronously launch a lazy task with allocator and handler.
815  
/** Asynchronously launch a lazy task with allocator and handler.
792  

816  

793  
    @param ex The executor to execute the task on.
817  
    @param ex The executor to execute the task on.
794  
    @param alloc The allocator for frame allocation (copied and stored).
818  
    @param alloc The allocator for frame allocation (copied and stored).
795  
    @param h1 The handler to invoke with the result (and optionally exception).
819  
    @param h1 The handler to invoke with the result (and optionally exception).
796  

820  

797  
    @return A wrapper that accepts a `task<T>` for immediate execution.
821  
    @return A wrapper that accepts a `task<T>` for immediate execution.
798  

822  

799  
    @see task
823  
    @see task
800  
    @see executor
824  
    @see executor
801  
*/
825  
*/
802  
template<Executor Ex, detail::Allocator Alloc, class H1>
826  
template<Executor Ex, detail::Allocator Alloc, class H1>
803  
[[nodiscard]] auto
827  
[[nodiscard]] auto
804  
run_async(Ex ex, Alloc alloc, H1 h1)
828  
run_async(Ex ex, Alloc alloc, H1 h1)
805  
{
829  
{
806  
    return run_async_wrapper<Ex, detail::handler_pair<H1, detail::default_handler>, Alloc>(
830  
    return run_async_wrapper<Ex, detail::handler_pair<H1, detail::default_handler>, Alloc>(
807  
        std::move(ex),
831  
        std::move(ex),
808  
        std::stop_token{},
832  
        std::stop_token{},
809  
        detail::handler_pair<H1, detail::default_handler>{std::move(h1)},
833  
        detail::handler_pair<H1, detail::default_handler>{std::move(h1)},
810  
        std::move(alloc));
834  
        std::move(alloc));
811  
}
835  
}
812  

836  

813  
/** Asynchronously launch a lazy task with allocator and handlers.
837  
/** Asynchronously launch a lazy task with allocator and handlers.
814  

838  

815  
    @param ex The executor to execute the task on.
839  
    @param ex The executor to execute the task on.
816  
    @param alloc The allocator for frame allocation (copied and stored).
840  
    @param alloc The allocator for frame allocation (copied and stored).
817  
    @param h1 The handler to invoke with the result on success.
841  
    @param h1 The handler to invoke with the result on success.
818  
    @param h2 The handler to invoke with the exception on failure.
842  
    @param h2 The handler to invoke with the exception on failure.
819  

843  

820  
    @return A wrapper that accepts a `task<T>` for immediate execution.
844  
    @return A wrapper that accepts a `task<T>` for immediate execution.
821  

845  

822  
    @see task
846  
    @see task
823  
    @see executor
847  
    @see executor
824  
*/
848  
*/
825  
template<Executor Ex, detail::Allocator Alloc, class H1, class H2>
849  
template<Executor Ex, detail::Allocator Alloc, class H1, class H2>
826  
[[nodiscard]] auto
850  
[[nodiscard]] auto
827  
run_async(Ex ex, Alloc alloc, H1 h1, H2 h2)
851  
run_async(Ex ex, Alloc alloc, H1 h1, H2 h2)
828  
{
852  
{
829  
    return run_async_wrapper<Ex, detail::handler_pair<H1, H2>, Alloc>(
853  
    return run_async_wrapper<Ex, detail::handler_pair<H1, H2>, Alloc>(
830  
        std::move(ex),
854  
        std::move(ex),
831  
        std::stop_token{},
855  
        std::stop_token{},
832  
        detail::handler_pair<H1, H2>{std::move(h1), std::move(h2)},
856  
        detail::handler_pair<H1, H2>{std::move(h1), std::move(h2)},
833  
        std::move(alloc));
857  
        std::move(alloc));
834  
}
858  
}
835  

859  

836  
// Ex + stop_token + standard Allocator
860  
// Ex + stop_token + standard Allocator
837  

861  

838  
/** Asynchronously launch a lazy task with stop token and allocator.
862  
/** Asynchronously launch a lazy task with stop token and allocator.
839  

863  

840  
    @param ex The executor to execute the task on.
864  
    @param ex The executor to execute the task on.
841  
    @param st The stop token for cooperative cancellation.
865  
    @param st The stop token for cooperative cancellation.
842  
    @param alloc The allocator for frame allocation (copied and stored).
866  
    @param alloc The allocator for frame allocation (copied and stored).
843  

867  

844  
    @return A wrapper that accepts a `task<T>` for immediate execution.
868  
    @return A wrapper that accepts a `task<T>` for immediate execution.
845  

869  

846  
    @see task
870  
    @see task
847  
    @see executor
871  
    @see executor
848  
*/
872  
*/
849  
template<Executor Ex, detail::Allocator Alloc>
873  
template<Executor Ex, detail::Allocator Alloc>
850  
[[nodiscard]] auto
874  
[[nodiscard]] auto
851  
run_async(Ex ex, std::stop_token st, Alloc alloc)
875  
run_async(Ex ex, std::stop_token st, Alloc alloc)
852  
{
876  
{
853  
    return run_async_wrapper<Ex, detail::default_handler, Alloc>(
877  
    return run_async_wrapper<Ex, detail::default_handler, Alloc>(
854  
        std::move(ex),
878  
        std::move(ex),
855  
        std::move(st),
879  
        std::move(st),
856  
        detail::default_handler{},
880  
        detail::default_handler{},
857  
        std::move(alloc));
881  
        std::move(alloc));
858  
}
882  
}
859  

883  

860  
/** Asynchronously launch a lazy task with stop token, allocator, and handler.
884  
/** Asynchronously launch a lazy task with stop token, allocator, and handler.
861  

885  

862  
    @param ex The executor to execute the task on.
886  
    @param ex The executor to execute the task on.
863  
    @param st The stop token for cooperative cancellation.
887  
    @param st The stop token for cooperative cancellation.
864  
    @param alloc The allocator for frame allocation (copied and stored).
888  
    @param alloc The allocator for frame allocation (copied and stored).
865  
    @param h1 The handler to invoke with the result (and optionally exception).
889  
    @param h1 The handler to invoke with the result (and optionally exception).
866  

890  

867  
    @return A wrapper that accepts a `task<T>` for immediate execution.
891  
    @return A wrapper that accepts a `task<T>` for immediate execution.
868  

892  

869  
    @see task
893  
    @see task
870  
    @see executor
894  
    @see executor
871  
*/
895  
*/
872  
template<Executor Ex, detail::Allocator Alloc, class H1>
896  
template<Executor Ex, detail::Allocator Alloc, class H1>
873  
[[nodiscard]] auto
897  
[[nodiscard]] auto
874  
run_async(Ex ex, std::stop_token st, Alloc alloc, H1 h1)
898  
run_async(Ex ex, std::stop_token st, Alloc alloc, H1 h1)
875  
{
899  
{
876  
    return run_async_wrapper<Ex, detail::handler_pair<H1, detail::default_handler>, Alloc>(
900  
    return run_async_wrapper<Ex, detail::handler_pair<H1, detail::default_handler>, Alloc>(
877  
        std::move(ex),
901  
        std::move(ex),
878  
        std::move(st),
902  
        std::move(st),
879  
        detail::handler_pair<H1, detail::default_handler>{std::move(h1)},
903  
        detail::handler_pair<H1, detail::default_handler>{std::move(h1)},
880  
        std::move(alloc));
904  
        std::move(alloc));
881  
}
905  
}
882  

906  

883  
/** Asynchronously launch a lazy task with stop token, allocator, and handlers.
907  
/** Asynchronously launch a lazy task with stop token, allocator, and handlers.
884  

908  

885  
    @param ex The executor to execute the task on.
909  
    @param ex The executor to execute the task on.
886  
    @param st The stop token for cooperative cancellation.
910  
    @param st The stop token for cooperative cancellation.
887  
    @param alloc The allocator for frame allocation (copied and stored).
911  
    @param alloc The allocator for frame allocation (copied and stored).
888  
    @param h1 The handler to invoke with the result on success.
912  
    @param h1 The handler to invoke with the result on success.
889  
    @param h2 The handler to invoke with the exception on failure.
913  
    @param h2 The handler to invoke with the exception on failure.
890  

914  

891  
    @return A wrapper that accepts a `task<T>` for immediate execution.
915  
    @return A wrapper that accepts a `task<T>` for immediate execution.
892  

916  

893  
    @see task
917  
    @see task
894  
    @see executor
918  
    @see executor
895  
*/
919  
*/
896  
template<Executor Ex, detail::Allocator Alloc, class H1, class H2>
920  
template<Executor Ex, detail::Allocator Alloc, class H1, class H2>
897  
[[nodiscard]] auto
921  
[[nodiscard]] auto
898  
run_async(Ex ex, std::stop_token st, Alloc alloc, H1 h1, H2 h2)
922  
run_async(Ex ex, std::stop_token st, Alloc alloc, H1 h1, H2 h2)
899  
{
923  
{
900  
    return run_async_wrapper<Ex, detail::handler_pair<H1, H2>, Alloc>(
924  
    return run_async_wrapper<Ex, detail::handler_pair<H1, H2>, Alloc>(
901  
        std::move(ex),
925  
        std::move(ex),
902  
        std::move(st),
926  
        std::move(st),
903  
        detail::handler_pair<H1, H2>{std::move(h1), std::move(h2)},
927  
        detail::handler_pair<H1, H2>{std::move(h1), std::move(h2)},
904  
        std::move(alloc));
928  
        std::move(alloc));
905  
}
929  
}
906  

930  

907  
} // namespace capy
931  
} // namespace capy
908  
} // namespace boost
932  
} // namespace boost
909  

933  

910  
#endif
934  
#endif