… | |
… | |
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) |
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 | |
|
|
83 | # ifndef EV_USE_KQUEUE |
60 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (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 |
62 | # endif |
89 | # endif |
63 | |
90 | |
64 | # if HAVE_PORT_H && HAVE_PORT_CREATE && !defined (EV_USE_PORT) |
91 | # ifndef EV_USE_PORT |
|
|
92 | # if HAVE_PORT_H && HAVE_PORT_CREATE |
65 | # define EV_USE_PORT 1 |
93 | # define EV_USE_PORT 1 |
|
|
94 | # else |
|
|
95 | # define EV_USE_PORT 0 |
|
|
96 | # endif |
66 | # endif |
97 | # endif |
67 | |
98 | |
68 | #endif |
99 | #endif |
69 | |
100 | |
70 | #include <math.h> |
101 | #include <math.h> |
… | |
… | |
80 | #include <time.h> |
111 | #include <time.h> |
81 | |
112 | |
82 | #include <signal.h> |
113 | #include <signal.h> |
83 | |
114 | |
84 | #ifndef _WIN32 |
115 | #ifndef _WIN32 |
85 | # include <unistd.h> |
|
|
86 | # include <sys/time.h> |
116 | # include <sys/time.h> |
87 | # include <sys/wait.h> |
117 | # include <sys/wait.h> |
|
|
118 | # include <unistd.h> |
88 | #else |
119 | #else |
89 | # define WIN32_LEAN_AND_MEAN |
120 | # define WIN32_LEAN_AND_MEAN |
90 | # include <windows.h> |
121 | # include <windows.h> |
91 | # ifndef EV_SELECT_IS_WINSOCKET |
122 | # ifndef EV_SELECT_IS_WINSOCKET |
92 | # define EV_SELECT_IS_WINSOCKET 1 |
123 | # define EV_SELECT_IS_WINSOCKET 1 |
… | |
… | |
127 | # define EV_USE_PORT 0 |
158 | # define EV_USE_PORT 0 |
128 | #endif |
159 | #endif |
129 | |
160 | |
130 | /**/ |
161 | /**/ |
131 | |
162 | |
132 | /* darwin simply cannot be helped */ |
|
|
133 | #ifdef __APPLE__ |
|
|
134 | # undef EV_USE_POLL |
|
|
135 | # undef EV_USE_KQUEUE |
|
|
136 | #endif |
|
|
137 | |
|
|
138 | #ifndef CLOCK_MONOTONIC |
163 | #ifndef CLOCK_MONOTONIC |
139 | # undef EV_USE_MONOTONIC |
164 | # undef EV_USE_MONOTONIC |
140 | # define EV_USE_MONOTONIC 0 |
165 | # define EV_USE_MONOTONIC 0 |
141 | #endif |
166 | #endif |
142 | |
167 | |
… | |
… | |
162 | # include "ev.h" |
187 | # include "ev.h" |
163 | #endif |
188 | #endif |
164 | |
189 | |
165 | #if __GNUC__ >= 3 |
190 | #if __GNUC__ >= 3 |
166 | # 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 |
167 | # 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 |
168 | #else |
200 | #else |
169 | # define expect(expr,value) (expr) |
201 | # define expect(expr,value) (expr) |
170 | # define inline static |
202 | # define inline_speed static |
|
|
203 | # define inline_minimal static |
|
|
204 | # define noinline |
171 | #endif |
205 | #endif |
172 | |
206 | |
173 | #define expect_false(expr) expect ((expr) != 0, 0) |
207 | #define expect_false(expr) expect ((expr) != 0, 0) |
174 | #define expect_true(expr) expect ((expr) != 0, 1) |
208 | #define expect_true(expr) expect ((expr) != 0, 1) |
175 | |
209 | |
… | |
… | |
177 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
211 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
178 | |
212 | |
179 | #define EMPTY0 /* required for microsofts broken pseudo-c compiler */ |
213 | #define EMPTY0 /* required for microsofts broken pseudo-c compiler */ |
180 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
214 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
181 | |
215 | |
182 | typedef struct ev_watcher *W; |
216 | typedef ev_watcher *W; |
183 | typedef struct ev_watcher_list *WL; |
217 | typedef ev_watcher_list *WL; |
184 | typedef struct ev_watcher_time *WT; |
218 | typedef ev_watcher_time *WT; |
185 | |
219 | |
186 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
220 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
187 | |
221 | |
188 | #ifdef _WIN32 |
222 | #ifdef _WIN32 |
189 | # include "ev_win32.c" |
223 | # include "ev_win32.c" |
… | |
… | |
191 | |
225 | |
192 | /*****************************************************************************/ |
226 | /*****************************************************************************/ |
193 | |
227 | |
194 | static void (*syserr_cb)(const char *msg); |
228 | static void (*syserr_cb)(const char *msg); |
195 | |
229 | |
|
|
230 | void |
196 | void ev_set_syserr_cb (void (*cb)(const char *msg)) |
231 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
197 | { |
232 | { |
198 | syserr_cb = cb; |
233 | syserr_cb = cb; |
199 | } |
234 | } |
200 | |
235 | |
201 | static void |
236 | static void noinline |
202 | syserr (const char *msg) |
237 | syserr (const char *msg) |
203 | { |
238 | { |
204 | if (!msg) |
239 | if (!msg) |
205 | msg = "(libev) system error"; |
240 | msg = "(libev) system error"; |
206 | |
241 | |
… | |
… | |
213 | } |
248 | } |
214 | } |
249 | } |
215 | |
250 | |
216 | static void *(*alloc)(void *ptr, long size); |
251 | static void *(*alloc)(void *ptr, long size); |
217 | |
252 | |
|
|
253 | void |
218 | void ev_set_allocator (void *(*cb)(void *ptr, long size)) |
254 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
219 | { |
255 | { |
220 | alloc = cb; |
256 | alloc = cb; |
221 | } |
257 | } |
222 | |
258 | |
223 | static void * |
259 | static void * |
… | |
… | |
295 | gettimeofday (&tv, 0); |
331 | gettimeofday (&tv, 0); |
296 | return tv.tv_sec + tv.tv_usec * 1e-6; |
332 | return tv.tv_sec + tv.tv_usec * 1e-6; |
297 | #endif |
333 | #endif |
298 | } |
334 | } |
299 | |
335 | |
300 | inline ev_tstamp |
336 | ev_tstamp inline_size |
301 | get_clock (void) |
337 | get_clock (void) |
302 | { |
338 | { |
303 | #if EV_USE_MONOTONIC |
339 | #if EV_USE_MONOTONIC |
304 | if (expect_true (have_monotonic)) |
340 | if (expect_true (have_monotonic)) |
305 | { |
341 | { |
… | |
… | |
348 | #define array_free(stem, idx) \ |
384 | #define array_free(stem, idx) \ |
349 | 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; |
350 | |
386 | |
351 | /*****************************************************************************/ |
387 | /*****************************************************************************/ |
352 | |
388 | |
353 | static void |
389 | void noinline |
354 | anfds_init (ANFD *base, int count) |
|
|
355 | { |
|
|
356 | while (count--) |
|
|
357 | { |
|
|
358 | base->head = 0; |
|
|
359 | base->events = EV_NONE; |
|
|
360 | base->reify = 0; |
|
|
361 | |
|
|
362 | ++base; |
|
|
363 | } |
|
|
364 | } |
|
|
365 | |
|
|
366 | void |
|
|
367 | ev_feed_event (EV_P_ void *w, int revents) |
390 | ev_feed_event (EV_P_ void *w, int revents) |
368 | { |
391 | { |
369 | W w_ = (W)w; |
392 | W w_ = (W)w; |
370 | |
393 | |
371 | if (w_->pending) |
394 | if (expect_false (w_->pending)) |
372 | { |
395 | { |
373 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
396 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
374 | return; |
397 | return; |
375 | } |
398 | } |
376 | |
399 | |
… | |
… | |
378 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
401 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
379 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
402 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
380 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
403 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
381 | } |
404 | } |
382 | |
405 | |
383 | static void |
406 | void inline_size |
384 | queue_events (EV_P_ W *events, int eventcnt, int type) |
407 | queue_events (EV_P_ W *events, int eventcnt, int type) |
385 | { |
408 | { |
386 | int i; |
409 | int i; |
387 | |
410 | |
388 | for (i = 0; i < eventcnt; ++i) |
411 | for (i = 0; i < eventcnt; ++i) |
389 | ev_feed_event (EV_A_ events [i], type); |
412 | ev_feed_event (EV_A_ events [i], type); |
390 | } |
413 | } |
391 | |
414 | |
392 | inline void |
415 | /*****************************************************************************/ |
|
|
416 | |
|
|
417 | void inline_size |
|
|
418 | anfds_init (ANFD *base, int count) |
|
|
419 | { |
|
|
420 | while (count--) |
|
|
421 | { |
|
|
422 | base->head = 0; |
|
|
423 | base->events = EV_NONE; |
|
|
424 | base->reify = 0; |
|
|
425 | |
|
|
426 | ++base; |
|
|
427 | } |
|
|
428 | } |
|
|
429 | |
|
|
430 | void inline_speed |
393 | fd_event (EV_P_ int fd, int revents) |
431 | fd_event (EV_P_ int fd, int revents) |
394 | { |
432 | { |
395 | ANFD *anfd = anfds + fd; |
433 | ANFD *anfd = anfds + fd; |
396 | struct ev_io *w; |
434 | ev_io *w; |
397 | |
435 | |
398 | 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) |
399 | { |
437 | { |
400 | int ev = w->events & revents; |
438 | int ev = w->events & revents; |
401 | |
439 | |
402 | if (ev) |
440 | if (ev) |
403 | ev_feed_event (EV_A_ (W)w, ev); |
441 | ev_feed_event (EV_A_ (W)w, ev); |
… | |
… | |
408 | ev_feed_fd_event (EV_P_ int fd, int revents) |
446 | ev_feed_fd_event (EV_P_ int fd, int revents) |
409 | { |
447 | { |
410 | fd_event (EV_A_ fd, revents); |
448 | fd_event (EV_A_ fd, revents); |
411 | } |
449 | } |
412 | |
450 | |
413 | /*****************************************************************************/ |
451 | void inline_size |
414 | |
|
|
415 | static void |
|
|
416 | fd_reify (EV_P) |
452 | fd_reify (EV_P) |
417 | { |
453 | { |
418 | int i; |
454 | int i; |
419 | |
455 | |
420 | for (i = 0; i < fdchangecnt; ++i) |
456 | for (i = 0; i < fdchangecnt; ++i) |
421 | { |
457 | { |
422 | int fd = fdchanges [i]; |
458 | int fd = fdchanges [i]; |
423 | ANFD *anfd = anfds + fd; |
459 | ANFD *anfd = anfds + fd; |
424 | struct ev_io *w; |
460 | ev_io *w; |
425 | |
461 | |
426 | int events = 0; |
462 | int events = 0; |
427 | |
463 | |
428 | 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) |
429 | events |= w->events; |
465 | events |= w->events; |
430 | |
466 | |
431 | #if EV_SELECT_IS_WINSOCKET |
467 | #if EV_SELECT_IS_WINSOCKET |
432 | if (events) |
468 | if (events) |
433 | { |
469 | { |
… | |
… | |
437 | } |
473 | } |
438 | #endif |
474 | #endif |
439 | |
475 | |
440 | anfd->reify = 0; |
476 | anfd->reify = 0; |
441 | |
477 | |
442 | method_modify (EV_A_ fd, anfd->events, events); |
478 | backend_modify (EV_A_ fd, anfd->events, events); |
443 | anfd->events = events; |
479 | anfd->events = events; |
444 | } |
480 | } |
445 | |
481 | |
446 | fdchangecnt = 0; |
482 | fdchangecnt = 0; |
447 | } |
483 | } |
448 | |
484 | |
449 | static void |
485 | void inline_size |
450 | fd_change (EV_P_ int fd) |
486 | fd_change (EV_P_ int fd) |
451 | { |
487 | { |
452 | if (anfds [fd].reify) |
488 | if (expect_false (anfds [fd].reify)) |
453 | return; |
489 | return; |
454 | |
490 | |
455 | anfds [fd].reify = 1; |
491 | anfds [fd].reify = 1; |
456 | |
492 | |
457 | ++fdchangecnt; |
493 | ++fdchangecnt; |
458 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
494 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
459 | fdchanges [fdchangecnt - 1] = fd; |
495 | fdchanges [fdchangecnt - 1] = fd; |
460 | } |
496 | } |
461 | |
497 | |
462 | static void |
498 | void inline_speed |
463 | fd_kill (EV_P_ int fd) |
499 | fd_kill (EV_P_ int fd) |
464 | { |
500 | { |
465 | struct ev_io *w; |
501 | ev_io *w; |
466 | |
502 | |
467 | while ((w = (struct ev_io *)anfds [fd].head)) |
503 | while ((w = (ev_io *)anfds [fd].head)) |
468 | { |
504 | { |
469 | ev_io_stop (EV_A_ w); |
505 | ev_io_stop (EV_A_ w); |
470 | 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); |
471 | } |
507 | } |
472 | } |
508 | } |
473 | |
509 | |
474 | static int |
510 | int inline_size |
475 | fd_valid (int fd) |
511 | fd_valid (int fd) |
476 | { |
512 | { |
477 | #ifdef _WIN32 |
513 | #ifdef _WIN32 |
478 | return _get_osfhandle (fd) != -1; |
514 | return _get_osfhandle (fd) != -1; |
479 | #else |
515 | #else |
480 | return fcntl (fd, F_GETFD) != -1; |
516 | return fcntl (fd, F_GETFD) != -1; |
481 | #endif |
517 | #endif |
482 | } |
518 | } |
483 | |
519 | |
484 | /* called on EBADF to verify fds */ |
520 | /* called on EBADF to verify fds */ |
485 | static void |
521 | static void noinline |
486 | fd_ebadf (EV_P) |
522 | fd_ebadf (EV_P) |
487 | { |
523 | { |
488 | int fd; |
524 | int fd; |
489 | |
525 | |
490 | for (fd = 0; fd < anfdmax; ++fd) |
526 | for (fd = 0; fd < anfdmax; ++fd) |
… | |
… | |
492 | if (!fd_valid (fd) == -1 && errno == EBADF) |
528 | if (!fd_valid (fd) == -1 && errno == EBADF) |
493 | fd_kill (EV_A_ fd); |
529 | fd_kill (EV_A_ fd); |
494 | } |
530 | } |
495 | |
531 | |
496 | /* 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 */ |
497 | static void |
533 | static void noinline |
498 | fd_enomem (EV_P) |
534 | fd_enomem (EV_P) |
499 | { |
535 | { |
500 | int fd; |
536 | int fd; |
501 | |
537 | |
502 | for (fd = anfdmax; fd--; ) |
538 | for (fd = anfdmax; fd--; ) |
… | |
… | |
505 | fd_kill (EV_A_ fd); |
541 | fd_kill (EV_A_ fd); |
506 | return; |
542 | return; |
507 | } |
543 | } |
508 | } |
544 | } |
509 | |
545 | |
510 | /* 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 */ |
511 | static void |
547 | static void noinline |
512 | fd_rearm_all (EV_P) |
548 | fd_rearm_all (EV_P) |
513 | { |
549 | { |
514 | int fd; |
550 | int fd; |
515 | |
551 | |
516 | /* 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 */ |
… | |
… | |
522 | } |
558 | } |
523 | } |
559 | } |
524 | |
560 | |
525 | /*****************************************************************************/ |
561 | /*****************************************************************************/ |
526 | |
562 | |
527 | static void |
563 | void inline_speed |
528 | upheap (WT *heap, int k) |
564 | upheap (WT *heap, int k) |
529 | { |
565 | { |
530 | WT w = heap [k]; |
566 | WT w = heap [k]; |
531 | |
567 | |
532 | while (k && heap [k >> 1]->at > w->at) |
568 | while (k && heap [k >> 1]->at > w->at) |
… | |
… | |
539 | heap [k] = w; |
575 | heap [k] = w; |
540 | ((W)heap [k])->active = k + 1; |
576 | ((W)heap [k])->active = k + 1; |
541 | |
577 | |
542 | } |
578 | } |
543 | |
579 | |
544 | static void |
580 | void inline_speed |
545 | downheap (WT *heap, int N, int k) |
581 | downheap (WT *heap, int N, int k) |
546 | { |
582 | { |
547 | WT w = heap [k]; |
583 | WT w = heap [k]; |
548 | |
584 | |
549 | while (k < (N >> 1)) |
585 | while (k < (N >> 1)) |
… | |
… | |
563 | |
599 | |
564 | heap [k] = w; |
600 | heap [k] = w; |
565 | ((W)heap [k])->active = k + 1; |
601 | ((W)heap [k])->active = k + 1; |
566 | } |
602 | } |
567 | |
603 | |
568 | inline void |
604 | void inline_size |
569 | adjustheap (WT *heap, int N, int k) |
605 | adjustheap (WT *heap, int N, int k) |
570 | { |
606 | { |
571 | upheap (heap, k); |
607 | upheap (heap, k); |
572 | downheap (heap, N, k); |
608 | downheap (heap, N, k); |
573 | } |
609 | } |
… | |
… | |
583 | static ANSIG *signals; |
619 | static ANSIG *signals; |
584 | static int signalmax; |
620 | static int signalmax; |
585 | |
621 | |
586 | static int sigpipe [2]; |
622 | static int sigpipe [2]; |
587 | static sig_atomic_t volatile gotsig; |
623 | static sig_atomic_t volatile gotsig; |
588 | static struct ev_io sigev; |
624 | static ev_io sigev; |
589 | |
625 | |
590 | static void |
626 | void inline_size |
591 | signals_init (ANSIG *base, int count) |
627 | signals_init (ANSIG *base, int count) |
592 | { |
628 | { |
593 | while (count--) |
629 | while (count--) |
594 | { |
630 | { |
595 | base->head = 0; |
631 | base->head = 0; |
… | |
… | |
615 | write (sigpipe [1], &signum, 1); |
651 | write (sigpipe [1], &signum, 1); |
616 | errno = old_errno; |
652 | errno = old_errno; |
617 | } |
653 | } |
618 | } |
654 | } |
619 | |
655 | |
620 | void |
656 | void noinline |
621 | ev_feed_signal_event (EV_P_ int signum) |
657 | ev_feed_signal_event (EV_P_ int signum) |
622 | { |
658 | { |
623 | WL w; |
659 | WL w; |
624 | |
660 | |
625 | #if EV_MULTIPLICITY |
661 | #if EV_MULTIPLICITY |
… | |
… | |
636 | for (w = signals [signum].head; w; w = w->next) |
672 | for (w = signals [signum].head; w; w = w->next) |
637 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
673 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
638 | } |
674 | } |
639 | |
675 | |
640 | static void |
676 | static void |
641 | sigcb (EV_P_ struct ev_io *iow, int revents) |
677 | sigcb (EV_P_ ev_io *iow, int revents) |
642 | { |
678 | { |
643 | int signum; |
679 | int signum; |
644 | |
680 | |
645 | read (sigpipe [0], &revents, 1); |
681 | read (sigpipe [0], &revents, 1); |
646 | gotsig = 0; |
682 | gotsig = 0; |
… | |
… | |
648 | for (signum = signalmax; signum--; ) |
684 | for (signum = signalmax; signum--; ) |
649 | if (signals [signum].gotsig) |
685 | if (signals [signum].gotsig) |
650 | ev_feed_signal_event (EV_A_ signum + 1); |
686 | ev_feed_signal_event (EV_A_ signum + 1); |
651 | } |
687 | } |
652 | |
688 | |
653 | inline void |
689 | void inline_size |
654 | fd_intern (int fd) |
690 | fd_intern (int fd) |
655 | { |
691 | { |
656 | #ifdef _WIN32 |
692 | #ifdef _WIN32 |
657 | int arg = 1; |
693 | int arg = 1; |
658 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
694 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
… | |
… | |
660 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
696 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
661 | fcntl (fd, F_SETFL, O_NONBLOCK); |
697 | fcntl (fd, F_SETFL, O_NONBLOCK); |
662 | #endif |
698 | #endif |
663 | } |
699 | } |
664 | |
700 | |
665 | static void |
701 | static void noinline |
666 | siginit (EV_P) |
702 | siginit (EV_P) |
667 | { |
703 | { |
668 | fd_intern (sigpipe [0]); |
704 | fd_intern (sigpipe [0]); |
669 | fd_intern (sigpipe [1]); |
705 | fd_intern (sigpipe [1]); |
670 | |
706 | |
… | |
… | |
673 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
709 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
674 | } |
710 | } |
675 | |
711 | |
676 | /*****************************************************************************/ |
712 | /*****************************************************************************/ |
677 | |
713 | |
678 | static struct ev_child *childs [PID_HASHSIZE]; |
714 | static ev_child *childs [PID_HASHSIZE]; |
679 | |
715 | |
680 | #ifndef _WIN32 |
716 | #ifndef _WIN32 |
681 | |
717 | |
682 | static struct ev_signal childev; |
718 | static ev_signal childev; |
683 | |
719 | |
684 | #ifndef WCONTINUED |
720 | void inline_speed |
685 | # define WCONTINUED 0 |
|
|
686 | #endif |
|
|
687 | |
|
|
688 | static void |
|
|
689 | 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) |
690 | { |
722 | { |
691 | struct ev_child *w; |
723 | ev_child *w; |
692 | |
724 | |
693 | 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) |
694 | if (w->pid == pid || !w->pid) |
726 | if (w->pid == pid || !w->pid) |
695 | { |
727 | { |
696 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
728 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
697 | w->rpid = pid; |
729 | w->rpid = pid; |
698 | w->rstatus = status; |
730 | w->rstatus = status; |
699 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
731 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
700 | } |
732 | } |
701 | } |
733 | } |
702 | |
734 | |
|
|
735 | #ifndef WCONTINUED |
|
|
736 | # define WCONTINUED 0 |
|
|
737 | #endif |
|
|
738 | |
703 | static void |
739 | static void |
704 | childcb (EV_P_ struct ev_signal *sw, int revents) |
740 | childcb (EV_P_ ev_signal *sw, int revents) |
705 | { |
741 | { |
706 | int pid, status; |
742 | int pid, status; |
707 | |
743 | |
|
|
744 | /* some systems define WCONTINUED but then fail to support it (linux 2.4) */ |
708 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
745 | if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
709 | { |
746 | if (!WCONTINUED |
|
|
747 | || errno != EINVAL |
|
|
748 | || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED))) |
|
|
749 | return; |
|
|
750 | |
710 | /* 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 */ |
711 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
753 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
712 | |
754 | |
713 | child_reap (EV_A_ sw, pid, pid, status); |
755 | child_reap (EV_A_ sw, pid, pid, status); |
714 | 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 */ |
715 | } |
|
|
716 | } |
757 | } |
717 | |
758 | |
718 | #endif |
759 | #endif |
719 | |
760 | |
720 | /*****************************************************************************/ |
761 | /*****************************************************************************/ |
… | |
… | |
746 | { |
787 | { |
747 | return EV_VERSION_MINOR; |
788 | return EV_VERSION_MINOR; |
748 | } |
789 | } |
749 | |
790 | |
750 | /* 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 */ |
751 | static int |
792 | int inline_size |
752 | enable_secure (void) |
793 | enable_secure (void) |
753 | { |
794 | { |
754 | #ifdef _WIN32 |
795 | #ifdef _WIN32 |
755 | return 0; |
796 | return 0; |
756 | #else |
797 | #else |
… | |
… | |
758 | || getgid () != getegid (); |
799 | || getgid () != getegid (); |
759 | #endif |
800 | #endif |
760 | } |
801 | } |
761 | |
802 | |
762 | unsigned int |
803 | unsigned int |
763 | ev_method (EV_P) |
804 | ev_supported_backends (void) |
764 | { |
805 | { |
765 | 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; |
766 | } |
847 | } |
767 | |
848 | |
768 | static void |
849 | static void |
769 | loop_init (EV_P_ unsigned int flags) |
850 | loop_init (EV_P_ unsigned int flags) |
770 | { |
851 | { |
771 | if (!method) |
852 | if (!backend) |
772 | { |
853 | { |
773 | #if EV_USE_MONOTONIC |
854 | #if EV_USE_MONOTONIC |
774 | { |
855 | { |
775 | struct timespec ts; |
856 | struct timespec ts; |
776 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
857 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
… | |
… | |
781 | ev_rt_now = ev_time (); |
862 | ev_rt_now = ev_time (); |
782 | mn_now = get_clock (); |
863 | mn_now = get_clock (); |
783 | now_floor = mn_now; |
864 | now_floor = mn_now; |
784 | rtmn_diff = ev_rt_now - mn_now; |
865 | rtmn_diff = ev_rt_now - mn_now; |
785 | |
866 | |
786 | if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS")) |
867 | if (!(flags & EVFLAG_NOENV) |
|
|
868 | && !enable_secure () |
|
|
869 | && getenv ("LIBEV_FLAGS")) |
787 | flags = atoi (getenv ("LIBEV_FLAGS")); |
870 | flags = atoi (getenv ("LIBEV_FLAGS")); |
788 | |
871 | |
789 | if (!(flags & 0x0000ffff)) |
872 | if (!(flags & 0x0000ffffUL)) |
790 | flags |= 0x0000ffff; |
873 | flags |= ev_recommended_backends (); |
791 | |
874 | |
792 | method = 0; |
875 | backend = 0; |
793 | #if EV_USE_PORT |
876 | #if EV_USE_PORT |
794 | if (!method && (flags & EVMETHOD_PORT )) method = port_init (EV_A_ flags); |
877 | if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); |
795 | #endif |
878 | #endif |
796 | #if EV_USE_KQUEUE |
879 | #if EV_USE_KQUEUE |
797 | if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags); |
880 | if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags); |
798 | #endif |
881 | #endif |
799 | #if EV_USE_EPOLL |
882 | #if EV_USE_EPOLL |
800 | if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags); |
883 | if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags); |
801 | #endif |
884 | #endif |
802 | #if EV_USE_POLL |
885 | #if EV_USE_POLL |
803 | if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags); |
886 | if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags); |
804 | #endif |
887 | #endif |
805 | #if EV_USE_SELECT |
888 | #if EV_USE_SELECT |
806 | if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags); |
889 | if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); |
807 | #endif |
890 | #endif |
808 | |
891 | |
809 | ev_init (&sigev, sigcb); |
892 | ev_init (&sigev, sigcb); |
810 | ev_set_priority (&sigev, EV_MAXPRI); |
893 | ev_set_priority (&sigev, EV_MAXPRI); |
811 | } |
894 | } |
812 | } |
895 | } |
813 | |
896 | |
814 | void |
897 | static void |
815 | loop_destroy (EV_P) |
898 | loop_destroy (EV_P) |
816 | { |
899 | { |
817 | int i; |
900 | int i; |
818 | |
901 | |
819 | #if EV_USE_PORT |
902 | #if EV_USE_PORT |
820 | if (method == EVMETHOD_PORT ) port_destroy (EV_A); |
903 | if (backend == EVBACKEND_PORT ) port_destroy (EV_A); |
821 | #endif |
904 | #endif |
822 | #if EV_USE_KQUEUE |
905 | #if EV_USE_KQUEUE |
823 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
906 | if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A); |
824 | #endif |
907 | #endif |
825 | #if EV_USE_EPOLL |
908 | #if EV_USE_EPOLL |
826 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
909 | if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A); |
827 | #endif |
910 | #endif |
828 | #if EV_USE_POLL |
911 | #if EV_USE_POLL |
829 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
912 | if (backend == EVBACKEND_POLL ) poll_destroy (EV_A); |
830 | #endif |
913 | #endif |
831 | #if EV_USE_SELECT |
914 | #if EV_USE_SELECT |
832 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
915 | if (backend == EVBACKEND_SELECT) select_destroy (EV_A); |
833 | #endif |
916 | #endif |
834 | |
917 | |
835 | for (i = NUMPRI; i--; ) |
918 | for (i = NUMPRI; i--; ) |
836 | array_free (pending, [i]); |
919 | array_free (pending, [i]); |
837 | |
920 | |
838 | /* have to use the microsoft-never-gets-it-right macro */ |
921 | /* have to use the microsoft-never-gets-it-right macro */ |
839 | array_free (fdchange, EMPTY0); |
922 | array_free (fdchange, EMPTY0); |
840 | array_free (timer, EMPTY0); |
923 | array_free (timer, EMPTY0); |
841 | #if EV_PERIODICS |
924 | #if EV_PERIODIC_ENABLE |
842 | array_free (periodic, EMPTY0); |
925 | array_free (periodic, EMPTY0); |
843 | #endif |
926 | #endif |
844 | array_free (idle, EMPTY0); |
927 | array_free (idle, EMPTY0); |
845 | array_free (prepare, EMPTY0); |
928 | array_free (prepare, EMPTY0); |
846 | array_free (check, EMPTY0); |
929 | array_free (check, EMPTY0); |
847 | |
930 | |
848 | method = 0; |
931 | backend = 0; |
849 | } |
932 | } |
850 | |
933 | |
851 | static void |
934 | static void |
852 | loop_fork (EV_P) |
935 | loop_fork (EV_P) |
853 | { |
936 | { |
854 | #if EV_USE_PORT |
937 | #if EV_USE_PORT |
855 | if (method == EVMETHOD_PORT ) port_fork (EV_A); |
938 | if (backend == EVBACKEND_PORT ) port_fork (EV_A); |
856 | #endif |
939 | #endif |
857 | #if EV_USE_KQUEUE |
940 | #if EV_USE_KQUEUE |
858 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
941 | if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A); |
859 | #endif |
942 | #endif |
860 | #if EV_USE_EPOLL |
943 | #if EV_USE_EPOLL |
861 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
944 | if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); |
862 | #endif |
945 | #endif |
863 | |
946 | |
864 | if (ev_is_active (&sigev)) |
947 | if (ev_is_active (&sigev)) |
865 | { |
948 | { |
866 | /* default loop */ |
949 | /* default loop */ |
… | |
… | |
887 | |
970 | |
888 | memset (loop, 0, sizeof (struct ev_loop)); |
971 | memset (loop, 0, sizeof (struct ev_loop)); |
889 | |
972 | |
890 | loop_init (EV_A_ flags); |
973 | loop_init (EV_A_ flags); |
891 | |
974 | |
892 | if (ev_method (EV_A)) |
975 | if (ev_backend (EV_A)) |
893 | return loop; |
976 | return loop; |
894 | |
977 | |
895 | return 0; |
978 | return 0; |
896 | } |
979 | } |
897 | |
980 | |
… | |
… | |
910 | |
993 | |
911 | #endif |
994 | #endif |
912 | |
995 | |
913 | #if EV_MULTIPLICITY |
996 | #if EV_MULTIPLICITY |
914 | struct ev_loop * |
997 | struct ev_loop * |
915 | ev_default_loop_ (unsigned int flags) |
998 | ev_default_loop_init (unsigned int flags) |
916 | #else |
999 | #else |
917 | int |
1000 | int |
918 | ev_default_loop (unsigned int flags) |
1001 | ev_default_loop (unsigned int flags) |
919 | #endif |
1002 | #endif |
920 | { |
1003 | { |
… | |
… | |
930 | ev_default_loop_ptr = 1; |
1013 | ev_default_loop_ptr = 1; |
931 | #endif |
1014 | #endif |
932 | |
1015 | |
933 | loop_init (EV_A_ flags); |
1016 | loop_init (EV_A_ flags); |
934 | |
1017 | |
935 | if (ev_method (EV_A)) |
1018 | if (ev_backend (EV_A)) |
936 | { |
1019 | { |
937 | siginit (EV_A); |
1020 | siginit (EV_A); |
938 | |
1021 | |
939 | #ifndef _WIN32 |
1022 | #ifndef _WIN32 |
940 | ev_signal_init (&childev, childcb, SIGCHLD); |
1023 | ev_signal_init (&childev, childcb, SIGCHLD); |
… | |
… | |
976 | { |
1059 | { |
977 | #if EV_MULTIPLICITY |
1060 | #if EV_MULTIPLICITY |
978 | struct ev_loop *loop = ev_default_loop_ptr; |
1061 | struct ev_loop *loop = ev_default_loop_ptr; |
979 | #endif |
1062 | #endif |
980 | |
1063 | |
981 | if (method) |
1064 | if (backend) |
982 | postfork = 1; |
1065 | postfork = 1; |
983 | } |
1066 | } |
984 | |
1067 | |
985 | /*****************************************************************************/ |
1068 | /*****************************************************************************/ |
986 | |
1069 | |
987 | static int |
1070 | int inline_size |
988 | any_pending (EV_P) |
1071 | any_pending (EV_P) |
989 | { |
1072 | { |
990 | int pri; |
1073 | int pri; |
991 | |
1074 | |
992 | for (pri = NUMPRI; pri--; ) |
1075 | for (pri = NUMPRI; pri--; ) |
… | |
… | |
994 | return 1; |
1077 | return 1; |
995 | |
1078 | |
996 | return 0; |
1079 | return 0; |
997 | } |
1080 | } |
998 | |
1081 | |
999 | static void |
1082 | void inline_speed |
1000 | call_pending (EV_P) |
1083 | call_pending (EV_P) |
1001 | { |
1084 | { |
1002 | int pri; |
1085 | int pri; |
1003 | |
1086 | |
1004 | for (pri = NUMPRI; pri--; ) |
1087 | for (pri = NUMPRI; pri--; ) |
1005 | while (pendingcnt [pri]) |
1088 | while (pendingcnt [pri]) |
1006 | { |
1089 | { |
1007 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1090 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1008 | |
1091 | |
1009 | if (p->w) |
1092 | if (expect_true (p->w)) |
1010 | { |
1093 | { |
|
|
1094 | assert (("non-pending watcher on pending list", p->w->pending)); |
|
|
1095 | |
1011 | p->w->pending = 0; |
1096 | p->w->pending = 0; |
1012 | EV_CB_INVOKE (p->w, p->events); |
1097 | EV_CB_INVOKE (p->w, p->events); |
1013 | } |
1098 | } |
1014 | } |
1099 | } |
1015 | } |
1100 | } |
1016 | |
1101 | |
1017 | static void |
1102 | void inline_size |
1018 | timers_reify (EV_P) |
1103 | timers_reify (EV_P) |
1019 | { |
1104 | { |
1020 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1105 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1021 | { |
1106 | { |
1022 | struct ev_timer *w = timers [0]; |
1107 | ev_timer *w = timers [0]; |
1023 | |
1108 | |
1024 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
1109 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
1025 | |
1110 | |
1026 | /* first reschedule or stop timer */ |
1111 | /* first reschedule or stop timer */ |
1027 | if (w->repeat) |
1112 | if (w->repeat) |
… | |
… | |
1039 | |
1124 | |
1040 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1125 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1041 | } |
1126 | } |
1042 | } |
1127 | } |
1043 | |
1128 | |
1044 | #if EV_PERIODICS |
1129 | #if EV_PERIODIC_ENABLE |
1045 | static void |
1130 | void inline_size |
1046 | periodics_reify (EV_P) |
1131 | periodics_reify (EV_P) |
1047 | { |
1132 | { |
1048 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1133 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1049 | { |
1134 | { |
1050 | struct ev_periodic *w = periodics [0]; |
1135 | ev_periodic *w = periodics [0]; |
1051 | |
1136 | |
1052 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1137 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1053 | |
1138 | |
1054 | /* first reschedule or stop timer */ |
1139 | /* first reschedule or stop timer */ |
1055 | if (w->reschedule_cb) |
1140 | if (w->reschedule_cb) |
… | |
… | |
1069 | |
1154 | |
1070 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1155 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1071 | } |
1156 | } |
1072 | } |
1157 | } |
1073 | |
1158 | |
1074 | static void |
1159 | static void noinline |
1075 | periodics_reschedule (EV_P) |
1160 | periodics_reschedule (EV_P) |
1076 | { |
1161 | { |
1077 | int i; |
1162 | int i; |
1078 | |
1163 | |
1079 | /* adjust periodics after time jump */ |
1164 | /* adjust periodics after time jump */ |
1080 | for (i = 0; i < periodiccnt; ++i) |
1165 | for (i = 0; i < periodiccnt; ++i) |
1081 | { |
1166 | { |
1082 | struct ev_periodic *w = periodics [i]; |
1167 | ev_periodic *w = periodics [i]; |
1083 | |
1168 | |
1084 | if (w->reschedule_cb) |
1169 | if (w->reschedule_cb) |
1085 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1170 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1086 | else if (w->interval) |
1171 | else if (w->interval) |
1087 | ((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; |
… | |
… | |
1091 | for (i = periodiccnt >> 1; i--; ) |
1176 | for (i = periodiccnt >> 1; i--; ) |
1092 | downheap ((WT *)periodics, periodiccnt, i); |
1177 | downheap ((WT *)periodics, periodiccnt, i); |
1093 | } |
1178 | } |
1094 | #endif |
1179 | #endif |
1095 | |
1180 | |
1096 | inline int |
1181 | int inline_size |
1097 | time_update_monotonic (EV_P) |
1182 | time_update_monotonic (EV_P) |
1098 | { |
1183 | { |
1099 | mn_now = get_clock (); |
1184 | mn_now = get_clock (); |
1100 | |
1185 | |
1101 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1186 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
… | |
… | |
1109 | ev_rt_now = ev_time (); |
1194 | ev_rt_now = ev_time (); |
1110 | return 1; |
1195 | return 1; |
1111 | } |
1196 | } |
1112 | } |
1197 | } |
1113 | |
1198 | |
1114 | static void |
1199 | void inline_size |
1115 | time_update (EV_P) |
1200 | time_update (EV_P) |
1116 | { |
1201 | { |
1117 | int i; |
1202 | int i; |
1118 | |
1203 | |
1119 | #if EV_USE_MONOTONIC |
1204 | #if EV_USE_MONOTONIC |
… | |
… | |
1121 | { |
1206 | { |
1122 | if (time_update_monotonic (EV_A)) |
1207 | if (time_update_monotonic (EV_A)) |
1123 | { |
1208 | { |
1124 | ev_tstamp odiff = rtmn_diff; |
1209 | ev_tstamp odiff = rtmn_diff; |
1125 | |
1210 | |
1126 | 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; ) |
1127 | { |
1220 | { |
1128 | rtmn_diff = ev_rt_now - mn_now; |
1221 | rtmn_diff = ev_rt_now - mn_now; |
1129 | |
1222 | |
1130 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1223 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1131 | return; /* all is well */ |
1224 | return; /* all is well */ |
… | |
… | |
1133 | ev_rt_now = ev_time (); |
1226 | ev_rt_now = ev_time (); |
1134 | mn_now = get_clock (); |
1227 | mn_now = get_clock (); |
1135 | now_floor = mn_now; |
1228 | now_floor = mn_now; |
1136 | } |
1229 | } |
1137 | |
1230 | |
1138 | # if EV_PERIODICS |
1231 | # if EV_PERIODIC_ENABLE |
1139 | periodics_reschedule (EV_A); |
1232 | periodics_reschedule (EV_A); |
1140 | # endif |
1233 | # endif |
1141 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1234 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1142 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1235 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1143 | } |
1236 | } |
… | |
… | |
1147 | { |
1240 | { |
1148 | ev_rt_now = ev_time (); |
1241 | ev_rt_now = ev_time (); |
1149 | |
1242 | |
1150 | 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)) |
1151 | { |
1244 | { |
1152 | #if EV_PERIODICS |
1245 | #if EV_PERIODIC_ENABLE |
1153 | periodics_reschedule (EV_A); |
1246 | periodics_reschedule (EV_A); |
1154 | #endif |
1247 | #endif |
1155 | |
1248 | |
1156 | /* 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 */ |
1157 | for (i = 0; i < timercnt; ++i) |
1250 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
1177 | static int loop_done; |
1270 | static int loop_done; |
1178 | |
1271 | |
1179 | void |
1272 | void |
1180 | ev_loop (EV_P_ int flags) |
1273 | ev_loop (EV_P_ int flags) |
1181 | { |
1274 | { |
1182 | double block; |
|
|
1183 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
1275 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) |
|
|
1276 | ? EVUNLOOP_ONE |
|
|
1277 | : EVUNLOOP_CANCEL; |
1184 | |
1278 | |
1185 | while (activecnt) |
1279 | while (activecnt) |
1186 | { |
1280 | { |
1187 | /* queue check watchers (and execute them) */ |
1281 | /* queue check watchers (and execute them) */ |
1188 | if (expect_false (preparecnt)) |
1282 | if (expect_false (preparecnt)) |
… | |
… | |
1197 | |
1291 | |
1198 | /* update fd-related kernel structures */ |
1292 | /* update fd-related kernel structures */ |
1199 | fd_reify (EV_A); |
1293 | fd_reify (EV_A); |
1200 | |
1294 | |
1201 | /* calculate blocking time */ |
1295 | /* calculate blocking time */ |
|
|
1296 | { |
|
|
1297 | double block; |
1202 | |
1298 | |
1203 | /* we only need this for !monotonic clock or timers, but as we basically |
1299 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1204 | 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 */ |
1205 | #if EV_USE_MONOTONIC |
1304 | #if EV_USE_MONOTONIC |
1206 | if (expect_true (have_monotonic)) |
1305 | if (expect_true (have_monotonic)) |
1207 | time_update_monotonic (EV_A); |
1306 | time_update_monotonic (EV_A); |
1208 | else |
1307 | else |
1209 | #endif |
1308 | #endif |
1210 | { |
1309 | { |
1211 | ev_rt_now = ev_time (); |
1310 | ev_rt_now = ev_time (); |
1212 | mn_now = ev_rt_now; |
1311 | mn_now = ev_rt_now; |
1213 | } |
1312 | } |
1214 | |
1313 | |
1215 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
|
|
1216 | block = 0.; |
|
|
1217 | else |
|
|
1218 | { |
|
|
1219 | block = MAX_BLOCKTIME; |
1314 | block = MAX_BLOCKTIME; |
1220 | |
1315 | |
1221 | if (timercnt) |
1316 | if (timercnt) |
1222 | { |
1317 | { |
1223 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1318 | ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; |
1224 | if (block > to) block = to; |
1319 | if (block > to) block = to; |
1225 | } |
1320 | } |
1226 | |
1321 | |
1227 | #if EV_PERIODICS |
1322 | #if EV_PERIODIC_ENABLE |
1228 | if (periodiccnt) |
1323 | if (periodiccnt) |
1229 | { |
1324 | { |
1230 | 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; |
1231 | if (block > to) block = to; |
1326 | if (block > to) block = to; |
1232 | } |
1327 | } |
1233 | #endif |
1328 | #endif |
1234 | |
1329 | |
1235 | if (block < 0.) block = 0.; |
1330 | if (expect_false (block < 0.)) block = 0.; |
1236 | } |
1331 | } |
1237 | |
1332 | |
1238 | method_poll (EV_A_ block); |
1333 | backend_poll (EV_A_ block); |
|
|
1334 | } |
1239 | |
1335 | |
1240 | /* update ev_rt_now, do magic */ |
1336 | /* update ev_rt_now, do magic */ |
1241 | time_update (EV_A); |
1337 | time_update (EV_A); |
1242 | |
1338 | |
1243 | /* queue pending timers and reschedule them */ |
1339 | /* queue pending timers and reschedule them */ |
1244 | timers_reify (EV_A); /* relative timers called last */ |
1340 | timers_reify (EV_A); /* relative timers called last */ |
1245 | #if EV_PERIODICS |
1341 | #if EV_PERIODIC_ENABLE |
1246 | periodics_reify (EV_A); /* absolute timers called first */ |
1342 | periodics_reify (EV_A); /* absolute timers called first */ |
1247 | #endif |
1343 | #endif |
1248 | |
1344 | |
1249 | /* queue idle watchers unless io or timers are pending */ |
1345 | /* queue idle watchers unless other events are pending */ |
1250 | if (idlecnt && !any_pending (EV_A)) |
1346 | if (idlecnt && !any_pending (EV_A)) |
1251 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1347 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1252 | |
1348 | |
1253 | /* queue check watchers, to be executed first */ |
1349 | /* queue check watchers, to be executed first */ |
1254 | if (checkcnt) |
1350 | if (expect_false (checkcnt)) |
1255 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1351 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1256 | |
1352 | |
1257 | call_pending (EV_A); |
1353 | call_pending (EV_A); |
1258 | |
1354 | |
1259 | if (loop_done) |
1355 | if (expect_false (loop_done)) |
1260 | break; |
1356 | break; |
1261 | } |
1357 | } |
1262 | |
1358 | |
1263 | if (loop_done != 2) |
1359 | if (loop_done == EVUNLOOP_ONE) |
1264 | loop_done = 0; |
1360 | loop_done = EVUNLOOP_CANCEL; |
1265 | } |
1361 | } |
1266 | |
1362 | |
1267 | void |
1363 | void |
1268 | ev_unloop (EV_P_ int how) |
1364 | ev_unloop (EV_P_ int how) |
1269 | { |
1365 | { |
1270 | loop_done = how; |
1366 | loop_done = how; |
1271 | } |
1367 | } |
1272 | |
1368 | |
1273 | /*****************************************************************************/ |
1369 | /*****************************************************************************/ |
1274 | |
1370 | |
1275 | inline void |
1371 | void inline_size |
1276 | wlist_add (WL *head, WL elem) |
1372 | wlist_add (WL *head, WL elem) |
1277 | { |
1373 | { |
1278 | elem->next = *head; |
1374 | elem->next = *head; |
1279 | *head = elem; |
1375 | *head = elem; |
1280 | } |
1376 | } |
1281 | |
1377 | |
1282 | inline void |
1378 | void inline_size |
1283 | wlist_del (WL *head, WL elem) |
1379 | wlist_del (WL *head, WL elem) |
1284 | { |
1380 | { |
1285 | while (*head) |
1381 | while (*head) |
1286 | { |
1382 | { |
1287 | if (*head == elem) |
1383 | if (*head == elem) |
… | |
… | |
1292 | |
1388 | |
1293 | head = &(*head)->next; |
1389 | head = &(*head)->next; |
1294 | } |
1390 | } |
1295 | } |
1391 | } |
1296 | |
1392 | |
1297 | inline void |
1393 | void inline_speed |
1298 | ev_clear_pending (EV_P_ W w) |
1394 | ev_clear_pending (EV_P_ W w) |
1299 | { |
1395 | { |
1300 | if (w->pending) |
1396 | if (w->pending) |
1301 | { |
1397 | { |
1302 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1398 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1303 | w->pending = 0; |
1399 | w->pending = 0; |
1304 | } |
1400 | } |
1305 | } |
1401 | } |
1306 | |
1402 | |
1307 | inline void |
1403 | void inline_speed |
1308 | ev_start (EV_P_ W w, int active) |
1404 | ev_start (EV_P_ W w, int active) |
1309 | { |
1405 | { |
1310 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1406 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1311 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
1407 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
1312 | |
1408 | |
1313 | w->active = active; |
1409 | w->active = active; |
1314 | ev_ref (EV_A); |
1410 | ev_ref (EV_A); |
1315 | } |
1411 | } |
1316 | |
1412 | |
1317 | inline void |
1413 | void inline_size |
1318 | ev_stop (EV_P_ W w) |
1414 | ev_stop (EV_P_ W w) |
1319 | { |
1415 | { |
1320 | ev_unref (EV_A); |
1416 | ev_unref (EV_A); |
1321 | w->active = 0; |
1417 | w->active = 0; |
1322 | } |
1418 | } |
1323 | |
1419 | |
1324 | /*****************************************************************************/ |
1420 | /*****************************************************************************/ |
1325 | |
1421 | |
1326 | void |
1422 | void |
1327 | ev_io_start (EV_P_ struct ev_io *w) |
1423 | ev_io_start (EV_P_ ev_io *w) |
1328 | { |
1424 | { |
1329 | int fd = w->fd; |
1425 | int fd = w->fd; |
1330 | |
1426 | |
1331 | if (ev_is_active (w)) |
1427 | if (expect_false (ev_is_active (w))) |
1332 | return; |
1428 | return; |
1333 | |
1429 | |
1334 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1430 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1335 | |
1431 | |
1336 | ev_start (EV_A_ (W)w, 1); |
1432 | ev_start (EV_A_ (W)w, 1); |
… | |
… | |
1339 | |
1435 | |
1340 | fd_change (EV_A_ fd); |
1436 | fd_change (EV_A_ fd); |
1341 | } |
1437 | } |
1342 | |
1438 | |
1343 | void |
1439 | void |
1344 | ev_io_stop (EV_P_ struct ev_io *w) |
1440 | ev_io_stop (EV_P_ ev_io *w) |
1345 | { |
1441 | { |
1346 | ev_clear_pending (EV_A_ (W)w); |
1442 | ev_clear_pending (EV_A_ (W)w); |
1347 | if (!ev_is_active (w)) |
1443 | if (expect_false (!ev_is_active (w))) |
1348 | return; |
1444 | return; |
1349 | |
1445 | |
1350 | 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)); |
1351 | |
1447 | |
1352 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1448 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
… | |
… | |
1354 | |
1450 | |
1355 | fd_change (EV_A_ w->fd); |
1451 | fd_change (EV_A_ w->fd); |
1356 | } |
1452 | } |
1357 | |
1453 | |
1358 | void |
1454 | void |
1359 | ev_timer_start (EV_P_ struct ev_timer *w) |
1455 | ev_timer_start (EV_P_ ev_timer *w) |
1360 | { |
1456 | { |
1361 | if (ev_is_active (w)) |
1457 | if (expect_false (ev_is_active (w))) |
1362 | return; |
1458 | return; |
1363 | |
1459 | |
1364 | ((WT)w)->at += mn_now; |
1460 | ((WT)w)->at += mn_now; |
1365 | |
1461 | |
1366 | 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.)); |
1367 | |
1463 | |
1368 | ev_start (EV_A_ (W)w, ++timercnt); |
1464 | ev_start (EV_A_ (W)w, ++timercnt); |
1369 | array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); |
1465 | array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); |
1370 | timers [timercnt - 1] = w; |
1466 | timers [timercnt - 1] = w; |
1371 | upheap ((WT *)timers, timercnt - 1); |
1467 | upheap ((WT *)timers, timercnt - 1); |
1372 | |
1468 | |
1373 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1469 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1374 | } |
1470 | } |
1375 | |
1471 | |
1376 | void |
1472 | void |
1377 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1473 | ev_timer_stop (EV_P_ ev_timer *w) |
1378 | { |
1474 | { |
1379 | ev_clear_pending (EV_A_ (W)w); |
1475 | ev_clear_pending (EV_A_ (W)w); |
1380 | if (!ev_is_active (w)) |
1476 | if (expect_false (!ev_is_active (w))) |
1381 | return; |
1477 | return; |
1382 | |
1478 | |
1383 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1479 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1384 | |
1480 | |
1385 | if (((W)w)->active < timercnt--) |
1481 | if (expect_true (((W)w)->active < timercnt--)) |
1386 | { |
1482 | { |
1387 | timers [((W)w)->active - 1] = timers [timercnt]; |
1483 | timers [((W)w)->active - 1] = timers [timercnt]; |
1388 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1484 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1389 | } |
1485 | } |
1390 | |
1486 | |
… | |
… | |
1392 | |
1488 | |
1393 | ev_stop (EV_A_ (W)w); |
1489 | ev_stop (EV_A_ (W)w); |
1394 | } |
1490 | } |
1395 | |
1491 | |
1396 | void |
1492 | void |
1397 | ev_timer_again (EV_P_ struct ev_timer *w) |
1493 | ev_timer_again (EV_P_ ev_timer *w) |
1398 | { |
1494 | { |
1399 | if (ev_is_active (w)) |
1495 | if (ev_is_active (w)) |
1400 | { |
1496 | { |
1401 | if (w->repeat) |
1497 | if (w->repeat) |
1402 | { |
1498 | { |
… | |
… | |
1411 | w->at = w->repeat; |
1507 | w->at = w->repeat; |
1412 | ev_timer_start (EV_A_ w); |
1508 | ev_timer_start (EV_A_ w); |
1413 | } |
1509 | } |
1414 | } |
1510 | } |
1415 | |
1511 | |
1416 | #if EV_PERIODICS |
1512 | #if EV_PERIODIC_ENABLE |
1417 | void |
1513 | void |
1418 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1514 | ev_periodic_start (EV_P_ ev_periodic *w) |
1419 | { |
1515 | { |
1420 | if (ev_is_active (w)) |
1516 | if (expect_false (ev_is_active (w))) |
1421 | return; |
1517 | return; |
1422 | |
1518 | |
1423 | if (w->reschedule_cb) |
1519 | if (w->reschedule_cb) |
1424 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1520 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1425 | else if (w->interval) |
1521 | else if (w->interval) |
… | |
… | |
1428 | /* 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 */ |
1429 | ((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; |
1430 | } |
1526 | } |
1431 | |
1527 | |
1432 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1528 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1433 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1529 | array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1434 | periodics [periodiccnt - 1] = w; |
1530 | periodics [periodiccnt - 1] = w; |
1435 | upheap ((WT *)periodics, periodiccnt - 1); |
1531 | upheap ((WT *)periodics, periodiccnt - 1); |
1436 | |
1532 | |
1437 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1533 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1438 | } |
1534 | } |
1439 | |
1535 | |
1440 | void |
1536 | void |
1441 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1537 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1442 | { |
1538 | { |
1443 | ev_clear_pending (EV_A_ (W)w); |
1539 | ev_clear_pending (EV_A_ (W)w); |
1444 | if (!ev_is_active (w)) |
1540 | if (expect_false (!ev_is_active (w))) |
1445 | return; |
1541 | return; |
1446 | |
1542 | |
1447 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1543 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1448 | |
1544 | |
1449 | if (((W)w)->active < periodiccnt--) |
1545 | if (expect_true (((W)w)->active < periodiccnt--)) |
1450 | { |
1546 | { |
1451 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1547 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1452 | adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1548 | adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1453 | } |
1549 | } |
1454 | |
1550 | |
1455 | ev_stop (EV_A_ (W)w); |
1551 | ev_stop (EV_A_ (W)w); |
1456 | } |
1552 | } |
1457 | |
1553 | |
1458 | void |
1554 | void |
1459 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1555 | ev_periodic_again (EV_P_ ev_periodic *w) |
1460 | { |
1556 | { |
1461 | /* TODO: use adjustheap and recalculation */ |
1557 | /* TODO: use adjustheap and recalculation */ |
1462 | ev_periodic_stop (EV_A_ w); |
1558 | ev_periodic_stop (EV_A_ w); |
1463 | ev_periodic_start (EV_A_ w); |
1559 | ev_periodic_start (EV_A_ w); |
1464 | } |
1560 | } |
1465 | #endif |
1561 | #endif |
1466 | |
1562 | |
1467 | void |
|
|
1468 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1469 | { |
|
|
1470 | if (ev_is_active (w)) |
|
|
1471 | return; |
|
|
1472 | |
|
|
1473 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1474 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
|
|
1475 | idles [idlecnt - 1] = w; |
|
|
1476 | } |
|
|
1477 | |
|
|
1478 | void |
|
|
1479 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1480 | { |
|
|
1481 | ev_clear_pending (EV_A_ (W)w); |
|
|
1482 | if (!ev_is_active (w)) |
|
|
1483 | return; |
|
|
1484 | |
|
|
1485 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
|
|
1486 | ev_stop (EV_A_ (W)w); |
|
|
1487 | } |
|
|
1488 | |
|
|
1489 | void |
|
|
1490 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1491 | { |
|
|
1492 | if (ev_is_active (w)) |
|
|
1493 | return; |
|
|
1494 | |
|
|
1495 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1496 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
|
|
1497 | prepares [preparecnt - 1] = w; |
|
|
1498 | } |
|
|
1499 | |
|
|
1500 | void |
|
|
1501 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1502 | { |
|
|
1503 | ev_clear_pending (EV_A_ (W)w); |
|
|
1504 | if (!ev_is_active (w)) |
|
|
1505 | return; |
|
|
1506 | |
|
|
1507 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
|
|
1508 | ev_stop (EV_A_ (W)w); |
|
|
1509 | } |
|
|
1510 | |
|
|
1511 | void |
|
|
1512 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1513 | { |
|
|
1514 | if (ev_is_active (w)) |
|
|
1515 | return; |
|
|
1516 | |
|
|
1517 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1518 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); |
|
|
1519 | checks [checkcnt - 1] = w; |
|
|
1520 | } |
|
|
1521 | |
|
|
1522 | void |
|
|
1523 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1524 | { |
|
|
1525 | ev_clear_pending (EV_A_ (W)w); |
|
|
1526 | if (!ev_is_active (w)) |
|
|
1527 | return; |
|
|
1528 | |
|
|
1529 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
|
|
1530 | ev_stop (EV_A_ (W)w); |
|
|
1531 | } |
|
|
1532 | |
|
|
1533 | #ifndef SA_RESTART |
1563 | #ifndef SA_RESTART |
1534 | # define SA_RESTART 0 |
1564 | # define SA_RESTART 0 |
1535 | #endif |
1565 | #endif |
1536 | |
1566 | |
1537 | void |
1567 | void |
1538 | ev_signal_start (EV_P_ struct ev_signal *w) |
1568 | ev_signal_start (EV_P_ ev_signal *w) |
1539 | { |
1569 | { |
1540 | #if EV_MULTIPLICITY |
1570 | #if EV_MULTIPLICITY |
1541 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1571 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1542 | #endif |
1572 | #endif |
1543 | if (ev_is_active (w)) |
1573 | if (expect_false (ev_is_active (w))) |
1544 | return; |
1574 | return; |
1545 | |
1575 | |
1546 | 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)); |
1547 | |
1577 | |
1548 | ev_start (EV_A_ (W)w, 1); |
1578 | ev_start (EV_A_ (W)w, 1); |
… | |
… | |
1562 | #endif |
1592 | #endif |
1563 | } |
1593 | } |
1564 | } |
1594 | } |
1565 | |
1595 | |
1566 | void |
1596 | void |
1567 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1597 | ev_signal_stop (EV_P_ ev_signal *w) |
1568 | { |
1598 | { |
1569 | ev_clear_pending (EV_A_ (W)w); |
1599 | ev_clear_pending (EV_A_ (W)w); |
1570 | if (!ev_is_active (w)) |
1600 | if (expect_false (!ev_is_active (w))) |
1571 | return; |
1601 | return; |
1572 | |
1602 | |
1573 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1603 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1574 | ev_stop (EV_A_ (W)w); |
1604 | ev_stop (EV_A_ (W)w); |
1575 | |
1605 | |
1576 | if (!signals [w->signum - 1].head) |
1606 | if (!signals [w->signum - 1].head) |
1577 | signal (w->signum, SIG_DFL); |
1607 | signal (w->signum, SIG_DFL); |
1578 | } |
1608 | } |
1579 | |
1609 | |
1580 | void |
1610 | void |
1581 | ev_child_start (EV_P_ struct ev_child *w) |
1611 | ev_child_start (EV_P_ ev_child *w) |
1582 | { |
1612 | { |
1583 | #if EV_MULTIPLICITY |
1613 | #if EV_MULTIPLICITY |
1584 | assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1614 | assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1585 | #endif |
1615 | #endif |
1586 | if (ev_is_active (w)) |
1616 | if (expect_false (ev_is_active (w))) |
1587 | return; |
1617 | return; |
1588 | |
1618 | |
1589 | ev_start (EV_A_ (W)w, 1); |
1619 | ev_start (EV_A_ (W)w, 1); |
1590 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1620 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1591 | } |
1621 | } |
1592 | |
1622 | |
1593 | void |
1623 | void |
1594 | ev_child_stop (EV_P_ struct ev_child *w) |
1624 | ev_child_stop (EV_P_ ev_child *w) |
1595 | { |
1625 | { |
1596 | ev_clear_pending (EV_A_ (W)w); |
1626 | ev_clear_pending (EV_A_ (W)w); |
1597 | if (!ev_is_active (w)) |
1627 | if (expect_false (!ev_is_active (w))) |
1598 | return; |
1628 | return; |
1599 | |
1629 | |
1600 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1630 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1601 | ev_stop (EV_A_ (W)w); |
1631 | ev_stop (EV_A_ (W)w); |
1602 | } |
1632 | } |
1603 | |
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 | |
1604 | /*****************************************************************************/ |
1831 | /*****************************************************************************/ |
1605 | |
1832 | |
1606 | struct ev_once |
1833 | struct ev_once |
1607 | { |
1834 | { |
1608 | struct ev_io io; |
1835 | ev_io io; |
1609 | struct ev_timer to; |
1836 | ev_timer to; |
1610 | void (*cb)(int revents, void *arg); |
1837 | void (*cb)(int revents, void *arg); |
1611 | void *arg; |
1838 | void *arg; |
1612 | }; |
1839 | }; |
1613 | |
1840 | |
1614 | static void |
1841 | static void |
… | |
… | |
1623 | |
1850 | |
1624 | cb (revents, arg); |
1851 | cb (revents, arg); |
1625 | } |
1852 | } |
1626 | |
1853 | |
1627 | static void |
1854 | static void |
1628 | once_cb_io (EV_P_ struct ev_io *w, int revents) |
1855 | once_cb_io (EV_P_ ev_io *w, int revents) |
1629 | { |
1856 | { |
1630 | 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); |
1631 | } |
1858 | } |
1632 | |
1859 | |
1633 | static void |
1860 | static void |
1634 | once_cb_to (EV_P_ struct ev_timer *w, int revents) |
1861 | once_cb_to (EV_P_ ev_timer *w, int revents) |
1635 | { |
1862 | { |
1636 | 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); |
1637 | } |
1864 | } |
1638 | |
1865 | |
1639 | void |
1866 | void |
1640 | 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) |
1641 | { |
1868 | { |
1642 | 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)); |
1643 | |
1870 | |
1644 | if (!once) |
1871 | if (expect_false (!once)) |
|
|
1872 | { |
1645 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1873 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1646 | else |
1874 | return; |
1647 | { |
1875 | } |
|
|
1876 | |
1648 | once->cb = cb; |
1877 | once->cb = cb; |
1649 | once->arg = arg; |
1878 | once->arg = arg; |
1650 | |
1879 | |
1651 | ev_init (&once->io, once_cb_io); |
1880 | ev_init (&once->io, once_cb_io); |
1652 | if (fd >= 0) |
1881 | if (fd >= 0) |
1653 | { |
1882 | { |
1654 | ev_io_set (&once->io, fd, events); |
1883 | ev_io_set (&once->io, fd, events); |
1655 | ev_io_start (EV_A_ &once->io); |
1884 | ev_io_start (EV_A_ &once->io); |
1656 | } |
1885 | } |
1657 | |
1886 | |
1658 | ev_init (&once->to, once_cb_to); |
1887 | ev_init (&once->to, once_cb_to); |
1659 | if (timeout >= 0.) |
1888 | if (timeout >= 0.) |
1660 | { |
1889 | { |
1661 | ev_timer_set (&once->to, timeout, 0.); |
1890 | ev_timer_set (&once->to, timeout, 0.); |
1662 | ev_timer_start (EV_A_ &once->to); |
1891 | ev_timer_start (EV_A_ &once->to); |
1663 | } |
|
|
1664 | } |
1892 | } |
1665 | } |
1893 | } |
1666 | |
1894 | |
1667 | #ifdef __cplusplus |
1895 | #ifdef __cplusplus |
1668 | } |
1896 | } |