win_iocp_handle_service.hpp 16 KB

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