Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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