1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 #include "atomicops-inl.h"
27 #include "handle-inl.h"
31 void uv_async_endgame(uv_loop_t* loop, uv_async_t* handle) {
32 if (handle->flags & UV_HANDLE_CLOSING &&
33 !handle->async_sent) {
34 assert(!(handle->flags & UV_HANDLE_CLOSED));
35 uv__handle_close(handle);
40 int uv_async_init(uv_loop_t* loop, uv_async_t* handle, uv_async_cb async_cb) {
43 uv__handle_init(loop, (uv_handle_t*) handle, UV_ASYNC);
44 handle->async_sent = 0;
45 handle->async_cb = async_cb;
47 req = &handle->async_req;
48 UV_REQ_INIT(req, UV_WAKEUP);
51 uv__handle_start(handle);
57 void uv_async_close(uv_loop_t* loop, uv_async_t* handle) {
58 if (!((uv_async_t*)handle)->async_sent) {
59 uv_want_endgame(loop, (uv_handle_t*) handle);
62 uv__handle_closing(handle);
66 int uv_async_send(uv_async_t* handle) {
67 uv_loop_t* loop = handle->loop;
69 if (handle->type != UV_ASYNC) {
70 /* Can't set errno because that's not thread-safe. */
74 /* The user should make sure never to call uv_async_send to a closing or
76 assert(!(handle->flags & UV_HANDLE_CLOSING));
78 if (!uv__atomic_exchange_set(&handle->async_sent)) {
79 POST_COMPLETION_FOR_REQ(loop, &handle->async_req);
86 void uv_process_async_wakeup_req(uv_loop_t* loop, uv_async_t* handle,
88 assert(handle->type == UV_ASYNC);
89 assert(req->type == UV_WAKEUP);
91 handle->async_sent = 0;
93 if (handle->flags & UV_HANDLE_CLOSING) {
94 uv_want_endgame(loop, (uv_handle_t*)handle);
95 } else if (handle->async_cb != NULL) {
96 handle->async_cb(handle);