include/boost/corosio/native/detail/uring/uring_op.hpp

100.0% Lines (12 / 12) 100.0% Functions (3 / 3)
uring_op.hpp
f(x) Functions (3)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Steve Gerbino
3 //
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)
6 //
7 // Official repository: https://github.com/cppalliance/corosio
8 //
9
10 #ifndef BOOST_COROSIO_NATIVE_DETAIL_URING_URING_OP_HPP
11 #define BOOST_COROSIO_NATIVE_DETAIL_URING_URING_OP_HPP
12
13 #include <boost/corosio/detail/platform.hpp>
14
15 #if BOOST_COROSIO_HAS_URING
16
17 #include <boost/corosio/native/detail/coro_op.hpp>
18 #include <boost/corosio/detail/ready_queue.hpp>
19
20 // Forward declare to avoid circular include with uring_scheduler.hpp.
21 namespace boost::corosio::detail {
22 class uring_scheduler;
23 } // namespace boost::corosio::detail
24
25 #include <atomic>
26
27 #include <liburing.h>
28
29 namespace boost::corosio::detail {
30
31 /** io_uring operation: the shared proactor op envelope plus the
32 io_uring-specific completion plumbing.
33
34 `coro_op` supplies the fields common to both proactor backends
35 (coroutine handle, executor, output pointers, stop_token wiring,
36 object_ref_ keepalive). This type adds the CQE result (`res`/`cqe_flags`),
37 the ring-cancel visibility flag (`sqe_set`), and the two function
38 pointers the run loop uses to prep an SQE and dispatch a CQE without
39 template instantiation.
40 */
41 struct uring_op : coro_op
42 {
43 /// CQE-side dispatcher type. Called once per completion event.
44 /// Pushes self into `local` rather than dispatching inline so
45 /// process_completions can splice the batch into completed_ops_
46 /// atomically and do_one dispatches one handler at a time.
47 using cqe_func_type = void (*)(
48 uring_op*, int res, unsigned flags, ready_queue& local) noexcept;
49
50 /// SQE-preparation dispatcher type. Called by the leader during
51 /// its drain step to fill an SQE for this op. Concrete op types
52 /// set this at construction so the new submit path is purely
53 /// data-driven (no template instantiation, no allocation).
54 using prep_func_type = void (*)(uring_op*, ::io_uring_sqe*) noexcept;
55
56 12629x explicit uring_op(
57 func_type post_func,
58 cqe_func_type cqe_fn,
59 prep_func_type prep_fn = nullptr) noexcept
60 12629x : coro_op(post_func)
61 12629x , cqe_func(cqe_fn)
62 12629x , prep_func(prep_fn)
63 {
64 12629x }
65
66 int res = 0;
67 unsigned cqe_flags = 0;
68 /// True after `io_uring_sqe_set_data` has linked an SQE to this op.
69 /// Until then, on_cancel() has nothing for the kernel to find.
70 std::atomic<bool> sqe_set{false};
71 cqe_func_type cqe_func;
72 /// SQE-preparation dispatcher. nullptr for ops still using the
73 /// old `uring_submit_op<PrepFn>(prep)` template path
74 /// (UDP/local/file/dgram during plan 5a). Set non-null by ops
75 /// migrated to the queue-based submit path.
76 prep_func_type prep_func;
77
78 /// Scheduler reference for submitting cancel SQEs on stop_token.
79 uring_scheduler* sched_ = nullptr;
80
81 /// Bridge virtual dispatch to func-pointer dispatch. Lets the run
82 /// loop dispatch any scheduler_op via `(*op)()` — both reactor-style
83 /// services posted into the queue and proactor-style io_uring ops.
84 /// `owner` is non-null per scheduler_op's completion-vs-destroy
85 /// convention (see scheduler_op.hpp).
86 27617x void operator()() override
87 {
88 27617x complete(this, 0, 0);
89 27617x }
90
91 /// Arm the stop-token callback. Must be called before the SQE submits.
92 /// Extends coro_op::start to also clear the ring-cancel flag.
93 27266x void start(std::stop_token const& token)
94 {
95 27266x sqe_set.store(false, std::memory_order_relaxed);
96 27266x coro_op::start(token);
97 27266x }
98
99 /// io_uring cancellation: record the request and, if an SQE was already
100 /// linked, ask the kernel to cancel it by user_data.
101 void on_cancel() noexcept override;
102 };
103
104 } // namespace boost::corosio::detail
105
106 #endif // BOOST_COROSIO_HAS_URING
107
108 #endif // BOOST_COROSIO_NATIVE_DETAIL_URING_URING_OP_HPP
109