Path: blob/master/Utilities/cmlibuv/src/win/loop-watcher.c
3153 views
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.1*2* Permission is hereby granted, free of charge, to any person obtaining a copy3* of this software and associated documentation files (the "Software"), to4* deal in the Software without restriction, including without limitation the5* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or6* sell copies of the Software, and to permit persons to whom the Software is7* furnished to do so, subject to the following conditions:8*9* The above copyright notice and this permission notice shall be included in10* all copies or substantial portions of the Software.11*12* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR13* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,14* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE15* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER16* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING17* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS18* IN THE SOFTWARE.19*/2021#include <assert.h>2223#include "uv.h"24#include "internal.h"25#include "handle-inl.h"262728void uv__loop_watcher_endgame(uv_loop_t* loop, uv_handle_t* handle) {29if (handle->flags & UV_HANDLE_CLOSING) {30assert(!(handle->flags & UV_HANDLE_CLOSED));31handle->flags |= UV_HANDLE_CLOSED;32uv__handle_close(handle);33}34}353637#define UV_LOOP_WATCHER_DEFINE(name, NAME) \38int uv_##name##_init(uv_loop_t* loop, uv_##name##_t* handle) { \39uv__handle_init(loop, (uv_handle_t*) handle, UV_##NAME); \40\41return 0; \42} \43\44\45int uv_##name##_start(uv_##name##_t* handle, uv_##name##_cb cb) { \46uv_loop_t* loop = handle->loop; \47uv_##name##_t* old_head; \48\49assert(handle->type == UV_##NAME); \50\51if (uv__is_active(handle)) \52return 0; \53\54if (cb == NULL) \55return UV_EINVAL; \56\57old_head = loop->name##_handles; \58\59handle->name##_next = old_head; \60handle->name##_prev = NULL; \61\62if (old_head) { \63old_head->name##_prev = handle; \64} \65\66loop->name##_handles = handle; \67\68handle->name##_cb = cb; \69uv__handle_start(handle); \70\71return 0; \72} \73\74\75int uv_##name##_stop(uv_##name##_t* handle) { \76uv_loop_t* loop = handle->loop; \77\78assert(handle->type == UV_##NAME); \79\80if (!uv__is_active(handle)) \81return 0; \82\83/* Update loop head if needed */ \84if (loop->name##_handles == handle) { \85loop->name##_handles = handle->name##_next; \86} \87\88/* Update the iterator-next pointer of needed */ \89if (loop->next_##name##_handle == handle) { \90loop->next_##name##_handle = handle->name##_next; \91} \92\93if (handle->name##_prev) { \94handle->name##_prev->name##_next = handle->name##_next; \95} \96if (handle->name##_next) { \97handle->name##_next->name##_prev = handle->name##_prev; \98} \99\100uv__handle_stop(handle); \101\102return 0; \103} \104\105\106void uv__##name##_invoke(uv_loop_t* loop) { \107uv_##name##_t* handle; \108\109(loop)->next_##name##_handle = (loop)->name##_handles; \110\111while ((loop)->next_##name##_handle != NULL) { \112handle = (loop)->next_##name##_handle; \113(loop)->next_##name##_handle = handle->name##_next; \114\115handle->name##_cb(handle); \116} \117}118119UV_LOOP_WATCHER_DEFINE(prepare, PREPARE)120UV_LOOP_WATCHER_DEFINE(check, CHECK)121UV_LOOP_WATCHER_DEFINE(idle, IDLE)122123124