io_uring_file_service.hpp 8.2 KB

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