2 // detail/impl/win_iocp_io_context.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2022 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()
123 void win_iocp_io_context::shutdown()
125 ::InterlockedExchange(&shutdown_, 1);
127 if (timer_thread_.get())
129 LARGE_INTEGER timeout;
130 timeout.QuadPart = 1;
131 ::SetWaitableTimer(waitable_timer_.handle, &timeout, 1, 0, 0, FALSE);
139 ::InterlockedDecrement(&outstanding_work_);
142 while (::InterlockedExchangeAdd(&outstanding_work_, 0) > 0)
144 op_queue<win_iocp_operation> ops;
145 timer_queues_.get_all_timers(ops);
146 ops.push(completed_ops_);
149 while (win_iocp_operation* op = ops.front())
152 ::InterlockedDecrement(&outstanding_work_);
158 DWORD bytes_transferred = 0;
159 dword_ptr_t completion_key = 0;
160 LPOVERLAPPED overlapped = 0;
161 ::GetQueuedCompletionStatus(iocp_.handle, &bytes_transferred,
162 &completion_key, &overlapped, gqcs_timeout_);
165 ::InterlockedDecrement(&outstanding_work_);
166 static_cast<win_iocp_operation*>(overlapped)->destroy();
171 if (timer_thread_.get())
172 timer_thread_->join();
175 boost::system::error_code win_iocp_io_context::register_handle(
176 HANDLE handle, boost::system::error_code& ec)
178 if (::CreateIoCompletionPort(handle, iocp_.handle, 0, 0) == 0)
180 DWORD last_error = ::GetLastError();
181 ec = boost::system::error_code(last_error,
182 boost::asio::error::get_system_category());
186 ec = boost::system::error_code();
191 size_t win_iocp_io_context::run(boost::system::error_code& ec)
193 if (::InterlockedExchangeAdd(&outstanding_work_, 0) == 0)
196 ec = boost::system::error_code();
200 win_iocp_thread_info this_thread;
201 thread_call_stack::context ctx(this, this_thread);
204 while (do_one(INFINITE, this_thread, ec))
205 if (n != (std::numeric_limits<size_t>::max)())
210 size_t win_iocp_io_context::run_one(boost::system::error_code& ec)
212 if (::InterlockedExchangeAdd(&outstanding_work_, 0) == 0)
215 ec = boost::system::error_code();
219 win_iocp_thread_info this_thread;
220 thread_call_stack::context ctx(this, this_thread);
222 return do_one(INFINITE, this_thread, ec);
225 size_t win_iocp_io_context::wait_one(long usec, boost::system::error_code& ec)
227 if (::InterlockedExchangeAdd(&outstanding_work_, 0) == 0)
230 ec = boost::system::error_code();
234 win_iocp_thread_info this_thread;
235 thread_call_stack::context ctx(this, this_thread);
237 return do_one(usec < 0 ? INFINITE : ((usec - 1) / 1000 + 1), this_thread, ec);
240 size_t win_iocp_io_context::poll(boost::system::error_code& ec)
242 if (::InterlockedExchangeAdd(&outstanding_work_, 0) == 0)
245 ec = boost::system::error_code();
249 win_iocp_thread_info this_thread;
250 thread_call_stack::context ctx(this, this_thread);
253 while (do_one(0, this_thread, ec))
254 if (n != (std::numeric_limits<size_t>::max)())
259 size_t win_iocp_io_context::poll_one(boost::system::error_code& ec)
261 if (::InterlockedExchangeAdd(&outstanding_work_, 0) == 0)
264 ec = boost::system::error_code();
268 win_iocp_thread_info this_thread;
269 thread_call_stack::context ctx(this, this_thread);
271 return do_one(0, this_thread, ec);
274 void win_iocp_io_context::stop()
276 if (::InterlockedExchange(&stopped_, 1) == 0)
278 if (::InterlockedExchange(&stop_event_posted_, 1) == 0)
280 if (!::PostQueuedCompletionStatus(iocp_.handle, 0, 0, 0))
282 DWORD last_error = ::GetLastError();
283 boost::system::error_code ec(last_error,
284 boost::asio::error::get_system_category());
285 boost::asio::detail::throw_error(ec, "pqcs");
291 bool win_iocp_io_context::can_dispatch()
293 return thread_call_stack::contains(this) != 0;
296 void win_iocp_io_context::capture_current_exception()
298 if (thread_info_base* this_thread = thread_call_stack::contains(this))
299 this_thread->capture_current_exception();
302 void win_iocp_io_context::post_deferred_completion(win_iocp_operation* op)
304 // Flag the operation as ready.
307 // Enqueue the operation on the I/O completion port.
308 if (!::PostQueuedCompletionStatus(iocp_.handle, 0, 0, op))
310 // Out of resources. Put on completed queue instead.
311 mutex::scoped_lock lock(dispatch_mutex_);
312 completed_ops_.push(op);
313 ::InterlockedExchange(&dispatch_required_, 1);
317 void win_iocp_io_context::post_deferred_completions(
318 op_queue<win_iocp_operation>& ops)
320 while (win_iocp_operation* op = ops.front())
324 // Flag the operation as ready.
327 // Enqueue the operation on the I/O completion port.
328 if (!::PostQueuedCompletionStatus(iocp_.handle, 0, 0, op))
330 // Out of resources. Put on completed queue instead.
331 mutex::scoped_lock lock(dispatch_mutex_);
332 completed_ops_.push(op);
333 completed_ops_.push(ops);
334 ::InterlockedExchange(&dispatch_required_, 1);
339 void win_iocp_io_context::abandon_operations(
340 op_queue<win_iocp_operation>& ops)
342 while (win_iocp_operation* op = ops.front())
345 ::InterlockedDecrement(&outstanding_work_);
350 void win_iocp_io_context::on_pending(win_iocp_operation* op)
352 if (::InterlockedCompareExchange(&op->ready_, 1, 0) == 1)
354 // Enqueue the operation on the I/O completion port.
355 if (!::PostQueuedCompletionStatus(iocp_.handle,
356 0, overlapped_contains_result, op))
358 // Out of resources. Put on completed queue instead.
359 mutex::scoped_lock lock(dispatch_mutex_);
360 completed_ops_.push(op);
361 ::InterlockedExchange(&dispatch_required_, 1);
366 void win_iocp_io_context::on_completion(win_iocp_operation* op,
367 DWORD last_error, DWORD bytes_transferred)
369 // Flag that the operation is ready for invocation.
372 // Store results in the OVERLAPPED structure.
373 op->Internal = reinterpret_cast<ulong_ptr_t>(
374 &boost::asio::error::get_system_category());
375 op->Offset = last_error;
376 op->OffsetHigh = bytes_transferred;
378 // Enqueue the operation on the I/O completion port.
379 if (!::PostQueuedCompletionStatus(iocp_.handle,
380 0, overlapped_contains_result, op))
382 // Out of resources. Put on completed queue instead.
383 mutex::scoped_lock lock(dispatch_mutex_);
384 completed_ops_.push(op);
385 ::InterlockedExchange(&dispatch_required_, 1);
389 void win_iocp_io_context::on_completion(win_iocp_operation* op,
390 const boost::system::error_code& ec, DWORD bytes_transferred)
392 // Flag that the operation is ready for invocation.
395 // Store results in the OVERLAPPED structure.
396 op->Internal = reinterpret_cast<ulong_ptr_t>(&ec.category());
397 op->Offset = ec.value();
398 op->OffsetHigh = bytes_transferred;
400 // Enqueue the operation on the I/O completion port.
401 if (!::PostQueuedCompletionStatus(iocp_.handle,
402 0, overlapped_contains_result, op))
404 // Out of resources. Put on completed queue instead.
405 mutex::scoped_lock lock(dispatch_mutex_);
406 completed_ops_.push(op);
407 ::InterlockedExchange(&dispatch_required_, 1);
411 size_t win_iocp_io_context::do_one(DWORD msec,
412 win_iocp_thread_info& this_thread, boost::system::error_code& ec)
416 // Try to acquire responsibility for dispatching timers and completed ops.
417 if (::InterlockedCompareExchange(&dispatch_required_, 0, 1) == 1)
419 mutex::scoped_lock lock(dispatch_mutex_);
421 // Dispatch pending timers and operations.
422 op_queue<win_iocp_operation> ops;
423 ops.push(completed_ops_);
424 timer_queues_.get_ready_timers(ops);
425 post_deferred_completions(ops);
429 // Get the next operation from the queue.
430 DWORD bytes_transferred = 0;
431 dword_ptr_t completion_key = 0;
432 LPOVERLAPPED overlapped = 0;
434 BOOL ok = ::GetQueuedCompletionStatus(iocp_.handle,
435 &bytes_transferred, &completion_key, &overlapped,
436 msec < gqcs_timeout_ ? msec : gqcs_timeout_);
437 DWORD last_error = ::GetLastError();
441 win_iocp_operation* op = static_cast<win_iocp_operation*>(overlapped);
442 boost::system::error_code result_ec(last_error,
443 boost::asio::error::get_system_category());
445 // We may have been passed the last_error and bytes_transferred in the
446 // OVERLAPPED structure itself.
447 if (completion_key == overlapped_contains_result)
449 result_ec = boost::system::error_code(static_cast<int>(op->Offset),
450 *reinterpret_cast<boost::system::error_category*>(op->Internal));
451 bytes_transferred = op->OffsetHigh;
454 // Otherwise ensure any result has been saved into the OVERLAPPED
458 op->Internal = reinterpret_cast<ulong_ptr_t>(&result_ec.category());
459 op->Offset = result_ec.value();
460 op->OffsetHigh = bytes_transferred;
463 // Dispatch the operation only if ready. The operation may not be ready
464 // if the initiating function (e.g. a call to WSARecv) has not yet
465 // returned. This is because the initiating function still wants access
466 // to the operation's OVERLAPPED structure.
467 if (::InterlockedCompareExchange(&op->ready_, 1, 0) == 1)
469 // Ensure the count of outstanding work is decremented on block exit.
470 work_finished_on_block_exit on_exit = { this };
473 op->complete(this, result_ec, bytes_transferred);
474 this_thread.rethrow_pending_exception();
475 ec = boost::system::error_code();
481 if (last_error != WAIT_TIMEOUT)
483 ec = boost::system::error_code(last_error,
484 boost::asio::error::get_system_category());
488 // If we're waiting indefinitely we need to keep going until we get a
490 if (msec == INFINITE)
493 ec = boost::system::error_code();
496 else if (completion_key == wake_for_dispatch)
498 // We have been woken up to try to acquire responsibility for dispatching
499 // timers and completed operations.
503 // Indicate that there is no longer an in-flight stop event.
504 ::InterlockedExchange(&stop_event_posted_, 0);
506 // The stopped_ flag is always checked to ensure that any leftover
507 // stop events from a previous run invocation are ignored.
508 if (::InterlockedExchangeAdd(&stopped_, 0) != 0)
510 // Wake up next thread that is blocked on GetQueuedCompletionStatus.
511 if (::InterlockedExchange(&stop_event_posted_, 1) == 0)
513 if (!::PostQueuedCompletionStatus(iocp_.handle, 0, 0, 0))
515 last_error = ::GetLastError();
516 ec = boost::system::error_code(last_error,
517 boost::asio::error::get_system_category());
522 ec = boost::system::error_code();
529 DWORD win_iocp_io_context::get_gqcs_timeout()
531 OSVERSIONINFOEX osvi;
532 ZeroMemory(&osvi, sizeof(osvi));
533 osvi.dwOSVersionInfoSize = sizeof(osvi);
534 osvi.dwMajorVersion = 6ul;
536 const uint64_t condition_mask = ::VerSetConditionMask(
537 0, VER_MAJORVERSION, VER_GREATER_EQUAL);
539 if (!!::VerifyVersionInfo(&osvi, VER_MAJORVERSION, condition_mask))
542 return default_gqcs_timeout;
545 void win_iocp_io_context::do_add_timer_queue(timer_queue_base& queue)
547 mutex::scoped_lock lock(dispatch_mutex_);
549 timer_queues_.insert(&queue);
551 if (!waitable_timer_.handle)
553 waitable_timer_.handle = ::CreateWaitableTimer(0, FALSE, 0);
554 if (waitable_timer_.handle == 0)
556 DWORD last_error = ::GetLastError();
557 boost::system::error_code ec(last_error,
558 boost::asio::error::get_system_category());
559 boost::asio::detail::throw_error(ec, "timer");
562 LARGE_INTEGER timeout;
563 timeout.QuadPart = -max_timeout_usec;
564 timeout.QuadPart *= 10;
565 ::SetWaitableTimer(waitable_timer_.handle,
566 &timeout, max_timeout_msec, 0, 0, FALSE);
569 if (!timer_thread_.get())
571 timer_thread_function thread_function = { this };
572 timer_thread_.reset(new thread(thread_function, 65536));
576 void win_iocp_io_context::do_remove_timer_queue(timer_queue_base& queue)
578 mutex::scoped_lock lock(dispatch_mutex_);
580 timer_queues_.erase(&queue);
583 void win_iocp_io_context::update_timeout()
585 if (timer_thread_.get())
587 // There's no point updating the waitable timer if the new timeout period
588 // exceeds the maximum timeout. In that case, we might as well wait for the
589 // existing period of the timer to expire.
590 long timeout_usec = timer_queues_.wait_duration_usec(max_timeout_usec);
591 if (timeout_usec < max_timeout_usec)
593 LARGE_INTEGER timeout;
594 timeout.QuadPart = -timeout_usec;
595 timeout.QuadPart *= 10;
596 ::SetWaitableTimer(waitable_timer_.handle,
597 &timeout, max_timeout_msec, 0, 0, FALSE);
602 } // namespace detail
606 #include <boost/asio/detail/pop_options.hpp>
608 #endif // defined(BOOST_ASIO_HAS_IOCP)
610 #endif // BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_IO_CONTEXT_IPP