diff --git a/Makefile.am b/Makefile.am index 8101110d..64a5e80e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/include/uv.h b/include/uv.h index 5c993b45..5821cce8 100644 --- a/include/uv.h +++ b/include/uv.h @@ -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); diff --git a/src/unix/async.c b/src/unix/async.c index f526ba3e..5ced3cbe 100644 --- a/src/unix/async.c +++ b/src/unix/async.c @@ -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); } } diff --git a/src/win/async.c b/src/win/async.c index edf65297..e192ead9 100644 --- a/src/win/async.c +++ b/src/win/async.c @@ -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); } } diff --git a/test/test-async-null-cb.c b/test/test-async-null-cb.c new file mode 100644 index 00000000..d6548842 --- /dev/null +++ b/test/test-async-null-cb.c @@ -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; +} diff --git a/test/test-list.h b/test/test-list.h index d3371a80..feca4670 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -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) diff --git a/uv.gyp b/uv.gyp index 0e377d12..da254a1a 100644 --- a/uv.gyp +++ b/uv.gyp @@ -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',