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

100.0% Lines (9 / 9) 100.0% Functions (2 / 2)
uring_descriptor_service.hpp
f(x) Functions (2)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Michael Vandeberg
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_DESCRIPTOR_SERVICE_HPP
11 #define BOOST_COROSIO_NATIVE_DETAIL_URING_URING_DESCRIPTOR_SERVICE_HPP
12
13 #include <boost/corosio/detail/platform.hpp>
14
15 #if BOOST_COROSIO_HAS_URING
16
17 #include <boost/corosio/detail/descriptor_service.hpp>
18 #include <boost/corosio/native/detail/uring/uring_descriptor.hpp>
19 #include <boost/corosio/native/detail/uring/uring_file_service_base.hpp>
20 #include <boost/corosio/native/detail/uring/uring_scheduler.hpp>
21 #include <boost/corosio/native/detail/validate_fd.hpp>
22 #include <boost/capy/ex/execution_context.hpp>
23
24 #include <system_error>
25
26 /* io_uring-backed descriptor_service.
27
28 assign_descriptor is the reactor version minus the registration
29 step: io_uring has no adopt-time registration syscall, so adoption
30 cannot fail once validation has passed, and a kernel refusal
31 surfaces at the first operation instead. It does probe whether the
32 kernel can poll the fd, which the reactors learn from registration.
33
34 Lifecycle comes from uring_file_service_base, as asio's file service
35 reuses its descriptor service.
36 */
37
38 namespace boost::corosio::detail {
39
40 /// Native io_uring descriptor service. Owns every @ref uring_descriptor
41 /// the context creates.
42 class BOOST_COROSIO_DECL uring_descriptor_service final
43 : public uring_file_service_base<
44 uring_descriptor_service,
45 descriptor_service,
46 uring_descriptor>
47 {
48 using base_service = uring_file_service_base<
49 uring_descriptor_service,
50 descriptor_service,
51 uring_descriptor>;
52
53 public:
54 53x explicit uring_descriptor_service(capy::execution_context& ctx)
55 53x : base_service(ctx.use_service<uring_scheduler>())
56 {
57 53x }
58
59 54x std::error_code assign_descriptor(
60 posix_stream_descriptor::implementation& impl_base,
61 native_handle_type fd) override
62 {
63 54x auto* impl = static_cast<uring_descriptor*>(&impl_base);
64
65 // The public assign() guarantees the object is closed.
66 54x if (auto ec = validate_descriptor_fd(fd))
67 2x return ec;
68
69 52x impl->set_descriptor(fd, fd_is_pollable(fd));
70 52x return {};
71 }
72 };
73
74 } // namespace boost::corosio::detail
75
76 #endif // BOOST_COROSIO_HAS_URING
77
78 #endif // BOOST_COROSIO_NATIVE_DETAIL_URING_URING_DESCRIPTOR_SERVICE_HPP
79