summaryrefslogtreecommitdiff
path: root/boost/asio/detail/impl/win_iocp_io_service.ipp
diff options
context:
space:
mode:
Diffstat (limited to 'boost/asio/detail/impl/win_iocp_io_service.ipp')
-rw-r--r--boost/asio/detail/impl/win_iocp_io_service.ipp87
1 files changed, 59 insertions, 28 deletions
diff --git a/boost/asio/detail/impl/win_iocp_io_service.ipp b/boost/asio/detail/impl/win_iocp_io_service.ipp
index 72f4af7524..fbc1cd0376 100644
--- a/boost/asio/detail/impl/win_iocp_io_service.ipp
+++ b/boost/asio/detail/impl/win_iocp_io_service.ipp
@@ -2,7 +2,7 @@
// detail/impl/win_iocp_io_service.ipp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
-// Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+// Copyright (c) 2003-2014 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -19,11 +19,12 @@
#if defined(BOOST_ASIO_HAS_IOCP)
-#include <boost/limits.hpp>
#include <boost/asio/error.hpp>
#include <boost/asio/io_service.hpp>
+#include <boost/asio/detail/cstdint.hpp>
#include <boost/asio/detail/handler_alloc_helpers.hpp>
#include <boost/asio/detail/handler_invoke_helpers.hpp>
+#include <boost/asio/detail/limits.hpp>
#include <boost/asio/detail/throw_error.hpp>
#include <boost/asio/detail/win_iocp_io_service.hpp>
@@ -68,13 +69,16 @@ win_iocp_io_service::win_iocp_io_service(
iocp_(),
outstanding_work_(0),
stopped_(0),
+ stop_event_posted_(0),
shutdown_(0),
+ gqcs_timeout_(get_gqcs_timeout()),
dispatch_required_(0)
{
BOOST_ASIO_HANDLER_TRACKING_INIT;
iocp_.handle = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, 0,
- static_cast<DWORD>((std::min<size_t>)(concurrency_hint, DWORD(~0))));
+ static_cast<DWORD>(concurrency_hint < DWORD(~0)
+ ? concurrency_hint : DWORD(~0)));
if (!iocp_.handle)
{
DWORD last_error = ::GetLastError();
@@ -115,7 +119,7 @@ void win_iocp_io_service::shutdown_service()
dword_ptr_t completion_key = 0;
LPOVERLAPPED overlapped = 0;
::GetQueuedCompletionStatus(iocp_.handle, &bytes_transferred,
- &completion_key, &overlapped, gqcs_timeout);
+ &completion_key, &overlapped, gqcs_timeout_);
if (overlapped)
{
::InterlockedDecrement(&outstanding_work_);
@@ -148,12 +152,13 @@ size_t win_iocp_io_service::run(boost::system::error_code& ec)
{
if (::InterlockedExchangeAdd(&outstanding_work_, 0) == 0)
{
- InterlockedExchange(&stopped_, 1);
+ stop();
ec = boost::system::error_code();
return 0;
}
- call_stack<win_iocp_io_service>::context ctx(this);
+ win_iocp_thread_info this_thread;
+ thread_call_stack::context ctx(this, this_thread);
size_t n = 0;
while (do_one(true, ec))
@@ -166,12 +171,13 @@ size_t win_iocp_io_service::run_one(boost::system::error_code& ec)
{
if (::InterlockedExchangeAdd(&outstanding_work_, 0) == 0)
{
- InterlockedExchange(&stopped_, 1);
+ stop();
ec = boost::system::error_code();
return 0;
}
- call_stack<win_iocp_io_service>::context ctx(this);
+ win_iocp_thread_info this_thread;
+ thread_call_stack::context ctx(this, this_thread);
return do_one(true, ec);
}
@@ -180,12 +186,13 @@ size_t win_iocp_io_service::poll(boost::system::error_code& ec)
{
if (::InterlockedExchangeAdd(&outstanding_work_, 0) == 0)
{
- InterlockedExchange(&stopped_, 1);
+ stop();
ec = boost::system::error_code();
return 0;
}
- call_stack<win_iocp_io_service>::context ctx(this);
+ win_iocp_thread_info this_thread;
+ thread_call_stack::context ctx(this, this_thread);
size_t n = 0;
while (do_one(false, ec))
@@ -198,12 +205,13 @@ size_t win_iocp_io_service::poll_one(boost::system::error_code& ec)
{
if (::InterlockedExchangeAdd(&outstanding_work_, 0) == 0)
{
- InterlockedExchange(&stopped_, 1);
+ stop();
ec = boost::system::error_code();
return 0;
}
- call_stack<win_iocp_io_service>::context ctx(this);
+ win_iocp_thread_info this_thread;
+ thread_call_stack::context ctx(this, this_thread);
return do_one(false, ec);
}
@@ -212,12 +220,15 @@ void win_iocp_io_service::stop()
{
if (::InterlockedExchange(&stopped_, 1) == 0)
{
- if (!::PostQueuedCompletionStatus(iocp_.handle, 0, 0, 0))
+ if (::InterlockedExchange(&stop_event_posted_, 1) == 0)
{
- DWORD last_error = ::GetLastError();
- boost::system::error_code ec(last_error,
- boost::asio::error::get_system_category());
- boost::asio::detail::throw_error(ec, "pqcs");
+ if (!::PostQueuedCompletionStatus(iocp_.handle, 0, 0, 0))
+ {
+ DWORD last_error = ::GetLastError();
+ boost::system::error_code ec(last_error,
+ boost::asio::error::get_system_category());
+ boost::asio::detail::throw_error(ec, "pqcs");
+ }
}
}
}
@@ -228,8 +239,7 @@ void win_iocp_io_service::post_deferred_completion(win_iocp_operation* op)
op->ready_ = 1;
// Enqueue the operation on the I/O completion port.
- if (!::PostQueuedCompletionStatus(iocp_.handle,
- 0, overlapped_contains_result, op))
+ if (!::PostQueuedCompletionStatus(iocp_.handle, 0, 0, op))
{
// Out of resources. Put on completed queue instead.
mutex::scoped_lock lock(dispatch_mutex_);
@@ -249,8 +259,7 @@ void win_iocp_io_service::post_deferred_completions(
op->ready_ = 1;
// Enqueue the operation on the I/O completion port.
- if (!::PostQueuedCompletionStatus(iocp_.handle,
- 0, overlapped_contains_result, op))
+ if (!::PostQueuedCompletionStatus(iocp_.handle, 0, 0, op))
{
// Out of resources. Put on completed queue instead.
mutex::scoped_lock lock(dispatch_mutex_);
@@ -356,7 +365,7 @@ size_t win_iocp_io_service::do_one(bool block, boost::system::error_code& ec)
LPOVERLAPPED overlapped = 0;
::SetLastError(0);
BOOL ok = ::GetQueuedCompletionStatus(iocp_.handle, &bytes_transferred,
- &completion_key, &overlapped, block ? gqcs_timeout : 0);
+ &completion_key, &overlapped, block ? gqcs_timeout_ : 0);
DWORD last_error = ::GetLastError();
if (overlapped)
@@ -421,17 +430,23 @@ size_t win_iocp_io_service::do_one(bool block, boost::system::error_code& ec)
}
else
{
+ // Indicate that there is no longer an in-flight stop event.
+ ::InterlockedExchange(&stop_event_posted_, 0);
+
// The stopped_ flag is always checked to ensure that any leftover
- // interrupts from a previous run invocation are ignored.
+ // stop events from a previous run invocation are ignored.
if (::InterlockedExchangeAdd(&stopped_, 0) != 0)
{
// Wake up next thread that is blocked on GetQueuedCompletionStatus.
- if (!::PostQueuedCompletionStatus(iocp_.handle, 0, 0, 0))
+ if (::InterlockedExchange(&stop_event_posted_, 1) == 0)
{
- last_error = ::GetLastError();
- ec = boost::system::error_code(last_error,
- boost::asio::error::get_system_category());
- return 0;
+ if (!::PostQueuedCompletionStatus(iocp_.handle, 0, 0, 0))
+ {
+ last_error = ::GetLastError();
+ ec = boost::system::error_code(last_error,
+ boost::asio::error::get_system_category());
+ return 0;
+ }
}
ec = boost::system::error_code();
@@ -441,6 +456,22 @@ size_t win_iocp_io_service::do_one(bool block, boost::system::error_code& ec)
}
}
+DWORD win_iocp_io_service::get_gqcs_timeout()
+{
+ OSVERSIONINFOEX osvi;
+ ZeroMemory(&osvi, sizeof(osvi));
+ osvi.dwOSVersionInfoSize = sizeof(osvi);
+ osvi.dwMajorVersion = 6ul;
+
+ const uint64_t condition_mask = ::VerSetConditionMask(
+ 0, VER_MAJORVERSION, VER_GREATER_EQUAL);
+
+ if (!!::VerifyVersionInfo(&osvi, VER_MAJORVERSION, condition_mask))
+ return INFINITE;
+
+ return default_gqcs_timeout;
+}
+
void win_iocp_io_service::do_add_timer_queue(timer_queue_base& queue)
{
mutex::scoped_lock lock(dispatch_mutex_);