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 "handle-inl.h"
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_civoid uv__loop_watcher_endgame(uv_loop_t* loop, uv_handle_t* handle) {
301cb0ef41Sopenharmony_ci  if (handle->flags & UV_HANDLE_CLOSING) {
311cb0ef41Sopenharmony_ci    assert(!(handle->flags & UV_HANDLE_CLOSED));
321cb0ef41Sopenharmony_ci    handle->flags |= UV_HANDLE_CLOSED;
331cb0ef41Sopenharmony_ci    uv__handle_close(handle);
341cb0ef41Sopenharmony_ci  }
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci#define UV_LOOP_WATCHER_DEFINE(name, NAME)                                    \
391cb0ef41Sopenharmony_ci  int uv_##name##_init(uv_loop_t* loop, uv_##name##_t* handle) {              \
401cb0ef41Sopenharmony_ci    uv__handle_init(loop, (uv_handle_t*) handle, UV_##NAME);                  \
411cb0ef41Sopenharmony_ci                                                                              \
421cb0ef41Sopenharmony_ci    return 0;                                                                 \
431cb0ef41Sopenharmony_ci  }                                                                           \
441cb0ef41Sopenharmony_ci                                                                              \
451cb0ef41Sopenharmony_ci                                                                              \
461cb0ef41Sopenharmony_ci  int uv_##name##_start(uv_##name##_t* handle, uv_##name##_cb cb) {           \
471cb0ef41Sopenharmony_ci    uv_loop_t* loop = handle->loop;                                           \
481cb0ef41Sopenharmony_ci    uv_##name##_t* old_head;                                                  \
491cb0ef41Sopenharmony_ci                                                                              \
501cb0ef41Sopenharmony_ci    assert(handle->type == UV_##NAME);                                        \
511cb0ef41Sopenharmony_ci                                                                              \
521cb0ef41Sopenharmony_ci    if (uv__is_active(handle))                                                \
531cb0ef41Sopenharmony_ci      return 0;                                                               \
541cb0ef41Sopenharmony_ci                                                                              \
551cb0ef41Sopenharmony_ci    if (cb == NULL)                                                           \
561cb0ef41Sopenharmony_ci      return UV_EINVAL;                                                       \
571cb0ef41Sopenharmony_ci                                                                              \
581cb0ef41Sopenharmony_ci    old_head = loop->name##_handles;                                          \
591cb0ef41Sopenharmony_ci                                                                              \
601cb0ef41Sopenharmony_ci    handle->name##_next = old_head;                                           \
611cb0ef41Sopenharmony_ci    handle->name##_prev = NULL;                                               \
621cb0ef41Sopenharmony_ci                                                                              \
631cb0ef41Sopenharmony_ci    if (old_head) {                                                           \
641cb0ef41Sopenharmony_ci      old_head->name##_prev = handle;                                         \
651cb0ef41Sopenharmony_ci    }                                                                         \
661cb0ef41Sopenharmony_ci                                                                              \
671cb0ef41Sopenharmony_ci    loop->name##_handles = handle;                                            \
681cb0ef41Sopenharmony_ci                                                                              \
691cb0ef41Sopenharmony_ci    handle->name##_cb = cb;                                                   \
701cb0ef41Sopenharmony_ci    uv__handle_start(handle);                                                 \
711cb0ef41Sopenharmony_ci                                                                              \
721cb0ef41Sopenharmony_ci    return 0;                                                                 \
731cb0ef41Sopenharmony_ci  }                                                                           \
741cb0ef41Sopenharmony_ci                                                                              \
751cb0ef41Sopenharmony_ci                                                                              \
761cb0ef41Sopenharmony_ci  int uv_##name##_stop(uv_##name##_t* handle) {                               \
771cb0ef41Sopenharmony_ci    uv_loop_t* loop = handle->loop;                                           \
781cb0ef41Sopenharmony_ci                                                                              \
791cb0ef41Sopenharmony_ci    assert(handle->type == UV_##NAME);                                        \
801cb0ef41Sopenharmony_ci                                                                              \
811cb0ef41Sopenharmony_ci    if (!uv__is_active(handle))                                               \
821cb0ef41Sopenharmony_ci      return 0;                                                               \
831cb0ef41Sopenharmony_ci                                                                              \
841cb0ef41Sopenharmony_ci    /* Update loop head if needed */                                          \
851cb0ef41Sopenharmony_ci    if (loop->name##_handles == handle) {                                     \
861cb0ef41Sopenharmony_ci      loop->name##_handles = handle->name##_next;                             \
871cb0ef41Sopenharmony_ci    }                                                                         \
881cb0ef41Sopenharmony_ci                                                                              \
891cb0ef41Sopenharmony_ci    /* Update the iterator-next pointer of needed */                          \
901cb0ef41Sopenharmony_ci    if (loop->next_##name##_handle == handle) {                               \
911cb0ef41Sopenharmony_ci      loop->next_##name##_handle = handle->name##_next;                       \
921cb0ef41Sopenharmony_ci    }                                                                         \
931cb0ef41Sopenharmony_ci                                                                              \
941cb0ef41Sopenharmony_ci    if (handle->name##_prev) {                                                \
951cb0ef41Sopenharmony_ci      handle->name##_prev->name##_next = handle->name##_next;                 \
961cb0ef41Sopenharmony_ci    }                                                                         \
971cb0ef41Sopenharmony_ci    if (handle->name##_next) {                                                \
981cb0ef41Sopenharmony_ci      handle->name##_next->name##_prev = handle->name##_prev;                 \
991cb0ef41Sopenharmony_ci    }                                                                         \
1001cb0ef41Sopenharmony_ci                                                                              \
1011cb0ef41Sopenharmony_ci    uv__handle_stop(handle);                                                  \
1021cb0ef41Sopenharmony_ci                                                                              \
1031cb0ef41Sopenharmony_ci    return 0;                                                                 \
1041cb0ef41Sopenharmony_ci  }                                                                           \
1051cb0ef41Sopenharmony_ci                                                                              \
1061cb0ef41Sopenharmony_ci                                                                              \
1071cb0ef41Sopenharmony_ci  void uv__##name##_invoke(uv_loop_t* loop) {                                 \
1081cb0ef41Sopenharmony_ci    uv_##name##_t* handle;                                                    \
1091cb0ef41Sopenharmony_ci                                                                              \
1101cb0ef41Sopenharmony_ci    (loop)->next_##name##_handle = (loop)->name##_handles;                    \
1111cb0ef41Sopenharmony_ci                                                                              \
1121cb0ef41Sopenharmony_ci    while ((loop)->next_##name##_handle != NULL) {                            \
1131cb0ef41Sopenharmony_ci      handle = (loop)->next_##name##_handle;                                  \
1141cb0ef41Sopenharmony_ci      (loop)->next_##name##_handle = handle->name##_next;                     \
1151cb0ef41Sopenharmony_ci                                                                              \
1161cb0ef41Sopenharmony_ci      handle->name##_cb(handle);                                              \
1171cb0ef41Sopenharmony_ci    }                                                                         \
1181cb0ef41Sopenharmony_ci  }
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ciUV_LOOP_WATCHER_DEFINE(prepare, PREPARE)
1211cb0ef41Sopenharmony_ciUV_LOOP_WATCHER_DEFINE(check, CHECK)
1221cb0ef41Sopenharmony_ciUV_LOOP_WATCHER_DEFINE(idle, IDLE)
123