include/boost/corosio/native/detail/io_uring/io_uring_random_access_file.hpp
98.3% Lines (117/0/119)
100.0% List of functions (16/0/16)
Functions (16)
Function
Calls
Lines
Blocks
boost::corosio::detail::io_uring_random_access_file::io_uring_random_access_file(boost::corosio::detail::io_uring_scheduler&)
:71
46x
100.0%
100.0%
boost::corosio::detail::io_uring_random_access_file::~io_uring_random_access_file()
:75
46x
100.0%
100.0%
boost::corosio::detail::io_uring_random_access_file::native_handle() const
:100
288x
100.0%
100.0%
boost::corosio::detail::io_uring_random_access_file::cancel()
:105
2x
100.0%
100.0%
boost::corosio::detail::io_uring_random_access_file::size() const
:111
4x
60.0%
62.0%
boost::corosio::detail::io_uring_random_access_file::resize(unsigned long)
:120
4x
100.0%
100.0%
boost::corosio::detail::io_uring_random_access_file::sync_data()
:130
2x
100.0%
100.0%
boost::corosio::detail::io_uring_random_access_file::sync_all()
:141
2x
100.0%
100.0%
boost::corosio::detail::io_uring_random_access_file::release()
:148
1x
100.0%
100.0%
boost::corosio::detail::io_uring_random_access_file::assign(int)
:155
3x
100.0%
100.0%
boost::corosio::detail::io_uring_random_access_file::open_file(std::filesystem::__cxx11::path const&, boost::corosio::file_base::flags)
:165
37x
100.0%
100.0%
boost::corosio::detail::io_uring_random_access_file::close_file()
:206
215x
100.0%
100.0%
boost::corosio::detail::io_uring_random_access_file::read_some_at(unsigned long, std::__n4861::coroutine_handle<void>, boost::capy::executor_ref, boost::corosio::buffer_param, std::stop_token, std::error_code*, unsigned long*)
:218
140x
100.0%
70.0%
boost::corosio::detail::io_uring_random_access_file::write_some_at(unsigned long, std::__n4861::coroutine_handle<void>, boost::capy::executor_ref, boost::corosio::buffer_param, std::stop_token, std::error_code*, unsigned long*)
:256
15x
100.0%
70.0%
boost::corosio::detail::io_uring_random_access_file_service::io_uring_random_access_file_service(boost::capy::execution_context&, boost::corosio::detail::io_uring_scheduler&)
:312
655x
100.0%
100.0%
boost::corosio::detail::io_uring_random_access_file_service::open_file(boost::corosio::random_access_file::implementation&, std::filesystem::__cxx11::path const&, boost::corosio::file_base::flags)
:320
37x
100.0%
100.0%
| 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_RANDOM_ACCESS_FILE_HPP | ||
| 11 | #define BOOST_COROSIO_NATIVE_DETAIL_IO_URING_IO_URING_RANDOM_ACCESS_FILE_HPP | ||
| 12 | |||
| 13 | #include <boost/corosio/detail/platform.hpp> | ||
| 14 | |||
| 15 | #if BOOST_COROSIO_HAS_IO_URING | ||
| 16 | |||
| 17 | #include <boost/corosio/detail/random_access_file_service.hpp> | ||
| 18 | #include <boost/corosio/detail/intrusive.hpp> | ||
| 19 | #include <boost/corosio/native/detail/io_uring/io_uring_file_ops.hpp> | ||
| 20 | #include <boost/corosio/native/detail/io_uring/io_uring_file_service_base.hpp> | ||
| 21 | #include <boost/corosio/native/detail/io_uring/io_uring_scheduler.hpp> | ||
| 22 | #include <boost/corosio/native/detail/make_err.hpp> | ||
| 23 | #include <boost/corosio/random_access_file.hpp> | ||
| 24 | |||
| 25 | #include <cstdint> | ||
| 26 | #include <filesystem> | ||
| 27 | #include <limits> | ||
| 28 | #include <memory> | ||
| 29 | #include <mutex> | ||
| 30 | #include <system_error> | ||
| 31 | #include <unordered_map> | ||
| 32 | |||
| 33 | #include <fcntl.h> | ||
| 34 | #include <sys/stat.h> | ||
| 35 | #include <sys/types.h> | ||
| 36 | #include <unistd.h> | ||
| 37 | |||
| 38 | namespace boost::corosio::detail { | ||
| 39 | |||
| 40 | class io_uring_random_access_file_service; | ||
| 41 | |||
| 42 | /** Native io_uring random-access-file implementation. | ||
| 43 | |||
| 44 | Async `read_some_at` / `write_some_at` submit `IORING_OP_READV` | ||
| 45 | / `IORING_OP_WRITEV` with the caller-supplied offset. Metadata | ||
| 46 | operations (open, size, resize, sync, close) are synchronous | ||
| 47 | syscalls. | ||
| 48 | |||
| 49 | @par Thread Safety | ||
| 50 | Concurrent `read_some_at` / `write_some_at` calls on the same | ||
| 51 | file at distinct offsets are safe; ordering between two | ||
| 52 | submissions at the same offset is unspecified at the kernel | ||
| 53 | level (matches POSIX `pread(2)` / `pwrite(2)` semantics). | ||
| 54 | */ | ||
| 55 | class BOOST_COROSIO_DECL io_uring_random_access_file final | ||
| 56 | : public random_access_file::implementation | ||
| 57 | , public std::enable_shared_from_this<io_uring_random_access_file> | ||
| 58 | , public intrusive_list<io_uring_random_access_file>::node | ||
| 59 | { | ||
| 60 | friend class io_uring_random_access_file_service; | ||
| 61 | |||
| 62 | int fd_ = -1; | ||
| 63 | io_uring_scheduler* sched_ = nullptr; | ||
| 64 | |||
| 65 | // Random-access files legitimately support concurrent ops at | ||
| 66 | // different offsets on the same fd (e.g. parallel reads in | ||
| 67 | // testConcurrentReads). Embedding a single slot would smash | ||
| 68 | // state across calls; ops are heap-allocated per submission. | ||
| 69 | |||
| 70 | public: | ||
| 71 | 46x | explicit io_uring_random_access_file(io_uring_scheduler& sched) noexcept | |
| 72 | 46x | : sched_(&sched) | |
| 73 | 46x | {} | |
| 74 | |||
| 75 | 46x | ~io_uring_random_access_file() override | |
| 76 | 46x | { | |
| 77 | 46x | close_file(); | |
| 78 | 46x | } | |
| 79 | |||
| 80 | // -- random_access_file::implementation -- | ||
| 81 | |||
| 82 | std::coroutine_handle<> read_some_at( | ||
| 83 | std::uint64_t, | ||
| 84 | std::coroutine_handle<>, | ||
| 85 | capy::executor_ref, | ||
| 86 | buffer_param, | ||
| 87 | std::stop_token, | ||
| 88 | std::error_code*, | ||
| 89 | std::size_t*) override; | ||
| 90 | |||
| 91 | std::coroutine_handle<> write_some_at( | ||
| 92 | std::uint64_t, | ||
| 93 | std::coroutine_handle<>, | ||
| 94 | capy::executor_ref, | ||
| 95 | buffer_param, | ||
| 96 | std::stop_token, | ||
| 97 | std::error_code*, | ||
| 98 | std::size_t*) override; | ||
| 99 | |||
| 100 | 288x | native_handle_type native_handle() const noexcept override | |
| 101 | { | ||
| 102 | 288x | return fd_; | |
| 103 | } | ||
| 104 | |||
| 105 | 2x | void cancel() noexcept override | |
| 106 | { | ||
| 107 | 2x | if (fd_ >= 0) | |
| 108 | 2x | sched_->submit_cancel_by_fd(fd_); | |
| 109 | 2x | } | |
| 110 | |||
| 111 | 4x | std::uint64_t size() const override | |
| 112 | { | ||
| 113 | struct stat st; | ||
| 114 | 4x | if (::fstat(fd_, &st) < 0) | |
| 115 | ✗ | throw_system_error( | |
| 116 | ✗ | make_err(errno), "random_access_file::size"); | |
| 117 | 4x | return static_cast<std::uint64_t>(st.st_size); | |
| 118 | } | ||
| 119 | |||
| 120 | 4x | std::error_code resize(std::uint64_t new_size) noexcept override | |
| 121 | { | ||
| 122 | 4x | if (new_size > static_cast<std::uint64_t>( | |
| 123 | 4x | (std::numeric_limits<off_t>::max)())) | |
| 124 | 1x | return make_err(EOVERFLOW); | |
| 125 | 3x | if (::ftruncate(fd_, static_cast<off_t>(new_size)) < 0) | |
| 126 | 1x | return make_err(errno); | |
| 127 | 2x | return {}; | |
| 128 | } | ||
| 129 | |||
| 130 | 2x | std::error_code sync_data() noexcept override | |
| 131 | { | ||
| 132 | #if BOOST_COROSIO_HAS_POSIX_SYNCHRONIZED_IO | ||
| 133 | 2x | if (::fdatasync(fd_) < 0) | |
| 134 | #else | ||
| 135 | if (::fsync(fd_) < 0) | ||
| 136 | #endif | ||
| 137 | 1x | return make_err(errno); | |
| 138 | 1x | return {}; | |
| 139 | } | ||
| 140 | |||
| 141 | 2x | std::error_code sync_all() noexcept override | |
| 142 | { | ||
| 143 | 2x | if (::fsync(fd_) < 0) | |
| 144 | 1x | return make_err(errno); | |
| 145 | 1x | return {}; | |
| 146 | } | ||
| 147 | |||
| 148 | 1x | native_handle_type release() override | |
| 149 | { | ||
| 150 | 1x | int fd = fd_; | |
| 151 | 1x | fd_ = -1; | |
| 152 | 1x | return fd; | |
| 153 | } | ||
| 154 | |||
| 155 | 3x | std::error_code assign(native_handle_type handle) noexcept override | |
| 156 | { | ||
| 157 | 3x | close_file(); | |
| 158 | 3x | fd_ = handle; | |
| 159 | 3x | return {}; | |
| 160 | } | ||
| 161 | |||
| 162 | // -- Internal -- | ||
| 163 | |||
| 164 | /// Open the file. Synchronous; sets `fd_`. Caller is the service. | ||
| 165 | 37x | std::error_code open_file( | |
| 166 | std::filesystem::path const& path, file_base::flags mode) | ||
| 167 | { | ||
| 168 | 37x | close_file(); | |
| 169 | |||
| 170 | 37x | int oflags = 0; | |
| 171 | 37x | unsigned access = static_cast<unsigned>(mode) & 3u; | |
| 172 | 37x | if (access == static_cast<unsigned>(file_base::read_write)) | |
| 173 | 8x | oflags |= O_RDWR; | |
| 174 | 29x | else if (access == static_cast<unsigned>(file_base::write_only)) | |
| 175 | 6x | oflags |= O_WRONLY; | |
| 176 | else | ||
| 177 | 23x | oflags |= O_RDONLY; | |
| 178 | |||
| 179 | 37x | if ((mode & file_base::create) != file_base::flags(0)) | |
| 180 | 9x | oflags |= O_CREAT; | |
| 181 | 37x | if ((mode & file_base::exclusive) != file_base::flags(0)) | |
| 182 | 2x | oflags |= O_EXCL; | |
| 183 | 37x | if ((mode & file_base::truncate) != file_base::flags(0)) | |
| 184 | 7x | oflags |= O_TRUNC; | |
| 185 | 37x | if ((mode & file_base::sync_all_on_write) != file_base::flags(0)) | |
| 186 | 1x | oflags |= O_SYNC; | |
| 187 | |||
| 188 | 37x | oflags |= O_CLOEXEC; | |
| 189 | |||
| 190 | 37x | int fd = ::open(path.c_str(), oflags, 0666); | |
| 191 | 37x | if (fd < 0) | |
| 192 | 2x | return make_err(errno); | |
| 193 | |||
| 194 | 35x | fd_ = fd; | |
| 195 | |||
| 196 | #ifdef POSIX_FADV_RANDOM | ||
| 197 | // Hint the page cache that access will be random; matches | ||
| 198 | // the POSIX backend. | ||
| 199 | 35x | ::posix_fadvise(fd_, 0, 0, POSIX_FADV_RANDOM); | |
| 200 | #endif | ||
| 201 | |||
| 202 | 35x | return {}; | |
| 203 | } | ||
| 204 | |||
| 205 | /// Cancel any in-flight ops and close the fd. Idempotent. | ||
| 206 | 215x | void close_file() noexcept | |
| 207 | { | ||
| 208 | 215x | if (fd_ >= 0) | |
| 209 | { | ||
| 210 | 37x | sched_->cancel_and_flush(fd_); | |
| 211 | 37x | ::close(fd_); | |
| 212 | 37x | fd_ = -1; | |
| 213 | } | ||
| 214 | 215x | } | |
| 215 | }; | ||
| 216 | |||
| 217 | inline std::coroutine_handle<> | ||
| 218 | 140x | io_uring_random_access_file::read_some_at( | |
| 219 | std::uint64_t user_offset, | ||
| 220 | std::coroutine_handle<> h, | ||
| 221 | capy::executor_ref ex, | ||
| 222 | buffer_param buffers, | ||
| 223 | std::stop_token token, | ||
| 224 | std::error_code* ec, | ||
| 225 | std::size_t* bytes) | ||
| 226 | { | ||
| 227 | 140x | auto op_guard = std::make_unique<uring_random_access_read_op>(); | |
| 228 | 280x | op_guard->prepare(h, ex, ec, bytes, fd_, | |
| 229 | static_cast<std::int64_t>(user_offset), | ||
| 230 | 280x | sched_, shared_from_this(), buffers, token); | |
| 231 | 140x | sched_->work_started(); | |
| 232 | |||
| 233 | // Closed-object contract outranks the zero-length no-op. | ||
| 234 | 140x | if (fd_ < 0) | |
| 235 | { | ||
| 236 | 2x | op_guard->empty_buffer = false; | |
| 237 | 2x | op_guard->res = -EBADF; | |
| 238 | 2x | io_uring_scheduler::lock_type lock(sched_->dispatch_mutex()); | |
| 239 | 2x | sched_->push_completed_locked(op_guard.release()); | |
| 240 | 2x | return std::noop_coroutine(); | |
| 241 | 2x | } | |
| 242 | |||
| 243 | 275x | if (op_guard->empty_buffer || | |
| 244 | 137x | op_guard->cancelled.load(std::memory_order_acquire)) | |
| 245 | { | ||
| 246 | 2x | io_uring_scheduler::lock_type lock(sched_->dispatch_mutex()); | |
| 247 | 2x | sched_->push_completed_locked(op_guard.release()); | |
| 248 | 2x | return std::noop_coroutine(); | |
| 249 | 2x | } | |
| 250 | |||
| 251 | 136x | io_uring_submit_op(*sched_, op_guard.release()); | |
| 252 | 136x | return std::noop_coroutine(); | |
| 253 | 140x | } | |
| 254 | |||
| 255 | inline std::coroutine_handle<> | ||
| 256 | 15x | io_uring_random_access_file::write_some_at( | |
| 257 | std::uint64_t user_offset, | ||
| 258 | std::coroutine_handle<> h, | ||
| 259 | capy::executor_ref ex, | ||
| 260 | buffer_param buffers, | ||
| 261 | std::stop_token token, | ||
| 262 | std::error_code* ec, | ||
| 263 | std::size_t* bytes) | ||
| 264 | { | ||
| 265 | 15x | auto op_guard = std::make_unique<uring_random_access_write_op>(); | |
| 266 | 30x | op_guard->prepare(h, ex, ec, bytes, fd_, | |
| 267 | static_cast<std::int64_t>(user_offset), | ||
| 268 | 30x | sched_, shared_from_this(), buffers, token); | |
| 269 | 15x | sched_->work_started(); | |
| 270 | |||
| 271 | // Closed-object contract outranks the zero-length no-op. | ||
| 272 | 15x | if (fd_ < 0) | |
| 273 | { | ||
| 274 | 1x | op_guard->empty_buffer = false; | |
| 275 | 1x | op_guard->res = -EBADF; | |
| 276 | 1x | io_uring_scheduler::lock_type lock(sched_->dispatch_mutex()); | |
| 277 | 1x | sched_->push_completed_locked(op_guard.release()); | |
| 278 | 1x | return std::noop_coroutine(); | |
| 279 | 1x | } | |
| 280 | |||
| 281 | 27x | if (op_guard->empty_buffer || | |
| 282 | 13x | op_guard->cancelled.load(std::memory_order_acquire)) | |
| 283 | { | ||
| 284 | 1x | io_uring_scheduler::lock_type lock(sched_->dispatch_mutex()); | |
| 285 | 1x | sched_->push_completed_locked(op_guard.release()); | |
| 286 | 1x | return std::noop_coroutine(); | |
| 287 | 1x | } | |
| 288 | |||
| 289 | 13x | io_uring_submit_op(*sched_, op_guard.release()); | |
| 290 | 13x | return std::noop_coroutine(); | |
| 291 | 15x | } | |
| 292 | |||
| 293 | /** Native io_uring random-access-file service. | ||
| 294 | |||
| 295 | Owns all `io_uring_random_access_file` impls. Replaces | ||
| 296 | `posix_random_access_file_service` for the io_uring backend; | ||
| 297 | registered under the abstract `random_access_file_service` key | ||
| 298 | by `io_uring_t::construct`. | ||
| 299 | */ | ||
| 300 | class BOOST_COROSIO_DECL io_uring_random_access_file_service final | ||
| 301 | : public io_uring_file_service_base< | ||
| 302 | io_uring_random_access_file_service, | ||
| 303 | random_access_file_service, | ||
| 304 | io_uring_random_access_file> | ||
| 305 | { | ||
| 306 | using base_service = io_uring_file_service_base< | ||
| 307 | io_uring_random_access_file_service, | ||
| 308 | random_access_file_service, | ||
| 309 | io_uring_random_access_file>; | ||
| 310 | |||
| 311 | public: | ||
| 312 | 655x | explicit io_uring_random_access_file_service( | |
| 313 | capy::execution_context& /*ctx*/, io_uring_scheduler& sched) | ||
| 314 | 655x | : base_service(sched) | |
| 315 | 655x | {} | |
| 316 | |||
| 317 | // construct / destroy / close / shutdown / scheduler() are inherited | ||
| 318 | // from io_uring_file_service_base. | ||
| 319 | |||
| 320 | 37x | std::error_code open_file( | |
| 321 | random_access_file::implementation& impl, | ||
| 322 | std::filesystem::path const& path, | ||
| 323 | file_base::flags mode) override | ||
| 324 | { | ||
| 325 | 37x | return static_cast<io_uring_random_access_file&>(impl).open_file( | |
| 326 | 37x | path, mode); | |
| 327 | } | ||
| 328 | }; | ||
| 329 | |||
| 330 | } // namespace boost::corosio::detail | ||
| 331 | |||
| 332 | #endif // BOOST_COROSIO_HAS_IO_URING | ||
| 333 | |||
| 334 | #endif // BOOST_COROSIO_NATIVE_DETAIL_IO_URING_IO_URING_RANDOM_ACCESS_FILE_HPP | ||
| 335 |