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

93.5% Lines (29/31) 100.0% List of functions (12/12)
io_uring_file_service_base.hpp
f(x) Functions (12)
Function Calls Lines Blocks
boost::corosio::detail::io_uring_file_service_base<boost::corosio::detail::io_uring_random_access_file_service, boost::corosio::detail::random_access_file_service, boost::corosio::detail::io_uring_random_access_file>::io_uring_file_service_base(boost::corosio::detail::io_uring_scheduler&) :62 551x 100.0% 100.0% boost::corosio::detail::io_uring_file_service_base<boost::corosio::detail::io_uring_stream_file_service, boost::corosio::detail::file_service, boost::corosio::detail::io_uring_stream_file>::io_uring_file_service_base(boost::corosio::detail::io_uring_scheduler&) :62 551x 100.0% 100.0% boost::corosio::detail::io_uring_file_service_base<boost::corosio::detail::io_uring_random_access_file_service, boost::corosio::detail::random_access_file_service, boost::corosio::detail::io_uring_random_access_file>::~io_uring_file_service_base() :68 551x 100.0% 100.0% boost::corosio::detail::io_uring_file_service_base<boost::corosio::detail::io_uring_stream_file_service, boost::corosio::detail::file_service, boost::corosio::detail::io_uring_stream_file>::~io_uring_file_service_base() :68 551x 100.0% 100.0% boost::corosio::detail::io_uring_file_service_base<boost::corosio::detail::io_uring_random_access_file_service, boost::corosio::detail::random_access_file_service, boost::corosio::detail::io_uring_random_access_file>::construct() :70 38x 100.0% 71.0% boost::corosio::detail::io_uring_file_service_base<boost::corosio::detail::io_uring_stream_file_service, boost::corosio::detail::file_service, boost::corosio::detail::io_uring_stream_file>::construct() :70 37x 100.0% 71.0% boost::corosio::detail::io_uring_file_service_base<boost::corosio::detail::io_uring_random_access_file_service, boost::corosio::detail::random_access_file_service, boost::corosio::detail::io_uring_random_access_file>::destroy(boost::corosio::io_object::implementation*) :82 38x 100.0% 70.0% boost::corosio::detail::io_uring_file_service_base<boost::corosio::detail::io_uring_stream_file_service, boost::corosio::detail::file_service, boost::corosio::detail::io_uring_stream_file>::destroy(boost::corosio::io_object::implementation*) :82 37x 100.0% 70.0% boost::corosio::detail::io_uring_file_service_base<boost::corosio::detail::io_uring_random_access_file_service, boost::corosio::detail::random_access_file_service, boost::corosio::detail::io_uring_random_access_file>::close(boost::corosio::io_object::handle&) :92 68x 100.0% 100.0% boost::corosio::detail::io_uring_file_service_base<boost::corosio::detail::io_uring_stream_file_service, boost::corosio::detail::file_service, boost::corosio::detail::io_uring_stream_file>::close(boost::corosio::io_object::handle&) :92 64x 100.0% 100.0% boost::corosio::detail::io_uring_file_service_base<boost::corosio::detail::io_uring_random_access_file_service, boost::corosio::detail::random_access_file_service, boost::corosio::detail::io_uring_random_access_file>::shutdown() :98 551x 71.4% 78.0% boost::corosio::detail::io_uring_file_service_base<boost::corosio::detail::io_uring_stream_file_service, boost::corosio::detail::file_service, boost::corosio::detail::io_uring_stream_file>::shutdown() :98 551x 71.4% 78.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_IO_URING_IO_URING_FILE_SERVICE_BASE_HPP
11 #define BOOST_COROSIO_NATIVE_DETAIL_IO_URING_IO_URING_FILE_SERVICE_BASE_HPP
12
13 #include <boost/corosio/detail/platform.hpp>
14
15 #if BOOST_COROSIO_HAS_IO_URING
16
17 #include <boost/corosio/detail/intrusive.hpp>
18 #include <boost/corosio/io/io_object.hpp>
19 #include <boost/corosio/native/detail/io_uring/io_uring_scheduler.hpp>
20
21 #include <memory>
22 #include <mutex>
23 #include <unordered_map>
24
25 /*
26 Shared lifecycle plumbing for io_uring file services.
27
28 io_uring_stream_file_service and io_uring_random_access_file_service were
29 byte-for-byte identical apart from the impl type and open_file's parameter
30 type: both make_shared the file impl from the scheduler, track it in an
31 intrusive list + raw->shared_ptr map, and close every file on shutdown.
32 This base factors that out; the concrete services add only open_file.
33
34 This is a separate base from io_uring_socket_service_base because file
35 services differ from socket services in three ways that match the reactor
36 socket service instead: they track via an intrusive list + map (sockets:
37 map only), they CLOSE files on shutdown (sockets: cancel only), and the
38 impl ctor takes just the scheduler (sockets: service + scheduler). See
39 tasks/proactor-dedup-decisions.md (#14).
40
41 Requirements on File: derive from enable_shared_from_this<File> and
42 intrusive_list<File>::node, a `File(io_uring_scheduler&)` constructor, and
43 a `void close_file() noexcept` method (cancel in-flight ops + close fd).
44
45 @tparam Derived The concrete service (CRTP; unused today but kept for
46 symmetry / future hooks).
47 @tparam ServiceBase The abstract service vtable base (file_service,
48 random_access_file_service).
49 @tparam File The concrete io_uring file impl type.
50 */
51
52 namespace boost::corosio::detail {
53
54 template<class Derived, class ServiceBase, class File>
55 class io_uring_file_service_base : public ServiceBase
56 {
57 friend Derived;
58
59 // Private CRTP ctor: only `Derived` (the concrete service, a friend)
60 // constructs the base — prevents inheriting with the wrong Derived
61 // (bugprone-crtp-constructor-accessibility).
62 1102x explicit io_uring_file_service_base(io_uring_scheduler& sched) noexcept
63 1102x : sched_(&sched)
64 {
65 1102x }
66
67 public:
68 1102x ~io_uring_file_service_base() override = default;
69
70 75x io_object::implementation* construct() override
71 {
72 75x auto ptr = std::make_shared<File>(*sched_);
73 75x auto* impl = ptr.get();
74 {
75 75x std::lock_guard<std::mutex> lock(mutex_);
76 75x file_list_.push_back(impl);
77 75x file_ptrs_[impl] = std::move(ptr);
78 75x }
79 75x return impl;
80 75x }
81
82 75x void destroy(io_object::implementation* p) override
83 {
84 // close_file() already does cancel_and_flush(fd_) before ::close.
85 75x auto& impl = static_cast<File&>(*p);
86 75x impl.close_file();
87 75x std::lock_guard<std::mutex> lock(mutex_);
88 75x file_list_.remove(&impl);
89 75x file_ptrs_.erase(&impl);
90 75x }
91
92 132x void close(io_object::handle& h) override
93 {
94 132x if (h.get())
95 132x static_cast<File&>(*h.get()).close_file();
96 132x }
97
98 1102x void shutdown() override
99 {
100 1102x std::lock_guard<std::mutex> lock(mutex_);
101 1102x for (auto* impl = file_list_.pop_front(); impl != nullptr;
102 impl = file_list_.pop_front())
103 {
104 impl->close_file();
105 }
106 1102x file_ptrs_.clear();
107 1102x }
108
109 /// Return the scheduler used by files created by this service.
110 io_uring_scheduler& scheduler() noexcept { return *sched_; }
111
112 protected:
113 io_uring_scheduler* sched_;
114 std::mutex mutex_;
115 intrusive_list<File> file_list_;
116 std::unordered_map<File*, std::shared_ptr<File>> file_ptrs_;
117
118 private:
119 io_uring_file_service_base(io_uring_file_service_base const&) = delete;
120 io_uring_file_service_base&
121 operator=(io_uring_file_service_base const&) = delete;
122 };
123
124 } // namespace boost::corosio::detail
125
126 #endif // BOOST_COROSIO_HAS_IO_URING
127
128 #endif // BOOST_COROSIO_NATIVE_DETAIL_IO_URING_IO_URING_FILE_SERVICE_BASE_HPP
129