unix, windows: add uv_loop_alive() function
Useful to know when the the event loop is empty, this can't be done with uv_run() without possibly blocking, or running some events (which might empty the event loop as a side-effect).
This commit is contained in:
parent
4a7a72fb32
commit
ed36b85d3b
@ -145,6 +145,7 @@ test_run_tests_SOURCES = test/blackhole-server.c \
|
|||||||
test/test-ipc.c \
|
test/test-ipc.c \
|
||||||
test/test-list.h \
|
test/test-list.h \
|
||||||
test/test-loop-handles.c \
|
test/test-loop-handles.c \
|
||||||
|
test/test-loop-alive.c \
|
||||||
test/test-loop-stop.c \
|
test/test-loop-stop.c \
|
||||||
test/test-loop-time.c \
|
test/test-loop-time.c \
|
||||||
test/test-multiple-listen.c \
|
test/test-multiple-listen.c \
|
||||||
|
|||||||
@ -272,6 +272,12 @@ UV_EXTERN uv_loop_t* uv_default_loop(void);
|
|||||||
*/
|
*/
|
||||||
UV_EXTERN int uv_run(uv_loop_t*, uv_run_mode mode);
|
UV_EXTERN int uv_run(uv_loop_t*, uv_run_mode mode);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This function checks whether the reference count, the number of active
|
||||||
|
* handles or requests left in the event loop, is non-zero.
|
||||||
|
*/
|
||||||
|
UV_EXTERN int uv_loop_alive(const uv_loop_t* loop);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This function will stop the event loop by forcing uv_run to end
|
* 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.
|
* as soon as possible, but not sooner than the next loop iteration.
|
||||||
|
|||||||
@ -248,13 +248,18 @@ int uv_backend_timeout(const uv_loop_t* loop) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int uv__loop_alive(uv_loop_t* loop) {
|
static int uv__loop_alive(const uv_loop_t* loop) {
|
||||||
return uv__has_active_handles(loop) ||
|
return uv__has_active_handles(loop) ||
|
||||||
uv__has_active_reqs(loop) ||
|
uv__has_active_reqs(loop) ||
|
||||||
loop->closing_handles != NULL;
|
loop->closing_handles != NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int uv_loop_alive(const uv_loop_t* loop) {
|
||||||
|
return uv__loop_alive(loop);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int uv_run(uv_loop_t* loop, uv_run_mode mode) {
|
int uv_run(uv_loop_t* loop, uv_run_mode mode) {
|
||||||
int timeout;
|
int timeout;
|
||||||
int r;
|
int r;
|
||||||
|
|||||||
@ -256,13 +256,18 @@ static void uv_poll_ex(uv_loop_t* loop, int block) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int uv__loop_alive(uv_loop_t* loop) {
|
static int uv__loop_alive(const uv_loop_t* loop) {
|
||||||
return loop->active_handles > 0 ||
|
return loop->active_handles > 0 ||
|
||||||
!QUEUE_EMPTY(&loop->active_reqs) ||
|
!QUEUE_EMPTY(&loop->active_reqs) ||
|
||||||
loop->endgame_handles != NULL;
|
loop->endgame_handles != NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int uv_loop_alive(const uv_loop_t* loop) {
|
||||||
|
return uv__loop_alive(loop);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int uv_run(uv_loop_t *loop, uv_run_mode mode) {
|
int uv_run(uv_loop_t *loop, uv_run_mode mode) {
|
||||||
int r;
|
int r;
|
||||||
void (*poll)(uv_loop_t* loop, int block);
|
void (*poll)(uv_loop_t* loop, int block);
|
||||||
|
|||||||
@ -24,6 +24,7 @@ TEST_DECLARE (callback_order)
|
|||||||
TEST_DECLARE (close_order)
|
TEST_DECLARE (close_order)
|
||||||
TEST_DECLARE (run_once)
|
TEST_DECLARE (run_once)
|
||||||
TEST_DECLARE (run_nowait)
|
TEST_DECLARE (run_nowait)
|
||||||
|
TEST_DECLARE (loop_alive)
|
||||||
TEST_DECLARE (loop_stop)
|
TEST_DECLARE (loop_stop)
|
||||||
TEST_DECLARE (loop_update_time)
|
TEST_DECLARE (loop_update_time)
|
||||||
TEST_DECLARE (barrier_1)
|
TEST_DECLARE (barrier_1)
|
||||||
@ -253,6 +254,7 @@ TASK_LIST_START
|
|||||||
TEST_ENTRY (close_order)
|
TEST_ENTRY (close_order)
|
||||||
TEST_ENTRY (run_once)
|
TEST_ENTRY (run_once)
|
||||||
TEST_ENTRY (run_nowait)
|
TEST_ENTRY (run_nowait)
|
||||||
|
TEST_ENTRY (loop_alive)
|
||||||
TEST_ENTRY (loop_stop)
|
TEST_ENTRY (loop_stop)
|
||||||
TEST_ENTRY (loop_update_time)
|
TEST_ENTRY (loop_update_time)
|
||||||
TEST_ENTRY (barrier_1)
|
TEST_ENTRY (barrier_1)
|
||||||
|
|||||||
68
test/test-loop-alive.c
Normal file
68
test/test-loop-alive.c
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/* 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 void timer_cb(uv_timer_t* handle, int status) {
|
||||||
|
ASSERT(handle);
|
||||||
|
ASSERT(status == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static uv_work_t work_req;
|
||||||
|
|
||||||
|
static void work_cb(uv_work_t* req) {
|
||||||
|
ASSERT(req);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void after_work_cb(uv_work_t* req, int status) {
|
||||||
|
ASSERT(req);
|
||||||
|
ASSERT(status == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST_IMPL(loop_alive) {
|
||||||
|
int r;
|
||||||
|
ASSERT(!uv_loop_alive(uv_default_loop()));
|
||||||
|
|
||||||
|
/* loops with handles are alive */
|
||||||
|
uv_timer_init(uv_default_loop(), &timer_handle);
|
||||||
|
uv_timer_start(&timer_handle, timer_cb, 100, 0);
|
||||||
|
ASSERT(uv_loop_alive(uv_default_loop()));
|
||||||
|
|
||||||
|
r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
|
||||||
|
ASSERT(r == 0);
|
||||||
|
ASSERT(!uv_loop_alive(uv_default_loop()));
|
||||||
|
|
||||||
|
/* loops with requests are alive */
|
||||||
|
r = uv_queue_work(uv_default_loop(), &work_req, work_cb, after_work_cb);
|
||||||
|
ASSERT(r == 0);
|
||||||
|
ASSERT(uv_loop_alive(uv_default_loop()));
|
||||||
|
|
||||||
|
r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
|
||||||
|
ASSERT(r == 0);
|
||||||
|
ASSERT(!uv_loop_alive(uv_default_loop()));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
1
uv.gyp
1
uv.gyp
@ -323,6 +323,7 @@
|
|||||||
'test/test-ipc-send-recv.c',
|
'test/test-ipc-send-recv.c',
|
||||||
'test/test-list.h',
|
'test/test-list.h',
|
||||||
'test/test-loop-handles.c',
|
'test/test-loop-handles.c',
|
||||||
|
'test/test-loop-alive.c',
|
||||||
'test/test-loop-stop.c',
|
'test/test-loop-stop.c',
|
||||||
'test/test-loop-time.c',
|
'test/test-loop-time.c',
|
||||||
'test/test-walk-handles.c',
|
'test/test-walk-handles.c',
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user