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