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