Path: blob/master/Utilities/cmlibuv/src/unix/loop-watcher.c
3156 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 "uv.h"22#include "internal.h"2324#define UV_LOOP_WATCHER_DEFINE(name, type) \25int uv_##name##_init(uv_loop_t* loop, uv_##name##_t* handle) { \26uv__handle_init(loop, (uv_handle_t*)handle, UV_##type); \27handle->name##_cb = NULL; \28return 0; \29} \30\31int uv_##name##_start(uv_##name##_t* handle, uv_##name##_cb cb) { \32if (uv__is_active(handle)) return 0; \33if (cb == NULL) return UV_EINVAL; \34QUEUE_INSERT_HEAD(&handle->loop->name##_handles, &handle->queue); \35handle->name##_cb = cb; \36uv__handle_start(handle); \37return 0; \38} \39\40int uv_##name##_stop(uv_##name##_t* handle) { \41if (!uv__is_active(handle)) return 0; \42QUEUE_REMOVE(&handle->queue); \43uv__handle_stop(handle); \44return 0; \45} \46\47void uv__run_##name(uv_loop_t* loop) { \48uv_##name##_t* h; \49QUEUE queue; \50QUEUE* q; \51QUEUE_MOVE(&loop->name##_handles, &queue); \52while (!QUEUE_EMPTY(&queue)) { \53q = QUEUE_HEAD(&queue); \54h = QUEUE_DATA(q, uv_##name##_t, queue); \55QUEUE_REMOVE(q); \56QUEUE_INSERT_TAIL(&loop->name##_handles, q); \57h->name##_cb(h); \58} \59} \60\61void uv__##name##_close(uv_##name##_t* handle) { \62uv_##name##_stop(handle); \63}6465UV_LOOP_WATCHER_DEFINE(prepare, PREPARE)66UV_LOOP_WATCHER_DEFINE(check, CHECK)67UV_LOOP_WATCHER_DEFINE(idle, IDLE)686970