From bb7a3686ba54dbe578931db8f054e76a2e879970 Mon Sep 17 00:00:00 2001 From: Bartosz Sosnowski Date: Tue, 26 Jul 2016 16:14:30 +0200 Subject: [PATCH] win: evaluate timers when system wakes up When Windows resumes after sleep GetQueuedCompletionStatus timeout is not updated. This commit adds a method for signaling all loops to wake up and update their timers. Fixes: https://github.com/nodejs/node/issues/6763 --- include/uv-win.h | 4 +++- src/win/core.c | 50 +++++++++++++++++++++++++++++++++++++++++++--- src/win/internal.h | 5 +++++ 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/include/uv-win.h b/include/uv-win.h index a75dba8d1c5..1564dea924c 100644 --- a/include/uv-win.h +++ b/include/uv-win.h @@ -343,7 +343,9 @@ RB_HEAD(uv_timer_tree_s, uv_timer_s); /* Threadpool */ \ void* wq[2]; \ uv_mutex_t wq_mutex; \ - uv_async_t wq_async; + uv_async_t wq_async; \ + /* Queue of all active uv_loops */ \ + void* uv_loops[2]; #define UV_REQ_TYPE_PRIVATE \ /* TODO: remove the req suffix */ \ diff --git a/src/win/core.c b/src/win/core.c index ba306ebc083..6e839f954f5 100644 --- a/src/win/core.c +++ b/src/win/core.c @@ -31,6 +31,7 @@ #include "uv.h" #include "internal.h" +#include "queue.h" #include "handle-inl.h" #include "req-inl.h" @@ -80,6 +81,38 @@ static void uv__crt_invalid_parameter_handler(const wchar_t* expression, } #endif +static QUEUE uv_loops; +static uv_mutex_t uv_loops_lock; + +static void uv__loops_init() { + uv_mutex_init(&uv_loops_lock); + QUEUE_INIT(&uv_loops); +} + +static void uv__loops_add(uv_loop_t* loop) { + uv_mutex_lock(&uv_loops_lock); + QUEUE_INIT(&loop->uv_loops); + QUEUE_INSERT_TAIL(&uv_loops, &loop->uv_loops); + uv_mutex_unlock(&uv_loops_lock); +} + +static void uv__loops_remove(uv_loop_t* loop) { + uv_mutex_lock(&uv_loops_lock); + QUEUE_REMOVE(&loop->uv_loops); + uv_mutex_unlock(&uv_loops_lock); +} + +void uv_wake_all_loops() { + QUEUE *q; + uv_loop_t* loop; + uv_mutex_lock(&uv_loops_lock); + QUEUE_FOREACH(q, &uv_loops) { + loop = (uv_loop_t*)QUEUE_DATA(q, uv_loop_t, uv_loops); + if (loop->iocp != INVALID_HANDLE_VALUE) + PostQueuedCompletionStatus(loop->iocp, 0, 0, NULL); + } + uv_mutex_unlock(&uv_loops_lock); +} static void uv_init(void) { /* Tell Windows that we will handle critical errors. */ @@ -101,6 +134,9 @@ static void uv_init(void) { _CrtSetReportHook(uv__crt_dbg_report_handler); #endif + /* Initialize tracking of all uv loops */ + uv__loops_init(); + /* Fetch winapi function pointers. This must be done first because other * initialization code might need these function pointers to be loaded. */ @@ -178,6 +214,8 @@ int uv_loop_init(uv_loop_t* loop) { uv__handle_unref(&loop->wq_async); loop->wq_async.flags |= UV__HANDLE_INTERNAL; + uv__loops_add(loop); + return 0; fail_async_init: @@ -199,6 +237,8 @@ void uv__once_init(void) { void uv__loop_close(uv_loop_t* loop) { size_t i; + uv__loops_remove(loop); + /* close the async handle without needing an extra loop iteration */ assert(!loop->wq_async.async_sent); loop->wq_async.close_cb = NULL; @@ -323,9 +363,13 @@ static void uv_poll_ex(uv_loop_t* loop, DWORD timeout) { if (success) { for (i = 0; i < count; i++) { - /* Package was dequeued */ - req = uv_overlapped_to_req(overlappeds[i].lpOverlapped); - uv_insert_pending_req(loop, req); + /* Package was dequeued, but see if it is not a empty package + * meant only to wake us up. + */ + if (overlappeds[i].lpOverlapped) { + req = uv_overlapped_to_req(overlappeds[i].lpOverlapped); + uv_insert_pending_req(loop, req); + } } /* Some time might have passed waiting for I/O, diff --git a/src/win/internal.h b/src/win/internal.h index 0a7c9404fa3..797ba814e5a 100644 --- a/src/win/internal.h +++ b/src/win/internal.h @@ -381,4 +381,9 @@ extern int uv_tcp_non_ifs_lsp_ipv6; extern struct sockaddr_in uv_addr_ip4_any_; extern struct sockaddr_in6 uv_addr_ip6_any_; +/* + * Wake all loops with fake message + */ +void uv_wake_all_loops(); + #endif /* UV_WIN_INTERNAL_H_ */