unix, windows: allow NULL async callback

Allow a NULL callback so the user doesn't have to provide a dummy when
the actual event is processed by e.g. a check handle callback.
This commit is contained in:
Ben Noordhuis 2013-08-12 08:42:49 +02:00
parent c6adab2e27
commit 1510ce81fd
7 changed files with 74 additions and 4 deletions

View File

@ -63,6 +63,7 @@ test_run_tests_SOURCES = test/blackhole-server.c \
test/task.h \
test/test-active.c \
test/test-async.c \
test/test-async-null-cb.c \
test/test-barrier.c \
test/test-callback-order.c \
test/test-callback-stack.c \

View File

@ -1239,6 +1239,12 @@ struct uv_async_s {
UV_ASYNC_PRIVATE_FIELDS
};
/*
* Initialize the uv_async_t handle. A NULL callback is allowed.
*
* Note that uv_async_init(), unlike other libuv functions, immediately
* starts the handle. To stop the handle again, close it with uv_close().
*/
UV_EXTERN int uv_async_init(uv_loop_t*, uv_async_t* async,
uv_async_cb async_cb);

View File

@ -78,8 +78,13 @@ static void uv__async_event(uv_loop_t* loop,
QUEUE_FOREACH(q, &loop->async_handles) {
h = QUEUE_DATA(q, uv_async_t, queue);
if (!h->pending) continue;
if (h->pending == 0)
continue;
h->pending = 0;
if (h->async_cb == NULL)
continue;
h->async_cb(h, 0);
}
}

View File

@ -91,9 +91,9 @@ void uv_process_async_wakeup_req(uv_loop_t* loop, uv_async_t* handle,
handle->async_sent = 0;
if (!(handle->flags & UV__HANDLE_CLOSING)) {
handle->async_cb((uv_async_t*) handle, 0);
} else {
if (handle->flags & UV__HANDLE_CLOSING) {
uv_want_endgame(loop, (uv_handle_t*)handle);
} else if (handle->async_cb != NULL) {
handle->async_cb(handle, 0);
}
}

55
test/test-async-null-cb.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_async_t async_handle;
static uv_check_t check_handle;
static int check_cb_called;
static uv_thread_t thread;
static void thread_cb(void* dummy) {
(void) &dummy;
uv_async_send(&async_handle);
}
static void check_cb(uv_check_t* handle, int status) {
ASSERT(check_cb_called == 0);
uv_close((uv_handle_t*) &async_handle, NULL);
uv_close((uv_handle_t*) &check_handle, NULL);
check_cb_called++;
}
TEST_IMPL(async_null_cb) {
ASSERT(0 == uv_async_init(uv_default_loop(), &async_handle, NULL));
ASSERT(0 == uv_check_init(uv_default_loop(), &check_handle));
ASSERT(0 == uv_check_start(&check_handle, check_cb));
ASSERT(0 == uv_thread_create(&thread, thread_cb, NULL));
ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
ASSERT(0 == uv_thread_join(&thread));
ASSERT(1 == check_cb_called);
MAKE_VALGRIND_HAPPY();
return 0;
}

View File

@ -134,6 +134,7 @@ TEST_DECLARE (has_ref)
TEST_DECLARE (active)
TEST_DECLARE (embed)
TEST_DECLARE (async)
TEST_DECLARE (async_null_cb)
TEST_DECLARE (get_currentexe)
TEST_DECLARE (process_title)
TEST_DECLARE (cwd_and_chdir)
@ -396,6 +397,7 @@ TASK_LIST_START
TEST_ENTRY (embed)
TEST_ENTRY (async)
TEST_ENTRY (async_null_cb)
TEST_ENTRY (get_currentexe)

1
uv.gyp
View File

@ -302,6 +302,7 @@
'test/test-util.c',
'test/test-active.c',
'test/test-async.c',
'test/test-async-null-cb.c',
'test/test-callback-stack.c',
'test/test-callback-order.c',
'test/test-connection-fail.c',