win_iocp_handle_service.ipp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. //
  2. // detail/impl/win_iocp_handle_service.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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_IMPL_WIN_IOCP_HANDLE_SERVICE_IPP
  12. #define ASIO_DETAIL_IMPL_WIN_IOCP_HANDLE_SERVICE_IPP
  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/detail/win_iocp_handle_service.hpp"
  19. #include "asio/detail/push_options.hpp"
  20. namespace asio {
  21. namespace detail {
  22. class win_iocp_handle_service::overlapped_wrapper
  23. : public OVERLAPPED
  24. {
  25. public:
  26. explicit overlapped_wrapper(asio::error_code& ec)
  27. {
  28. Internal = 0;
  29. InternalHigh = 0;
  30. Offset = 0;
  31. OffsetHigh = 0;
  32. // Create a non-signalled manual-reset event, for GetOverlappedResult.
  33. hEvent = ::CreateEvent(0, TRUE, FALSE, 0);
  34. if (hEvent)
  35. {
  36. // As documented in GetQueuedCompletionStatus, setting the low order
  37. // bit of this event prevents our synchronous writes from being treated
  38. // as completion port events.
  39. *reinterpret_cast<DWORD_PTR*>(&hEvent) |= 1;
  40. }
  41. else
  42. {
  43. DWORD last_error = ::GetLastError();
  44. ec = asio::error_code(last_error,
  45. asio::error::get_system_category());
  46. }
  47. }
  48. ~overlapped_wrapper()
  49. {
  50. if (hEvent)
  51. {
  52. ::CloseHandle(hEvent);
  53. }
  54. }
  55. };
  56. win_iocp_handle_service::win_iocp_handle_service(
  57. asio::io_service& io_service)
  58. : iocp_service_(asio::use_service<win_iocp_io_service>(io_service)),
  59. mutex_(),
  60. impl_list_(0)
  61. {
  62. }
  63. void win_iocp_handle_service::shutdown_service()
  64. {
  65. // Close all implementations, causing all operations to complete.
  66. asio::detail::mutex::scoped_lock lock(mutex_);
  67. implementation_type* impl = impl_list_;
  68. while (impl)
  69. {
  70. close_for_destruction(*impl);
  71. impl = impl->next_;
  72. }
  73. }
  74. void win_iocp_handle_service::construct(
  75. win_iocp_handle_service::implementation_type& impl)
  76. {
  77. impl.handle_ = INVALID_HANDLE_VALUE;
  78. impl.safe_cancellation_thread_id_ = 0;
  79. // Insert implementation into linked list of all implementations.
  80. asio::detail::mutex::scoped_lock lock(mutex_);
  81. impl.next_ = impl_list_;
  82. impl.prev_ = 0;
  83. if (impl_list_)
  84. impl_list_->prev_ = &impl;
  85. impl_list_ = &impl;
  86. }
  87. void win_iocp_handle_service::destroy(
  88. win_iocp_handle_service::implementation_type& impl)
  89. {
  90. close_for_destruction(impl);
  91. // Remove implementation from linked list of all implementations.
  92. asio::detail::mutex::scoped_lock lock(mutex_);
  93. if (impl_list_ == &impl)
  94. impl_list_ = impl.next_;
  95. if (impl.prev_)
  96. impl.prev_->next_ = impl.next_;
  97. if (impl.next_)
  98. impl.next_->prev_= impl.prev_;
  99. impl.next_ = 0;
  100. impl.prev_ = 0;
  101. }
  102. asio::error_code win_iocp_handle_service::assign(
  103. win_iocp_handle_service::implementation_type& impl,
  104. const native_type& native_handle, asio::error_code& ec)
  105. {
  106. if (is_open(impl))
  107. {
  108. ec = asio::error::already_open;
  109. return ec;
  110. }
  111. if (iocp_service_.register_handle(native_handle, ec))
  112. return ec;
  113. impl.handle_ = native_handle;
  114. ec = asio::error_code();
  115. return ec;
  116. }
  117. asio::error_code win_iocp_handle_service::close(
  118. win_iocp_handle_service::implementation_type& impl,
  119. asio::error_code& ec)
  120. {
  121. if (is_open(impl))
  122. {
  123. if (!::CloseHandle(impl.handle_))
  124. {
  125. DWORD last_error = ::GetLastError();
  126. ec = asio::error_code(last_error,
  127. asio::error::get_system_category());
  128. return ec;
  129. }
  130. impl.handle_ = INVALID_HANDLE_VALUE;
  131. impl.safe_cancellation_thread_id_ = 0;
  132. }
  133. ec = asio::error_code();
  134. return ec;
  135. }
  136. asio::error_code win_iocp_handle_service::cancel(
  137. win_iocp_handle_service::implementation_type& impl,
  138. asio::error_code& ec)
  139. {
  140. if (!is_open(impl))
  141. {
  142. ec = asio::error::bad_descriptor;
  143. }
  144. else if (FARPROC cancel_io_ex_ptr = ::GetProcAddress(
  145. ::GetModuleHandleA("KERNEL32"), "CancelIoEx"))
  146. {
  147. // The version of Windows supports cancellation from any thread.
  148. typedef BOOL (WINAPI* cancel_io_ex_t)(HANDLE, LPOVERLAPPED);
  149. cancel_io_ex_t cancel_io_ex = (cancel_io_ex_t)cancel_io_ex_ptr;
  150. if (!cancel_io_ex(impl.handle_, 0))
  151. {
  152. DWORD last_error = ::GetLastError();
  153. if (last_error == ERROR_NOT_FOUND)
  154. {
  155. // ERROR_NOT_FOUND means that there were no operations to be
  156. // cancelled. We swallow this error to match the behaviour on other
  157. // platforms.
  158. ec = asio::error_code();
  159. }
  160. else
  161. {
  162. ec = asio::error_code(last_error,
  163. asio::error::get_system_category());
  164. }
  165. }
  166. else
  167. {
  168. ec = asio::error_code();
  169. }
  170. }
  171. else if (impl.safe_cancellation_thread_id_ == 0)
  172. {
  173. // No operations have been started, so there's nothing to cancel.
  174. ec = asio::error_code();
  175. }
  176. else if (impl.safe_cancellation_thread_id_ == ::GetCurrentThreadId())
  177. {
  178. // Asynchronous operations have been started from the current thread only,
  179. // so it is safe to try to cancel them using CancelIo.
  180. if (!::CancelIo(impl.handle_))
  181. {
  182. DWORD last_error = ::GetLastError();
  183. ec = asio::error_code(last_error,
  184. asio::error::get_system_category());
  185. }
  186. else
  187. {
  188. ec = asio::error_code();
  189. }
  190. }
  191. else
  192. {
  193. // Asynchronous operations have been started from more than one thread,
  194. // so cancellation is not safe.
  195. ec = asio::error::operation_not_supported;
  196. }
  197. return ec;
  198. }
  199. size_t win_iocp_handle_service::do_write(
  200. win_iocp_handle_service::implementation_type& impl, boost::uint64_t offset,
  201. const asio::const_buffer& buffer, asio::error_code& ec)
  202. {
  203. if (!is_open(impl))
  204. {
  205. ec = asio::error::bad_descriptor;
  206. return 0;
  207. }
  208. // A request to write 0 bytes on a handle is a no-op.
  209. if (asio::buffer_size(buffer) == 0)
  210. {
  211. ec = asio::error_code();
  212. return 0;
  213. }
  214. overlapped_wrapper overlapped(ec);
  215. if (ec)
  216. {
  217. return 0;
  218. }
  219. // Write the data.
  220. overlapped.Offset = offset & 0xFFFFFFFF;
  221. overlapped.OffsetHigh = (offset >> 32) & 0xFFFFFFFF;
  222. BOOL ok = ::WriteFile(impl.handle_,
  223. asio::buffer_cast<LPCVOID>(buffer),
  224. static_cast<DWORD>(asio::buffer_size(buffer)), 0, &overlapped);
  225. if (!ok)
  226. {
  227. DWORD last_error = ::GetLastError();
  228. if (last_error != ERROR_IO_PENDING)
  229. {
  230. ec = asio::error_code(last_error,
  231. asio::error::get_system_category());
  232. return 0;
  233. }
  234. }
  235. // Wait for the operation to complete.
  236. DWORD bytes_transferred = 0;
  237. ok = ::GetOverlappedResult(impl.handle_,
  238. &overlapped, &bytes_transferred, TRUE);
  239. if (!ok)
  240. {
  241. DWORD last_error = ::GetLastError();
  242. ec = asio::error_code(last_error,
  243. asio::error::get_system_category());
  244. return 0;
  245. }
  246. ec = asio::error_code();
  247. return bytes_transferred;
  248. }
  249. void win_iocp_handle_service::start_write_op(
  250. win_iocp_handle_service::implementation_type& impl, boost::uint64_t offset,
  251. const asio::const_buffer& buffer, operation* op)
  252. {
  253. update_cancellation_thread_id(impl);
  254. iocp_service_.work_started();
  255. if (!is_open(impl))
  256. {
  257. iocp_service_.on_completion(op, asio::error::bad_descriptor);
  258. }
  259. else if (asio::buffer_size(buffer) == 0)
  260. {
  261. // A request to write 0 bytes on a handle is a no-op.
  262. iocp_service_.on_completion(op);
  263. }
  264. else
  265. {
  266. DWORD bytes_transferred = 0;
  267. op->Offset = offset & 0xFFFFFFFF;
  268. op->OffsetHigh = (offset >> 32) & 0xFFFFFFFF;
  269. BOOL ok = ::WriteFile(impl.handle_,
  270. asio::buffer_cast<LPCVOID>(buffer),
  271. static_cast<DWORD>(asio::buffer_size(buffer)),
  272. &bytes_transferred, op);
  273. DWORD last_error = ::GetLastError();
  274. if (!ok && last_error != ERROR_IO_PENDING
  275. && last_error != ERROR_MORE_DATA)
  276. {
  277. iocp_service_.on_completion(op, last_error, bytes_transferred);
  278. }
  279. else
  280. {
  281. iocp_service_.on_pending(op);
  282. }
  283. }
  284. }
  285. size_t win_iocp_handle_service::do_read(
  286. win_iocp_handle_service::implementation_type& impl, boost::uint64_t offset,
  287. const asio::mutable_buffer& buffer, asio::error_code& ec)
  288. {
  289. if (!is_open(impl))
  290. {
  291. ec = asio::error::bad_descriptor;
  292. return 0;
  293. }
  294. // A request to read 0 bytes on a stream handle is a no-op.
  295. if (asio::buffer_size(buffer) == 0)
  296. {
  297. ec = asio::error_code();
  298. return 0;
  299. }
  300. overlapped_wrapper overlapped(ec);
  301. if (ec)
  302. {
  303. return 0;
  304. }
  305. // Read some data.
  306. overlapped.Offset = offset & 0xFFFFFFFF;
  307. overlapped.OffsetHigh = (offset >> 32) & 0xFFFFFFFF;
  308. BOOL ok = ::ReadFile(impl.handle_,
  309. asio::buffer_cast<LPVOID>(buffer),
  310. static_cast<DWORD>(asio::buffer_size(buffer)), 0, &overlapped);
  311. if (!ok)
  312. {
  313. DWORD last_error = ::GetLastError();
  314. if (last_error != ERROR_IO_PENDING && last_error != ERROR_MORE_DATA)
  315. {
  316. if (last_error == ERROR_HANDLE_EOF)
  317. {
  318. ec = asio::error::eof;
  319. }
  320. else
  321. {
  322. ec = asio::error_code(last_error,
  323. asio::error::get_system_category());
  324. }
  325. return 0;
  326. }
  327. }
  328. // Wait for the operation to complete.
  329. DWORD bytes_transferred = 0;
  330. ok = ::GetOverlappedResult(impl.handle_,
  331. &overlapped, &bytes_transferred, TRUE);
  332. if (!ok)
  333. {
  334. DWORD last_error = ::GetLastError();
  335. if (last_error == ERROR_HANDLE_EOF)
  336. {
  337. ec = asio::error::eof;
  338. }
  339. else
  340. {
  341. ec = asio::error_code(last_error,
  342. asio::error::get_system_category());
  343. }
  344. return 0;
  345. }
  346. ec = asio::error_code();
  347. return bytes_transferred;
  348. }
  349. void win_iocp_handle_service::start_read_op(
  350. win_iocp_handle_service::implementation_type& impl, boost::uint64_t offset,
  351. const asio::mutable_buffer& buffer, operation* op)
  352. {
  353. update_cancellation_thread_id(impl);
  354. iocp_service_.work_started();
  355. if (!is_open(impl))
  356. {
  357. iocp_service_.on_completion(op, asio::error::bad_descriptor);
  358. }
  359. else if (asio::buffer_size(buffer) == 0)
  360. {
  361. // A request to read 0 bytes on a handle is a no-op.
  362. iocp_service_.on_completion(op);
  363. }
  364. else
  365. {
  366. DWORD bytes_transferred = 0;
  367. op->Offset = offset & 0xFFFFFFFF;
  368. op->OffsetHigh = (offset >> 32) & 0xFFFFFFFF;
  369. BOOL ok = ::ReadFile(impl.handle_,
  370. asio::buffer_cast<LPVOID>(buffer),
  371. static_cast<DWORD>(asio::buffer_size(buffer)),
  372. &bytes_transferred, op);
  373. DWORD last_error = ::GetLastError();
  374. if (!ok && last_error != ERROR_IO_PENDING
  375. && last_error != ERROR_MORE_DATA)
  376. {
  377. iocp_service_.on_completion(op, last_error, bytes_transferred);
  378. }
  379. else
  380. {
  381. iocp_service_.on_pending(op);
  382. }
  383. }
  384. }
  385. void win_iocp_handle_service::update_cancellation_thread_id(
  386. win_iocp_handle_service::implementation_type& impl)
  387. {
  388. if (impl.safe_cancellation_thread_id_ == 0)
  389. impl.safe_cancellation_thread_id_ = ::GetCurrentThreadId();
  390. else if (impl.safe_cancellation_thread_id_ != ::GetCurrentThreadId())
  391. impl.safe_cancellation_thread_id_ = ~DWORD(0);
  392. }
  393. void win_iocp_handle_service::close_for_destruction(implementation_type& impl)
  394. {
  395. if (is_open(impl))
  396. {
  397. ::CloseHandle(impl.handle_);
  398. impl.handle_ = INVALID_HANDLE_VALUE;
  399. impl.safe_cancellation_thread_id_ = 0;
  400. }
  401. }
  402. } // namespace detail
  403. } // namespace asio
  404. #include "asio/detail/pop_options.hpp"
  405. #endif // defined(ASIO_HAS_IOCP)
  406. #endif // ASIO_DETAIL_IMPL_WIN_IOCP_HANDLE_SERVICE_IPP