win_iocp_handle_service.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. //
  2. // detail/win_iocp_handle_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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. #ifndef ASIO_DETAIL_WIN_IOCP_HANDLE_SERVICE_HPP
  12. #define ASIO_DETAIL_WIN_IOCP_HANDLE_SERVICE_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include "asio/detail/config.hpp"
  17. #if defined(ASIO_HAS_IOCP)
  18. #include "asio/associated_cancellation_slot.hpp"
  19. #include "asio/error.hpp"
  20. #include "asio/execution_context.hpp"
  21. #include "asio/detail/buffer_sequence_adapter.hpp"
  22. #include "asio/detail/cstdint.hpp"
  23. #include "asio/detail/handler_alloc_helpers.hpp"
  24. #include "asio/detail/memory.hpp"
  25. #include "asio/detail/mutex.hpp"
  26. #include "asio/detail/operation.hpp"
  27. #include "asio/detail/win_iocp_handle_read_op.hpp"
  28. #include "asio/detail/win_iocp_handle_write_op.hpp"
  29. #include "asio/detail/win_iocp_io_context.hpp"
  30. #include "asio/detail/push_options.hpp"
  31. namespace asio {
  32. namespace detail {
  33. class win_iocp_handle_service :
  34. public execution_context_service_base<win_iocp_handle_service>
  35. {
  36. public:
  37. // The native type of a stream handle.
  38. typedef HANDLE native_handle_type;
  39. // The implementation type of the stream handle.
  40. class implementation_type
  41. {
  42. public:
  43. // Default constructor.
  44. implementation_type()
  45. : handle_(INVALID_HANDLE_VALUE),
  46. safe_cancellation_thread_id_(0),
  47. next_(0),
  48. prev_(0)
  49. {
  50. }
  51. private:
  52. // Only this service will have access to the internal values.
  53. friend class win_iocp_handle_service;
  54. // The native stream handle representation.
  55. native_handle_type handle_;
  56. // The ID of the thread from which it is safe to cancel asynchronous
  57. // operations. 0 means no asynchronous operations have been started yet.
  58. // ~0 means asynchronous operations have been started from more than one
  59. // thread, and cancellation is not supported for the handle.
  60. DWORD safe_cancellation_thread_id_;
  61. // Pointers to adjacent handle implementations in linked list.
  62. implementation_type* next_;
  63. implementation_type* prev_;
  64. };
  65. ASIO_DECL win_iocp_handle_service(execution_context& context);
  66. // Destroy all user-defined handler objects owned by the service.
  67. ASIO_DECL void shutdown();
  68. // Construct a new handle implementation.
  69. ASIO_DECL void construct(implementation_type& impl);
  70. // Move-construct a new handle implementation.
  71. ASIO_DECL void move_construct(implementation_type& impl,
  72. implementation_type& other_impl);
  73. // Move-assign from another handle implementation.
  74. ASIO_DECL void move_assign(implementation_type& impl,
  75. win_iocp_handle_service& other_service,
  76. implementation_type& other_impl);
  77. // Destroy a handle implementation.
  78. ASIO_DECL void destroy(implementation_type& impl);
  79. // Assign a native handle to a handle implementation.
  80. ASIO_DECL asio::error_code assign(implementation_type& impl,
  81. const native_handle_type& handle, asio::error_code& ec);
  82. // Determine whether the handle is open.
  83. bool is_open(const implementation_type& impl) const
  84. {
  85. return impl.handle_ != INVALID_HANDLE_VALUE;
  86. }
  87. // Destroy a handle implementation.
  88. ASIO_DECL asio::error_code close(implementation_type& impl,
  89. asio::error_code& ec);
  90. // Release ownership of a handle.
  91. ASIO_DECL native_handle_type release(implementation_type& impl,
  92. asio::error_code& ec);
  93. // Get the native handle representation.
  94. native_handle_type native_handle(const implementation_type& impl) const
  95. {
  96. return impl.handle_;
  97. }
  98. // Cancel all operations associated with the handle.
  99. ASIO_DECL asio::error_code cancel(implementation_type& impl,
  100. asio::error_code& ec);
  101. // Write the given data. Returns the number of bytes written.
  102. template <typename ConstBufferSequence>
  103. size_t write_some(implementation_type& impl,
  104. const ConstBufferSequence& buffers, asio::error_code& ec)
  105. {
  106. return write_some_at(impl, 0, buffers, ec);
  107. }
  108. // Write the given data at the specified offset. Returns the number of bytes
  109. // written.
  110. template <typename ConstBufferSequence>
  111. size_t write_some_at(implementation_type& impl, uint64_t offset,
  112. const ConstBufferSequence& buffers, asio::error_code& ec)
  113. {
  114. asio::const_buffer buffer =
  115. buffer_sequence_adapter<asio::const_buffer,
  116. ConstBufferSequence>::first(buffers);
  117. return do_write(impl, offset, buffer, ec);
  118. }
  119. // Start an asynchronous write. The data being written must be valid for the
  120. // lifetime of the asynchronous operation.
  121. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  122. void async_write_some(implementation_type& impl,
  123. const ConstBufferSequence& buffers,
  124. Handler& handler, const IoExecutor& io_ex)
  125. {
  126. associated_cancellation_slot_t<Handler> slot
  127. = asio::get_associated_cancellation_slot(handler);
  128. // Allocate and construct an operation to wrap the handler.
  129. typedef win_iocp_handle_write_op<
  130. ConstBufferSequence, Handler, IoExecutor> op;
  131. typename op::ptr p = { asio::detail::addressof(handler),
  132. op::ptr::allocate(handler), 0 };
  133. operation* o = p.p = new (p.v) op(buffers, handler, io_ex);
  134. ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
  135. reinterpret_cast<uintmax_t>(impl.handle_), "async_write_some"));
  136. // Optionally register for per-operation cancellation.
  137. if (slot.is_connected())
  138. o = &slot.template emplace<iocp_op_cancellation>(impl.handle_, o);
  139. start_write_op(impl, 0,
  140. buffer_sequence_adapter<asio::const_buffer,
  141. ConstBufferSequence>::first(buffers), o);
  142. p.v = p.p = 0;
  143. }
  144. // Start an asynchronous write at a specified offset. The data being written
  145. // must be valid for the lifetime of the asynchronous operation.
  146. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  147. void async_write_some_at(implementation_type& impl,
  148. uint64_t offset, const ConstBufferSequence& buffers,
  149. Handler& handler, const IoExecutor& io_ex)
  150. {
  151. associated_cancellation_slot_t<Handler> slot
  152. = asio::get_associated_cancellation_slot(handler);
  153. // Allocate and construct an operation to wrap the handler.
  154. typedef win_iocp_handle_write_op<
  155. ConstBufferSequence, Handler, IoExecutor> op;
  156. typename op::ptr p = { asio::detail::addressof(handler),
  157. op::ptr::allocate(handler), 0 };
  158. operation* o = p.p = new (p.v) op(buffers, handler, io_ex);
  159. ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
  160. reinterpret_cast<uintmax_t>(impl.handle_), "async_write_some_at"));
  161. // Optionally register for per-operation cancellation.
  162. if (slot.is_connected())
  163. o = &slot.template emplace<iocp_op_cancellation>(impl.handle_, o);
  164. start_write_op(impl, offset,
  165. buffer_sequence_adapter<asio::const_buffer,
  166. ConstBufferSequence>::first(buffers), o);
  167. p.v = p.p = 0;
  168. }
  169. // Read some data. Returns the number of bytes received.
  170. template <typename MutableBufferSequence>
  171. size_t read_some(implementation_type& impl,
  172. const MutableBufferSequence& buffers, asio::error_code& ec)
  173. {
  174. return read_some_at(impl, 0, buffers, ec);
  175. }
  176. // Read some data at a specified offset. Returns the number of bytes received.
  177. template <typename MutableBufferSequence>
  178. size_t read_some_at(implementation_type& impl, uint64_t offset,
  179. const MutableBufferSequence& buffers, asio::error_code& ec)
  180. {
  181. asio::mutable_buffer buffer =
  182. buffer_sequence_adapter<asio::mutable_buffer,
  183. MutableBufferSequence>::first(buffers);
  184. return do_read(impl, offset, buffer, ec);
  185. }
  186. // Start an asynchronous read. The buffer for the data being received must be
  187. // valid for the lifetime of the asynchronous operation.
  188. template <typename MutableBufferSequence,
  189. typename Handler, typename IoExecutor>
  190. void async_read_some(implementation_type& impl,
  191. const MutableBufferSequence& buffers,
  192. Handler& handler, const IoExecutor& io_ex)
  193. {
  194. associated_cancellation_slot_t<Handler> slot
  195. = asio::get_associated_cancellation_slot(handler);
  196. // Allocate and construct an operation to wrap the handler.
  197. typedef win_iocp_handle_read_op<
  198. MutableBufferSequence, Handler, IoExecutor> op;
  199. typename op::ptr p = { asio::detail::addressof(handler),
  200. op::ptr::allocate(handler), 0 };
  201. operation* o = p.p = new (p.v) op(buffers, handler, io_ex);
  202. ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
  203. reinterpret_cast<uintmax_t>(impl.handle_), "async_read_some"));
  204. // Optionally register for per-operation cancellation.
  205. if (slot.is_connected())
  206. o = &slot.template emplace<iocp_op_cancellation>(impl.handle_, o);
  207. start_read_op(impl, 0,
  208. buffer_sequence_adapter<asio::mutable_buffer,
  209. MutableBufferSequence>::first(buffers), o);
  210. p.v = p.p = 0;
  211. }
  212. // Start an asynchronous read at a specified offset. The buffer for the data
  213. // being received must be valid for the lifetime of the asynchronous
  214. // operation.
  215. template <typename MutableBufferSequence,
  216. typename Handler, typename IoExecutor>
  217. void async_read_some_at(implementation_type& impl,
  218. uint64_t offset, const MutableBufferSequence& buffers,
  219. Handler& handler, const IoExecutor& io_ex)
  220. {
  221. associated_cancellation_slot_t<Handler> slot
  222. = asio::get_associated_cancellation_slot(handler);
  223. // Allocate and construct an operation to wrap the handler.
  224. typedef win_iocp_handle_read_op<
  225. MutableBufferSequence, Handler, IoExecutor> op;
  226. typename op::ptr p = { asio::detail::addressof(handler),
  227. op::ptr::allocate(handler), 0 };
  228. operation* o = p.p = new (p.v) op(buffers, handler, io_ex);
  229. ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
  230. reinterpret_cast<uintmax_t>(impl.handle_), "async_read_some_at"));
  231. // Optionally register for per-operation cancellation.
  232. if (slot.is_connected())
  233. o = &slot.template emplace<iocp_op_cancellation>(impl.handle_, o);
  234. start_read_op(impl, offset,
  235. buffer_sequence_adapter<asio::mutable_buffer,
  236. MutableBufferSequence>::first(buffers), o);
  237. p.v = p.p = 0;
  238. }
  239. private:
  240. // Prevent the use of the null_buffers type with this service.
  241. size_t write_some(implementation_type& impl,
  242. const null_buffers& buffers, asio::error_code& ec);
  243. size_t write_some_at(implementation_type& impl, uint64_t offset,
  244. const null_buffers& buffers, asio::error_code& ec);
  245. template <typename Handler, typename IoExecutor>
  246. void async_write_some(implementation_type& impl,
  247. const null_buffers& buffers, Handler& handler,
  248. const IoExecutor& io_ex);
  249. template <typename Handler, typename IoExecutor>
  250. void async_write_some_at(implementation_type& impl, uint64_t offset,
  251. const null_buffers& buffers, Handler& handler, const IoExecutor& io_ex);
  252. size_t read_some(implementation_type& impl,
  253. const null_buffers& buffers, asio::error_code& ec);
  254. size_t read_some_at(implementation_type& impl, uint64_t offset,
  255. const null_buffers& buffers, asio::error_code& ec);
  256. template <typename Handler, typename IoExecutor>
  257. void async_read_some(implementation_type& impl,
  258. const null_buffers& buffers, Handler& handler,
  259. const IoExecutor& io_ex);
  260. template <typename Handler, typename IoExecutor>
  261. void async_read_some_at(implementation_type& impl, uint64_t offset,
  262. const null_buffers& buffers, Handler& handler, const IoExecutor& io_ex);
  263. // Helper class for waiting for synchronous operations to complete.
  264. class overlapped_wrapper;
  265. // Helper function to perform a synchronous write operation.
  266. ASIO_DECL size_t do_write(implementation_type& impl,
  267. uint64_t offset, const asio::const_buffer& buffer,
  268. asio::error_code& ec);
  269. // Helper function to start a write operation.
  270. ASIO_DECL void start_write_op(implementation_type& impl,
  271. uint64_t offset, const asio::const_buffer& buffer,
  272. operation* op);
  273. // Helper function to perform a synchronous write operation.
  274. ASIO_DECL size_t do_read(implementation_type& impl,
  275. uint64_t offset, const asio::mutable_buffer& buffer,
  276. asio::error_code& ec);
  277. // Helper function to start a read operation.
  278. ASIO_DECL void start_read_op(implementation_type& impl,
  279. uint64_t offset, const asio::mutable_buffer& buffer,
  280. operation* op);
  281. // Update the ID of the thread from which cancellation is safe.
  282. ASIO_DECL void update_cancellation_thread_id(implementation_type& impl);
  283. // Helper function to close a handle when the associated object is being
  284. // destroyed.
  285. ASIO_DECL void close_for_destruction(implementation_type& impl);
  286. // The type of a NtSetInformationFile function pointer.
  287. typedef LONG (NTAPI *nt_set_info_fn)(HANDLE, ULONG_PTR*, void*, ULONG, ULONG);
  288. // Helper function to get the NtSetInformationFile function pointer. If no
  289. // NtSetInformationFile pointer has been obtained yet, one is obtained using
  290. // GetProcAddress and the pointer is cached. Returns a null pointer if
  291. // NtSetInformationFile is not available.
  292. ASIO_DECL nt_set_info_fn get_nt_set_info();
  293. // Helper function to emulate InterlockedCompareExchangePointer functionality
  294. // for:
  295. // - very old Platform SDKs; and
  296. // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
  297. ASIO_DECL void* interlocked_compare_exchange_pointer(
  298. void** dest, void* exch, void* cmp);
  299. // Helper function to emulate InterlockedExchangePointer functionality for:
  300. // - very old Platform SDKs; and
  301. // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
  302. ASIO_DECL void* interlocked_exchange_pointer(void** dest, void* val);
  303. // Helper class used to implement per operation cancellation.
  304. class iocp_op_cancellation : public operation
  305. {
  306. public:
  307. iocp_op_cancellation(HANDLE h, operation* target)
  308. : operation(&iocp_op_cancellation::do_complete),
  309. handle_(h),
  310. target_(target)
  311. {
  312. }
  313. static void do_complete(void* owner, operation* base,
  314. const asio::error_code& result_ec,
  315. std::size_t bytes_transferred)
  316. {
  317. iocp_op_cancellation* o = static_cast<iocp_op_cancellation*>(base);
  318. o->target_->complete(owner, result_ec, bytes_transferred);
  319. }
  320. void operator()(cancellation_type_t type)
  321. {
  322. #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  323. if (!!(type &
  324. (cancellation_type::terminal
  325. | cancellation_type::partial
  326. | cancellation_type::total)))
  327. {
  328. ::CancelIoEx(handle_, this);
  329. }
  330. #else // defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  331. (void)type;
  332. #endif // defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  333. }
  334. private:
  335. HANDLE handle_;
  336. operation* target_;
  337. };
  338. // The IOCP service used for running asynchronous operations and dispatching
  339. // handlers.
  340. win_iocp_io_context& iocp_service_;
  341. // Pointer to NtSetInformationFile implementation.
  342. void* nt_set_info_;
  343. // Mutex to protect access to the linked list of implementations.
  344. mutex mutex_;
  345. // The head of a linked list of all implementations.
  346. implementation_type* impl_list_;
  347. };
  348. } // namespace detail
  349. } // namespace asio
  350. #include "asio/detail/pop_options.hpp"
  351. #if defined(ASIO_HEADER_ONLY)
  352. # include "asio/detail/impl/win_iocp_handle_service.ipp"
  353. #endif // defined(ASIO_HEADER_ONLY)
  354. #endif // defined(ASIO_HAS_IOCP)
  355. #endif // ASIO_DETAIL_WIN_IOCP_HANDLE_SERVICE_HPP