io_uring_file_service.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // detail/io_uring_file_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_IO_URING_FILE_SERVICE_HPP
  11. #define BOOST_ASIO_DETAIL_IO_URING_FILE_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_FILE) \
  17. && defined(BOOST_ASIO_HAS_IO_URING)
  18. #include <string>
  19. #include <boost/asio/detail/cstdint.hpp>
  20. #include <boost/asio/detail/descriptor_ops.hpp>
  21. #include <boost/asio/detail/io_uring_descriptor_service.hpp>
  22. #include <boost/asio/error.hpp>
  23. #include <boost/asio/execution_context.hpp>
  24. #include <boost/asio/file_base.hpp>
  25. #include <boost/asio/detail/push_options.hpp>
  26. namespace boost {
  27. namespace asio {
  28. namespace detail {
  29. // Extend the io_uring_descriptor_service to provide file support.
  30. class io_uring_file_service :
  31. public execution_context_service_base<io_uring_file_service>
  32. {
  33. public:
  34. typedef io_uring_descriptor_service descriptor_service;
  35. // The native type of a file.
  36. typedef descriptor_service::native_handle_type native_handle_type;
  37. // The implementation type of the file.
  38. class implementation_type : descriptor_service::implementation_type
  39. {
  40. private:
  41. // Only this service will have access to the internal values.
  42. friend class io_uring_file_service;
  43. bool is_stream_;
  44. };
  45. BOOST_ASIO_DECL io_uring_file_service(execution_context& context);
  46. // Destroy all user-defined handler objects owned by the service.
  47. BOOST_ASIO_DECL void shutdown();
  48. // Construct a new file implementation.
  49. void construct(implementation_type& impl)
  50. {
  51. descriptor_service_.construct(impl);
  52. impl.is_stream_ = false;
  53. }
  54. // Move-construct a new file implementation.
  55. void move_construct(implementation_type& impl,
  56. implementation_type& other_impl)
  57. {
  58. descriptor_service_.move_construct(impl, other_impl);
  59. impl.is_stream_ = other_impl.is_stream_;
  60. }
  61. // Move-assign from another file implementation.
  62. void move_assign(implementation_type& impl,
  63. io_uring_file_service& other_service,
  64. implementation_type& other_impl)
  65. {
  66. descriptor_service_.move_assign(impl,
  67. other_service.descriptor_service_, other_impl);
  68. impl.is_stream_ = other_impl.is_stream_;
  69. }
  70. // Destroy a file implementation.
  71. void destroy(implementation_type& impl)
  72. {
  73. descriptor_service_.destroy(impl);
  74. }
  75. // Open the file using the specified path name.
  76. BOOST_ASIO_DECL boost::system::error_code open(implementation_type& impl,
  77. const char* path, file_base::flags open_flags,
  78. boost::system::error_code& ec);
  79. // Assign a native descriptor to a file implementation.
  80. boost::system::error_code assign(implementation_type& impl,
  81. const native_handle_type& native_descriptor,
  82. boost::system::error_code& ec)
  83. {
  84. return descriptor_service_.assign(impl, native_descriptor, ec);
  85. }
  86. // Set whether the implementation is stream-oriented.
  87. void set_is_stream(implementation_type& impl, bool is_stream)
  88. {
  89. impl.is_stream_ = is_stream;
  90. }
  91. // Determine whether the file is open.
  92. bool is_open(const implementation_type& impl) const
  93. {
  94. return descriptor_service_.is_open(impl);
  95. }
  96. // Destroy a file implementation.
  97. boost::system::error_code close(implementation_type& impl,
  98. boost::system::error_code& ec)
  99. {
  100. return descriptor_service_.close(impl, ec);
  101. }
  102. // Get the native file representation.
  103. native_handle_type native_handle(const implementation_type& impl) const
  104. {
  105. return descriptor_service_.native_handle(impl);
  106. }
  107. // Release ownership of the native descriptor representation.
  108. native_handle_type release(implementation_type& impl,
  109. boost::system::error_code& ec)
  110. {
  111. return descriptor_service_.release(impl, ec);
  112. }
  113. // Cancel all operations associated with the file.
  114. boost::system::error_code cancel(implementation_type& impl,
  115. boost::system::error_code& ec)
  116. {
  117. return descriptor_service_.cancel(impl, ec);
  118. }
  119. // Get the size of the file.
  120. BOOST_ASIO_DECL uint64_t size(const implementation_type& impl,
  121. boost::system::error_code& ec) const;
  122. // Alter the size of the file.
  123. BOOST_ASIO_DECL boost::system::error_code resize(implementation_type& impl,
  124. uint64_t n, boost::system::error_code& ec);
  125. // Synchronise the file to disk.
  126. BOOST_ASIO_DECL boost::system::error_code sync_all(implementation_type& impl,
  127. boost::system::error_code& ec);
  128. // Synchronise the file data to disk.
  129. BOOST_ASIO_DECL boost::system::error_code sync_data(implementation_type& impl,
  130. boost::system::error_code& ec);
  131. // Seek to a position in the file.
  132. BOOST_ASIO_DECL uint64_t seek(implementation_type& impl, int64_t offset,
  133. file_base::seek_basis whence, boost::system::error_code& ec);
  134. // Write the given data. Returns the number of bytes written.
  135. template <typename ConstBufferSequence>
  136. size_t write_some(implementation_type& impl,
  137. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  138. {
  139. return descriptor_service_.write_some(impl, buffers, ec);
  140. }
  141. // Start an asynchronous write. The data being written must be valid for the
  142. // lifetime of the asynchronous operation.
  143. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  144. void async_write_some(implementation_type& impl,
  145. const ConstBufferSequence& buffers,
  146. Handler& handler, const IoExecutor& io_ex)
  147. {
  148. descriptor_service_.async_write_some(impl, buffers, handler, io_ex);
  149. }
  150. // Write the given data at the specified location. Returns the number of
  151. // bytes written.
  152. template <typename ConstBufferSequence>
  153. size_t write_some_at(implementation_type& impl, uint64_t offset,
  154. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  155. {
  156. return descriptor_service_.write_some_at(impl, offset, buffers, ec);
  157. }
  158. // Start an asynchronous write at the specified location. The data being
  159. // written must be valid for the lifetime of the asynchronous operation.
  160. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  161. void async_write_some_at(implementation_type& impl,
  162. uint64_t offset, const ConstBufferSequence& buffers,
  163. Handler& handler, const IoExecutor& io_ex)
  164. {
  165. descriptor_service_.async_write_some_at(
  166. impl, offset, buffers, handler, io_ex);
  167. }
  168. // Read some data. Returns the number of bytes read.
  169. template <typename MutableBufferSequence>
  170. size_t read_some(implementation_type& impl,
  171. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  172. {
  173. return descriptor_service_.read_some(impl, buffers, ec);
  174. }
  175. // Start an asynchronous read. The buffer for the data being read must be
  176. // valid for the lifetime of the asynchronous operation.
  177. template <typename MutableBufferSequence,
  178. typename Handler, typename IoExecutor>
  179. void async_read_some(implementation_type& impl,
  180. const MutableBufferSequence& buffers,
  181. Handler& handler, const IoExecutor& io_ex)
  182. {
  183. descriptor_service_.async_read_some(impl, buffers, handler, io_ex);
  184. }
  185. // Read some data. Returns the number of bytes read.
  186. template <typename MutableBufferSequence>
  187. size_t read_some_at(implementation_type& impl, uint64_t offset,
  188. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  189. {
  190. return descriptor_service_.read_some_at(impl, offset, buffers, ec);
  191. }
  192. // Start an asynchronous read. The buffer for the data being read must be
  193. // valid for the lifetime of the asynchronous operation.
  194. template <typename MutableBufferSequence,
  195. typename Handler, typename IoExecutor>
  196. void async_read_some_at(implementation_type& impl,
  197. uint64_t offset, const MutableBufferSequence& buffers,
  198. Handler& handler, const IoExecutor& io_ex)
  199. {
  200. descriptor_service_.async_read_some_at(
  201. impl, offset, buffers, handler, io_ex);
  202. }
  203. private:
  204. // The implementation used for initiating asynchronous operations.
  205. descriptor_service descriptor_service_;
  206. // Cached success value to avoid accessing category singleton.
  207. const boost::system::error_code success_ec_;
  208. };
  209. } // namespace detail
  210. } // namespace asio
  211. } // namespace boost
  212. #include <boost/asio/detail/pop_options.hpp>
  213. #if defined(BOOST_ASIO_HEADER_ONLY)
  214. # include <boost/asio/detail/impl/io_uring_file_service.ipp>
  215. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  216. #endif // defined(BOOST_ASIO_HAS_FILE)
  217. // && defined(BOOST_ASIO_HAS_IO_URING)
  218. #endif // BOOST_ASIO_DETAIL_IO_URING_FILE_SERVICE_HPP