include/boost/corosio/detail/dispatch_coro.hpp

100.0% Lines (4 / 4) 100.0% Functions (1 / 1)
dispatch_coro.hpp
f(x) Functions (1)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2026 Steve Gerbino
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/corosio
9 //
10
11 #ifndef BOOST_COROSIO_DETAIL_DISPATCH_CORO_HPP
12 #define BOOST_COROSIO_DETAIL_DISPATCH_CORO_HPP
13
14 #include <boost/corosio/io_context.hpp>
15 #include <boost/capy/continuation.hpp>
16 #include <boost/capy/ex/executor_ref.hpp>
17 #include <boost/capy/detail/type_id.hpp>
18 #include <coroutine>
19
20 namespace boost::corosio::detail {
21
22 /** Returns a handle for symmetric transfer on I/O completion.
23
24 Returns `c.h` when the executor's context is running on the
25 calling thread, so the awaiter resumes by symmetric transfer.
26 Otherwise the executor queues `c` and `noop_coroutine()` is
27 returned. An `io_context` executor is dispatched without type
28 erasure.
29
30 Callers in coroutine machinery should return the result
31 for symmetric transfer. Callers at the scheduler pump
32 level should call `.resume()` on the result.
33
34 @note A `dispatch` that throws calls `std::terminate`.
35
36 @param ex The executor to dispatch through.
37 @param c The continuation to dispatch. Must remain at a
38 stable address until dequeued by the executor.
39
40 @return A handle for symmetric transfer or `std::noop_coroutine()`.
41 */
42 inline std::coroutine_handle<>
43 490548x dispatch_coro(capy::executor_ref ex, capy::continuation& c) noexcept
44 {
45 // An exception from dispatch would leave the completed operation
46 // with nobody to resume or abandon its coroutine.
47 // A completion from another context must not resume the awaiter
48 // on this thread, so the fast path still asks the executor.
49 490548x if (auto const* io_ex = ex.target<io_context::executor_type>())
50 489417x return io_ex->dispatch(c);
51 1131x return ex.dispatch(c);
52 }
53
54 } // namespace boost::corosio::detail
55
56 #endif
57