diff --git a/src/win/core.c b/src/win/core.c index e2d5a32c..ed870efe 100644 --- a/src/win/core.c +++ b/src/win/core.c @@ -85,20 +85,24 @@ void uv_unref() { } -static void uv_poll() { +static void uv_poll(int block) { BOOL success; - DWORD bytes; + DWORD bytes, timeout; ULONG_PTR key; OVERLAPPED* overlapped; uv_req_t* req; + if (block) { + timeout = uv_get_poll_timeout(); + } else { + timeout = 0; + } + success = GetQueuedCompletionStatus(LOOP->iocp, &bytes, &key, &overlapped, - uv_get_poll_timeout()); - - uv_update_time(); + timeout); if (overlapped) { /* Package was dequeued */ @@ -118,30 +122,30 @@ static void uv_poll() { int uv_run() { - while (1) { + while (LOOP->refs > 0) { uv_update_time(); uv_process_timers(); - /* Terrible: please fix me! */ - while (LOOP->refs > 0 && - (LOOP->idle_handles || LOOP->pending_reqs_tail || LOOP->endgame_handles)) { - /* Terrible: please fix me! */ - while (LOOP->pending_reqs_tail || LOOP->endgame_handles) { - uv_process_endgames(); - uv_process_reqs(); - } - - /* Call idle callbacks */ + /* Call idle callbacks if nothing to do. */ + if (LOOP->pending_reqs_tail == NULL && LOOP->endgame_handles == NULL) { uv_idle_invoke(); } + /* Completely flush all pending reqs and endgames. */ + /* We do even when we just called the idle callbacks because those may */ + /* have closed handles or started requests that short-circuited. */ + while (LOOP->pending_reqs_tail || LOOP->endgame_handles) { + uv_process_endgames(); + uv_process_reqs(); + } + if (LOOP->refs <= 0) { break; } uv_prepare_invoke(); - uv_poll(); + uv_poll(LOOP->idle_handles == NULL); uv_check_invoke(); } diff --git a/src/win/handle.c b/src/win/handle.c index 2c3acb79..cc6d2f88 100644 --- a/src/win/handle.c +++ b/src/win/handle.c @@ -45,7 +45,7 @@ static void uv_close_error(uv_handle_t* handle, uv_err_t e) { uv_pipe_t* pipe; if (handle->flags & UV_HANDLE_CLOSING) { - return 0; + return; } handle->error = e;