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

100.0% Lines (25 / 25) 100.0% Functions (21 / 21)
uring_file_service_base.hpp
f(x) Functions (21)
Function Calls Lines Blocks
boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_descriptor_service, boost::corosio::detail::descriptor_service, boost::corosio::detail::uring_descriptor>::uring_file_service_base(boost::corosio::detail::uring_scheduler&) :65 71x 100.0% 100.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_random_access_file_service, boost::corosio::detail::random_access_file_service, boost::corosio::detail::uring_random_access_file>::uring_file_service_base(boost::corosio::detail::uring_scheduler&) :65 86x 100.0% 100.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_stream_file_service, boost::corosio::detail::file_service, boost::corosio::detail::uring_stream_file>::uring_file_service_base(boost::corosio::detail::uring_scheduler&) :65 76x 100.0% 100.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_descriptor_service, boost::corosio::detail::descriptor_service, boost::corosio::detail::uring_descriptor>::~uring_file_service_base() :71 71x 100.0% 100.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_random_access_file_service, boost::corosio::detail::random_access_file_service, boost::corosio::detail::uring_random_access_file>::~uring_file_service_base() :71 86x 100.0% 100.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_stream_file_service, boost::corosio::detail::file_service, boost::corosio::detail::uring_stream_file>::~uring_file_service_base() :71 76x 100.0% 100.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_descriptor_service, boost::corosio::detail::descriptor_service, boost::corosio::detail::uring_descriptor>::construct() :73 113x 100.0% 100.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_random_access_file_service, boost::corosio::detail::random_access_file_service, boost::corosio::detail::uring_random_access_file>::construct() :73 115x 100.0% 100.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_stream_file_service, boost::corosio::detail::file_service, boost::corosio::detail::uring_stream_file>::construct() :73 136x 100.0% 100.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_descriptor_service, boost::corosio::detail::descriptor_service, boost::corosio::detail::uring_descriptor>::destroy(boost::corosio::io_object::implementation*) :78 113x 100.0% 100.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_random_access_file_service, boost::corosio::detail::random_access_file_service, boost::corosio::detail::uring_random_access_file>::destroy(boost::corosio::io_object::implementation*) :78 115x 100.0% 100.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_stream_file_service, boost::corosio::detail::file_service, boost::corosio::detail::uring_stream_file>::destroy(boost::corosio::io_object::implementation*) :78 136x 100.0% 100.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_descriptor_service, boost::corosio::detail::descriptor_service, boost::corosio::detail::uring_descriptor>::close(boost::corosio::io_object::handle&) :86 185x 100.0% 100.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_random_access_file_service, boost::corosio::detail::random_access_file_service, boost::corosio::detail::uring_random_access_file>::close(boost::corosio::io_object::handle&) :86 212x 100.0% 100.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_stream_file_service, boost::corosio::detail::file_service, boost::corosio::detail::uring_stream_file>::close(boost::corosio::io_object::handle&) :86 255x 100.0% 100.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_descriptor_service, boost::corosio::detail::descriptor_service, boost::corosio::detail::uring_descriptor>::shutdown() :92 71x 75.0% 80.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_random_access_file_service, boost::corosio::detail::random_access_file_service, boost::corosio::detail::uring_random_access_file>::shutdown() :92 86x 75.0% 80.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_stream_file_service, boost::corosio::detail::file_service, boost::corosio::detail::uring_stream_file>::shutdown() :92 76x 75.0% 80.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_descriptor_service, boost::corosio::detail::descriptor_service, boost::corosio::detail::uring_descriptor>::shutdown()::{lambda(boost::corosio::detail::uring_descriptor*)#1}::operator()(boost::corosio::detail::uring_descriptor*) const :101 6x 100.0% 100.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_random_access_file_service, boost::corosio::detail::random_access_file_service, boost::corosio::detail::uring_random_access_file>::shutdown()::{lambda(boost::corosio::detail::uring_random_access_file*)#1}::operator()(boost::corosio::detail::uring_random_access_file*) const :101 5x 100.0% 100.0% boost::corosio::detail::uring_file_service_base<boost::corosio::detail::uring_stream_file_service, boost::corosio::detail::file_service, boost::corosio::detail::uring_stream_file>::shutdown()::{lambda(boost::corosio::detail::uring_stream_file*)#1}::operator()(boost::corosio::detail::uring_stream_file*) const :101 1x 100.0% 100.0%
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_FILE_SERVICE_BASE_HPP
11 #define BOOST_COROSIO_NATIVE_DETAIL_URING_URING_FILE_SERVICE_BASE_HPP
12
13 #include <boost/corosio/detail/platform.hpp>
14
15 #if BOOST_COROSIO_HAS_URING
16
17 #include <boost/corosio/detail/object_pool.hpp>
18 #include <boost/corosio/detail/object_ref.hpp>
19 #include <boost/corosio/io/io_object.hpp>
20 #include <boost/corosio/native/detail/uring/uring_scheduler.hpp>
21
22 #include <vector>
23
24 /*
25 Shared lifecycle plumbing for io_uring file services.
26
27 uring_stream_file_service and uring_random_access_file_service were
28 byte-for-byte identical apart from the impl type and open_file's parameter
29 type: both pop-or-new the file impl from a per-service recycling pool
30 and close every file on shutdown. This base factors that out; the
31 concrete services add only open_file.
32
33 This is a separate base from uring_socket_service_base because file
34 services differ from socket services in two ways that match the reactor
35 socket service instead: they CLOSE files on shutdown (sockets: cancel
36 only), and the impl ctor takes just the scheduler (sockets: service +
37 scheduler). See tasks/proactor-dedup-decisions.md (#14).
38
39 Requirements on File: derive from intrusive_list<File>::node, a
40 `File(Derived&, uring_scheduler&)` constructor, a
41 `void close_file() noexcept` method (cancel in-flight ops + close fd),
42 a `void reuse() noexcept` method, and a `retire()` override that
43 recycles through the owning service's private `pool_` member
44 (reachable because `File` is a friend). uring_descriptor_service
45 reuses it too, as asio's file service reuses its descriptor service.
46
47 @tparam Derived The concrete service (CRTP; unused today but kept for
48 symmetry / future hooks).
49 @tparam ServiceBase The abstract service vtable base (file_service,
50 random_access_file_service).
51 @tparam File The concrete io_uring file impl type.
52 */
53
54 namespace boost::corosio::detail {
55
56 template<class Derived, class ServiceBase, class File>
57 class uring_file_service_base : public ServiceBase
58 {
59 friend Derived;
60 friend File;
61
62 // Private CRTP ctor: only `Derived` (the concrete service, a friend)
63 // constructs the base — prevents inheriting with the wrong Derived
64 // (bugprone-crtp-constructor-accessibility).
65 233x explicit uring_file_service_base(uring_scheduler& sched) noexcept
66 233x : sched_(&sched)
67 {
68 233x }
69
70 public:
71 233x ~uring_file_service_base() override = default;
72
73 364x io_object::implementation* construct() override
74 {
75 364x return pool_.acquire(static_cast<Derived&>(*this), *sched_);
76 }
77
78 364x void destroy(io_object::implementation* p) override
79 {
80 // close_file() closes the fd only once its cancel reached the kernel.
81 364x auto& impl = static_cast<File&>(*p);
82 364x impl.close_file();
83 364x release(&impl);
84 364x }
85
86 652x void close(io_object::handle& h) override
87 {
88 652x if (h.get())
89 652x static_cast<File&>(*h.get()).close_file();
90 652x }
91
92 233x void shutdown() override
93 {
94 // See uring_socket_service_base::shutdown(): snapshot under an
95 // acquired reference, close without the pool lock held;
96 // shutdown() sets shutting-down and takes the snapshot in one
97 // critical section, so a close that drops the last ref deletes
98 // rather than recycles.
99 233x std::vector<File*> live;
100 233x pool_.shutdown(
101 233x [&](File* f)
102 {
103 12x acquire(f);
104 12x live.push_back(f);
105 });
106 245x for (auto* f : live)
107 {
108 12x f->close_file();
109 12x release(f);
110 }
111 233x }
112
113 /// Return the scheduler used by files created by this service.
114 uring_scheduler& scheduler() noexcept
115 {
116 return *sched_;
117 }
118
119 protected:
120 uring_scheduler* sched_;
121 object_pool<File> pool_;
122
123 private:
124 uring_file_service_base(uring_file_service_base const&) = delete;
125 uring_file_service_base& operator=(uring_file_service_base const&) = delete;
126 };
127
128 } // namespace boost::corosio::detail
129
130 #endif // BOOST_COROSIO_HAS_URING
131
132 #endif // BOOST_COROSIO_NATIVE_DETAIL_URING_URING_FILE_SERVICE_BASE_HPP
133