unix, windows: add uv_stop, stop running event loop

This commit is contained in:
Saúl Ibarra Corretgé 2013-01-16 09:36:20 +01:00 committed by Ben Noordhuis
parent 79880121ce
commit bb3d1e24da
9 changed files with 90 additions and 2 deletions

View File

@ -85,6 +85,7 @@ TESTS= \
test/test-ipc.o \
test/test-ipc-send-recv.o \
test/test-loop-handles.o \
test/test-loop-stop.o \
test/test-multiple-listen.o \
test/test-mutexes.o \
test/test-pass-always.o \

View File

@ -258,6 +258,14 @@ UV_EXTERN uv_loop_t* uv_default_loop(void);
*/
UV_EXTERN int uv_run(uv_loop_t*, uv_run_mode mode);
/*
* This function will stop the event loop by forcing uv_run to end
* as soon as possible, but not sooner than the next loop iteration.
* If this function was called before blocking for i/o, the loop won't
* block for i/o on this iteration.
*/
UV_EXTERN void uv_stop(uv_loop_t*);
/*
* Manually modify the event loop's reference count. Useful if the user wants
* to have a handle or timeout that doesn't keep the loop alive.
@ -1934,6 +1942,8 @@ struct uv_loop_s {
unsigned int active_handles;
ngx_queue_t handle_queue;
ngx_queue_t active_reqs;
/* Internal flag to signal loop stop */
unsigned int stop_flag;
UV_LOOP_PRIVATE_FIELDS
};

View File

@ -280,18 +280,26 @@ static int uv__loop_alive(uv_loop_t* loop) {
int uv_run(uv_loop_t* loop, uv_run_mode mode) {
int r;
int r, timeout;
if (!uv__loop_alive(loop))
return 0;
do {
if (loop->stop_flag) {
loop->stop_flag = 0;
return uv__loop_alive(loop);
}
uv__update_time(loop);
uv__run_timers(loop);
uv__run_idle(loop);
uv__run_prepare(loop);
uv__run_pending(loop);
uv__io_poll(loop, (mode & UV_RUN_NOWAIT ? 0 : uv_backend_timeout(loop)));
if (mode & UV_RUN_NOWAIT || loop->stop_flag)
timeout = 0;
else
timeout = uv_backend_timeout(loop);
uv__io_poll(loop, timeout);
uv__run_check(loop);
uv__run_closing_handles(loop);
r = uv__loop_alive(loop);

View File

@ -57,6 +57,7 @@ int uv__loop_init(uv_loop_t* loop, int default_loop) {
loop->emfile_fd = -1;
loop->timer_counter = 0;
loop->stop_flag = 0;
if (uv__platform_loop_init(loop, default_loop))
return -1;

View File

@ -377,3 +377,8 @@ void uv_ref(uv_handle_t* handle) {
void uv_unref(uv_handle_t* handle) {
uv__handle_unref(handle);
}
void uv_stop(uv_loop_t* loop) {
loop->stop_flag = 1;
}

View File

@ -270,6 +270,10 @@ int uv_run(uv_loop_t *loop, uv_run_mode mode) {
return 0;
do {
if (loop->stop_flag) {
loop->stop_flag = 0;
return uv__loop_alive(loop);
}
uv_update_time(loop);
uv_process_timers(loop);
@ -287,6 +291,7 @@ int uv_run(uv_loop_t *loop, uv_run_mode mode) {
(*poll)(loop, loop->idle_handles == NULL &&
loop->pending_reqs_tail == NULL &&
loop->endgame_handles == NULL &&
!loop->stop_flag &&
(loop->active_handles > 0 ||
!ngx_queue_empty(&loop->active_reqs)) &&
!(mode & UV_RUN_NOWAIT));

View File

@ -23,6 +23,7 @@ TEST_DECLARE (platform_output)
TEST_DECLARE (callback_order)
TEST_DECLARE (run_once)
TEST_DECLARE (run_nowait)
TEST_DECLARE (loop_stop)
TEST_DECLARE (barrier_1)
TEST_DECLARE (barrier_2)
TEST_DECLARE (barrier_3)
@ -230,6 +231,7 @@ TASK_LIST_START
#endif
TEST_ENTRY (run_once)
TEST_ENTRY (run_nowait)
TEST_ENTRY (loop_stop)
TEST_ENTRY (barrier_1)
TEST_ENTRY (barrier_2)
TEST_ENTRY (barrier_3)

55
test/test-loop-stop.c Normal file
View File

@ -0,0 +1,55 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "uv.h"
#include "task.h"
static uv_timer_t timer_handle;
static int timer_called = 0;
static int num_ticks = 10;
static void timer_cb(uv_timer_t* handle, int status) {
ASSERT(handle == &timer_handle);
ASSERT(status == 0);
timer_called++;
if (timer_called == 1)
uv_stop(uv_default_loop());
else if (timer_called == num_ticks)
uv_timer_stop(handle);
}
TEST_IMPL(loop_stop) {
int r;
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_DEFAULT);
ASSERT(r == 0);
ASSERT(timer_called == 10);
return 0;
}

1
uv.gyp
View File

@ -280,6 +280,7 @@
'test/test-ipc-send-recv.c',
'test/test-list.h',
'test/test-loop-handles.c',
'test/test-loop-stop.c',
'test/test-walk-handles.c',
'test/test-multiple-listen.c',
'test/test-pass-always.c',