11cb0ef41Sopenharmony_ci/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 21cb0ef41Sopenharmony_ci * 31cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 41cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to 51cb0ef41Sopenharmony_ci * deal in the Software without restriction, including without limitation the 61cb0ef41Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 71cb0ef41Sopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is 81cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions: 91cb0ef41Sopenharmony_ci * 101cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice shall be included in 111cb0ef41Sopenharmony_ci * all copies or substantial portions of the Software. 121cb0ef41Sopenharmony_ci * 131cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 141cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 151cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 161cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 171cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 181cb0ef41Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 191cb0ef41Sopenharmony_ci * IN THE SOFTWARE. 201cb0ef41Sopenharmony_ci */ 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci#include <assert.h> 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci#include "uv.h" 251cb0ef41Sopenharmony_ci#include "internal.h" 261cb0ef41Sopenharmony_ci#include "atomicops-inl.h" 271cb0ef41Sopenharmony_ci#include "handle-inl.h" 281cb0ef41Sopenharmony_ci#include "req-inl.h" 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_civoid uv__async_endgame(uv_loop_t* loop, uv_async_t* handle) { 321cb0ef41Sopenharmony_ci if (handle->flags & UV_HANDLE_CLOSING && 331cb0ef41Sopenharmony_ci !handle->async_sent) { 341cb0ef41Sopenharmony_ci assert(!(handle->flags & UV_HANDLE_CLOSED)); 351cb0ef41Sopenharmony_ci uv__handle_close(handle); 361cb0ef41Sopenharmony_ci } 371cb0ef41Sopenharmony_ci} 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ciint uv_async_init(uv_loop_t* loop, uv_async_t* handle, uv_async_cb async_cb) { 411cb0ef41Sopenharmony_ci uv_req_t* req; 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci uv__handle_init(loop, (uv_handle_t*) handle, UV_ASYNC); 441cb0ef41Sopenharmony_ci handle->async_sent = 0; 451cb0ef41Sopenharmony_ci handle->async_cb = async_cb; 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci req = &handle->async_req; 481cb0ef41Sopenharmony_ci UV_REQ_INIT(req, UV_WAKEUP); 491cb0ef41Sopenharmony_ci req->data = handle; 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci uv__handle_start(handle); 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci return 0; 541cb0ef41Sopenharmony_ci} 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_civoid uv__async_close(uv_loop_t* loop, uv_async_t* handle) { 581cb0ef41Sopenharmony_ci if (!((uv_async_t*)handle)->async_sent) { 591cb0ef41Sopenharmony_ci uv__want_endgame(loop, (uv_handle_t*) handle); 601cb0ef41Sopenharmony_ci } 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci uv__handle_closing(handle); 631cb0ef41Sopenharmony_ci} 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ciint uv_async_send(uv_async_t* handle) { 671cb0ef41Sopenharmony_ci uv_loop_t* loop = handle->loop; 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci if (handle->type != UV_ASYNC) { 701cb0ef41Sopenharmony_ci /* Can't set errno because that's not thread-safe. */ 711cb0ef41Sopenharmony_ci return -1; 721cb0ef41Sopenharmony_ci } 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci /* The user should make sure never to call uv_async_send to a closing or 751cb0ef41Sopenharmony_ci * closed handle. */ 761cb0ef41Sopenharmony_ci assert(!(handle->flags & UV_HANDLE_CLOSING)); 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci if (!uv__atomic_exchange_set(&handle->async_sent)) { 791cb0ef41Sopenharmony_ci POST_COMPLETION_FOR_REQ(loop, &handle->async_req); 801cb0ef41Sopenharmony_ci } 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci return 0; 831cb0ef41Sopenharmony_ci} 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_civoid uv__process_async_wakeup_req(uv_loop_t* loop, uv_async_t* handle, 871cb0ef41Sopenharmony_ci uv_req_t* req) { 881cb0ef41Sopenharmony_ci assert(handle->type == UV_ASYNC); 891cb0ef41Sopenharmony_ci assert(req->type == UV_WAKEUP); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci handle->async_sent = 0; 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_ci if (handle->flags & UV_HANDLE_CLOSING) { 941cb0ef41Sopenharmony_ci uv__want_endgame(loop, (uv_handle_t*)handle); 951cb0ef41Sopenharmony_ci } else if (handle->async_cb != NULL) { 961cb0ef41Sopenharmony_ci handle->async_cb(handle); 971cb0ef41Sopenharmony_ci } 981cb0ef41Sopenharmony_ci} 99