libuv-svace-build
122 строки · 7.8 Кб
1/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2*
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:
9*
10* The above copyright notice and this permission notice shall be included in
11* all copies or substantial portions of the Software.
12*
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
19* IN THE SOFTWARE.
20*/
21
22#include <assert.h>
23
24#include "uv.h"
25#include "internal.h"
26#include "handle-inl.h"
27
28
29void uv__loop_watcher_endgame(uv_loop_t* loop, uv_handle_t* handle) {
30if (handle->flags & UV_HANDLE_CLOSING) {
31assert(!(handle->flags & UV_HANDLE_CLOSED));
32handle->flags |= UV_HANDLE_CLOSED;
33uv__handle_close(handle);
34}
35}
36
37
38#define UV_LOOP_WATCHER_DEFINE(name, NAME) \
39int uv_##name##_init(uv_loop_t* loop, uv_##name##_t* handle) { \
40uv__handle_init(loop, (uv_handle_t*) handle, UV_##NAME); \
41\
42return 0; \
43} \
44\
45\
46int uv_##name##_start(uv_##name##_t* handle, uv_##name##_cb cb) { \
47uv_loop_t* loop = handle->loop; \
48uv_##name##_t* old_head; \
49\
50assert(handle->type == UV_##NAME); \
51\
52if (uv__is_active(handle)) \
53return 0; \
54\
55if (cb == NULL) \
56return UV_EINVAL; \
57\
58old_head = loop->name##_handles; \
59\
60handle->name##_next = old_head; \
61handle->name##_prev = NULL; \
62\
63if (old_head) { \
64old_head->name##_prev = handle; \
65} \
66\
67loop->name##_handles = handle; \
68\
69handle->name##_cb = cb; \
70uv__handle_start(handle); \
71\
72return 0; \
73} \
74\
75\
76int uv_##name##_stop(uv_##name##_t* handle) { \
77uv_loop_t* loop = handle->loop; \
78\
79assert(handle->type == UV_##NAME); \
80\
81if (!uv__is_active(handle)) \
82return 0; \
83\
84/* Update loop head if needed */ \
85if (loop->name##_handles == handle) { \
86loop->name##_handles = handle->name##_next; \
87} \
88\
89/* Update the iterator-next pointer of needed */ \
90if (loop->next_##name##_handle == handle) { \
91loop->next_##name##_handle = handle->name##_next; \
92} \
93\
94if (handle->name##_prev) { \
95handle->name##_prev->name##_next = handle->name##_next; \
96} \
97if (handle->name##_next) { \
98handle->name##_next->name##_prev = handle->name##_prev; \
99} \
100\
101uv__handle_stop(handle); \
102\
103return 0; \
104} \
105\
106\
107void uv__##name##_invoke(uv_loop_t* loop) { \
108uv_##name##_t* handle; \
109\
110(loop)->next_##name##_handle = (loop)->name##_handles; \
111\
112while ((loop)->next_##name##_handle != NULL) { \
113handle = (loop)->next_##name##_handle; \
114(loop)->next_##name##_handle = handle->name##_next; \
115\
116handle->name##_cb(handle); \
117} \
118}
119
120UV_LOOP_WATCHER_DEFINE(prepare, PREPARE)
121UV_LOOP_WATCHER_DEFINE(check, CHECK)
122UV_LOOP_WATCHER_DEFINE(idle, IDLE)
123