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