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