… | |
… | |
32 | #ifdef __cplusplus |
32 | #ifdef __cplusplus |
33 | extern "C" { |
33 | extern "C" { |
34 | #endif |
34 | #endif |
35 | |
35 | |
36 | #ifndef EV_STANDALONE |
36 | #ifndef EV_STANDALONE |
|
|
37 | # ifdef EV_CONFIG_H |
|
|
38 | # include EV_CONFIG_H |
|
|
39 | # else |
37 | # include "config.h" |
40 | # include "config.h" |
|
|
41 | # endif |
38 | |
42 | |
39 | # if HAVE_CLOCK_GETTIME |
43 | # if HAVE_CLOCK_GETTIME |
40 | # ifndef EV_USE_MONOTONIC |
44 | # ifndef EV_USE_MONOTONIC |
41 | # define EV_USE_MONOTONIC 1 |
45 | # define EV_USE_MONOTONIC 1 |
42 | # endif |
46 | # endif |
43 | # ifndef EV_USE_REALTIME |
47 | # ifndef EV_USE_REALTIME |
44 | # define EV_USE_REALTIME 1 |
48 | # define EV_USE_REALTIME 1 |
45 | # endif |
49 | # endif |
|
|
50 | # else |
|
|
51 | # ifndef EV_USE_MONOTONIC |
|
|
52 | # define EV_USE_MONOTONIC 0 |
|
|
53 | # endif |
|
|
54 | # ifndef EV_USE_REALTIME |
|
|
55 | # define EV_USE_REALTIME 0 |
|
|
56 | # endif |
46 | # endif |
57 | # endif |
47 | |
58 | |
48 | # if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT) |
59 | # ifndef EV_USE_SELECT |
|
|
60 | # if HAVE_SELECT && HAVE_SYS_SELECT_H |
49 | # define EV_USE_SELECT 1 |
61 | # define EV_USE_SELECT 1 |
|
|
62 | # else |
|
|
63 | # define EV_USE_SELECT 0 |
|
|
64 | # endif |
50 | # endif |
65 | # endif |
51 | |
66 | |
52 | # if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) && !defined (__APPLE__) |
67 | # ifndef EV_USE_POLL |
|
|
68 | # if HAVE_POLL && HAVE_POLL_H |
53 | # define EV_USE_POLL 1 |
69 | # define EV_USE_POLL 1 |
|
|
70 | # else |
|
|
71 | # define EV_USE_POLL 0 |
|
|
72 | # endif |
54 | # endif |
73 | # endif |
55 | |
74 | |
56 | # if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) |
75 | # ifndef EV_USE_EPOLL |
|
|
76 | # if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
57 | # define EV_USE_EPOLL 1 |
77 | # define EV_USE_EPOLL 1 |
|
|
78 | # else |
|
|
79 | # define EV_USE_EPOLL 0 |
|
|
80 | # endif |
58 | # endif |
81 | # endif |
59 | |
82 | |
60 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) && !defined (__APPLE__) |
83 | # ifndef EV_USE_KQUEUE |
|
|
84 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
61 | # define EV_USE_KQUEUE 1 |
85 | # define EV_USE_KQUEUE 1 |
|
|
86 | # else |
|
|
87 | # define EV_USE_KQUEUE 0 |
|
|
88 | # endif |
|
|
89 | # endif |
|
|
90 | |
|
|
91 | # ifndef EV_USE_PORT |
|
|
92 | # if HAVE_PORT_H && HAVE_PORT_CREATE |
|
|
93 | # define EV_USE_PORT 1 |
|
|
94 | # else |
|
|
95 | # define EV_USE_PORT 0 |
|
|
96 | # endif |
62 | # endif |
97 | # endif |
63 | |
98 | |
64 | #endif |
99 | #endif |
65 | |
100 | |
66 | #include <math.h> |
101 | #include <math.h> |
… | |
… | |
76 | #include <time.h> |
111 | #include <time.h> |
77 | |
112 | |
78 | #include <signal.h> |
113 | #include <signal.h> |
79 | |
114 | |
80 | #ifndef _WIN32 |
115 | #ifndef _WIN32 |
81 | # include <unistd.h> |
|
|
82 | # include <sys/time.h> |
116 | # include <sys/time.h> |
83 | # include <sys/wait.h> |
117 | # include <sys/wait.h> |
|
|
118 | # include <unistd.h> |
84 | #else |
119 | #else |
85 | # define WIN32_LEAN_AND_MEAN |
120 | # define WIN32_LEAN_AND_MEAN |
86 | # include <windows.h> |
121 | # include <windows.h> |
87 | # ifndef EV_SELECT_IS_WINSOCKET |
122 | # ifndef EV_SELECT_IS_WINSOCKET |
88 | # define EV_SELECT_IS_WINSOCKET 1 |
123 | # define EV_SELECT_IS_WINSOCKET 1 |
… | |
… | |
90 | #endif |
125 | #endif |
91 | |
126 | |
92 | /**/ |
127 | /**/ |
93 | |
128 | |
94 | #ifndef EV_USE_MONOTONIC |
129 | #ifndef EV_USE_MONOTONIC |
95 | # define EV_USE_MONOTONIC 1 |
130 | # define EV_USE_MONOTONIC 0 |
|
|
131 | #endif |
|
|
132 | |
|
|
133 | #ifndef EV_USE_REALTIME |
|
|
134 | # define EV_USE_REALTIME 0 |
96 | #endif |
135 | #endif |
97 | |
136 | |
98 | #ifndef EV_USE_SELECT |
137 | #ifndef EV_USE_SELECT |
99 | # define EV_USE_SELECT 1 |
138 | # define EV_USE_SELECT 1 |
100 | # define EV_SELECT_USE_FD_SET 1 |
|
|
101 | #endif |
139 | #endif |
102 | |
140 | |
103 | #ifndef EV_USE_POLL |
141 | #ifndef EV_USE_POLL |
104 | # ifdef _WIN32 |
142 | # ifdef _WIN32 |
105 | # define EV_USE_POLL 0 |
143 | # define EV_USE_POLL 0 |
… | |
… | |
114 | |
152 | |
115 | #ifndef EV_USE_KQUEUE |
153 | #ifndef EV_USE_KQUEUE |
116 | # define EV_USE_KQUEUE 0 |
154 | # define EV_USE_KQUEUE 0 |
117 | #endif |
155 | #endif |
118 | |
156 | |
119 | #ifndef EV_USE_REALTIME |
157 | #ifndef EV_USE_PORT |
120 | # define EV_USE_REALTIME 1 |
158 | # define EV_USE_PORT 0 |
121 | #endif |
159 | #endif |
122 | |
160 | |
123 | /**/ |
161 | /**/ |
124 | |
162 | |
125 | #ifndef CLOCK_MONOTONIC |
163 | #ifndef CLOCK_MONOTONIC |
… | |
… | |
137 | #endif |
175 | #endif |
138 | |
176 | |
139 | /**/ |
177 | /**/ |
140 | |
178 | |
141 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
179 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
142 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
180 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
143 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
181 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
144 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
182 | /*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
145 | |
183 | |
146 | #ifdef EV_H |
184 | #ifdef EV_H |
147 | # include EV_H |
185 | # include EV_H |
148 | #else |
186 | #else |
149 | # include "ev.h" |
187 | # include "ev.h" |
150 | #endif |
188 | #endif |
151 | |
189 | |
152 | #if __GNUC__ >= 3 |
190 | #if __GNUC__ >= 3 |
153 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
191 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
192 | # define inline_size static inline /* inline for codesize */ |
|
|
193 | # if EV_MINIMAL |
154 | # define inline inline |
194 | # define noinline __attribute__ ((noinline)) |
|
|
195 | # define inline_speed static noinline |
|
|
196 | # else |
|
|
197 | # define noinline |
|
|
198 | # define inline_speed static inline |
|
|
199 | # endif |
155 | #else |
200 | #else |
156 | # define expect(expr,value) (expr) |
201 | # define expect(expr,value) (expr) |
157 | # define inline static |
202 | # define inline_speed static |
|
|
203 | # define inline_minimal static |
|
|
204 | # define noinline |
158 | #endif |
205 | #endif |
159 | |
206 | |
160 | #define expect_false(expr) expect ((expr) != 0, 0) |
207 | #define expect_false(expr) expect ((expr) != 0, 0) |
161 | #define expect_true(expr) expect ((expr) != 0, 1) |
208 | #define expect_true(expr) expect ((expr) != 0, 1) |
162 | |
209 | |
163 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
210 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
164 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
211 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
165 | |
212 | |
166 | #define EMPTY /* required for microsofts broken pseudo-c compiler */ |
213 | #define EMPTY0 /* required for microsofts broken pseudo-c compiler */ |
|
|
214 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
167 | |
215 | |
168 | typedef struct ev_watcher *W; |
216 | typedef ev_watcher *W; |
169 | typedef struct ev_watcher_list *WL; |
217 | typedef ev_watcher_list *WL; |
170 | typedef struct ev_watcher_time *WT; |
218 | typedef ev_watcher_time *WT; |
171 | |
219 | |
172 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
220 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
173 | |
221 | |
174 | #ifdef _WIN32 |
222 | #ifdef _WIN32 |
175 | # include "ev_win32.c" |
223 | # include "ev_win32.c" |
… | |
… | |
177 | |
225 | |
178 | /*****************************************************************************/ |
226 | /*****************************************************************************/ |
179 | |
227 | |
180 | static void (*syserr_cb)(const char *msg); |
228 | static void (*syserr_cb)(const char *msg); |
181 | |
229 | |
|
|
230 | void |
182 | void ev_set_syserr_cb (void (*cb)(const char *msg)) |
231 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
183 | { |
232 | { |
184 | syserr_cb = cb; |
233 | syserr_cb = cb; |
185 | } |
234 | } |
186 | |
235 | |
187 | static void |
236 | static void noinline |
188 | syserr (const char *msg) |
237 | syserr (const char *msg) |
189 | { |
238 | { |
190 | if (!msg) |
239 | if (!msg) |
191 | msg = "(libev) system error"; |
240 | msg = "(libev) system error"; |
192 | |
241 | |
… | |
… | |
199 | } |
248 | } |
200 | } |
249 | } |
201 | |
250 | |
202 | static void *(*alloc)(void *ptr, long size); |
251 | static void *(*alloc)(void *ptr, long size); |
203 | |
252 | |
|
|
253 | void |
204 | void ev_set_allocator (void *(*cb)(void *ptr, long size)) |
254 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
205 | { |
255 | { |
206 | alloc = cb; |
256 | alloc = cb; |
207 | } |
257 | } |
208 | |
258 | |
209 | static void * |
259 | static void * |
… | |
… | |
251 | #include "ev_vars.h" |
301 | #include "ev_vars.h" |
252 | #undef VAR |
302 | #undef VAR |
253 | }; |
303 | }; |
254 | #include "ev_wrap.h" |
304 | #include "ev_wrap.h" |
255 | |
305 | |
256 | struct ev_loop default_loop_struct; |
306 | static struct ev_loop default_loop_struct; |
257 | static struct ev_loop *default_loop; |
307 | struct ev_loop *ev_default_loop_ptr; |
258 | |
308 | |
259 | #else |
309 | #else |
260 | |
310 | |
261 | ev_tstamp ev_rt_now; |
311 | ev_tstamp ev_rt_now; |
262 | #define VAR(name,decl) static decl; |
312 | #define VAR(name,decl) static decl; |
263 | #include "ev_vars.h" |
313 | #include "ev_vars.h" |
264 | #undef VAR |
314 | #undef VAR |
265 | |
315 | |
266 | static int default_loop; |
316 | static int ev_default_loop_ptr; |
267 | |
317 | |
268 | #endif |
318 | #endif |
269 | |
319 | |
270 | /*****************************************************************************/ |
320 | /*****************************************************************************/ |
271 | |
321 | |
… | |
… | |
281 | gettimeofday (&tv, 0); |
331 | gettimeofday (&tv, 0); |
282 | return tv.tv_sec + tv.tv_usec * 1e-6; |
332 | return tv.tv_sec + tv.tv_usec * 1e-6; |
283 | #endif |
333 | #endif |
284 | } |
334 | } |
285 | |
335 | |
286 | inline ev_tstamp |
336 | ev_tstamp inline_size |
287 | get_clock (void) |
337 | get_clock (void) |
288 | { |
338 | { |
289 | #if EV_USE_MONOTONIC |
339 | #if EV_USE_MONOTONIC |
290 | if (expect_true (have_monotonic)) |
340 | if (expect_true (have_monotonic)) |
291 | { |
341 | { |
… | |
… | |
304 | { |
354 | { |
305 | return ev_rt_now; |
355 | return ev_rt_now; |
306 | } |
356 | } |
307 | #endif |
357 | #endif |
308 | |
358 | |
309 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
359 | #define array_roundsize(type,n) (((n) | 4) & ~3) |
310 | |
360 | |
311 | #define array_needsize(type,base,cur,cnt,init) \ |
361 | #define array_needsize(type,base,cur,cnt,init) \ |
312 | if (expect_false ((cnt) > cur)) \ |
362 | if (expect_false ((cnt) > cur)) \ |
313 | { \ |
363 | { \ |
314 | int newcnt = cur; \ |
364 | int newcnt = cur; \ |
… | |
… | |
334 | #define array_free(stem, idx) \ |
384 | #define array_free(stem, idx) \ |
335 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
385 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
336 | |
386 | |
337 | /*****************************************************************************/ |
387 | /*****************************************************************************/ |
338 | |
388 | |
339 | static void |
389 | void noinline |
|
|
390 | ev_feed_event (EV_P_ void *w, int revents) |
|
|
391 | { |
|
|
392 | W w_ = (W)w; |
|
|
393 | |
|
|
394 | if (expect_false (w_->pending)) |
|
|
395 | { |
|
|
396 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
|
|
397 | return; |
|
|
398 | } |
|
|
399 | |
|
|
400 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
|
|
401 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
|
|
402 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
|
|
403 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
|
|
404 | } |
|
|
405 | |
|
|
406 | void inline_size |
|
|
407 | queue_events (EV_P_ W *events, int eventcnt, int type) |
|
|
408 | { |
|
|
409 | int i; |
|
|
410 | |
|
|
411 | for (i = 0; i < eventcnt; ++i) |
|
|
412 | ev_feed_event (EV_A_ events [i], type); |
|
|
413 | } |
|
|
414 | |
|
|
415 | /*****************************************************************************/ |
|
|
416 | |
|
|
417 | void inline_size |
340 | anfds_init (ANFD *base, int count) |
418 | anfds_init (ANFD *base, int count) |
341 | { |
419 | { |
342 | while (count--) |
420 | while (count--) |
343 | { |
421 | { |
344 | base->head = 0; |
422 | base->head = 0; |
… | |
… | |
347 | |
425 | |
348 | ++base; |
426 | ++base; |
349 | } |
427 | } |
350 | } |
428 | } |
351 | |
429 | |
352 | void |
430 | void inline_speed |
353 | ev_feed_event (EV_P_ void *w, int revents) |
|
|
354 | { |
|
|
355 | W w_ = (W)w; |
|
|
356 | |
|
|
357 | if (w_->pending) |
|
|
358 | { |
|
|
359 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
|
|
360 | return; |
|
|
361 | } |
|
|
362 | |
|
|
363 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
|
|
364 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
|
|
365 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
|
|
366 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
|
|
367 | } |
|
|
368 | |
|
|
369 | static void |
|
|
370 | queue_events (EV_P_ W *events, int eventcnt, int type) |
|
|
371 | { |
|
|
372 | int i; |
|
|
373 | |
|
|
374 | for (i = 0; i < eventcnt; ++i) |
|
|
375 | ev_feed_event (EV_A_ events [i], type); |
|
|
376 | } |
|
|
377 | |
|
|
378 | inline void |
|
|
379 | fd_event (EV_P_ int fd, int revents) |
431 | fd_event (EV_P_ int fd, int revents) |
380 | { |
432 | { |
381 | ANFD *anfd = anfds + fd; |
433 | ANFD *anfd = anfds + fd; |
382 | struct ev_io *w; |
434 | ev_io *w; |
383 | |
435 | |
384 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
436 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
385 | { |
437 | { |
386 | int ev = w->events & revents; |
438 | int ev = w->events & revents; |
387 | |
439 | |
388 | if (ev) |
440 | if (ev) |
389 | ev_feed_event (EV_A_ (W)w, ev); |
441 | ev_feed_event (EV_A_ (W)w, ev); |
… | |
… | |
394 | ev_feed_fd_event (EV_P_ int fd, int revents) |
446 | ev_feed_fd_event (EV_P_ int fd, int revents) |
395 | { |
447 | { |
396 | fd_event (EV_A_ fd, revents); |
448 | fd_event (EV_A_ fd, revents); |
397 | } |
449 | } |
398 | |
450 | |
399 | /*****************************************************************************/ |
451 | void inline_size |
400 | |
|
|
401 | static void |
|
|
402 | fd_reify (EV_P) |
452 | fd_reify (EV_P) |
403 | { |
453 | { |
404 | int i; |
454 | int i; |
405 | |
455 | |
406 | for (i = 0; i < fdchangecnt; ++i) |
456 | for (i = 0; i < fdchangecnt; ++i) |
407 | { |
457 | { |
408 | int fd = fdchanges [i]; |
458 | int fd = fdchanges [i]; |
409 | ANFD *anfd = anfds + fd; |
459 | ANFD *anfd = anfds + fd; |
410 | struct ev_io *w; |
460 | ev_io *w; |
411 | |
461 | |
412 | int events = 0; |
462 | int events = 0; |
413 | |
463 | |
414 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
464 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
415 | events |= w->events; |
465 | events |= w->events; |
416 | |
466 | |
417 | #if EV_SELECT_IS_WINSOCKET |
467 | #if EV_SELECT_IS_WINSOCKET |
418 | if (events) |
468 | if (events) |
419 | { |
469 | { |
… | |
… | |
423 | } |
473 | } |
424 | #endif |
474 | #endif |
425 | |
475 | |
426 | anfd->reify = 0; |
476 | anfd->reify = 0; |
427 | |
477 | |
428 | method_modify (EV_A_ fd, anfd->events, events); |
478 | backend_modify (EV_A_ fd, anfd->events, events); |
429 | anfd->events = events; |
479 | anfd->events = events; |
430 | } |
480 | } |
431 | |
481 | |
432 | fdchangecnt = 0; |
482 | fdchangecnt = 0; |
433 | } |
483 | } |
434 | |
484 | |
435 | static void |
485 | void inline_size |
436 | fd_change (EV_P_ int fd) |
486 | fd_change (EV_P_ int fd) |
437 | { |
487 | { |
438 | if (anfds [fd].reify) |
488 | if (expect_false (anfds [fd].reify)) |
439 | return; |
489 | return; |
440 | |
490 | |
441 | anfds [fd].reify = 1; |
491 | anfds [fd].reify = 1; |
442 | |
492 | |
443 | ++fdchangecnt; |
493 | ++fdchangecnt; |
444 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
494 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
445 | fdchanges [fdchangecnt - 1] = fd; |
495 | fdchanges [fdchangecnt - 1] = fd; |
446 | } |
496 | } |
447 | |
497 | |
448 | static void |
498 | void inline_speed |
449 | fd_kill (EV_P_ int fd) |
499 | fd_kill (EV_P_ int fd) |
450 | { |
500 | { |
451 | struct ev_io *w; |
501 | ev_io *w; |
452 | |
502 | |
453 | while ((w = (struct ev_io *)anfds [fd].head)) |
503 | while ((w = (ev_io *)anfds [fd].head)) |
454 | { |
504 | { |
455 | ev_io_stop (EV_A_ w); |
505 | ev_io_stop (EV_A_ w); |
456 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
506 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
457 | } |
507 | } |
458 | } |
508 | } |
459 | |
509 | |
460 | static int |
510 | int inline_size |
461 | fd_valid (int fd) |
511 | fd_valid (int fd) |
462 | { |
512 | { |
463 | #ifdef _WIN32 |
513 | #ifdef _WIN32 |
464 | return _get_osfhandle (fd) != -1; |
514 | return _get_osfhandle (fd) != -1; |
465 | #else |
515 | #else |
466 | return fcntl (fd, F_GETFD) != -1; |
516 | return fcntl (fd, F_GETFD) != -1; |
467 | #endif |
517 | #endif |
468 | } |
518 | } |
469 | |
519 | |
470 | /* called on EBADF to verify fds */ |
520 | /* called on EBADF to verify fds */ |
471 | static void |
521 | static void noinline |
472 | fd_ebadf (EV_P) |
522 | fd_ebadf (EV_P) |
473 | { |
523 | { |
474 | int fd; |
524 | int fd; |
475 | |
525 | |
476 | for (fd = 0; fd < anfdmax; ++fd) |
526 | for (fd = 0; fd < anfdmax; ++fd) |
… | |
… | |
478 | if (!fd_valid (fd) == -1 && errno == EBADF) |
528 | if (!fd_valid (fd) == -1 && errno == EBADF) |
479 | fd_kill (EV_A_ fd); |
529 | fd_kill (EV_A_ fd); |
480 | } |
530 | } |
481 | |
531 | |
482 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
532 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
483 | static void |
533 | static void noinline |
484 | fd_enomem (EV_P) |
534 | fd_enomem (EV_P) |
485 | { |
535 | { |
486 | int fd; |
536 | int fd; |
487 | |
537 | |
488 | for (fd = anfdmax; fd--; ) |
538 | for (fd = anfdmax; fd--; ) |
… | |
… | |
491 | fd_kill (EV_A_ fd); |
541 | fd_kill (EV_A_ fd); |
492 | return; |
542 | return; |
493 | } |
543 | } |
494 | } |
544 | } |
495 | |
545 | |
496 | /* usually called after fork if method needs to re-arm all fds from scratch */ |
546 | /* usually called after fork if backend needs to re-arm all fds from scratch */ |
497 | static void |
547 | static void noinline |
498 | fd_rearm_all (EV_P) |
548 | fd_rearm_all (EV_P) |
499 | { |
549 | { |
500 | int fd; |
550 | int fd; |
501 | |
551 | |
502 | /* this should be highly optimised to not do anything but set a flag */ |
552 | /* this should be highly optimised to not do anything but set a flag */ |
… | |
… | |
508 | } |
558 | } |
509 | } |
559 | } |
510 | |
560 | |
511 | /*****************************************************************************/ |
561 | /*****************************************************************************/ |
512 | |
562 | |
513 | static void |
563 | void inline_speed |
514 | upheap (WT *heap, int k) |
564 | upheap (WT *heap, int k) |
515 | { |
565 | { |
516 | WT w = heap [k]; |
566 | WT w = heap [k]; |
517 | |
567 | |
518 | while (k && heap [k >> 1]->at > w->at) |
568 | while (k && heap [k >> 1]->at > w->at) |
… | |
… | |
525 | heap [k] = w; |
575 | heap [k] = w; |
526 | ((W)heap [k])->active = k + 1; |
576 | ((W)heap [k])->active = k + 1; |
527 | |
577 | |
528 | } |
578 | } |
529 | |
579 | |
530 | static void |
580 | void inline_speed |
531 | downheap (WT *heap, int N, int k) |
581 | downheap (WT *heap, int N, int k) |
532 | { |
582 | { |
533 | WT w = heap [k]; |
583 | WT w = heap [k]; |
534 | |
584 | |
535 | while (k < (N >> 1)) |
585 | while (k < (N >> 1)) |
… | |
… | |
549 | |
599 | |
550 | heap [k] = w; |
600 | heap [k] = w; |
551 | ((W)heap [k])->active = k + 1; |
601 | ((W)heap [k])->active = k + 1; |
552 | } |
602 | } |
553 | |
603 | |
554 | inline void |
604 | void inline_size |
555 | adjustheap (WT *heap, int N, int k) |
605 | adjustheap (WT *heap, int N, int k) |
556 | { |
606 | { |
557 | upheap (heap, k); |
607 | upheap (heap, k); |
558 | downheap (heap, N, k); |
608 | downheap (heap, N, k); |
559 | } |
609 | } |
… | |
… | |
569 | static ANSIG *signals; |
619 | static ANSIG *signals; |
570 | static int signalmax; |
620 | static int signalmax; |
571 | |
621 | |
572 | static int sigpipe [2]; |
622 | static int sigpipe [2]; |
573 | static sig_atomic_t volatile gotsig; |
623 | static sig_atomic_t volatile gotsig; |
574 | static struct ev_io sigev; |
624 | static ev_io sigev; |
575 | |
625 | |
576 | static void |
626 | void inline_size |
577 | signals_init (ANSIG *base, int count) |
627 | signals_init (ANSIG *base, int count) |
578 | { |
628 | { |
579 | while (count--) |
629 | while (count--) |
580 | { |
630 | { |
581 | base->head = 0; |
631 | base->head = 0; |
… | |
… | |
601 | write (sigpipe [1], &signum, 1); |
651 | write (sigpipe [1], &signum, 1); |
602 | errno = old_errno; |
652 | errno = old_errno; |
603 | } |
653 | } |
604 | } |
654 | } |
605 | |
655 | |
606 | void |
656 | void noinline |
607 | ev_feed_signal_event (EV_P_ int signum) |
657 | ev_feed_signal_event (EV_P_ int signum) |
608 | { |
658 | { |
609 | WL w; |
659 | WL w; |
610 | |
660 | |
611 | #if EV_MULTIPLICITY |
661 | #if EV_MULTIPLICITY |
612 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
662 | assert (("feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr)); |
613 | #endif |
663 | #endif |
614 | |
664 | |
615 | --signum; |
665 | --signum; |
616 | |
666 | |
617 | if (signum < 0 || signum >= signalmax) |
667 | if (signum < 0 || signum >= signalmax) |
… | |
… | |
622 | for (w = signals [signum].head; w; w = w->next) |
672 | for (w = signals [signum].head; w; w = w->next) |
623 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
673 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
624 | } |
674 | } |
625 | |
675 | |
626 | static void |
676 | static void |
627 | sigcb (EV_P_ struct ev_io *iow, int revents) |
677 | sigcb (EV_P_ ev_io *iow, int revents) |
628 | { |
678 | { |
629 | int signum; |
679 | int signum; |
630 | |
680 | |
631 | read (sigpipe [0], &revents, 1); |
681 | read (sigpipe [0], &revents, 1); |
632 | gotsig = 0; |
682 | gotsig = 0; |
… | |
… | |
634 | for (signum = signalmax; signum--; ) |
684 | for (signum = signalmax; signum--; ) |
635 | if (signals [signum].gotsig) |
685 | if (signals [signum].gotsig) |
636 | ev_feed_signal_event (EV_A_ signum + 1); |
686 | ev_feed_signal_event (EV_A_ signum + 1); |
637 | } |
687 | } |
638 | |
688 | |
639 | inline void |
689 | void inline_size |
640 | fd_intern (int fd) |
690 | fd_intern (int fd) |
641 | { |
691 | { |
642 | #ifdef _WIN32 |
692 | #ifdef _WIN32 |
643 | int arg = 1; |
693 | int arg = 1; |
644 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
694 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
… | |
… | |
646 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
696 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
647 | fcntl (fd, F_SETFL, O_NONBLOCK); |
697 | fcntl (fd, F_SETFL, O_NONBLOCK); |
648 | #endif |
698 | #endif |
649 | } |
699 | } |
650 | |
700 | |
651 | static void |
701 | static void noinline |
652 | siginit (EV_P) |
702 | siginit (EV_P) |
653 | { |
703 | { |
654 | fd_intern (sigpipe [0]); |
704 | fd_intern (sigpipe [0]); |
655 | fd_intern (sigpipe [1]); |
705 | fd_intern (sigpipe [1]); |
656 | |
706 | |
… | |
… | |
659 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
709 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
660 | } |
710 | } |
661 | |
711 | |
662 | /*****************************************************************************/ |
712 | /*****************************************************************************/ |
663 | |
713 | |
664 | static struct ev_child *childs [PID_HASHSIZE]; |
714 | static ev_child *childs [PID_HASHSIZE]; |
665 | |
715 | |
666 | #ifndef _WIN32 |
716 | #ifndef _WIN32 |
667 | |
717 | |
668 | static struct ev_signal childev; |
718 | static ev_signal childev; |
669 | |
719 | |
670 | #ifndef WCONTINUED |
720 | void inline_speed |
671 | # define WCONTINUED 0 |
|
|
672 | #endif |
|
|
673 | |
|
|
674 | static void |
|
|
675 | child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) |
721 | child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) |
676 | { |
722 | { |
677 | struct ev_child *w; |
723 | ev_child *w; |
678 | |
724 | |
679 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
725 | for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
680 | if (w->pid == pid || !w->pid) |
726 | if (w->pid == pid || !w->pid) |
681 | { |
727 | { |
682 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
728 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
683 | w->rpid = pid; |
729 | w->rpid = pid; |
684 | w->rstatus = status; |
730 | w->rstatus = status; |
685 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
731 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
686 | } |
732 | } |
687 | } |
733 | } |
688 | |
734 | |
|
|
735 | #ifndef WCONTINUED |
|
|
736 | # define WCONTINUED 0 |
|
|
737 | #endif |
|
|
738 | |
689 | static void |
739 | static void |
690 | childcb (EV_P_ struct ev_signal *sw, int revents) |
740 | childcb (EV_P_ ev_signal *sw, int revents) |
691 | { |
741 | { |
692 | int pid, status; |
742 | int pid, status; |
693 | |
743 | |
|
|
744 | /* some systems define WCONTINUED but then fail to support it (linux 2.4) */ |
694 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
745 | if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
695 | { |
746 | if (!WCONTINUED |
|
|
747 | || errno != EINVAL |
|
|
748 | || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED))) |
|
|
749 | return; |
|
|
750 | |
696 | /* make sure we are called again until all childs have been reaped */ |
751 | /* make sure we are called again until all childs have been reaped */ |
|
|
752 | /* we need to do it this way so that the callback gets called before we continue */ |
697 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
753 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
698 | |
754 | |
699 | child_reap (EV_A_ sw, pid, pid, status); |
755 | child_reap (EV_A_ sw, pid, pid, status); |
700 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
756 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ |
701 | } |
|
|
702 | } |
757 | } |
703 | |
758 | |
704 | #endif |
759 | #endif |
705 | |
760 | |
706 | /*****************************************************************************/ |
761 | /*****************************************************************************/ |
707 | |
762 | |
|
|
763 | #if EV_USE_PORT |
|
|
764 | # include "ev_port.c" |
|
|
765 | #endif |
708 | #if EV_USE_KQUEUE |
766 | #if EV_USE_KQUEUE |
709 | # include "ev_kqueue.c" |
767 | # include "ev_kqueue.c" |
710 | #endif |
768 | #endif |
711 | #if EV_USE_EPOLL |
769 | #if EV_USE_EPOLL |
712 | # include "ev_epoll.c" |
770 | # include "ev_epoll.c" |
… | |
… | |
729 | { |
787 | { |
730 | return EV_VERSION_MINOR; |
788 | return EV_VERSION_MINOR; |
731 | } |
789 | } |
732 | |
790 | |
733 | /* return true if we are running with elevated privileges and should ignore env variables */ |
791 | /* return true if we are running with elevated privileges and should ignore env variables */ |
734 | static int |
792 | int inline_size |
735 | enable_secure (void) |
793 | enable_secure (void) |
736 | { |
794 | { |
737 | #ifdef _WIN32 |
795 | #ifdef _WIN32 |
738 | return 0; |
796 | return 0; |
739 | #else |
797 | #else |
740 | return getuid () != geteuid () |
798 | return getuid () != geteuid () |
741 | || getgid () != getegid (); |
799 | || getgid () != getegid (); |
742 | #endif |
800 | #endif |
743 | } |
801 | } |
744 | |
802 | |
745 | int |
803 | unsigned int |
746 | ev_method (EV_P) |
804 | ev_supported_backends (void) |
747 | { |
805 | { |
748 | return method; |
806 | unsigned int flags = 0; |
|
|
807 | |
|
|
808 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
|
|
809 | if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; |
|
|
810 | if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL; |
|
|
811 | if (EV_USE_POLL ) flags |= EVBACKEND_POLL; |
|
|
812 | if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; |
|
|
813 | |
|
|
814 | return flags; |
|
|
815 | } |
|
|
816 | |
|
|
817 | unsigned int |
|
|
818 | ev_recommended_backends (void) |
|
|
819 | { |
|
|
820 | unsigned int flags = ev_supported_backends (); |
|
|
821 | |
|
|
822 | #ifndef __NetBSD__ |
|
|
823 | /* kqueue is borked on everything but netbsd apparently */ |
|
|
824 | /* it usually doesn't work correctly on anything but sockets and pipes */ |
|
|
825 | flags &= ~EVBACKEND_KQUEUE; |
|
|
826 | #endif |
|
|
827 | #ifdef __APPLE__ |
|
|
828 | // flags &= ~EVBACKEND_KQUEUE; for documentation |
|
|
829 | flags &= ~EVBACKEND_POLL; |
|
|
830 | #endif |
|
|
831 | |
|
|
832 | return flags; |
|
|
833 | } |
|
|
834 | |
|
|
835 | unsigned int |
|
|
836 | ev_embeddable_backends (void) |
|
|
837 | { |
|
|
838 | return EVBACKEND_EPOLL |
|
|
839 | | EVBACKEND_KQUEUE |
|
|
840 | | EVBACKEND_PORT; |
|
|
841 | } |
|
|
842 | |
|
|
843 | unsigned int |
|
|
844 | ev_backend (EV_P) |
|
|
845 | { |
|
|
846 | return backend; |
749 | } |
847 | } |
750 | |
848 | |
751 | static void |
849 | static void |
752 | loop_init (EV_P_ int methods) |
850 | loop_init (EV_P_ unsigned int flags) |
753 | { |
851 | { |
754 | if (!method) |
852 | if (!backend) |
755 | { |
853 | { |
756 | #if EV_USE_MONOTONIC |
854 | #if EV_USE_MONOTONIC |
757 | { |
855 | { |
758 | struct timespec ts; |
856 | struct timespec ts; |
759 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
857 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
… | |
… | |
764 | ev_rt_now = ev_time (); |
862 | ev_rt_now = ev_time (); |
765 | mn_now = get_clock (); |
863 | mn_now = get_clock (); |
766 | now_floor = mn_now; |
864 | now_floor = mn_now; |
767 | rtmn_diff = ev_rt_now - mn_now; |
865 | rtmn_diff = ev_rt_now - mn_now; |
768 | |
866 | |
769 | if (methods == EVMETHOD_AUTO) |
867 | if (!(flags & EVFLAG_NOENV) |
770 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
868 | && !enable_secure () |
|
|
869 | && getenv ("LIBEV_FLAGS")) |
771 | methods = atoi (getenv ("LIBEV_METHODS")); |
870 | flags = atoi (getenv ("LIBEV_FLAGS")); |
772 | else |
|
|
773 | methods = EVMETHOD_ANY; |
|
|
774 | |
871 | |
775 | method = 0; |
872 | if (!(flags & 0x0000ffffUL)) |
|
|
873 | flags |= ev_recommended_backends (); |
|
|
874 | |
|
|
875 | backend = 0; |
|
|
876 | #if EV_USE_PORT |
|
|
877 | if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); |
|
|
878 | #endif |
776 | #if EV_USE_KQUEUE |
879 | #if EV_USE_KQUEUE |
777 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
880 | if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags); |
778 | #endif |
881 | #endif |
779 | #if EV_USE_EPOLL |
882 | #if EV_USE_EPOLL |
780 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
883 | if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags); |
781 | #endif |
884 | #endif |
782 | #if EV_USE_POLL |
885 | #if EV_USE_POLL |
783 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
886 | if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags); |
784 | #endif |
887 | #endif |
785 | #if EV_USE_SELECT |
888 | #if EV_USE_SELECT |
786 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
889 | if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); |
787 | #endif |
890 | #endif |
788 | |
891 | |
789 | ev_init (&sigev, sigcb); |
892 | ev_init (&sigev, sigcb); |
790 | ev_set_priority (&sigev, EV_MAXPRI); |
893 | ev_set_priority (&sigev, EV_MAXPRI); |
791 | } |
894 | } |
792 | } |
895 | } |
793 | |
896 | |
794 | void |
897 | static void |
795 | loop_destroy (EV_P) |
898 | loop_destroy (EV_P) |
796 | { |
899 | { |
797 | int i; |
900 | int i; |
798 | |
901 | |
|
|
902 | #if EV_USE_PORT |
|
|
903 | if (backend == EVBACKEND_PORT ) port_destroy (EV_A); |
|
|
904 | #endif |
799 | #if EV_USE_KQUEUE |
905 | #if EV_USE_KQUEUE |
800 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
906 | if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A); |
801 | #endif |
907 | #endif |
802 | #if EV_USE_EPOLL |
908 | #if EV_USE_EPOLL |
803 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
909 | if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A); |
804 | #endif |
910 | #endif |
805 | #if EV_USE_POLL |
911 | #if EV_USE_POLL |
806 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
912 | if (backend == EVBACKEND_POLL ) poll_destroy (EV_A); |
807 | #endif |
913 | #endif |
808 | #if EV_USE_SELECT |
914 | #if EV_USE_SELECT |
809 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
915 | if (backend == EVBACKEND_SELECT) select_destroy (EV_A); |
810 | #endif |
916 | #endif |
811 | |
917 | |
812 | for (i = NUMPRI; i--; ) |
918 | for (i = NUMPRI; i--; ) |
813 | array_free (pending, [i]); |
919 | array_free (pending, [i]); |
814 | |
920 | |
815 | /* have to use the microsoft-never-gets-it-right macro */ |
921 | /* have to use the microsoft-never-gets-it-right macro */ |
816 | array_free (fdchange, EMPTY); |
922 | array_free (fdchange, EMPTY0); |
817 | array_free (timer, EMPTY); |
923 | array_free (timer, EMPTY0); |
818 | #if EV_PERIODICS |
924 | #if EV_PERIODIC_ENABLE |
819 | array_free (periodic, EMPTY); |
925 | array_free (periodic, EMPTY0); |
820 | #endif |
926 | #endif |
821 | array_free (idle, EMPTY); |
927 | array_free (idle, EMPTY0); |
822 | array_free (prepare, EMPTY); |
928 | array_free (prepare, EMPTY0); |
823 | array_free (check, EMPTY); |
929 | array_free (check, EMPTY0); |
824 | |
930 | |
825 | method = 0; |
931 | backend = 0; |
826 | } |
932 | } |
827 | |
933 | |
828 | static void |
934 | static void |
829 | loop_fork (EV_P) |
935 | loop_fork (EV_P) |
830 | { |
936 | { |
|
|
937 | #if EV_USE_PORT |
|
|
938 | if (backend == EVBACKEND_PORT ) port_fork (EV_A); |
|
|
939 | #endif |
|
|
940 | #if EV_USE_KQUEUE |
|
|
941 | if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A); |
|
|
942 | #endif |
831 | #if EV_USE_EPOLL |
943 | #if EV_USE_EPOLL |
832 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
944 | if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); |
833 | #endif |
|
|
834 | #if EV_USE_KQUEUE |
|
|
835 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
|
|
836 | #endif |
945 | #endif |
837 | |
946 | |
838 | if (ev_is_active (&sigev)) |
947 | if (ev_is_active (&sigev)) |
839 | { |
948 | { |
840 | /* default loop */ |
949 | /* default loop */ |
… | |
… | |
853 | postfork = 0; |
962 | postfork = 0; |
854 | } |
963 | } |
855 | |
964 | |
856 | #if EV_MULTIPLICITY |
965 | #if EV_MULTIPLICITY |
857 | struct ev_loop * |
966 | struct ev_loop * |
858 | ev_loop_new (int methods) |
967 | ev_loop_new (unsigned int flags) |
859 | { |
968 | { |
860 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
969 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
861 | |
970 | |
862 | memset (loop, 0, sizeof (struct ev_loop)); |
971 | memset (loop, 0, sizeof (struct ev_loop)); |
863 | |
972 | |
864 | loop_init (EV_A_ methods); |
973 | loop_init (EV_A_ flags); |
865 | |
974 | |
866 | if (ev_method (EV_A)) |
975 | if (ev_backend (EV_A)) |
867 | return loop; |
976 | return loop; |
868 | |
977 | |
869 | return 0; |
978 | return 0; |
870 | } |
979 | } |
871 | |
980 | |
… | |
… | |
884 | |
993 | |
885 | #endif |
994 | #endif |
886 | |
995 | |
887 | #if EV_MULTIPLICITY |
996 | #if EV_MULTIPLICITY |
888 | struct ev_loop * |
997 | struct ev_loop * |
|
|
998 | ev_default_loop_init (unsigned int flags) |
889 | #else |
999 | #else |
890 | int |
1000 | int |
|
|
1001 | ev_default_loop (unsigned int flags) |
891 | #endif |
1002 | #endif |
892 | ev_default_loop (int methods) |
|
|
893 | { |
1003 | { |
894 | if (sigpipe [0] == sigpipe [1]) |
1004 | if (sigpipe [0] == sigpipe [1]) |
895 | if (pipe (sigpipe)) |
1005 | if (pipe (sigpipe)) |
896 | return 0; |
1006 | return 0; |
897 | |
1007 | |
898 | if (!default_loop) |
1008 | if (!ev_default_loop_ptr) |
899 | { |
1009 | { |
900 | #if EV_MULTIPLICITY |
1010 | #if EV_MULTIPLICITY |
901 | struct ev_loop *loop = default_loop = &default_loop_struct; |
1011 | struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct; |
902 | #else |
1012 | #else |
903 | default_loop = 1; |
1013 | ev_default_loop_ptr = 1; |
904 | #endif |
1014 | #endif |
905 | |
1015 | |
906 | loop_init (EV_A_ methods); |
1016 | loop_init (EV_A_ flags); |
907 | |
1017 | |
908 | if (ev_method (EV_A)) |
1018 | if (ev_backend (EV_A)) |
909 | { |
1019 | { |
910 | siginit (EV_A); |
1020 | siginit (EV_A); |
911 | |
1021 | |
912 | #ifndef _WIN32 |
1022 | #ifndef _WIN32 |
913 | ev_signal_init (&childev, childcb, SIGCHLD); |
1023 | ev_signal_init (&childev, childcb, SIGCHLD); |
… | |
… | |
915 | ev_signal_start (EV_A_ &childev); |
1025 | ev_signal_start (EV_A_ &childev); |
916 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
1026 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
917 | #endif |
1027 | #endif |
918 | } |
1028 | } |
919 | else |
1029 | else |
920 | default_loop = 0; |
1030 | ev_default_loop_ptr = 0; |
921 | } |
1031 | } |
922 | |
1032 | |
923 | return default_loop; |
1033 | return ev_default_loop_ptr; |
924 | } |
1034 | } |
925 | |
1035 | |
926 | void |
1036 | void |
927 | ev_default_destroy (void) |
1037 | ev_default_destroy (void) |
928 | { |
1038 | { |
929 | #if EV_MULTIPLICITY |
1039 | #if EV_MULTIPLICITY |
930 | struct ev_loop *loop = default_loop; |
1040 | struct ev_loop *loop = ev_default_loop_ptr; |
931 | #endif |
1041 | #endif |
932 | |
1042 | |
933 | #ifndef _WIN32 |
1043 | #ifndef _WIN32 |
934 | ev_ref (EV_A); /* child watcher */ |
1044 | ev_ref (EV_A); /* child watcher */ |
935 | ev_signal_stop (EV_A_ &childev); |
1045 | ev_signal_stop (EV_A_ &childev); |
… | |
… | |
946 | |
1056 | |
947 | void |
1057 | void |
948 | ev_default_fork (void) |
1058 | ev_default_fork (void) |
949 | { |
1059 | { |
950 | #if EV_MULTIPLICITY |
1060 | #if EV_MULTIPLICITY |
951 | struct ev_loop *loop = default_loop; |
1061 | struct ev_loop *loop = ev_default_loop_ptr; |
952 | #endif |
1062 | #endif |
953 | |
1063 | |
954 | if (method) |
1064 | if (backend) |
955 | postfork = 1; |
1065 | postfork = 1; |
956 | } |
1066 | } |
957 | |
1067 | |
958 | /*****************************************************************************/ |
1068 | /*****************************************************************************/ |
959 | |
1069 | |
960 | static int |
1070 | int inline_size |
961 | any_pending (EV_P) |
1071 | any_pending (EV_P) |
962 | { |
1072 | { |
963 | int pri; |
1073 | int pri; |
964 | |
1074 | |
965 | for (pri = NUMPRI; pri--; ) |
1075 | for (pri = NUMPRI; pri--; ) |
… | |
… | |
967 | return 1; |
1077 | return 1; |
968 | |
1078 | |
969 | return 0; |
1079 | return 0; |
970 | } |
1080 | } |
971 | |
1081 | |
972 | static void |
1082 | void inline_speed |
973 | call_pending (EV_P) |
1083 | call_pending (EV_P) |
974 | { |
1084 | { |
975 | int pri; |
1085 | int pri; |
976 | |
1086 | |
977 | for (pri = NUMPRI; pri--; ) |
1087 | for (pri = NUMPRI; pri--; ) |
978 | while (pendingcnt [pri]) |
1088 | while (pendingcnt [pri]) |
979 | { |
1089 | { |
980 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1090 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
981 | |
1091 | |
982 | if (p->w) |
1092 | if (expect_true (p->w)) |
983 | { |
1093 | { |
|
|
1094 | assert (("non-pending watcher on pending list", p->w->pending)); |
|
|
1095 | |
984 | p->w->pending = 0; |
1096 | p->w->pending = 0; |
985 | EV_CB_INVOKE (p->w, p->events); |
1097 | EV_CB_INVOKE (p->w, p->events); |
986 | } |
1098 | } |
987 | } |
1099 | } |
988 | } |
1100 | } |
989 | |
1101 | |
990 | static void |
1102 | void inline_size |
991 | timers_reify (EV_P) |
1103 | timers_reify (EV_P) |
992 | { |
1104 | { |
993 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1105 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
994 | { |
1106 | { |
995 | struct ev_timer *w = timers [0]; |
1107 | ev_timer *w = timers [0]; |
996 | |
1108 | |
997 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
1109 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
998 | |
1110 | |
999 | /* first reschedule or stop timer */ |
1111 | /* first reschedule or stop timer */ |
1000 | if (w->repeat) |
1112 | if (w->repeat) |
… | |
… | |
1012 | |
1124 | |
1013 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1125 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1014 | } |
1126 | } |
1015 | } |
1127 | } |
1016 | |
1128 | |
1017 | #if EV_PERIODICS |
1129 | #if EV_PERIODIC_ENABLE |
1018 | static void |
1130 | void inline_size |
1019 | periodics_reify (EV_P) |
1131 | periodics_reify (EV_P) |
1020 | { |
1132 | { |
1021 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1133 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1022 | { |
1134 | { |
1023 | struct ev_periodic *w = periodics [0]; |
1135 | ev_periodic *w = periodics [0]; |
1024 | |
1136 | |
1025 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1137 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1026 | |
1138 | |
1027 | /* first reschedule or stop timer */ |
1139 | /* first reschedule or stop timer */ |
1028 | if (w->reschedule_cb) |
1140 | if (w->reschedule_cb) |
1029 | { |
1141 | { |
1030 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
1142 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
1031 | |
|
|
1032 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
1143 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
1033 | downheap ((WT *)periodics, periodiccnt, 0); |
1144 | downheap ((WT *)periodics, periodiccnt, 0); |
1034 | } |
1145 | } |
1035 | else if (w->interval) |
1146 | else if (w->interval) |
1036 | { |
1147 | { |
… | |
… | |
1043 | |
1154 | |
1044 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1155 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1045 | } |
1156 | } |
1046 | } |
1157 | } |
1047 | |
1158 | |
1048 | static void |
1159 | static void noinline |
1049 | periodics_reschedule (EV_P) |
1160 | periodics_reschedule (EV_P) |
1050 | { |
1161 | { |
1051 | int i; |
1162 | int i; |
1052 | |
1163 | |
1053 | /* adjust periodics after time jump */ |
1164 | /* adjust periodics after time jump */ |
1054 | for (i = 0; i < periodiccnt; ++i) |
1165 | for (i = 0; i < periodiccnt; ++i) |
1055 | { |
1166 | { |
1056 | struct ev_periodic *w = periodics [i]; |
1167 | ev_periodic *w = periodics [i]; |
1057 | |
1168 | |
1058 | if (w->reschedule_cb) |
1169 | if (w->reschedule_cb) |
1059 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1170 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1060 | else if (w->interval) |
1171 | else if (w->interval) |
1061 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1172 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
… | |
… | |
1065 | for (i = periodiccnt >> 1; i--; ) |
1176 | for (i = periodiccnt >> 1; i--; ) |
1066 | downheap ((WT *)periodics, periodiccnt, i); |
1177 | downheap ((WT *)periodics, periodiccnt, i); |
1067 | } |
1178 | } |
1068 | #endif |
1179 | #endif |
1069 | |
1180 | |
1070 | inline int |
1181 | int inline_size |
1071 | time_update_monotonic (EV_P) |
1182 | time_update_monotonic (EV_P) |
1072 | { |
1183 | { |
1073 | mn_now = get_clock (); |
1184 | mn_now = get_clock (); |
1074 | |
1185 | |
1075 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1186 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
… | |
… | |
1083 | ev_rt_now = ev_time (); |
1194 | ev_rt_now = ev_time (); |
1084 | return 1; |
1195 | return 1; |
1085 | } |
1196 | } |
1086 | } |
1197 | } |
1087 | |
1198 | |
1088 | static void |
1199 | void inline_size |
1089 | time_update (EV_P) |
1200 | time_update (EV_P) |
1090 | { |
1201 | { |
1091 | int i; |
1202 | int i; |
1092 | |
1203 | |
1093 | #if EV_USE_MONOTONIC |
1204 | #if EV_USE_MONOTONIC |
… | |
… | |
1095 | { |
1206 | { |
1096 | if (time_update_monotonic (EV_A)) |
1207 | if (time_update_monotonic (EV_A)) |
1097 | { |
1208 | { |
1098 | ev_tstamp odiff = rtmn_diff; |
1209 | ev_tstamp odiff = rtmn_diff; |
1099 | |
1210 | |
1100 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1211 | /* loop a few times, before making important decisions. |
|
|
1212 | * on the choice of "4": one iteration isn't enough, |
|
|
1213 | * in case we get preempted during the calls to |
|
|
1214 | * ev_time and get_clock. a second call is almost guarenteed |
|
|
1215 | * to succeed in that case, though. and looping a few more times |
|
|
1216 | * doesn't hurt either as we only do this on time-jumps or |
|
|
1217 | * in the unlikely event of getting preempted here. |
|
|
1218 | */ |
|
|
1219 | for (i = 4; --i; ) |
1101 | { |
1220 | { |
1102 | rtmn_diff = ev_rt_now - mn_now; |
1221 | rtmn_diff = ev_rt_now - mn_now; |
1103 | |
1222 | |
1104 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1223 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1105 | return; /* all is well */ |
1224 | return; /* all is well */ |
… | |
… | |
1107 | ev_rt_now = ev_time (); |
1226 | ev_rt_now = ev_time (); |
1108 | mn_now = get_clock (); |
1227 | mn_now = get_clock (); |
1109 | now_floor = mn_now; |
1228 | now_floor = mn_now; |
1110 | } |
1229 | } |
1111 | |
1230 | |
1112 | # if EV_PERIODICS |
1231 | # if EV_PERIODIC_ENABLE |
1113 | periodics_reschedule (EV_A); |
1232 | periodics_reschedule (EV_A); |
1114 | # endif |
1233 | # endif |
1115 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1234 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1116 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1235 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1117 | } |
1236 | } |
… | |
… | |
1121 | { |
1240 | { |
1122 | ev_rt_now = ev_time (); |
1241 | ev_rt_now = ev_time (); |
1123 | |
1242 | |
1124 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1243 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1125 | { |
1244 | { |
1126 | #if EV_PERIODICS |
1245 | #if EV_PERIODIC_ENABLE |
1127 | periodics_reschedule (EV_A); |
1246 | periodics_reschedule (EV_A); |
1128 | #endif |
1247 | #endif |
1129 | |
1248 | |
1130 | /* adjust timers. this is easy, as the offset is the same for all */ |
1249 | /* adjust timers. this is easy, as the offset is the same for all */ |
1131 | for (i = 0; i < timercnt; ++i) |
1250 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
1151 | static int loop_done; |
1270 | static int loop_done; |
1152 | |
1271 | |
1153 | void |
1272 | void |
1154 | ev_loop (EV_P_ int flags) |
1273 | ev_loop (EV_P_ int flags) |
1155 | { |
1274 | { |
1156 | double block; |
|
|
1157 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
1275 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) |
|
|
1276 | ? EVUNLOOP_ONE |
|
|
1277 | : EVUNLOOP_CANCEL; |
1158 | |
1278 | |
1159 | do |
1279 | while (activecnt) |
1160 | { |
1280 | { |
1161 | /* queue check watchers (and execute them) */ |
1281 | /* queue check watchers (and execute them) */ |
1162 | if (expect_false (preparecnt)) |
1282 | if (expect_false (preparecnt)) |
1163 | { |
1283 | { |
1164 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1284 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
… | |
… | |
1171 | |
1291 | |
1172 | /* update fd-related kernel structures */ |
1292 | /* update fd-related kernel structures */ |
1173 | fd_reify (EV_A); |
1293 | fd_reify (EV_A); |
1174 | |
1294 | |
1175 | /* calculate blocking time */ |
1295 | /* calculate blocking time */ |
|
|
1296 | { |
|
|
1297 | double block; |
1176 | |
1298 | |
1177 | /* we only need this for !monotonic clock or timers, but as we basically |
1299 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1178 | always have timers, we just calculate it always */ |
1300 | block = 0.; /* do not block at all */ |
|
|
1301 | else |
|
|
1302 | { |
|
|
1303 | /* update time to cancel out callback processing overhead */ |
1179 | #if EV_USE_MONOTONIC |
1304 | #if EV_USE_MONOTONIC |
1180 | if (expect_true (have_monotonic)) |
1305 | if (expect_true (have_monotonic)) |
1181 | time_update_monotonic (EV_A); |
1306 | time_update_monotonic (EV_A); |
1182 | else |
1307 | else |
1183 | #endif |
1308 | #endif |
1184 | { |
1309 | { |
1185 | ev_rt_now = ev_time (); |
1310 | ev_rt_now = ev_time (); |
1186 | mn_now = ev_rt_now; |
1311 | mn_now = ev_rt_now; |
1187 | } |
1312 | } |
1188 | |
1313 | |
1189 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
|
|
1190 | block = 0.; |
|
|
1191 | else |
|
|
1192 | { |
|
|
1193 | block = MAX_BLOCKTIME; |
1314 | block = MAX_BLOCKTIME; |
1194 | |
1315 | |
1195 | if (timercnt) |
1316 | if (timercnt) |
1196 | { |
1317 | { |
1197 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1318 | ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; |
1198 | if (block > to) block = to; |
1319 | if (block > to) block = to; |
1199 | } |
1320 | } |
1200 | |
1321 | |
1201 | #if EV_PERIODICS |
1322 | #if EV_PERIODIC_ENABLE |
1202 | if (periodiccnt) |
1323 | if (periodiccnt) |
1203 | { |
1324 | { |
1204 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1325 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; |
1205 | if (block > to) block = to; |
1326 | if (block > to) block = to; |
1206 | } |
1327 | } |
1207 | #endif |
1328 | #endif |
1208 | |
1329 | |
1209 | if (block < 0.) block = 0.; |
1330 | if (expect_false (block < 0.)) block = 0.; |
1210 | } |
1331 | } |
1211 | |
1332 | |
1212 | method_poll (EV_A_ block); |
1333 | backend_poll (EV_A_ block); |
|
|
1334 | } |
1213 | |
1335 | |
1214 | /* update ev_rt_now, do magic */ |
1336 | /* update ev_rt_now, do magic */ |
1215 | time_update (EV_A); |
1337 | time_update (EV_A); |
1216 | |
1338 | |
1217 | /* queue pending timers and reschedule them */ |
1339 | /* queue pending timers and reschedule them */ |
1218 | timers_reify (EV_A); /* relative timers called last */ |
1340 | timers_reify (EV_A); /* relative timers called last */ |
1219 | #if EV_PERIODICS |
1341 | #if EV_PERIODIC_ENABLE |
1220 | periodics_reify (EV_A); /* absolute timers called first */ |
1342 | periodics_reify (EV_A); /* absolute timers called first */ |
1221 | #endif |
1343 | #endif |
1222 | |
1344 | |
1223 | /* queue idle watchers unless io or timers are pending */ |
1345 | /* queue idle watchers unless other events are pending */ |
1224 | if (idlecnt && !any_pending (EV_A)) |
1346 | if (idlecnt && !any_pending (EV_A)) |
1225 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1347 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1226 | |
1348 | |
1227 | /* queue check watchers, to be executed first */ |
1349 | /* queue check watchers, to be executed first */ |
1228 | if (checkcnt) |
1350 | if (expect_false (checkcnt)) |
1229 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1351 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1230 | |
1352 | |
1231 | call_pending (EV_A); |
1353 | call_pending (EV_A); |
1232 | } |
|
|
1233 | while (activecnt && !loop_done); |
|
|
1234 | |
1354 | |
1235 | if (loop_done != 2) |
1355 | if (expect_false (loop_done)) |
1236 | loop_done = 0; |
1356 | break; |
|
|
1357 | } |
|
|
1358 | |
|
|
1359 | if (loop_done == EVUNLOOP_ONE) |
|
|
1360 | loop_done = EVUNLOOP_CANCEL; |
1237 | } |
1361 | } |
1238 | |
1362 | |
1239 | void |
1363 | void |
1240 | ev_unloop (EV_P_ int how) |
1364 | ev_unloop (EV_P_ int how) |
1241 | { |
1365 | { |
1242 | loop_done = how; |
1366 | loop_done = how; |
1243 | } |
1367 | } |
1244 | |
1368 | |
1245 | /*****************************************************************************/ |
1369 | /*****************************************************************************/ |
1246 | |
1370 | |
1247 | inline void |
1371 | void inline_size |
1248 | wlist_add (WL *head, WL elem) |
1372 | wlist_add (WL *head, WL elem) |
1249 | { |
1373 | { |
1250 | elem->next = *head; |
1374 | elem->next = *head; |
1251 | *head = elem; |
1375 | *head = elem; |
1252 | } |
1376 | } |
1253 | |
1377 | |
1254 | inline void |
1378 | void inline_size |
1255 | wlist_del (WL *head, WL elem) |
1379 | wlist_del (WL *head, WL elem) |
1256 | { |
1380 | { |
1257 | while (*head) |
1381 | while (*head) |
1258 | { |
1382 | { |
1259 | if (*head == elem) |
1383 | if (*head == elem) |
… | |
… | |
1264 | |
1388 | |
1265 | head = &(*head)->next; |
1389 | head = &(*head)->next; |
1266 | } |
1390 | } |
1267 | } |
1391 | } |
1268 | |
1392 | |
1269 | inline void |
1393 | void inline_speed |
1270 | ev_clear_pending (EV_P_ W w) |
1394 | ev_clear_pending (EV_P_ W w) |
1271 | { |
1395 | { |
1272 | if (w->pending) |
1396 | if (w->pending) |
1273 | { |
1397 | { |
1274 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1398 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1275 | w->pending = 0; |
1399 | w->pending = 0; |
1276 | } |
1400 | } |
1277 | } |
1401 | } |
1278 | |
1402 | |
1279 | inline void |
1403 | void inline_speed |
1280 | ev_start (EV_P_ W w, int active) |
1404 | ev_start (EV_P_ W w, int active) |
1281 | { |
1405 | { |
1282 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1406 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1283 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
1407 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
1284 | |
1408 | |
1285 | w->active = active; |
1409 | w->active = active; |
1286 | ev_ref (EV_A); |
1410 | ev_ref (EV_A); |
1287 | } |
1411 | } |
1288 | |
1412 | |
1289 | inline void |
1413 | void inline_size |
1290 | ev_stop (EV_P_ W w) |
1414 | ev_stop (EV_P_ W w) |
1291 | { |
1415 | { |
1292 | ev_unref (EV_A); |
1416 | ev_unref (EV_A); |
1293 | w->active = 0; |
1417 | w->active = 0; |
1294 | } |
1418 | } |
1295 | |
1419 | |
1296 | /*****************************************************************************/ |
1420 | /*****************************************************************************/ |
1297 | |
1421 | |
1298 | void |
1422 | void |
1299 | ev_io_start (EV_P_ struct ev_io *w) |
1423 | ev_io_start (EV_P_ ev_io *w) |
1300 | { |
1424 | { |
1301 | int fd = w->fd; |
1425 | int fd = w->fd; |
1302 | |
1426 | |
1303 | if (ev_is_active (w)) |
1427 | if (expect_false (ev_is_active (w))) |
1304 | return; |
1428 | return; |
1305 | |
1429 | |
1306 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1430 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1307 | |
1431 | |
1308 | ev_start (EV_A_ (W)w, 1); |
1432 | ev_start (EV_A_ (W)w, 1); |
… | |
… | |
1311 | |
1435 | |
1312 | fd_change (EV_A_ fd); |
1436 | fd_change (EV_A_ fd); |
1313 | } |
1437 | } |
1314 | |
1438 | |
1315 | void |
1439 | void |
1316 | ev_io_stop (EV_P_ struct ev_io *w) |
1440 | ev_io_stop (EV_P_ ev_io *w) |
1317 | { |
1441 | { |
1318 | ev_clear_pending (EV_A_ (W)w); |
1442 | ev_clear_pending (EV_A_ (W)w); |
1319 | if (!ev_is_active (w)) |
1443 | if (expect_false (!ev_is_active (w))) |
1320 | return; |
1444 | return; |
1321 | |
1445 | |
1322 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
1446 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
1323 | |
1447 | |
1324 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1448 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
… | |
… | |
1326 | |
1450 | |
1327 | fd_change (EV_A_ w->fd); |
1451 | fd_change (EV_A_ w->fd); |
1328 | } |
1452 | } |
1329 | |
1453 | |
1330 | void |
1454 | void |
1331 | ev_timer_start (EV_P_ struct ev_timer *w) |
1455 | ev_timer_start (EV_P_ ev_timer *w) |
1332 | { |
1456 | { |
1333 | if (ev_is_active (w)) |
1457 | if (expect_false (ev_is_active (w))) |
1334 | return; |
1458 | return; |
1335 | |
1459 | |
1336 | ((WT)w)->at += mn_now; |
1460 | ((WT)w)->at += mn_now; |
1337 | |
1461 | |
1338 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1462 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1339 | |
1463 | |
1340 | ev_start (EV_A_ (W)w, ++timercnt); |
1464 | ev_start (EV_A_ (W)w, ++timercnt); |
1341 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1465 | array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); |
1342 | timers [timercnt - 1] = w; |
1466 | timers [timercnt - 1] = w; |
1343 | upheap ((WT *)timers, timercnt - 1); |
1467 | upheap ((WT *)timers, timercnt - 1); |
1344 | |
1468 | |
1345 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1469 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1346 | } |
1470 | } |
1347 | |
1471 | |
1348 | void |
1472 | void |
1349 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1473 | ev_timer_stop (EV_P_ ev_timer *w) |
1350 | { |
1474 | { |
1351 | ev_clear_pending (EV_A_ (W)w); |
1475 | ev_clear_pending (EV_A_ (W)w); |
1352 | if (!ev_is_active (w)) |
1476 | if (expect_false (!ev_is_active (w))) |
1353 | return; |
1477 | return; |
1354 | |
1478 | |
1355 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1479 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1356 | |
1480 | |
1357 | if (((W)w)->active < timercnt--) |
1481 | if (expect_true (((W)w)->active < timercnt--)) |
1358 | { |
1482 | { |
1359 | timers [((W)w)->active - 1] = timers [timercnt]; |
1483 | timers [((W)w)->active - 1] = timers [timercnt]; |
1360 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1484 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1361 | } |
1485 | } |
1362 | |
1486 | |
… | |
… | |
1364 | |
1488 | |
1365 | ev_stop (EV_A_ (W)w); |
1489 | ev_stop (EV_A_ (W)w); |
1366 | } |
1490 | } |
1367 | |
1491 | |
1368 | void |
1492 | void |
1369 | ev_timer_again (EV_P_ struct ev_timer *w) |
1493 | ev_timer_again (EV_P_ ev_timer *w) |
1370 | { |
1494 | { |
1371 | if (ev_is_active (w)) |
1495 | if (ev_is_active (w)) |
1372 | { |
1496 | { |
1373 | if (w->repeat) |
1497 | if (w->repeat) |
1374 | { |
1498 | { |
… | |
… | |
1377 | } |
1501 | } |
1378 | else |
1502 | else |
1379 | ev_timer_stop (EV_A_ w); |
1503 | ev_timer_stop (EV_A_ w); |
1380 | } |
1504 | } |
1381 | else if (w->repeat) |
1505 | else if (w->repeat) |
|
|
1506 | { |
|
|
1507 | w->at = w->repeat; |
1382 | ev_timer_start (EV_A_ w); |
1508 | ev_timer_start (EV_A_ w); |
|
|
1509 | } |
1383 | } |
1510 | } |
1384 | |
1511 | |
1385 | #if EV_PERIODICS |
1512 | #if EV_PERIODIC_ENABLE |
1386 | void |
1513 | void |
1387 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1514 | ev_periodic_start (EV_P_ ev_periodic *w) |
1388 | { |
1515 | { |
1389 | if (ev_is_active (w)) |
1516 | if (expect_false (ev_is_active (w))) |
1390 | return; |
1517 | return; |
1391 | |
1518 | |
1392 | if (w->reschedule_cb) |
1519 | if (w->reschedule_cb) |
1393 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1520 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1394 | else if (w->interval) |
1521 | else if (w->interval) |
… | |
… | |
1397 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1524 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1398 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1525 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1399 | } |
1526 | } |
1400 | |
1527 | |
1401 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1528 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1402 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1529 | array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1403 | periodics [periodiccnt - 1] = w; |
1530 | periodics [periodiccnt - 1] = w; |
1404 | upheap ((WT *)periodics, periodiccnt - 1); |
1531 | upheap ((WT *)periodics, periodiccnt - 1); |
1405 | |
1532 | |
1406 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1533 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1407 | } |
1534 | } |
1408 | |
1535 | |
1409 | void |
1536 | void |
1410 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1537 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1411 | { |
1538 | { |
1412 | ev_clear_pending (EV_A_ (W)w); |
1539 | ev_clear_pending (EV_A_ (W)w); |
1413 | if (!ev_is_active (w)) |
1540 | if (expect_false (!ev_is_active (w))) |
1414 | return; |
1541 | return; |
1415 | |
1542 | |
1416 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1543 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1417 | |
1544 | |
1418 | if (((W)w)->active < periodiccnt--) |
1545 | if (expect_true (((W)w)->active < periodiccnt--)) |
1419 | { |
1546 | { |
1420 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1547 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1421 | adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1548 | adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1422 | } |
1549 | } |
1423 | |
1550 | |
1424 | ev_stop (EV_A_ (W)w); |
1551 | ev_stop (EV_A_ (W)w); |
1425 | } |
1552 | } |
1426 | |
1553 | |
1427 | void |
1554 | void |
1428 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1555 | ev_periodic_again (EV_P_ ev_periodic *w) |
1429 | { |
1556 | { |
1430 | /* TODO: use adjustheap and recalculation */ |
1557 | /* TODO: use adjustheap and recalculation */ |
1431 | ev_periodic_stop (EV_A_ w); |
1558 | ev_periodic_stop (EV_A_ w); |
1432 | ev_periodic_start (EV_A_ w); |
1559 | ev_periodic_start (EV_A_ w); |
1433 | } |
1560 | } |
1434 | #endif |
1561 | #endif |
1435 | |
1562 | |
1436 | void |
|
|
1437 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1438 | { |
|
|
1439 | if (ev_is_active (w)) |
|
|
1440 | return; |
|
|
1441 | |
|
|
1442 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1443 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
|
|
1444 | idles [idlecnt - 1] = w; |
|
|
1445 | } |
|
|
1446 | |
|
|
1447 | void |
|
|
1448 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1449 | { |
|
|
1450 | ev_clear_pending (EV_A_ (W)w); |
|
|
1451 | if (!ev_is_active (w)) |
|
|
1452 | return; |
|
|
1453 | |
|
|
1454 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
|
|
1455 | ev_stop (EV_A_ (W)w); |
|
|
1456 | } |
|
|
1457 | |
|
|
1458 | void |
|
|
1459 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1460 | { |
|
|
1461 | if (ev_is_active (w)) |
|
|
1462 | return; |
|
|
1463 | |
|
|
1464 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1465 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
|
|
1466 | prepares [preparecnt - 1] = w; |
|
|
1467 | } |
|
|
1468 | |
|
|
1469 | void |
|
|
1470 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1471 | { |
|
|
1472 | ev_clear_pending (EV_A_ (W)w); |
|
|
1473 | if (!ev_is_active (w)) |
|
|
1474 | return; |
|
|
1475 | |
|
|
1476 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
|
|
1477 | ev_stop (EV_A_ (W)w); |
|
|
1478 | } |
|
|
1479 | |
|
|
1480 | void |
|
|
1481 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1482 | { |
|
|
1483 | if (ev_is_active (w)) |
|
|
1484 | return; |
|
|
1485 | |
|
|
1486 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1487 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
|
|
1488 | checks [checkcnt - 1] = w; |
|
|
1489 | } |
|
|
1490 | |
|
|
1491 | void |
|
|
1492 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1493 | { |
|
|
1494 | ev_clear_pending (EV_A_ (W)w); |
|
|
1495 | if (!ev_is_active (w)) |
|
|
1496 | return; |
|
|
1497 | |
|
|
1498 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
|
|
1499 | ev_stop (EV_A_ (W)w); |
|
|
1500 | } |
|
|
1501 | |
|
|
1502 | #ifndef SA_RESTART |
1563 | #ifndef SA_RESTART |
1503 | # define SA_RESTART 0 |
1564 | # define SA_RESTART 0 |
1504 | #endif |
1565 | #endif |
1505 | |
1566 | |
1506 | void |
1567 | void |
1507 | ev_signal_start (EV_P_ struct ev_signal *w) |
1568 | ev_signal_start (EV_P_ ev_signal *w) |
1508 | { |
1569 | { |
1509 | #if EV_MULTIPLICITY |
1570 | #if EV_MULTIPLICITY |
1510 | assert (("signal watchers are only supported in the default loop", loop == default_loop)); |
1571 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1511 | #endif |
1572 | #endif |
1512 | if (ev_is_active (w)) |
1573 | if (expect_false (ev_is_active (w))) |
1513 | return; |
1574 | return; |
1514 | |
1575 | |
1515 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1576 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1516 | |
1577 | |
1517 | ev_start (EV_A_ (W)w, 1); |
1578 | ev_start (EV_A_ (W)w, 1); |
… | |
… | |
1531 | #endif |
1592 | #endif |
1532 | } |
1593 | } |
1533 | } |
1594 | } |
1534 | |
1595 | |
1535 | void |
1596 | void |
1536 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1597 | ev_signal_stop (EV_P_ ev_signal *w) |
1537 | { |
1598 | { |
1538 | ev_clear_pending (EV_A_ (W)w); |
1599 | ev_clear_pending (EV_A_ (W)w); |
1539 | if (!ev_is_active (w)) |
1600 | if (expect_false (!ev_is_active (w))) |
1540 | return; |
1601 | return; |
1541 | |
1602 | |
1542 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1603 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1543 | ev_stop (EV_A_ (W)w); |
1604 | ev_stop (EV_A_ (W)w); |
1544 | |
1605 | |
1545 | if (!signals [w->signum - 1].head) |
1606 | if (!signals [w->signum - 1].head) |
1546 | signal (w->signum, SIG_DFL); |
1607 | signal (w->signum, SIG_DFL); |
1547 | } |
1608 | } |
1548 | |
1609 | |
1549 | void |
1610 | void |
1550 | ev_child_start (EV_P_ struct ev_child *w) |
1611 | ev_child_start (EV_P_ ev_child *w) |
1551 | { |
1612 | { |
1552 | #if EV_MULTIPLICITY |
1613 | #if EV_MULTIPLICITY |
1553 | assert (("child watchers are only supported in the default loop", loop == default_loop)); |
1614 | assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1554 | #endif |
1615 | #endif |
1555 | if (ev_is_active (w)) |
1616 | if (expect_false (ev_is_active (w))) |
1556 | return; |
1617 | return; |
1557 | |
1618 | |
1558 | ev_start (EV_A_ (W)w, 1); |
1619 | ev_start (EV_A_ (W)w, 1); |
1559 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1620 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1560 | } |
1621 | } |
1561 | |
1622 | |
1562 | void |
1623 | void |
1563 | ev_child_stop (EV_P_ struct ev_child *w) |
1624 | ev_child_stop (EV_P_ ev_child *w) |
1564 | { |
1625 | { |
1565 | ev_clear_pending (EV_A_ (W)w); |
1626 | ev_clear_pending (EV_A_ (W)w); |
1566 | if (!ev_is_active (w)) |
1627 | if (expect_false (!ev_is_active (w))) |
1567 | return; |
1628 | return; |
1568 | |
1629 | |
1569 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1630 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1570 | ev_stop (EV_A_ (W)w); |
1631 | ev_stop (EV_A_ (W)w); |
1571 | } |
1632 | } |
1572 | |
1633 | |
|
|
1634 | #if EV_STAT_ENABLE |
|
|
1635 | |
|
|
1636 | # ifdef _WIN32 |
|
|
1637 | # define lstat(a,b) stat(a,b) |
|
|
1638 | # endif |
|
|
1639 | |
|
|
1640 | #define DEF_STAT_INTERVAL 5.0074891 |
|
|
1641 | #define MIN_STAT_INTERVAL 0.1074891 |
|
|
1642 | |
|
|
1643 | void |
|
|
1644 | ev_stat_stat (EV_P_ ev_stat *w) |
|
|
1645 | { |
|
|
1646 | if (lstat (w->path, &w->attr) < 0) |
|
|
1647 | w->attr.st_nlink = 0; |
|
|
1648 | else if (!w->attr.st_nlink) |
|
|
1649 | w->attr.st_nlink = 1; |
|
|
1650 | } |
|
|
1651 | |
|
|
1652 | static void |
|
|
1653 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
|
|
1654 | { |
|
|
1655 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
|
|
1656 | |
|
|
1657 | /* we copy this here each the time so that */ |
|
|
1658 | /* prev has the old value when the callback gets invoked */ |
|
|
1659 | w->prev = w->attr; |
|
|
1660 | ev_stat_stat (EV_A_ w); |
|
|
1661 | |
|
|
1662 | if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata))) |
|
|
1663 | ev_feed_event (EV_A_ w, EV_STAT); |
|
|
1664 | } |
|
|
1665 | |
|
|
1666 | void |
|
|
1667 | ev_stat_start (EV_P_ ev_stat *w) |
|
|
1668 | { |
|
|
1669 | if (expect_false (ev_is_active (w))) |
|
|
1670 | return; |
|
|
1671 | |
|
|
1672 | /* since we use memcmp, we need to clear any padding data etc. */ |
|
|
1673 | memset (&w->prev, 0, sizeof (ev_statdata)); |
|
|
1674 | memset (&w->attr, 0, sizeof (ev_statdata)); |
|
|
1675 | |
|
|
1676 | ev_stat_stat (EV_A_ w); |
|
|
1677 | |
|
|
1678 | if (w->interval < MIN_STAT_INTERVAL) |
|
|
1679 | w->interval = w->interval ? MIN_STAT_INTERVAL : DEF_STAT_INTERVAL; |
|
|
1680 | |
|
|
1681 | ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval); |
|
|
1682 | ev_set_priority (&w->timer, ev_priority (w)); |
|
|
1683 | ev_timer_start (EV_A_ &w->timer); |
|
|
1684 | |
|
|
1685 | ev_start (EV_A_ (W)w, 1); |
|
|
1686 | } |
|
|
1687 | |
|
|
1688 | void |
|
|
1689 | ev_stat_stop (EV_P_ ev_stat *w) |
|
|
1690 | { |
|
|
1691 | ev_clear_pending (EV_A_ (W)w); |
|
|
1692 | if (expect_false (!ev_is_active (w))) |
|
|
1693 | return; |
|
|
1694 | |
|
|
1695 | ev_timer_stop (EV_A_ &w->timer); |
|
|
1696 | |
|
|
1697 | ev_stop (EV_A_ (W)w); |
|
|
1698 | } |
|
|
1699 | #endif |
|
|
1700 | |
|
|
1701 | void |
|
|
1702 | ev_idle_start (EV_P_ ev_idle *w) |
|
|
1703 | { |
|
|
1704 | if (expect_false (ev_is_active (w))) |
|
|
1705 | return; |
|
|
1706 | |
|
|
1707 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1708 | array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
|
|
1709 | idles [idlecnt - 1] = w; |
|
|
1710 | } |
|
|
1711 | |
|
|
1712 | void |
|
|
1713 | ev_idle_stop (EV_P_ ev_idle *w) |
|
|
1714 | { |
|
|
1715 | ev_clear_pending (EV_A_ (W)w); |
|
|
1716 | if (expect_false (!ev_is_active (w))) |
|
|
1717 | return; |
|
|
1718 | |
|
|
1719 | { |
|
|
1720 | int active = ((W)w)->active; |
|
|
1721 | idles [active - 1] = idles [--idlecnt]; |
|
|
1722 | ((W)idles [active - 1])->active = active; |
|
|
1723 | } |
|
|
1724 | |
|
|
1725 | ev_stop (EV_A_ (W)w); |
|
|
1726 | } |
|
|
1727 | |
|
|
1728 | void |
|
|
1729 | ev_prepare_start (EV_P_ ev_prepare *w) |
|
|
1730 | { |
|
|
1731 | if (expect_false (ev_is_active (w))) |
|
|
1732 | return; |
|
|
1733 | |
|
|
1734 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1735 | array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
|
|
1736 | prepares [preparecnt - 1] = w; |
|
|
1737 | } |
|
|
1738 | |
|
|
1739 | void |
|
|
1740 | ev_prepare_stop (EV_P_ ev_prepare *w) |
|
|
1741 | { |
|
|
1742 | ev_clear_pending (EV_A_ (W)w); |
|
|
1743 | if (expect_false (!ev_is_active (w))) |
|
|
1744 | return; |
|
|
1745 | |
|
|
1746 | { |
|
|
1747 | int active = ((W)w)->active; |
|
|
1748 | prepares [active - 1] = prepares [--preparecnt]; |
|
|
1749 | ((W)prepares [active - 1])->active = active; |
|
|
1750 | } |
|
|
1751 | |
|
|
1752 | ev_stop (EV_A_ (W)w); |
|
|
1753 | } |
|
|
1754 | |
|
|
1755 | void |
|
|
1756 | ev_check_start (EV_P_ ev_check *w) |
|
|
1757 | { |
|
|
1758 | if (expect_false (ev_is_active (w))) |
|
|
1759 | return; |
|
|
1760 | |
|
|
1761 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1762 | array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2); |
|
|
1763 | checks [checkcnt - 1] = w; |
|
|
1764 | } |
|
|
1765 | |
|
|
1766 | void |
|
|
1767 | ev_check_stop (EV_P_ ev_check *w) |
|
|
1768 | { |
|
|
1769 | ev_clear_pending (EV_A_ (W)w); |
|
|
1770 | if (expect_false (!ev_is_active (w))) |
|
|
1771 | return; |
|
|
1772 | |
|
|
1773 | { |
|
|
1774 | int active = ((W)w)->active; |
|
|
1775 | checks [active - 1] = checks [--checkcnt]; |
|
|
1776 | ((W)checks [active - 1])->active = active; |
|
|
1777 | } |
|
|
1778 | |
|
|
1779 | ev_stop (EV_A_ (W)w); |
|
|
1780 | } |
|
|
1781 | |
|
|
1782 | #if EV_EMBED_ENABLE |
|
|
1783 | void noinline |
|
|
1784 | ev_embed_sweep (EV_P_ ev_embed *w) |
|
|
1785 | { |
|
|
1786 | ev_loop (w->loop, EVLOOP_NONBLOCK); |
|
|
1787 | } |
|
|
1788 | |
|
|
1789 | static void |
|
|
1790 | embed_cb (EV_P_ ev_io *io, int revents) |
|
|
1791 | { |
|
|
1792 | ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); |
|
|
1793 | |
|
|
1794 | if (ev_cb (w)) |
|
|
1795 | ev_feed_event (EV_A_ (W)w, EV_EMBED); |
|
|
1796 | else |
|
|
1797 | ev_embed_sweep (loop, w); |
|
|
1798 | } |
|
|
1799 | |
|
|
1800 | void |
|
|
1801 | ev_embed_start (EV_P_ ev_embed *w) |
|
|
1802 | { |
|
|
1803 | if (expect_false (ev_is_active (w))) |
|
|
1804 | return; |
|
|
1805 | |
|
|
1806 | { |
|
|
1807 | struct ev_loop *loop = w->loop; |
|
|
1808 | assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); |
|
|
1809 | ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); |
|
|
1810 | } |
|
|
1811 | |
|
|
1812 | ev_set_priority (&w->io, ev_priority (w)); |
|
|
1813 | ev_io_start (EV_A_ &w->io); |
|
|
1814 | |
|
|
1815 | ev_start (EV_A_ (W)w, 1); |
|
|
1816 | } |
|
|
1817 | |
|
|
1818 | void |
|
|
1819 | ev_embed_stop (EV_P_ ev_embed *w) |
|
|
1820 | { |
|
|
1821 | ev_clear_pending (EV_A_ (W)w); |
|
|
1822 | if (expect_false (!ev_is_active (w))) |
|
|
1823 | return; |
|
|
1824 | |
|
|
1825 | ev_io_stop (EV_A_ &w->io); |
|
|
1826 | |
|
|
1827 | ev_stop (EV_A_ (W)w); |
|
|
1828 | } |
|
|
1829 | #endif |
|
|
1830 | |
1573 | /*****************************************************************************/ |
1831 | /*****************************************************************************/ |
1574 | |
1832 | |
1575 | struct ev_once |
1833 | struct ev_once |
1576 | { |
1834 | { |
1577 | struct ev_io io; |
1835 | ev_io io; |
1578 | struct ev_timer to; |
1836 | ev_timer to; |
1579 | void (*cb)(int revents, void *arg); |
1837 | void (*cb)(int revents, void *arg); |
1580 | void *arg; |
1838 | void *arg; |
1581 | }; |
1839 | }; |
1582 | |
1840 | |
1583 | static void |
1841 | static void |
… | |
… | |
1592 | |
1850 | |
1593 | cb (revents, arg); |
1851 | cb (revents, arg); |
1594 | } |
1852 | } |
1595 | |
1853 | |
1596 | static void |
1854 | static void |
1597 | once_cb_io (EV_P_ struct ev_io *w, int revents) |
1855 | once_cb_io (EV_P_ ev_io *w, int revents) |
1598 | { |
1856 | { |
1599 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1857 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1600 | } |
1858 | } |
1601 | |
1859 | |
1602 | static void |
1860 | static void |
1603 | once_cb_to (EV_P_ struct ev_timer *w, int revents) |
1861 | once_cb_to (EV_P_ ev_timer *w, int revents) |
1604 | { |
1862 | { |
1605 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1863 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1606 | } |
1864 | } |
1607 | |
1865 | |
1608 | void |
1866 | void |
1609 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1867 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1610 | { |
1868 | { |
1611 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1869 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1612 | |
1870 | |
1613 | if (!once) |
1871 | if (expect_false (!once)) |
|
|
1872 | { |
1614 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1873 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1615 | else |
1874 | return; |
1616 | { |
1875 | } |
|
|
1876 | |
1617 | once->cb = cb; |
1877 | once->cb = cb; |
1618 | once->arg = arg; |
1878 | once->arg = arg; |
1619 | |
1879 | |
1620 | ev_init (&once->io, once_cb_io); |
1880 | ev_init (&once->io, once_cb_io); |
1621 | if (fd >= 0) |
1881 | if (fd >= 0) |
1622 | { |
1882 | { |
1623 | ev_io_set (&once->io, fd, events); |
1883 | ev_io_set (&once->io, fd, events); |
1624 | ev_io_start (EV_A_ &once->io); |
1884 | ev_io_start (EV_A_ &once->io); |
1625 | } |
1885 | } |
1626 | |
1886 | |
1627 | ev_init (&once->to, once_cb_to); |
1887 | ev_init (&once->to, once_cb_to); |
1628 | if (timeout >= 0.) |
1888 | if (timeout >= 0.) |
1629 | { |
1889 | { |
1630 | ev_timer_set (&once->to, timeout, 0.); |
1890 | ev_timer_set (&once->to, timeout, 0.); |
1631 | ev_timer_start (EV_A_ &once->to); |
1891 | ev_timer_start (EV_A_ &once->to); |
1632 | } |
|
|
1633 | } |
1892 | } |
1634 | } |
1893 | } |
1635 | |
1894 | |
1636 | #ifdef __cplusplus |
1895 | #ifdef __cplusplus |
1637 | } |
1896 | } |