This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
ev-emul.h
268 lines (213 loc) · 7.29 KB
/
ev-emul.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* Copyright Joyent, Inc. and other Node contributors.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef NODE_EV_EMUL_H_
#define NODE_EV_EMUL_H_
#include "uv.h"
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
#undef ev_init
#undef ev_set_cb
#undef ev_set_priority
#undef ev_is_active
#undef ev_timer_init
#undef ev_timer_set
#undef ev_io_init
#undef ev_io_set
#undef EV_P
#undef EV_P_
#undef EV_A
#undef EV_A_
#undef EV_DEFAULT
#undef EV_DEFAULT_
#undef EV_DEFAULT_UC
#undef EV_DEFAULT_UC_
#define EV_P void
#define EV_P_
#define EV_A
#define EV_A_
#define EV_DEFAULT
#define EV_DEFAULT_
#define EV_DEFAULT_UC
#define EV_DEFAULT_UC_
#define ev_init(w, cb_) \
do { \
void* data = (w)->data; \
memset((w), 0, sizeof(*(w))); \
(w)->data = data; \
(w)->cb = (cb_); \
} \
while (0)
#define ev_set_cb(w, cb_) \
do \
(w)->cb = (cb_); \
while (0)
#define ev_is_active(w) \
(uv_is_active((uv_handle_t*) &(w)->handle))
#define __uv_container_of(ptr, type, field) \
((type*) ((char*) (ptr) - offsetof(type, field)))
#define __uv_warn_of(old_, new_) \
do { \
if (__uv_warn_##old_ || node::no_deprecation) break; \
__uv_warn_##old_ = 1; \
fputs("WARNING: " #old_ " is deprecated, use " #new_ "\n", stderr); \
} \
while (0)
static int __uv_warn_ev_io __attribute__((unused));
static int __uv_warn_ev_timer __attribute__((unused));
static int __uv_warn_ev_ref __attribute__((unused));
static int __uv_warn_ev_unref __attribute__((unused));
struct __ev_io;
typedef struct __ev_io __ev_io;
typedef void (*__ev_io_cb)(__ev_io*, int);
struct __ev_timer;
typedef struct __ev_timer __ev_timer;
typedef void (*__ev_timer_cb)(__ev_timer*, int);
/* Field order matters. Don't touch. */
struct __ev_io {
__ev_io_cb cb;
void* data;
int flags; /* bit 1 == initialized, yes or no? */
uv_poll_t handle;
int fd;
int events;
};
/* Field order matters. Don't touch. */
struct __ev_timer {
__ev_timer_cb cb;
void* data;
int flags;
uv_timer_t handle;
double delay;
double repeat;
};
inline static void __uv_poll_cb(uv_poll_t* handle, int status, int events) {
__ev_io* w = __uv_container_of(handle, __ev_io, handle);
w->cb(w, events);
(void) status;
}
inline static void __uv_timer_cb(uv_timer_t* handle, int status) {
__ev_timer* w = __uv_container_of(handle, __ev_timer, handle);
w->cb(w, 0);
(void) status;
}
inline static void __ev_io_init(__ev_io* w, __ev_io_cb cb, int fd, int events) {
__uv_warn_of(ev_io, uv_poll_t);
ev_init(w, cb);
w->fd = fd;
w->events = events;
}
inline static void __ev_io_set(__ev_io* w, int fd, int events) {
__uv_warn_of(ev_io, uv_poll_t);
w->fd = fd;
w->events = events;
}
inline static void __ev_io_start(__ev_io* w) {
__uv_warn_of(ev_io, uv_poll_t);
if (!(w->flags & 1)) {
uv_poll_init(uv_default_loop(), &w->handle, w->fd);
w->flags |= 1;
}
uv_poll_start(&w->handle, w->events, __uv_poll_cb);
}
inline static void __ev_io_stop(__ev_io* w) {
__uv_warn_of(ev_io, uv_poll_t);
uv_poll_stop(&w->handle);
}
inline static void __ev_timer_init(__ev_timer* w,
__ev_timer_cb cb,
double delay,
double repeat) {
__uv_warn_of(ev_timer, uv_timer_t);
ev_init(w, cb);
w->delay = delay;
w->repeat = repeat;
}
inline static void __ev_timer_set(__ev_timer* w,
double delay,
double repeat) {
__uv_warn_of(ev_timer, uv_timer_t);
w->delay = delay;
w->repeat = repeat;
}
inline static void __ev_timer_start(__ev_timer* w) {
__uv_warn_of(ev_timer, uv_timer_t);
if (!(w->flags & 1)) {
uv_timer_init(uv_default_loop(), &w->handle);
w->flags |= 1;
}
uv_timer_start(&w->handle,
__uv_timer_cb,
(int64_t) (w->delay * 1000),
(int64_t) (w->repeat * 1000));
}
inline static void __ev_timer_stop(__ev_timer* w) {
__uv_warn_of(ev_timer, uv_timer_t);
uv_timer_stop(&w->handle);
}
inline static void __ev_timer_again(__ev_timer* w) {
__uv_warn_of(ev_timer, uv_timer_t);
if (w->flags & 1)
uv_timer_again(&w->handle);
else
__ev_timer_start(w); /* work around node-zookeeper bug */
}
/* Evil. Using this will void your warranty. */
inline static void __ev_ref(void) {
__uv_warn_of(ev_ref, uv_ref);
uv_default_loop()->active_handles++;
}
/* Evil. Using this will void your warranty. */
inline static void __ev_unref(void) {
__uv_warn_of(ev_unref, uv_unref);
uv_default_loop()->active_handles--;
}
inline static double __ev_now(...) {
return uv_hrtime() / 1e9;
}
inline static void __ev_set_priority(...) {
}
#define ev_io __ev_io
#define ev_io_init __ev_io_init
#define ev_io_set __ev_io_set
#define ev_io_start __ev_io_start
#define ev_io_stop __ev_io_stop
#define ev_timer __ev_timer
#define ev_timer_init __ev_timer_init
#define ev_timer_set __ev_timer_set
#define ev_timer_start __ev_timer_start
#define ev_timer_stop __ev_timer_stop
#define ev_timer_again __ev_timer_again
#define ev_ref __ev_ref
#define ev_unref __ev_unref
#define ev_now __ev_now
#define ev_set_priority __ev_set_priority
#undef __uv_container_of
#undef __uv_warn_of
#ifdef __cplusplus
}
#endif
#endif /* NODE_EV_EMUL_H_ */