100.00% Lines (37/37) 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_STREAM_FILE_HPP 11   #ifndef BOOST_COROSIO_NATIVE_NATIVE_STREAM_FILE_HPP
11   #define BOOST_COROSIO_NATIVE_NATIVE_STREAM_FILE_HPP 12   #define BOOST_COROSIO_NATIVE_NATIVE_STREAM_FILE_HPP
12   13  
13   #include <boost/corosio/stream_file.hpp> 14   #include <boost/corosio/stream_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_stream_file_service.hpp> 20   #include <boost/corosio/native/detail/posix/posix_stream_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_stream_file.hpp> 24   #include <boost/corosio/native/detail/io_uring/io_uring_stream_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_file_service.hpp> 28   #include <boost/corosio/native/detail/iocp/win_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 sequential file with devirtualized async I/O operations. 34   /** A sequential file with devirtualized async I/O operations.
34   35  
35   This class template inherits from @ref stream_file and shadows 36   This class template inherits from @ref stream_file and shadows
36   `read_some` / `write_some` with versions that call the backend 37   `read_some` / `write_some` with versions that call the backend
37   implementation directly, allowing the compiler to inline through 38   implementation directly, allowing the compiler to inline through
38   the entire call chain. 39   the entire call chain.
39   40  
40   Non-async operations (`open`, `close`, `size`, `resize`, `seek`, 41   Non-async operations (`open`, `close`, `size`, `resize`, `seek`,
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_stream_file` IS-A `stream_file` and can be passed to 45   A `native_stream_file` IS-A `stream_file` and can be passed to
45   any function expecting `stream_file&` or `io_stream&`, in which 46   any function expecting `stream_file&` or `io_stream&`, in which
46   case virtual dispatch is used transparently. 47   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 stream_file. 60   Same as @ref stream_file.
60   61  
61   @par Example 62   @par Example
62 - @code 63 + @par !example native_stream_file
63 - #include <boost/corosio/native/native_stream_file.hpp>  
64 -  
65 - native_io_context<epoll> ctx;  
66 - native_stream_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(  
71 - capy::mutable_buffer(buf, sizeof(buf)));  
72 - @endcode  
73   64  
74   @see stream_file, epoll_t, iocp_t 65   @see stream_file, epoll_t, iocp_t
75   */ 66   */
76   template<auto Backend> 67   template<auto Backend>
77   class native_stream_file : public stream_file 68   class native_stream_file : public stream_file
78   { 69   {
79   using backend_type = decltype(Backend); 70   using backend_type = decltype(Backend);
80   using impl_type = typename backend_type::stream_file_type; 71   using impl_type = typename backend_type::stream_file_type;
81   using service_type = typename backend_type::stream_file_service_type; 72   using service_type = typename backend_type::stream_file_service_type;
82   73  
HITCBC 83   12 impl_type& get_impl() noexcept 74   12 impl_type& get_impl() noexcept
84   { 75   {
HITCBC 85   12 return *static_cast<impl_type*>(h_.get()); 76   12 return *static_cast<impl_type*>(h_.get());
86   } 77   }
87   78  
88   template<class MutableBufferSequence> 79   template<class MutableBufferSequence>
89   struct native_read_awaitable 80   struct native_read_awaitable
90   { 81   {
91   native_stream_file& self_; 82   native_stream_file& self_;
92   MutableBufferSequence buffers_; 83   MutableBufferSequence buffers_;
93   std::stop_token token_; 84   std::stop_token token_;
94   mutable std::error_code ec_; 85   mutable std::error_code ec_;
95   mutable std::size_t bytes_transferred_ = 0; 86   mutable std::size_t bytes_transferred_ = 0;
96   87  
HITCBC 97   6 native_read_awaitable( 88   6 native_read_awaitable(
98   native_stream_file& self, 89   native_stream_file& self,
99   MutableBufferSequence buffers) noexcept 90   MutableBufferSequence buffers) noexcept
HITCBC 100   6 : self_(self) 91   6 : self_(self)
HITCBC 101   6 , buffers_(std::move(buffers)) 92   6 , buffers_(std::move(buffers))
102   { 93   {
HITCBC 103   6 } 94   6 }
104   95  
HITCBC 105   6 bool await_ready() const noexcept 96   6 bool await_ready() const noexcept
106   { 97   {
107   // A pre-set ec_ means the initiator failed before 98   // A pre-set ec_ means the initiator failed before
108   // dispatch (e.g. a closed object). 99   // dispatch (e.g. a closed object).
HITCBC 109   6 return static_cast<bool>(ec_) || token_.stop_requested(); 100   6 return static_cast<bool>(ec_) || token_.stop_requested();
110   } 101   }
111   102  
HITCBC 112   6 [[nodiscard]] capy::io_result<std::size_t> await_resume() const noexcept 103   6 [[nodiscard]] capy::io_result<std::size_t> await_resume() const noexcept
113   { 104   {
HITCBC 114   6 if (token_.stop_requested()) 105   6 if (token_.stop_requested())
HITCBC 115   2 return {make_error_code(std::errc::operation_canceled), 0}; 106   2 return {make_error_code(std::errc::operation_canceled), 0};
HITCBC 116   4 return {ec_, bytes_transferred_}; 107   4 return {ec_, bytes_transferred_};
117   } 108   }
118   109  
HITCBC 119   6 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) 110   6 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
120   -> std::coroutine_handle<> 111   -> std::coroutine_handle<>
121   { 112   {
HITCBC 122   6 token_ = env->stop_token; 113   6 token_ = env->stop_token;
HITCBC 123   18 return self_.get_impl().read_some( 114   18 return self_.get_impl().read_some(
HITCBC 124   18 h, env->executor, buffers_, token_, &ec_, &bytes_transferred_); 115   18 h, env->executor, buffers_, token_, &ec_, &bytes_transferred_);
125   } 116   }
126   }; 117   };
127   118  
128   template<class ConstBufferSequence> 119   template<class ConstBufferSequence>
129   struct native_write_awaitable 120   struct native_write_awaitable
130   { 121   {
131   native_stream_file& self_; 122   native_stream_file& self_;
132   ConstBufferSequence buffers_; 123   ConstBufferSequence buffers_;
133   std::stop_token token_; 124   std::stop_token token_;
134   mutable std::error_code ec_; 125   mutable std::error_code ec_;
135   mutable std::size_t bytes_transferred_ = 0; 126   mutable std::size_t bytes_transferred_ = 0;
136   127  
HITCBC 137   6 native_write_awaitable( 128   6 native_write_awaitable(
138   native_stream_file& self, 129   native_stream_file& self,
139   ConstBufferSequence buffers) noexcept 130   ConstBufferSequence buffers) noexcept
HITCBC 140   6 : self_(self) 131   6 : self_(self)
HITCBC 141   6 , buffers_(std::move(buffers)) 132   6 , buffers_(std::move(buffers))
142   { 133   {
HITCBC 143   6 } 134   6 }
144   135  
HITCBC 145   6 bool await_ready() const noexcept 136   6 bool await_ready() const noexcept
146   { 137   {
147   // A pre-set ec_ means the initiator failed before 138   // A pre-set ec_ means the initiator failed before
148   // dispatch (e.g. a closed object). 139   // dispatch (e.g. a closed object).
HITCBC 149   6 return static_cast<bool>(ec_) || token_.stop_requested(); 140   6 return static_cast<bool>(ec_) || token_.stop_requested();
150   } 141   }
151   142  
HITCBC 152   6 [[nodiscard]] capy::io_result<std::size_t> await_resume() const noexcept 143   6 [[nodiscard]] capy::io_result<std::size_t> await_resume() const noexcept
153   { 144   {
HITCBC 154   6 if (token_.stop_requested()) 145   6 if (token_.stop_requested())
HITCBC 155   2 return {make_error_code(std::errc::operation_canceled), 0}; 146   2 return {make_error_code(std::errc::operation_canceled), 0};
HITCBC 156   4 return {ec_, bytes_transferred_}; 147   4 return {ec_, bytes_transferred_};
157   } 148   }
158   149  
HITCBC 159   6 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) 150   6 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
160   -> std::coroutine_handle<> 151   -> std::coroutine_handle<>
161   { 152   {
HITCBC 162   6 token_ = env->stop_token; 153   6 token_ = env->stop_token;
HITCBC 163   18 return self_.get_impl().write_some( 154   18 return self_.get_impl().write_some(
HITCBC 164   18 h, env->executor, buffers_, token_, &ec_, &bytes_transferred_); 155   18 h, env->executor, buffers_, token_, &ec_, &bytes_transferred_);
165   } 156   }
166   }; 157   };
167   158  
168   public: 159   public:
169   /** Construct a native stream file from an execution context. 160   /** Construct a native stream file from an execution context.
170   161  
171   @param ctx The execution context that will own this file. 162   @param ctx The execution context that will own this file.
172   */ 163   */
HITCBC 173   16 explicit native_stream_file(capy::execution_context& ctx) 164   16 explicit native_stream_file(capy::execution_context& ctx)
HITCBC 174   16 : io_object(create_handle<service_type>(ctx)) 165   16 : io_object(create_handle<service_type>(ctx))
175   { 166   {
HITCBC 176   16 } 167   16 }
177   168  
178   /** Construct a native stream file from an executor. 169   /** Construct a native stream file from an executor.
179   170  
180   @param ex The executor whose context will own this file. 171   @param ex The executor whose context will own this file.
181   */ 172   */
182   template<class Ex> 173   template<class Ex>
183   requires(!std::same_as<std::remove_cvref_t<Ex>, native_stream_file>) && 174   requires(!std::same_as<std::remove_cvref_t<Ex>, native_stream_file>) &&
184   capy::Executor<Ex> 175   capy::Executor<Ex>
185   explicit native_stream_file(Ex const& ex) : native_stream_file(ex.context()) 176   explicit native_stream_file(Ex const& ex) : native_stream_file(ex.context())
186   { 177   {
187   } 178   }
188   179  
189   /// Move construct. 180   /// Move construct.
190   native_stream_file(native_stream_file&&) noexcept = default; 181   native_stream_file(native_stream_file&&) noexcept = default;
191   182  
192   /// Move assign. 183   /// Move assign.
193   native_stream_file& operator=(native_stream_file&&) noexcept = default; 184   native_stream_file& operator=(native_stream_file&&) noexcept = default;
194   185  
195   native_stream_file(native_stream_file const&) = delete; 186   native_stream_file(native_stream_file const&) = delete;
196   native_stream_file& operator=(native_stream_file const&) = delete; 187   native_stream_file& operator=(native_stream_file const&) = delete;
197   188  
198   /** Asynchronously read data from the file. 189   /** Asynchronously read data from the file.
199   190  
200   Calls the backend implementation directly, bypassing virtual 191   Calls the backend implementation directly, bypassing virtual
201   dispatch. Otherwise identical to @ref io_stream::read_some. 192   dispatch. Otherwise identical to @ref io_stream::read_some.
202   */ 193   */
203   template<capy::MutableBufferSequence MB> 194   template<capy::MutableBufferSequence MB>
HITCBC 204   6 [[nodiscard]] auto read_some(MB const& buffers) 195   6 [[nodiscard]] auto read_some(MB const& buffers)
205   { 196   {
HITCBC 206   6 return native_read_awaitable<MB>(*this, buffers); 197   6 return native_read_awaitable<MB>(*this, buffers);
207   } 198   }
208   199  
209   /** Asynchronously write data to the file. 200   /** Asynchronously write data to the file.
210   201  
211   Calls the backend implementation directly, bypassing virtual 202   Calls the backend implementation directly, bypassing virtual
212   dispatch. Otherwise identical to @ref io_stream::write_some. 203   dispatch. Otherwise identical to @ref io_stream::write_some.
213   */ 204   */
214   template<capy::ConstBufferSequence CB> 205   template<capy::ConstBufferSequence CB>
HITCBC 215   6 [[nodiscard]] auto write_some(CB const& buffers) 206   6 [[nodiscard]] auto write_some(CB const& buffers)
216   { 207   {
HITCBC 217   6 return native_write_awaitable<CB>(*this, buffers); 208   6 return native_write_awaitable<CB>(*this, buffers);
218   } 209   }
219   }; 210   };
220   211  
221   } // namespace boost::corosio 212   } // namespace boost::corosio
222   213  
223   #endif // BOOST_COROSIO_NATIVE_NATIVE_STREAM_FILE_HPP 214   #endif // BOOST_COROSIO_NATIVE_NATIVE_STREAM_FILE_HPP