2 // detail/impl/win_iocp_io_context.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_IO_CONTEXT_IPP
12 #define BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_IO_CONTEXT_IPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_HAS_IOCP)
22 #include <boost/asio/error.hpp>
23 #include <boost/asio/detail/cstdint.hpp>
24 #include <boost/asio/detail/handler_alloc_helpers.hpp>
25 #include <boost/asio/detail/handler_invoke_helpers.hpp>
26 #include <boost/asio/detail/limits.hpp>
27 #include <boost/asio/detail/thread.hpp>
28 #include <boost/asio/detail/throw_error.hpp>
29 #include <boost/asio/detail/win_iocp_io_context.hpp>
31 #include <boost/asio/detail/push_options.hpp>
37 struct win_iocp_io_context::thread_function
39 explicit thread_function(win_iocp_io_context* s)
46 boost::system::error_code ec;
50 win_iocp_io_context* this_;
53 struct win_iocp_io_context::work_finished_on_block_exit
55 ~work_finished_on_block_exit()
57 io_context_->work_finished();
60 win_iocp_io_context* io_context_;
63 struct win_iocp_io_context::timer_thread_function
67 while (::InterlockedExchangeAdd(&io_context_->shutdown_, 0) == 0)
69 if (::WaitForSingleObject(io_context_->waitable_timer_.handle,
70 INFINITE) == WAIT_OBJECT_0)
72 ::InterlockedExchange(&io_context_->dispatch_required_, 1);
73 ::PostQueuedCompletionStatus(io_context_->iocp_.handle,
74 0, wake_for_dispatch, 0);
79 win_iocp_io_context* io_context_;
82 win_iocp_io_context::win_iocp_io_context(
83 boost::asio::execution_context& ctx, int concurrency_hint, bool own_thread)
84 : execution_context_service_base<win_iocp_io_context>(ctx),
88 stop_event_posted_(0),
90 gqcs_timeout_(get_gqcs_timeout()),
91 dispatch_required_(0),
92 concurrency_hint_(concurrency_hint)
94 BOOST_ASIO_HANDLER_TRACKING_INIT;
96 iocp_.handle = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, 0,
97 static_cast<DWORD>(concurrency_hint >= 0 ? concurrency_hint : DWORD(~0)));
100 DWORD last_error = ::GetLastError();
101 boost::system::error_code ec(last_error,
102 boost::asio::error::get_system_category());
103 boost::asio::detail::throw_error(ec, "iocp");
108 ::InterlockedIncrement(&outstanding_work_);
109 thread_.reset(new boost::asio::detail::thread(thread_function(this)));
113 win_iocp_io_context::~win_iocp_io_context()
122 void win_iocp_io_context::shutdown()
124 ::InterlockedExchange(&shutdown_, 1);
126 if (timer_thread_.get())
128 LARGE_INTEGER timeout;
129 timeout.QuadPart = 1;
130 ::SetWaitableTimer(waitable_timer_.handle, &timeout, 1, 0, 0, FALSE);
137 ::InterlockedDecrement(&outstanding_work_);
140 while (::InterlockedExchangeAdd(&outstanding_work_, 0) > 0)
142 op_queue<win_iocp_operation> ops;
143 timer_queues_.get_all_timers(ops);
144 ops.push(completed_ops_);
147 while (win_iocp_operation* op = ops.front())
150 ::InterlockedDecrement(&outstanding_work_);
156 DWORD bytes_transferred = 0;
157 dword_ptr_t completion_key = 0;
158 LPOVERLAPPED overlapped = 0;
159 ::GetQueuedCompletionStatus(iocp_.handle, &bytes_transferred,
160 &completion_key, &overlapped, gqcs_timeout_);
163 ::InterlockedDecrement(&outstanding_work_);
164 static_cast<win_iocp_operation*>(overlapped)->destroy();
169 if (timer_thread_.get())
170 timer_thread_->join();
173 boost::system::error_code win_iocp_io_context::register_handle(
174 HANDLE handle, boost::system::error_code& ec)
176 if (::CreateIoCompletionPort(handle, iocp_.handle, 0, 0) == 0)
178 DWORD last_error = ::GetLastError();
179 ec = boost::system::error_code(last_error,
180 boost::asio::error::get_system_category());
184 ec = boost::system::error_code();
189 size_t win_iocp_io_context::run(boost::system::error_code& ec)
191 if (::InterlockedExchangeAdd(&outstanding_work_, 0) == 0)
194 ec = boost::system::error_code();
198 win_iocp_thread_info this_thread;
199 thread_call_stack::context ctx(this, this_thread);
202 while (do_one(INFINITE, ec))
203 if (n != (std::numeric_limits<size_t>::max)())
208 size_t win_iocp_io_context::run_one(boost::system::error_code& ec)
210 if (::InterlockedExchangeAdd(&outstanding_work_, 0) == 0)
213 ec = boost::system::error_code();
217 win_iocp_thread_info this_thread;
218 thread_call_stack::context ctx(this, this_thread);
220 return do_one(INFINITE, ec);
223 size_t win_iocp_io_context::wait_one(long usec, boost::system::error_code& ec)
225 if (::InterlockedExchangeAdd(&outstanding_work_, 0) == 0)
228 ec = boost::system::error_code();
232 win_iocp_thread_info this_thread;
233 thread_call_stack::context ctx(this, this_thread);
235 return do_one(usec < 0 ? INFINITE : ((usec - 1) / 1000 + 1), ec);
238 size_t win_iocp_io_context::poll(boost::system::error_code& ec)
240 if (::InterlockedExchangeAdd(&outstanding_work_, 0) == 0)
243 ec = boost::system::error_code();
247 win_iocp_thread_info this_thread;
248 thread_call_stack::context ctx(this, this_thread);
251 while (do_one(0, ec))
252 if (n != (std::numeric_limits<size_t>::max)())
257 size_t win_iocp_io_context::poll_one(boost::system::error_code& ec)
259 if (::InterlockedExchangeAdd(&outstanding_work_, 0) == 0)
262 ec = boost::system::error_code();
266 win_iocp_thread_info this_thread;
267 thread_call_stack::context ctx(this, this_thread);
269 return do_one(0, ec);
272 void win_iocp_io_context::stop()
274 if (::InterlockedExchange(&stopped_, 1) == 0)
276 if (::InterlockedExchange(&stop_event_posted_, 1) == 0)
278 if (!::PostQueuedCompletionStatus(iocp_.handle, 0, 0, 0))
280 DWORD last_error = ::GetLastError();
281 boost::system::error_code ec(last_error,
282 boost::asio::error::get_system_category());
283 boost::asio::detail::throw_error(ec, "pqcs");
289 void win_iocp_io_context::post_deferred_completion(win_iocp_operation* op)
291 // Flag the operation as ready.
294 // Enqueue the operation on the I/O completion port.
295 if (!::PostQueuedCompletionStatus(iocp_.handle, 0, 0, op))
297 // Out of resources. Put on completed queue instead.
298 mutex::scoped_lock lock(dispatch_mutex_);
299 completed_ops_.push(op);
300 ::InterlockedExchange(&dispatch_required_, 1);
304 void win_iocp_io_context::post_deferred_completions(
305 op_queue<win_iocp_operation>& ops)
307 while (win_iocp_operation* op = ops.front())
311 // Flag the operation as ready.
314 // Enqueue the operation on the I/O completion port.
315 if (!::PostQueuedCompletionStatus(iocp_.handle, 0, 0, op))
317 // Out of resources. Put on completed queue instead.
318 mutex::scoped_lock lock(dispatch_mutex_);
319 completed_ops_.push(op);
320 completed_ops_.push(ops);
321 ::InterlockedExchange(&dispatch_required_, 1);
326 void win_iocp_io_context::abandon_operations(
327 op_queue<win_iocp_operation>& ops)
329 while (win_iocp_operation* op = ops.front())
332 ::InterlockedDecrement(&outstanding_work_);
337 void win_iocp_io_context::on_pending(win_iocp_operation* op)
339 if (::InterlockedCompareExchange(&op->ready_, 1, 0) == 1)
341 // Enqueue the operation on the I/O completion port.
342 if (!::PostQueuedCompletionStatus(iocp_.handle,
343 0, overlapped_contains_result, op))
345 // Out of resources. Put on completed queue instead.
346 mutex::scoped_lock lock(dispatch_mutex_);
347 completed_ops_.push(op);
348 ::InterlockedExchange(&dispatch_required_, 1);
353 void win_iocp_io_context::on_completion(win_iocp_operation* op,
354 DWORD last_error, DWORD bytes_transferred)
356 // Flag that the operation is ready for invocation.
359 // Store results in the OVERLAPPED structure.
360 op->Internal = reinterpret_cast<ulong_ptr_t>(
361 &boost::asio::error::get_system_category());
362 op->Offset = last_error;
363 op->OffsetHigh = bytes_transferred;
365 // Enqueue the operation on the I/O completion port.
366 if (!::PostQueuedCompletionStatus(iocp_.handle,
367 0, overlapped_contains_result, op))
369 // Out of resources. Put on completed queue instead.
370 mutex::scoped_lock lock(dispatch_mutex_);
371 completed_ops_.push(op);
372 ::InterlockedExchange(&dispatch_required_, 1);
376 void win_iocp_io_context::on_completion(win_iocp_operation* op,
377 const boost::system::error_code& ec, DWORD bytes_transferred)
379 // Flag that the operation is ready for invocation.
382 // Store results in the OVERLAPPED structure.
383 op->Internal = reinterpret_cast<ulong_ptr_t>(&ec.category());
384 op->Offset = ec.value();
385 op->OffsetHigh = bytes_transferred;
387 // Enqueue the operation on the I/O completion port.
388 if (!::PostQueuedCompletionStatus(iocp_.handle,
389 0, overlapped_contains_result, op))
391 // Out of resources. Put on completed queue instead.
392 mutex::scoped_lock lock(dispatch_mutex_);
393 completed_ops_.push(op);
394 ::InterlockedExchange(&dispatch_required_, 1);
398 size_t win_iocp_io_context::do_one(DWORD msec, boost::system::error_code& ec)
402 // Try to acquire responsibility for dispatching timers and completed ops.
403 if (::InterlockedCompareExchange(&dispatch_required_, 0, 1) == 1)
405 mutex::scoped_lock lock(dispatch_mutex_);
407 // Dispatch pending timers and operations.
408 op_queue<win_iocp_operation> ops;
409 ops.push(completed_ops_);
410 timer_queues_.get_ready_timers(ops);
411 post_deferred_completions(ops);
415 // Get the next operation from the queue.
416 DWORD bytes_transferred = 0;
417 dword_ptr_t completion_key = 0;
418 LPOVERLAPPED overlapped = 0;
420 BOOL ok = ::GetQueuedCompletionStatus(iocp_.handle,
421 &bytes_transferred, &completion_key, &overlapped,
422 msec < gqcs_timeout_ ? msec : gqcs_timeout_);
423 DWORD last_error = ::GetLastError();
427 win_iocp_operation* op = static_cast<win_iocp_operation*>(overlapped);
428 boost::system::error_code result_ec(last_error,
429 boost::asio::error::get_system_category());
431 // We may have been passed the last_error and bytes_transferred in the
432 // OVERLAPPED structure itself.
433 if (completion_key == overlapped_contains_result)
435 result_ec = boost::system::error_code(static_cast<int>(op->Offset),
436 *reinterpret_cast<boost::system::error_category*>(op->Internal));
437 bytes_transferred = op->OffsetHigh;
440 // Otherwise ensure any result has been saved into the OVERLAPPED
444 op->Internal = reinterpret_cast<ulong_ptr_t>(&result_ec.category());
445 op->Offset = result_ec.value();
446 op->OffsetHigh = bytes_transferred;
449 // Dispatch the operation only if ready. The operation may not be ready
450 // if the initiating function (e.g. a call to WSARecv) has not yet
451 // returned. This is because the initiating function still wants access
452 // to the operation's OVERLAPPED structure.
453 if (::InterlockedCompareExchange(&op->ready_, 1, 0) == 1)
455 // Ensure the count of outstanding work is decremented on block exit.
456 work_finished_on_block_exit on_exit = { this };
459 op->complete(this, result_ec, bytes_transferred);
460 ec = boost::system::error_code();
466 if (last_error != WAIT_TIMEOUT)
468 ec = boost::system::error_code(last_error,
469 boost::asio::error::get_system_category());
473 // If we're waiting indefinitely we need to keep going until we get a
475 if (msec == INFINITE)
478 ec = boost::system::error_code();
481 else if (completion_key == wake_for_dispatch)
483 // We have been woken up to try to acquire responsibility for dispatching
484 // timers and completed operations.
488 // Indicate that there is no longer an in-flight stop event.
489 ::InterlockedExchange(&stop_event_posted_, 0);
491 // The stopped_ flag is always checked to ensure that any leftover
492 // stop events from a previous run invocation are ignored.
493 if (::InterlockedExchangeAdd(&stopped_, 0) != 0)
495 // Wake up next thread that is blocked on GetQueuedCompletionStatus.
496 if (::InterlockedExchange(&stop_event_posted_, 1) == 0)
498 if (!::PostQueuedCompletionStatus(iocp_.handle, 0, 0, 0))
500 last_error = ::GetLastError();
501 ec = boost::system::error_code(last_error,
502 boost::asio::error::get_system_category());
507 ec = boost::system::error_code();
514 DWORD win_iocp_io_context::get_gqcs_timeout()
516 OSVERSIONINFOEX osvi;
517 ZeroMemory(&osvi, sizeof(osvi));
518 osvi.dwOSVersionInfoSize = sizeof(osvi);
519 osvi.dwMajorVersion = 6ul;
521 const uint64_t condition_mask = ::VerSetConditionMask(
522 0, VER_MAJORVERSION, VER_GREATER_EQUAL);
524 if (!!::VerifyVersionInfo(&osvi, VER_MAJORVERSION, condition_mask))
527 return default_gqcs_timeout;
530 void win_iocp_io_context::do_add_timer_queue(timer_queue_base& queue)
532 mutex::scoped_lock lock(dispatch_mutex_);
534 timer_queues_.insert(&queue);
536 if (!waitable_timer_.handle)
538 waitable_timer_.handle = ::CreateWaitableTimer(0, FALSE, 0);
539 if (waitable_timer_.handle == 0)
541 DWORD last_error = ::GetLastError();
542 boost::system::error_code ec(last_error,
543 boost::asio::error::get_system_category());
544 boost::asio::detail::throw_error(ec, "timer");
547 LARGE_INTEGER timeout;
548 timeout.QuadPart = -max_timeout_usec;
549 timeout.QuadPart *= 10;
550 ::SetWaitableTimer(waitable_timer_.handle,
551 &timeout, max_timeout_msec, 0, 0, FALSE);
554 if (!timer_thread_.get())
556 timer_thread_function thread_function = { this };
557 timer_thread_.reset(new thread(thread_function, 65536));
561 void win_iocp_io_context::do_remove_timer_queue(timer_queue_base& queue)
563 mutex::scoped_lock lock(dispatch_mutex_);
565 timer_queues_.erase(&queue);
568 void win_iocp_io_context::update_timeout()
570 if (timer_thread_.get())
572 // There's no point updating the waitable timer if the new timeout period
573 // exceeds the maximum timeout. In that case, we might as well wait for the
574 // existing period of the timer to expire.
575 long timeout_usec = timer_queues_.wait_duration_usec(max_timeout_usec);
576 if (timeout_usec < max_timeout_usec)
578 LARGE_INTEGER timeout;
579 timeout.QuadPart = -timeout_usec;
580 timeout.QuadPart *= 10;
581 ::SetWaitableTimer(waitable_timer_.handle,
582 &timeout, max_timeout_msec, 0, 0, FALSE);
587 } // namespace detail
591 #include <boost/asio/detail/pop_options.hpp>
593 #endif // defined(BOOST_ASIO_HAS_IOCP)
595 #endif // BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_IO_CONTEXT_IPP