]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/detail/win_iocp_handle_service.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / asio / detail / win_iocp_handle_service.hpp
1 //
2 // detail/win_iocp_handle_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
7 //
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 //
11
12 #ifndef BOOST_ASIO_DETAIL_WIN_IOCP_HANDLE_SERVICE_HPP
13 #define BOOST_ASIO_DETAIL_WIN_IOCP_HANDLE_SERVICE_HPP
14
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 # pragma once
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
19 #include <boost/asio/detail/config.hpp>
20
21 #if defined(BOOST_ASIO_HAS_IOCP)
22
23 #include <boost/asio/error.hpp>
24 #include <boost/asio/io_context.hpp>
25 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
26 #include <boost/asio/detail/cstdint.hpp>
27 #include <boost/asio/detail/handler_alloc_helpers.hpp>
28 #include <boost/asio/detail/memory.hpp>
29 #include <boost/asio/detail/mutex.hpp>
30 #include <boost/asio/detail/operation.hpp>
31 #include <boost/asio/detail/win_iocp_handle_read_op.hpp>
32 #include <boost/asio/detail/win_iocp_handle_write_op.hpp>
33 #include <boost/asio/detail/win_iocp_io_context.hpp>
34
35 #include <boost/asio/detail/push_options.hpp>
36
37 namespace boost {
38 namespace asio {
39 namespace detail {
40
41 class win_iocp_handle_service :
42 public service_base<win_iocp_handle_service>
43 {
44 public:
45 // The native type of a stream handle.
46 typedef HANDLE native_handle_type;
47
48 // The implementation type of the stream handle.
49 class implementation_type
50 {
51 public:
52 // Default constructor.
53 implementation_type()
54 : handle_(INVALID_HANDLE_VALUE),
55 safe_cancellation_thread_id_(0),
56 next_(0),
57 prev_(0)
58 {
59 }
60
61 private:
62 // Only this service will have access to the internal values.
63 friend class win_iocp_handle_service;
64
65 // The native stream handle representation.
66 native_handle_type handle_;
67
68 // The ID of the thread from which it is safe to cancel asynchronous
69 // operations. 0 means no asynchronous operations have been started yet.
70 // ~0 means asynchronous operations have been started from more than one
71 // thread, and cancellation is not supported for the handle.
72 DWORD safe_cancellation_thread_id_;
73
74 // Pointers to adjacent handle implementations in linked list.
75 implementation_type* next_;
76 implementation_type* prev_;
77 };
78
79 BOOST_ASIO_DECL win_iocp_handle_service(boost::asio::io_context& io_context);
80
81 // Destroy all user-defined handler objects owned by the service.
82 BOOST_ASIO_DECL void shutdown();
83
84 // Construct a new handle implementation.
85 BOOST_ASIO_DECL void construct(implementation_type& impl);
86
87 // Move-construct a new handle implementation.
88 BOOST_ASIO_DECL void move_construct(implementation_type& impl,
89 implementation_type& other_impl);
90
91 // Move-assign from another handle implementation.
92 BOOST_ASIO_DECL void move_assign(implementation_type& impl,
93 win_iocp_handle_service& other_service,
94 implementation_type& other_impl);
95
96 // Destroy a handle implementation.
97 BOOST_ASIO_DECL void destroy(implementation_type& impl);
98
99 // Assign a native handle to a handle implementation.
100 BOOST_ASIO_DECL boost::system::error_code assign(implementation_type& impl,
101 const native_handle_type& handle, boost::system::error_code& ec);
102
103 // Determine whether the handle is open.
104 bool is_open(const implementation_type& impl) const
105 {
106 return impl.handle_ != INVALID_HANDLE_VALUE;
107 }
108
109 // Destroy a handle implementation.
110 BOOST_ASIO_DECL boost::system::error_code close(implementation_type& impl,
111 boost::system::error_code& ec);
112
113 // Get the native handle representation.
114 native_handle_type native_handle(const implementation_type& impl) const
115 {
116 return impl.handle_;
117 }
118
119 // Cancel all operations associated with the handle.
120 BOOST_ASIO_DECL boost::system::error_code cancel(implementation_type& impl,
121 boost::system::error_code& ec);
122
123 // Write the given data. Returns the number of bytes written.
124 template <typename ConstBufferSequence>
125 size_t write_some(implementation_type& impl,
126 const ConstBufferSequence& buffers, boost::system::error_code& ec)
127 {
128 return write_some_at(impl, 0, buffers, ec);
129 }
130
131 // Write the given data at the specified offset. Returns the number of bytes
132 // written.
133 template <typename ConstBufferSequence>
134 size_t write_some_at(implementation_type& impl, uint64_t offset,
135 const ConstBufferSequence& buffers, boost::system::error_code& ec)
136 {
137 boost::asio::const_buffer buffer =
138 buffer_sequence_adapter<boost::asio::const_buffer,
139 ConstBufferSequence>::first(buffers);
140
141 return do_write(impl, offset, buffer, ec);
142 }
143
144 // Start an asynchronous write. The data being written must be valid for the
145 // lifetime of the asynchronous operation.
146 template <typename ConstBufferSequence, typename Handler>
147 void async_write_some(implementation_type& impl,
148 const ConstBufferSequence& buffers, Handler& handler)
149 {
150 // Allocate and construct an operation to wrap the handler.
151 typedef win_iocp_handle_write_op<ConstBufferSequence, Handler> op;
152 typename op::ptr p = { boost::asio::detail::addressof(handler),
153 op::ptr::allocate(handler), 0 };
154 p.p = new (p.v) op(buffers, handler);
155
156 BOOST_ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
157 reinterpret_cast<uintmax_t>(impl.handle_), "async_write_some"));
158
159 start_write_op(impl, 0,
160 buffer_sequence_adapter<boost::asio::const_buffer,
161 ConstBufferSequence>::first(buffers), p.p);
162 p.v = p.p = 0;
163 }
164
165 // Start an asynchronous write at a specified offset. The data being written
166 // must be valid for the lifetime of the asynchronous operation.
167 template <typename ConstBufferSequence, typename Handler>
168 void async_write_some_at(implementation_type& impl, uint64_t offset,
169 const ConstBufferSequence& buffers, Handler& handler)
170 {
171 // Allocate and construct an operation to wrap the handler.
172 typedef win_iocp_handle_write_op<ConstBufferSequence, Handler> op;
173 typename op::ptr p = { boost::asio::detail::addressof(handler),
174 op::ptr::allocate(handler), 0 };
175 p.p = new (p.v) op(buffers, handler);
176
177 BOOST_ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
178 reinterpret_cast<uintmax_t>(impl.handle_), "async_write_some_at"));
179
180 start_write_op(impl, offset,
181 buffer_sequence_adapter<boost::asio::const_buffer,
182 ConstBufferSequence>::first(buffers), p.p);
183 p.v = p.p = 0;
184 }
185
186 // Read some data. Returns the number of bytes received.
187 template <typename MutableBufferSequence>
188 size_t read_some(implementation_type& impl,
189 const MutableBufferSequence& buffers, boost::system::error_code& ec)
190 {
191 return read_some_at(impl, 0, buffers, ec);
192 }
193
194 // Read some data at a specified offset. Returns the number of bytes received.
195 template <typename MutableBufferSequence>
196 size_t read_some_at(implementation_type& impl, uint64_t offset,
197 const MutableBufferSequence& buffers, boost::system::error_code& ec)
198 {
199 boost::asio::mutable_buffer buffer =
200 buffer_sequence_adapter<boost::asio::mutable_buffer,
201 MutableBufferSequence>::first(buffers);
202
203 return do_read(impl, offset, buffer, ec);
204 }
205
206 // Start an asynchronous read. The buffer for the data being received must be
207 // valid for the lifetime of the asynchronous operation.
208 template <typename MutableBufferSequence, typename Handler>
209 void async_read_some(implementation_type& impl,
210 const MutableBufferSequence& buffers, Handler& handler)
211 {
212 // Allocate and construct an operation to wrap the handler.
213 typedef win_iocp_handle_read_op<MutableBufferSequence, Handler> op;
214 typename op::ptr p = { boost::asio::detail::addressof(handler),
215 op::ptr::allocate(handler), 0 };
216 p.p = new (p.v) op(buffers, handler);
217
218 BOOST_ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
219 reinterpret_cast<uintmax_t>(impl.handle_), "async_read_some"));
220
221 start_read_op(impl, 0,
222 buffer_sequence_adapter<boost::asio::mutable_buffer,
223 MutableBufferSequence>::first(buffers), p.p);
224 p.v = p.p = 0;
225 }
226
227 // Start an asynchronous read at a specified offset. The buffer for the data
228 // being received must be valid for the lifetime of the asynchronous
229 // operation.
230 template <typename MutableBufferSequence, typename Handler>
231 void async_read_some_at(implementation_type& impl, uint64_t offset,
232 const MutableBufferSequence& buffers, Handler& handler)
233 {
234 // Allocate and construct an operation to wrap the handler.
235 typedef win_iocp_handle_read_op<MutableBufferSequence, Handler> op;
236 typename op::ptr p = { boost::asio::detail::addressof(handler),
237 op::ptr::allocate(handler), 0 };
238 p.p = new (p.v) op(buffers, handler);
239
240 BOOST_ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
241 reinterpret_cast<uintmax_t>(impl.handle_), "async_read_some_at"));
242
243 start_read_op(impl, offset,
244 buffer_sequence_adapter<boost::asio::mutable_buffer,
245 MutableBufferSequence>::first(buffers), p.p);
246 p.v = p.p = 0;
247 }
248
249 private:
250 // Prevent the use of the null_buffers type with this service.
251 size_t write_some(implementation_type& impl,
252 const null_buffers& buffers, boost::system::error_code& ec);
253 size_t write_some_at(implementation_type& impl, uint64_t offset,
254 const null_buffers& buffers, boost::system::error_code& ec);
255 template <typename Handler>
256 void async_write_some(implementation_type& impl,
257 const null_buffers& buffers, Handler& handler);
258 template <typename Handler>
259 void async_write_some_at(implementation_type& impl, uint64_t offset,
260 const null_buffers& buffers, Handler& handler);
261 size_t read_some(implementation_type& impl,
262 const null_buffers& buffers, boost::system::error_code& ec);
263 size_t read_some_at(implementation_type& impl, uint64_t offset,
264 const null_buffers& buffers, boost::system::error_code& ec);
265 template <typename Handler>
266 void async_read_some(implementation_type& impl,
267 const null_buffers& buffers, Handler& handler);
268 template <typename Handler>
269 void async_read_some_at(implementation_type& impl, uint64_t offset,
270 const null_buffers& buffers, Handler& handler);
271
272 // Helper class for waiting for synchronous operations to complete.
273 class overlapped_wrapper;
274
275 // Helper function to perform a synchronous write operation.
276 BOOST_ASIO_DECL size_t do_write(implementation_type& impl,
277 uint64_t offset, const boost::asio::const_buffer& buffer,
278 boost::system::error_code& ec);
279
280 // Helper function to start a write operation.
281 BOOST_ASIO_DECL void start_write_op(implementation_type& impl,
282 uint64_t offset, const boost::asio::const_buffer& buffer,
283 operation* op);
284
285 // Helper function to perform a synchronous write operation.
286 BOOST_ASIO_DECL size_t do_read(implementation_type& impl,
287 uint64_t offset, const boost::asio::mutable_buffer& buffer,
288 boost::system::error_code& ec);
289
290 // Helper function to start a read operation.
291 BOOST_ASIO_DECL void start_read_op(implementation_type& impl,
292 uint64_t offset, const boost::asio::mutable_buffer& buffer,
293 operation* op);
294
295 // Update the ID of the thread from which cancellation is safe.
296 BOOST_ASIO_DECL void update_cancellation_thread_id(implementation_type& impl);
297
298 // Helper function to close a handle when the associated object is being
299 // destroyed.
300 BOOST_ASIO_DECL void close_for_destruction(implementation_type& impl);
301
302 // The IOCP service used for running asynchronous operations and dispatching
303 // handlers.
304 win_iocp_io_context& iocp_service_;
305
306 // Mutex to protect access to the linked list of implementations.
307 mutex mutex_;
308
309 // The head of a linked list of all implementations.
310 implementation_type* impl_list_;
311 };
312
313 } // namespace detail
314 } // namespace asio
315 } // namespace boost
316
317 #include <boost/asio/detail/pop_options.hpp>
318
319 #if defined(BOOST_ASIO_HEADER_ONLY)
320 # include <boost/asio/detail/impl/win_iocp_handle_service.ipp>
321 #endif // defined(BOOST_ASIO_HEADER_ONLY)
322
323 #endif // defined(BOOST_ASIO_HAS_IOCP)
324
325 #endif // BOOST_ASIO_DETAIL_WIN_IOCP_HANDLE_SERVICE_HPP