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