Permalink
Browse files

windows: fix uv_stop in ONCE and NOWAIT modes

Same as the fix in 492efb9 but this time for uv-win.
  • Loading branch information...
saghul authored and bnoordhuis committed Feb 25, 2013
1 parent 492efb9 commit 4b957482ba1ba12e40933ef1815ee6a098450280
Showing with 29 additions and 6 deletions.
  1. +11 −6 src/win/core.c
  2. +18 −0 test/test-loop-stop.c
View
@@ -269,11 +269,8 @@ int uv_run(uv_loop_t *loop, uv_run_mode mode) {
if (!uv__loop_alive(loop))
return 0;
- do {
- if (loop->stop_flag) {
- loop->stop_flag = 0;
- return uv__loop_alive(loop);
- }
+ r = uv__loop_alive(loop);
+ while (r != 0 && loop->stop_flag == 0) {
uv_update_time(loop);
uv_process_timers(loop);
@@ -298,7 +295,15 @@ int uv_run(uv_loop_t *loop, uv_run_mode mode) {
uv_check_invoke(loop);
r = uv__loop_alive(loop);
- } while (r && !(mode & (UV_RUN_ONCE | UV_RUN_NOWAIT)));
+ if (mode & (UV_RUN_ONCE | UV_RUN_NOWAIT))
+ break;
+ }
+
+ /* The if statement lets the compiler compile it to a conditional store.
+ * Avoids dirtying a cache line.
+ */
+ if (loop->stop_flag != 0)
+ loop->stop_flag = 0;
return r;
}
View
@@ -22,11 +22,22 @@
#include "uv.h"
#include "task.h"
+static uv_prepare_t prepare_handle;
static uv_timer_t timer_handle;
+static int prepare_called = 0;
static int timer_called = 0;
static int num_ticks = 10;
+static void prepare_cb(uv_prepare_t* handle, int status) {
+ ASSERT(handle == &prepare_handle);
+ ASSERT(status == 0);
+ prepare_called++;
+ if (prepare_called == num_ticks)
+ uv_prepare_stop(handle);
+}
+
+
static void timer_cb(uv_timer_t* handle, int status) {
ASSERT(handle == &timer_handle);
ASSERT(status == 0);
@@ -40,16 +51,23 @@ static void timer_cb(uv_timer_t* handle, int status) {
TEST_IMPL(loop_stop) {
int r;
+ uv_prepare_init(uv_default_loop(), &prepare_handle);
+ uv_prepare_start(&prepare_handle, prepare_cb);
uv_timer_init(uv_default_loop(), &timer_handle);
uv_timer_start(&timer_handle, timer_cb, 100, 100);
r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
ASSERT(r != 0);
ASSERT(timer_called == 1);
+ r = uv_run(uv_default_loop(), UV_RUN_NOWAIT);
+ ASSERT(r != 0);
+ ASSERT(prepare_called == 3);
+
r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
ASSERT(r == 0);
ASSERT(timer_called == 10);
+ ASSERT(prepare_called == 10);
return 0;
}

0 comments on commit 4b95748

Please sign in to comment.