From 85e4e99e7f7ec37496def5272efd2d4674fed1d6 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 23 Jul 2013 19:19:11 +0800 Subject: [PATCH] [Win] Poll events with GetQueuedCompletionStatus, fixes #41. --- common/node_bindings_win.cc | 39 ++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/common/node_bindings_win.cc b/common/node_bindings_win.cc index 6cfa12c3..f9177d78 100644 --- a/common/node_bindings_win.cc +++ b/common/node_bindings_win.cc @@ -7,8 +7,11 @@ #include #include "base/logging.h" -#include "vendor/node/src/node.h" -#include "vendor/node/src/node_internals.h" + +extern "C" { +#include "vendor/node/deps/uv/include/uv-private/ngx-queue.h" +#include "vendor/node/deps/uv/src/win/internal.h" +} namespace atom { @@ -20,7 +23,37 @@ NodeBindingsWin::~NodeBindingsWin() { } void NodeBindingsWin::PollEvents() { - Sleep(50); + // Unlike Unix, in which we can just rely on one backend fd to determine + // whether we should iterate libuv loop, on Window, IOCP is just one part + // of the libuv loop, we should also check whether we have other types of + // events. + bool block = uv_loop_->idle_handles == NULL && + uv_loop_->pending_reqs_tail == NULL && + uv_loop_->endgame_handles == NULL && + !uv_loop_->stop_flag && + (uv_loop_->active_handles > 0 || + !ngx_queue_empty(&uv_loop_->active_reqs)); + + // When there is no other types of events, we block on the IOCP. + if (block) { + DWORD bytes, timeout; + ULONG_PTR key; + OVERLAPPED* overlapped; + + timeout = uv_get_poll_timeout(uv_loop_); + GetQueuedCompletionStatus(uv_loop_->iocp, + &bytes, + &key, + &overlapped, + timeout); + + // Give the event back so libuv can deal with it. + if (overlapped != NULL) + PostQueuedCompletionStatus(uv_loop_->iocp, + bytes, + key, + overlapped); + } } // static -- 2.43.0