95.12% Lines (39/41) 100.00% Functions (12/12)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2026 Steve Gerbino 2   // Copyright (c) 2026 Steve Gerbino
3   // 3   //
4   // Distributed under the Boost Software License, Version 1.0. (See accompanying 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) 5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6   // 6   //
7   // Official repository: https://github.com/cppalliance/corosio 7   // Official repository: https://github.com/cppalliance/corosio
8   // 8   //
9   9  
10   #ifndef BOOST_COROSIO_NATIVE_NATIVE_RANDOM_ACCESS_FILE_HPP 10   #ifndef BOOST_COROSIO_NATIVE_NATIVE_RANDOM_ACCESS_FILE_HPP
11   #define BOOST_COROSIO_NATIVE_NATIVE_RANDOM_ACCESS_FILE_HPP 11   #define BOOST_COROSIO_NATIVE_NATIVE_RANDOM_ACCESS_FILE_HPP
12   12  
13   #include <boost/corosio/random_access_file.hpp> 13   #include <boost/corosio/random_access_file.hpp>
14   #include <boost/corosio/backend.hpp> 14   #include <boost/corosio/backend.hpp>
15   15  
16   #ifndef BOOST_COROSIO_MRDOCS 16   #ifndef BOOST_COROSIO_MRDOCS
17   #if BOOST_COROSIO_HAS_EPOLL || BOOST_COROSIO_HAS_SELECT || \ 17   #if BOOST_COROSIO_HAS_EPOLL || BOOST_COROSIO_HAS_SELECT || \
18   BOOST_COROSIO_HAS_KQUEUE 18   BOOST_COROSIO_HAS_KQUEUE
19   #include <boost/corosio/native/detail/posix/posix_random_access_file_service.hpp> 19   #include <boost/corosio/native/detail/posix/posix_random_access_file_service.hpp>
20   #endif 20   #endif
21   21  
  22 + #if BOOST_COROSIO_HAS_IO_URING
  23 + #include <boost/corosio/native/detail/io_uring/io_uring_random_access_file.hpp>
  24 + #endif
  25 +
22   #if BOOST_COROSIO_HAS_IOCP 26   #if BOOST_COROSIO_HAS_IOCP
23   #include <boost/corosio/native/detail/iocp/win_random_access_file_service.hpp> 27   #include <boost/corosio/native/detail/iocp/win_random_access_file_service.hpp>
24   #endif 28   #endif
25   #endif // !BOOST_COROSIO_MRDOCS 29   #endif // !BOOST_COROSIO_MRDOCS
26   30  
27   namespace boost::corosio { 31   namespace boost::corosio {
28   32  
29   /** A random-access file with devirtualized async I/O operations. 33   /** A random-access file with devirtualized async I/O operations.
30   34  
31   This class template inherits from @ref random_access_file and 35   This class template inherits from @ref random_access_file and
32   shadows `read_some_at` / `write_some_at` with versions that 36   shadows `read_some_at` / `write_some_at` with versions that
33   call the backend implementation directly, allowing the compiler 37   call the backend implementation directly, allowing the compiler
34   to inline through the entire call chain. 38   to inline through the entire call chain.
35   39  
36   Non-async operations (`open`, `close`, `size`, `resize`, 40   Non-async operations (`open`, `close`, `size`, `resize`,
37   `sync_data`, `sync_all`) remain unchanged and dispatch through 41   `sync_data`, `sync_all`) remain unchanged and dispatch through
38   the compiled library. 42   the compiled library.
39   43  
40   A `native_random_access_file` IS-A `random_access_file` and 44   A `native_random_access_file` IS-A `random_access_file` and
41   can be passed to any function expecting `random_access_file&`, 45   can be passed to any function expecting `random_access_file&`,
42   in which case virtual dispatch is used transparently. 46   in which case virtual dispatch is used transparently.
43   47  
44   @note On POSIX platforms, file I/O is dispatched to a thread 48   @note On POSIX platforms, file I/O is dispatched to a thread
45   pool regardless of the chosen reactor backend, so all three 49   pool regardless of the chosen reactor backend, so all three
46   reactor tags (`epoll`, `select`, `kqueue`) resolve to the same 50   reactor tags (`epoll`, `select`, `kqueue`) resolve to the same
47   underlying implementation. The `Backend` template parameter 51   underlying implementation. The `Backend` template parameter
48   exists for API symmetry with @ref native_tcp_socket and friends. 52   exists for API symmetry with @ref native_tcp_socket and friends.
49   The vtable savings are smaller relative to the thread-pool / 53   The vtable savings are smaller relative to the thread-pool /
50   overlapped-I/O cost than they are for socket operations. 54   overlapped-I/O cost than they are for socket operations.
51   55  
52   @tparam Backend A backend tag value (e.g., `epoll`, `iocp`). 56   @tparam Backend A backend tag value (e.g., `epoll`, `iocp`).
53   57  
54   @par Thread Safety 58   @par Thread Safety
55   Same as @ref random_access_file. 59   Same as @ref random_access_file.
56   60  
57   @par Example 61   @par Example
58   @code 62   @code
59   #include <boost/corosio/native/native_random_access_file.hpp> 63   #include <boost/corosio/native/native_random_access_file.hpp>
60   64  
61   native_io_context<epoll> ctx; 65   native_io_context<epoll> ctx;
62   native_random_access_file<epoll> f(ctx); 66   native_random_access_file<epoll> f(ctx);
63   f.open("data.bin", file_base::read_only); 67   f.open("data.bin", file_base::read_only);
64   char buf[4096]; 68   char buf[4096];
65   auto [ec, n] = co_await f.read_some_at( 69   auto [ec, n] = co_await f.read_some_at(
66   0, capy::mutable_buffer(buf, sizeof(buf))); 70   0, capy::mutable_buffer(buf, sizeof(buf)));
67   @endcode 71   @endcode
68   72  
69   @see random_access_file, epoll_t, iocp_t 73   @see random_access_file, epoll_t, iocp_t
70   */ 74   */
71   template<auto Backend> 75   template<auto Backend>
72   class native_random_access_file : public random_access_file 76   class native_random_access_file : public random_access_file
73   { 77   {
74   using backend_type = decltype(Backend); 78   using backend_type = decltype(Backend);
75   using impl_type = typename backend_type::random_access_file_type; 79   using impl_type = typename backend_type::random_access_file_type;
76   using service_type = 80   using service_type =
77   typename backend_type::random_access_file_service_type; 81   typename backend_type::random_access_file_service_type;
78   82  
HITCBC 79   4 impl_type& get_impl() noexcept 83   4 impl_type& get_impl() noexcept
80   { 84   {
HITCBC 81   4 return *static_cast<impl_type*>(h_.get()); 85   4 return *static_cast<impl_type*>(h_.get());
82   } 86   }
83   87  
84   template<class MutableBufferSequence> 88   template<class MutableBufferSequence>
85   struct native_read_at_awaitable 89   struct native_read_at_awaitable
86   { 90   {
87   native_random_access_file& self_; 91   native_random_access_file& self_;
88   std::uint64_t offset_; 92   std::uint64_t offset_;
89   MutableBufferSequence buffers_; 93   MutableBufferSequence buffers_;
90   std::stop_token token_; 94   std::stop_token token_;
91   mutable std::error_code ec_; 95   mutable std::error_code ec_;
92   mutable std::size_t bytes_transferred_ = 0; 96   mutable std::size_t bytes_transferred_ = 0;
93   97  
HITCBC 94   2 native_read_at_awaitable( 98   2 native_read_at_awaitable(
95   native_random_access_file& self, 99   native_random_access_file& self,
96   std::uint64_t offset, 100   std::uint64_t offset,
97   MutableBufferSequence buffers) noexcept 101   MutableBufferSequence buffers) noexcept
HITCBC 98   2 : self_(self) 102   2 : self_(self)
HITCBC 99   2 , offset_(offset) 103   2 , offset_(offset)
HITCBC 100   2 , buffers_(std::move(buffers)) 104   2 , buffers_(std::move(buffers))
101   { 105   {
HITCBC 102   2 } 106   2 }
103   107  
HITCBC 104   2 bool await_ready() const noexcept 108   2 bool await_ready() const noexcept
105   { 109   {
HITCBC 106   2 return token_.stop_requested(); 110   2 return token_.stop_requested();
107   } 111   }
108   112  
HITCBC 109   2 capy::io_result<std::size_t> await_resume() const noexcept 113   2 capy::io_result<std::size_t> await_resume() const noexcept
110   { 114   {
HITCBC 111   2 if (token_.stop_requested()) 115   2 if (token_.stop_requested())
MISUBC 112   return {make_error_code(std::errc::operation_canceled), 0}; 116   return {make_error_code(std::errc::operation_canceled), 0};
HITCBC 113   2 return {ec_, bytes_transferred_}; 117   2 return {ec_, bytes_transferred_};
114   } 118   }
115   119  
HITCBC 116   2 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) 120   2 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
117   -> std::coroutine_handle<> 121   -> std::coroutine_handle<>
118   { 122   {
HITCBC 119   2 token_ = env->stop_token; 123   2 token_ = env->stop_token;
HITCBC 120   6 return self_.get_impl().read_some_at( 124   6 return self_.get_impl().read_some_at(
HITCBC 121   2 offset_, h, env->executor, buffers_, 125   2 offset_, h, env->executor, buffers_,
HITCBC 122   6 token_, &ec_, &bytes_transferred_); 126   6 token_, &ec_, &bytes_transferred_);
123   } 127   }
124   }; 128   };
125   129  
126   template<class ConstBufferSequence> 130   template<class ConstBufferSequence>
127   struct native_write_at_awaitable 131   struct native_write_at_awaitable
128   { 132   {
129   native_random_access_file& self_; 133   native_random_access_file& self_;
130   std::uint64_t offset_; 134   std::uint64_t offset_;
131   ConstBufferSequence buffers_; 135   ConstBufferSequence buffers_;
132   std::stop_token token_; 136   std::stop_token token_;
133   mutable std::error_code ec_; 137   mutable std::error_code ec_;
134   mutable std::size_t bytes_transferred_ = 0; 138   mutable std::size_t bytes_transferred_ = 0;
135   139  
HITCBC 136   2 native_write_at_awaitable( 140   2 native_write_at_awaitable(
137   native_random_access_file& self, 141   native_random_access_file& self,
138   std::uint64_t offset, 142   std::uint64_t offset,
139   ConstBufferSequence buffers) noexcept 143   ConstBufferSequence buffers) noexcept
HITCBC 140   2 : self_(self) 144   2 : self_(self)
HITCBC 141   2 , offset_(offset) 145   2 , offset_(offset)
HITCBC 142   2 , buffers_(std::move(buffers)) 146   2 , buffers_(std::move(buffers))
143   { 147   {
HITCBC 144   2 } 148   2 }
145   149  
HITCBC 146   2 bool await_ready() const noexcept 150   2 bool await_ready() const noexcept
147   { 151   {
HITCBC 148   2 return token_.stop_requested(); 152   2 return token_.stop_requested();
149   } 153   }
150   154  
HITCBC 151   2 capy::io_result<std::size_t> await_resume() const noexcept 155   2 capy::io_result<std::size_t> await_resume() const noexcept
152   { 156   {
HITCBC 153   2 if (token_.stop_requested()) 157   2 if (token_.stop_requested())
MISUBC 154   return {make_error_code(std::errc::operation_canceled), 0}; 158   return {make_error_code(std::errc::operation_canceled), 0};
HITCBC 155   2 return {ec_, bytes_transferred_}; 159   2 return {ec_, bytes_transferred_};
156   } 160   }
157   161  
HITCBC 158   2 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) 162   2 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
159   -> std::coroutine_handle<> 163   -> std::coroutine_handle<>
160   { 164   {
HITCBC 161   2 token_ = env->stop_token; 165   2 token_ = env->stop_token;
HITCBC 162   6 return self_.get_impl().write_some_at( 166   6 return self_.get_impl().write_some_at(
HITCBC 163   2 offset_, h, env->executor, buffers_, 167   2 offset_, h, env->executor, buffers_,
HITCBC 164   6 token_, &ec_, &bytes_transferred_); 168   6 token_, &ec_, &bytes_transferred_);
165   } 169   }
166   }; 170   };
167   171  
168   public: 172   public:
169   /** Construct a native random-access file from an execution context. 173   /** Construct a native random-access file from an execution context.
170   174  
171   @param ctx The execution context that will own this file. 175   @param ctx The execution context that will own this file.
172   */ 176   */
HITCBC 173   10 explicit native_random_access_file(capy::execution_context& ctx) 177   10 explicit native_random_access_file(capy::execution_context& ctx)
HITCBC 174   10 : random_access_file(create_handle<service_type>(ctx)) 178   10 : random_access_file(create_handle<service_type>(ctx))
175   { 179   {
HITCBC 176   10 } 180   10 }
177   181  
178   /** Construct a native random-access file from an executor. 182   /** Construct a native random-access file from an executor.
179   183  
180   @param ex The executor whose context will own this file. 184   @param ex The executor whose context will own this file.
181   */ 185   */
182   template<class Ex> 186   template<class Ex>
183   requires(!std::same_as< 187   requires(!std::same_as<
184   std::remove_cvref_t<Ex>, 188   std::remove_cvref_t<Ex>,
185   native_random_access_file>) && 189   native_random_access_file>) &&
186   capy::Executor<Ex> 190   capy::Executor<Ex>
187   explicit native_random_access_file(Ex const& ex) 191   explicit native_random_access_file(Ex const& ex)
188   : native_random_access_file(ex.context()) 192   : native_random_access_file(ex.context())
189   { 193   {
190   } 194   }
191   195  
192   /// Move construct. 196   /// Move construct.
193   native_random_access_file(native_random_access_file&&) noexcept = default; 197   native_random_access_file(native_random_access_file&&) noexcept = default;
194   198  
195   /// Move assign. 199   /// Move assign.
196   native_random_access_file& 200   native_random_access_file&
197   operator=(native_random_access_file&&) noexcept = default; 201   operator=(native_random_access_file&&) noexcept = default;
198   202  
199   native_random_access_file(native_random_access_file const&) = delete; 203   native_random_access_file(native_random_access_file const&) = delete;
200   native_random_access_file& 204   native_random_access_file&
201   operator=(native_random_access_file const&) = delete; 205   operator=(native_random_access_file const&) = delete;
202   206  
203   /** Asynchronously read at the given offset. 207   /** Asynchronously read at the given offset.
204   208  
205   Calls the backend implementation directly, bypassing virtual 209   Calls the backend implementation directly, bypassing virtual
206   dispatch. Otherwise identical to @ref random_access_file::read_some_at. 210   dispatch. Otherwise identical to @ref random_access_file::read_some_at.
207   */ 211   */
208   template<capy::MutableBufferSequence MB> 212   template<capy::MutableBufferSequence MB>
HITCBC 209   2 auto read_some_at(std::uint64_t offset, MB const& buffers) 213   2 auto read_some_at(std::uint64_t offset, MB const& buffers)
210   { 214   {
HITCBC 211   2 return native_read_at_awaitable<MB>(*this, offset, buffers); 215   2 return native_read_at_awaitable<MB>(*this, offset, buffers);
212   } 216   }
213   217  
214   /** Asynchronously write at the given offset. 218   /** Asynchronously write at the given offset.
215   219  
216   Calls the backend implementation directly, bypassing virtual 220   Calls the backend implementation directly, bypassing virtual
217   dispatch. Otherwise identical to @ref random_access_file::write_some_at. 221   dispatch. Otherwise identical to @ref random_access_file::write_some_at.
218   */ 222   */
219   template<capy::ConstBufferSequence CB> 223   template<capy::ConstBufferSequence CB>
HITCBC 220   2 auto write_some_at(std::uint64_t offset, CB const& buffers) 224   2 auto write_some_at(std::uint64_t offset, CB const& buffers)
221   { 225   {
HITCBC 222   2 return native_write_at_awaitable<CB>(*this, offset, buffers); 226   2 return native_write_at_awaitable<CB>(*this, offset, buffers);
223   } 227   }
224   }; 228   };
225   229  
226   } // namespace boost::corosio 230   } // namespace boost::corosio
227   231  
228   #endif // BOOST_COROSIO_NATIVE_NATIVE_RANDOM_ACCESS_FILE_HPP 232   #endif // BOOST_COROSIO_NATIVE_NATIVE_RANDOM_ACCESS_FILE_HPP