include/boost/corosio/native/detail/io_uring/io_uring_op.hpp

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