… | |
… | |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
30 | */ |
31 | #ifndef EV_STANDALONE |
31 | #ifndef EV_EMBED |
32 | # include "config.h" |
32 | # include "config.h" |
33 | #endif |
33 | #endif |
34 | |
34 | |
35 | #include <math.h> |
35 | #include <math.h> |
36 | #include <stdlib.h> |
36 | #include <stdlib.h> |
… | |
… | |
93 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
93 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
94 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
94 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
95 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
95 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
96 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
96 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
97 | |
97 | |
|
|
98 | #ifndef EV_EMBED |
98 | #include "ev.h" |
99 | # include "ev.h" |
|
|
100 | #endif |
99 | |
101 | |
100 | #if __GNUC__ >= 3 |
102 | #if __GNUC__ >= 3 |
101 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
103 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
102 | # define inline inline |
104 | # define inline inline |
103 | #else |
105 | #else |
… | |
… | |
113 | |
115 | |
114 | typedef struct ev_watcher *W; |
116 | typedef struct ev_watcher *W; |
115 | typedef struct ev_watcher_list *WL; |
117 | typedef struct ev_watcher_list *WL; |
116 | typedef struct ev_watcher_time *WT; |
118 | typedef struct ev_watcher_time *WT; |
117 | |
119 | |
118 | static ev_tstamp now_floor, mn_now, diff; /* monotonic clock */ |
120 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
119 | static ev_tstamp rt_now; |
|
|
120 | static int method; |
|
|
121 | |
121 | |
122 | static int have_monotonic; /* runtime */ |
122 | /*****************************************************************************/ |
123 | |
123 | |
124 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
124 | typedef struct |
125 | static void (*method_modify)(EV_P_ int fd, int oev, int nev); |
125 | { |
126 | static void (*method_poll)(EV_P_ ev_tstamp timeout); |
126 | struct ev_watcher_list *head; |
|
|
127 | unsigned char events; |
|
|
128 | unsigned char reify; |
|
|
129 | } ANFD; |
127 | |
130 | |
128 | static int activecnt; /* number of active events */ |
131 | typedef struct |
|
|
132 | { |
|
|
133 | W w; |
|
|
134 | int events; |
|
|
135 | } ANPENDING; |
129 | |
136 | |
130 | #if EV_USE_SELECT |
137 | #if EV_MULTIPLICITY |
131 | static unsigned char *vec_ri, *vec_ro, *vec_wi, *vec_wo; |
|
|
132 | static int vec_max; |
|
|
133 | #endif |
|
|
134 | |
138 | |
135 | #if EV_USEV_POLL |
139 | struct ev_loop |
136 | static struct pollfd *polls; |
140 | { |
137 | static int pollmax, pollcnt; |
141 | # define VAR(name,decl) decl; |
138 | static int *pollidxs; /* maps fds into structure indices */ |
142 | # include "ev_vars.h" |
139 | static int pollidxmax; |
143 | }; |
140 | #endif |
144 | # undef VAR |
|
|
145 | # include "ev_wrap.h" |
141 | |
146 | |
142 | #if EV_USE_EPOLL |
147 | #else |
143 | static int epoll_fd = -1; |
|
|
144 | |
148 | |
145 | static struct epoll_event *events; |
149 | # define VAR(name,decl) static decl; |
146 | static int eventmax; |
150 | # include "ev_vars.h" |
147 | #endif |
151 | # undef VAR |
148 | |
152 | |
149 | #if EV_USE_KQUEUE |
|
|
150 | static int kqueue_fd; |
|
|
151 | static struct kevent *kqueue_changes; |
|
|
152 | static int kqueue_changemax, kqueue_changecnt; |
|
|
153 | static struct kevent *kqueue_events; |
|
|
154 | static int kqueue_eventmax; |
|
|
155 | #endif |
153 | #endif |
156 | |
154 | |
157 | /*****************************************************************************/ |
155 | /*****************************************************************************/ |
158 | |
156 | |
159 | inline ev_tstamp |
157 | inline ev_tstamp |
… | |
… | |
208 | cur = newcnt; \ |
206 | cur = newcnt; \ |
209 | } |
207 | } |
210 | |
208 | |
211 | /*****************************************************************************/ |
209 | /*****************************************************************************/ |
212 | |
210 | |
213 | typedef struct |
|
|
214 | { |
|
|
215 | struct ev_watcher_list *head; |
|
|
216 | unsigned char events; |
|
|
217 | unsigned char reify; |
|
|
218 | } ANFD; |
|
|
219 | |
|
|
220 | static ANFD *anfds; |
|
|
221 | static int anfdmax; |
|
|
222 | |
|
|
223 | static void |
211 | static void |
224 | anfds_init (ANFD *base, int count) |
212 | anfds_init (ANFD *base, int count) |
225 | { |
213 | { |
226 | while (count--) |
214 | while (count--) |
227 | { |
215 | { |
… | |
… | |
230 | base->reify = 0; |
218 | base->reify = 0; |
231 | |
219 | |
232 | ++base; |
220 | ++base; |
233 | } |
221 | } |
234 | } |
222 | } |
235 | |
|
|
236 | typedef struct |
|
|
237 | { |
|
|
238 | W w; |
|
|
239 | int events; |
|
|
240 | } ANPENDING; |
|
|
241 | |
|
|
242 | static ANPENDING *pendings [NUMPRI]; |
|
|
243 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
|
|
244 | |
223 | |
245 | static void |
224 | static void |
246 | event (EV_P_ W w, int events) |
225 | event (EV_P_ W w, int events) |
247 | { |
226 | { |
248 | if (w->pending) |
227 | if (w->pending) |
… | |
… | |
280 | event (EV_A_ (W)w, ev); |
259 | event (EV_A_ (W)w, ev); |
281 | } |
260 | } |
282 | } |
261 | } |
283 | |
262 | |
284 | /*****************************************************************************/ |
263 | /*****************************************************************************/ |
285 | |
|
|
286 | static int *fdchanges; |
|
|
287 | static int fdchangemax, fdchangecnt; |
|
|
288 | |
264 | |
289 | static void |
265 | static void |
290 | fd_reify (EV_P) |
266 | fd_reify (EV_P) |
291 | { |
267 | { |
292 | int i; |
268 | int i; |
… | |
… | |
364 | fd_kill (EV_A_ fd); |
340 | fd_kill (EV_A_ fd); |
365 | return; |
341 | return; |
366 | } |
342 | } |
367 | } |
343 | } |
368 | |
344 | |
|
|
345 | /* susually called after fork if method needs to re-arm all fds from scratch */ |
|
|
346 | static void |
|
|
347 | fd_rearm_all (EV_P) |
|
|
348 | { |
|
|
349 | int fd; |
|
|
350 | |
|
|
351 | /* this should be highly optimised to not do anything but set a flag */ |
|
|
352 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
353 | if (anfds [fd].events) |
|
|
354 | { |
|
|
355 | anfds [fd].events = 0; |
|
|
356 | fd_change (fd); |
|
|
357 | } |
|
|
358 | } |
|
|
359 | |
369 | /*****************************************************************************/ |
360 | /*****************************************************************************/ |
370 | |
361 | |
371 | static struct ev_timer **timers; |
|
|
372 | static int timermax, timercnt; |
|
|
373 | |
|
|
374 | static struct ev_periodic **periodics; |
|
|
375 | static int periodicmax, periodiccnt; |
|
|
376 | |
|
|
377 | static void |
362 | static void |
378 | upheap (WT *timers, int k) |
363 | upheap (WT *heap, int k) |
379 | { |
364 | { |
380 | WT w = timers [k]; |
365 | WT w = heap [k]; |
381 | |
366 | |
382 | while (k && timers [k >> 1]->at > w->at) |
367 | while (k && heap [k >> 1]->at > w->at) |
383 | { |
368 | { |
384 | timers [k] = timers [k >> 1]; |
369 | heap [k] = heap [k >> 1]; |
385 | timers [k]->active = k + 1; |
370 | heap [k]->active = k + 1; |
386 | k >>= 1; |
371 | k >>= 1; |
387 | } |
372 | } |
388 | |
373 | |
389 | timers [k] = w; |
374 | heap [k] = w; |
390 | timers [k]->active = k + 1; |
375 | heap [k]->active = k + 1; |
391 | |
376 | |
392 | } |
377 | } |
393 | |
378 | |
394 | static void |
379 | static void |
395 | downheap (WT *timers, int N, int k) |
380 | downheap (WT *heap, int N, int k) |
396 | { |
381 | { |
397 | WT w = timers [k]; |
382 | WT w = heap [k]; |
398 | |
383 | |
399 | while (k < (N >> 1)) |
384 | while (k < (N >> 1)) |
400 | { |
385 | { |
401 | int j = k << 1; |
386 | int j = k << 1; |
402 | |
387 | |
403 | if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
388 | if (j + 1 < N && heap [j]->at > heap [j + 1]->at) |
404 | ++j; |
389 | ++j; |
405 | |
390 | |
406 | if (w->at <= timers [j]->at) |
391 | if (w->at <= heap [j]->at) |
407 | break; |
392 | break; |
408 | |
393 | |
409 | timers [k] = timers [j]; |
394 | heap [k] = heap [j]; |
410 | timers [k]->active = k + 1; |
395 | heap [k]->active = k + 1; |
411 | k = j; |
396 | k = j; |
412 | } |
397 | } |
413 | |
398 | |
414 | timers [k] = w; |
399 | heap [k] = w; |
415 | timers [k]->active = k + 1; |
400 | heap [k]->active = k + 1; |
416 | } |
401 | } |
417 | |
402 | |
418 | /*****************************************************************************/ |
403 | /*****************************************************************************/ |
419 | |
404 | |
420 | typedef struct |
405 | typedef struct |
… | |
… | |
426 | static ANSIG *signals; |
411 | static ANSIG *signals; |
427 | static int signalmax; |
412 | static int signalmax; |
428 | |
413 | |
429 | static int sigpipe [2]; |
414 | static int sigpipe [2]; |
430 | static sig_atomic_t volatile gotsig; |
415 | static sig_atomic_t volatile gotsig; |
431 | static struct ev_io sigev; |
|
|
432 | |
416 | |
433 | static void |
417 | static void |
434 | signals_init (ANSIG *base, int count) |
418 | signals_init (ANSIG *base, int count) |
435 | { |
419 | { |
436 | while (count--) |
420 | while (count--) |
… | |
… | |
486 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
470 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
487 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
471 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
488 | #endif |
472 | #endif |
489 | |
473 | |
490 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
474 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
491 | ev_io_start (&sigev); |
475 | ev_io_start (EV_A_ &sigev); |
492 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
476 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
493 | } |
477 | } |
494 | |
478 | |
495 | /*****************************************************************************/ |
479 | /*****************************************************************************/ |
496 | |
|
|
497 | static struct ev_idle **idles; |
|
|
498 | static int idlemax, idlecnt; |
|
|
499 | |
|
|
500 | static struct ev_prepare **prepares; |
|
|
501 | static int preparemax, preparecnt; |
|
|
502 | |
|
|
503 | static struct ev_check **checks; |
|
|
504 | static int checkmax, checkcnt; |
|
|
505 | |
|
|
506 | /*****************************************************************************/ |
|
|
507 | |
|
|
508 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
509 | static struct ev_signal childev; |
|
|
510 | |
480 | |
511 | #ifndef WIN32 |
481 | #ifndef WIN32 |
512 | |
482 | |
513 | #ifndef WCONTINUED |
483 | #ifndef WCONTINUED |
514 | # define WCONTINUED 0 |
484 | # define WCONTINUED 0 |
… | |
… | |
589 | ev_method (EV_P) |
559 | ev_method (EV_P) |
590 | { |
560 | { |
591 | return method; |
561 | return method; |
592 | } |
562 | } |
593 | |
563 | |
594 | int |
564 | static void |
595 | ev_init (EV_P_ int methods) |
565 | loop_init (EV_P_ int methods) |
596 | { |
566 | { |
597 | if (!method) |
567 | if (!method) |
598 | { |
568 | { |
599 | #if EV_USE_MONOTONIC |
569 | #if EV_USE_MONOTONIC |
600 | { |
570 | { |
… | |
… | |
605 | #endif |
575 | #endif |
606 | |
576 | |
607 | rt_now = ev_time (); |
577 | rt_now = ev_time (); |
608 | mn_now = get_clock (); |
578 | mn_now = get_clock (); |
609 | now_floor = mn_now; |
579 | now_floor = mn_now; |
610 | diff = rt_now - mn_now; |
580 | rtmn_diff = rt_now - mn_now; |
611 | |
|
|
612 | if (pipe (sigpipe)) |
|
|
613 | return 0; |
|
|
614 | |
581 | |
615 | if (methods == EVMETHOD_AUTO) |
582 | if (methods == EVMETHOD_AUTO) |
616 | if (!enable_secure () && getenv ("LIBmethodS")) |
583 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
617 | methods = atoi (getenv ("LIBmethodS")); |
584 | methods = atoi (getenv ("LIBEV_METHODS")); |
618 | else |
585 | else |
619 | methods = EVMETHOD_ANY; |
586 | methods = EVMETHOD_ANY; |
620 | |
587 | |
621 | method = 0; |
588 | method = 0; |
622 | #if EV_USE_KQUEUE |
589 | #if EV_USE_KQUEUE |
… | |
… | |
629 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
596 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
630 | #endif |
597 | #endif |
631 | #if EV_USE_SELECT |
598 | #if EV_USE_SELECT |
632 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
599 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
633 | #endif |
600 | #endif |
|
|
601 | } |
|
|
602 | } |
634 | |
603 | |
|
|
604 | void |
|
|
605 | loop_destroy (EV_P) |
|
|
606 | { |
|
|
607 | #if EV_USE_KQUEUE |
|
|
608 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
|
|
609 | #endif |
|
|
610 | #if EV_USE_EPOLL |
|
|
611 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
|
|
612 | #endif |
|
|
613 | #if EV_USEV_POLL |
|
|
614 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
|
|
615 | #endif |
|
|
616 | #if EV_USE_SELECT |
|
|
617 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
|
|
618 | #endif |
|
|
619 | |
|
|
620 | method = 0; |
|
|
621 | /*TODO*/ |
|
|
622 | } |
|
|
623 | |
|
|
624 | void |
|
|
625 | loop_fork (EV_P) |
|
|
626 | { |
|
|
627 | /*TODO*/ |
|
|
628 | #if EV_USE_EPOLL |
|
|
629 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
|
|
630 | #endif |
|
|
631 | #if EV_USE_KQUEUE |
|
|
632 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
|
|
633 | #endif |
|
|
634 | } |
|
|
635 | |
|
|
636 | #if EV_MULTIPLICITY |
|
|
637 | struct ev_loop * |
|
|
638 | ev_loop_new (int methods) |
|
|
639 | { |
|
|
640 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
|
|
641 | |
|
|
642 | loop_init (EV_A_ methods); |
|
|
643 | |
|
|
644 | if (ev_methods (EV_A)) |
|
|
645 | return loop; |
|
|
646 | |
|
|
647 | return 0; |
|
|
648 | } |
|
|
649 | |
|
|
650 | void |
|
|
651 | ev_loop_destroy (EV_P) |
|
|
652 | { |
|
|
653 | loop_destroy (EV_A); |
|
|
654 | free (loop); |
|
|
655 | } |
|
|
656 | |
|
|
657 | void |
|
|
658 | ev_loop_fork (EV_P) |
|
|
659 | { |
|
|
660 | loop_fork (EV_A); |
|
|
661 | } |
|
|
662 | |
|
|
663 | #endif |
|
|
664 | |
|
|
665 | #if EV_MULTIPLICITY |
|
|
666 | struct ev_loop default_loop_struct; |
|
|
667 | static struct ev_loop *default_loop; |
|
|
668 | |
|
|
669 | struct ev_loop * |
|
|
670 | #else |
|
|
671 | static int default_loop; |
|
|
672 | |
|
|
673 | int |
|
|
674 | #endif |
|
|
675 | ev_default_loop (int methods) |
|
|
676 | { |
|
|
677 | if (sigpipe [0] == sigpipe [1]) |
|
|
678 | if (pipe (sigpipe)) |
|
|
679 | return 0; |
|
|
680 | |
|
|
681 | if (!default_loop) |
|
|
682 | { |
|
|
683 | #if EV_MULTIPLICITY |
|
|
684 | struct ev_loop *loop = default_loop = &default_loop_struct; |
|
|
685 | #else |
|
|
686 | default_loop = 1; |
|
|
687 | #endif |
|
|
688 | |
|
|
689 | loop_init (EV_A_ methods); |
|
|
690 | |
635 | if (method) |
691 | if (ev_method (EV_A)) |
636 | { |
692 | { |
637 | ev_watcher_init (&sigev, sigcb); |
693 | ev_watcher_init (&sigev, sigcb); |
638 | ev_set_priority (&sigev, EV_MAXPRI); |
694 | ev_set_priority (&sigev, EV_MAXPRI); |
639 | siginit (EV_A); |
695 | siginit (EV_A); |
640 | |
696 | |
… | |
… | |
643 | ev_set_priority (&childev, EV_MAXPRI); |
699 | ev_set_priority (&childev, EV_MAXPRI); |
644 | ev_signal_start (EV_A_ &childev); |
700 | ev_signal_start (EV_A_ &childev); |
645 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
701 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
646 | #endif |
702 | #endif |
647 | } |
703 | } |
|
|
704 | else |
|
|
705 | default_loop = 0; |
648 | } |
706 | } |
649 | |
707 | |
650 | return method; |
708 | return default_loop; |
651 | } |
709 | } |
652 | |
710 | |
653 | /*****************************************************************************/ |
|
|
654 | |
|
|
655 | void |
711 | void |
656 | ev_fork_prepare (void) |
712 | ev_default_destroy (void) |
657 | { |
713 | { |
658 | /* nop */ |
714 | #if EV_MULTIPLICITY |
659 | } |
715 | struct ev_loop *loop = default_loop; |
660 | |
|
|
661 | void |
|
|
662 | ev_fork_parent (void) |
|
|
663 | { |
|
|
664 | /* nop */ |
|
|
665 | } |
|
|
666 | |
|
|
667 | void |
|
|
668 | ev_fork_child (void) |
|
|
669 | { |
|
|
670 | #if EV_USE_EPOLL |
|
|
671 | if (method == EVMETHOD_EPOLL) |
|
|
672 | epoll_postfork_child (); |
|
|
673 | #endif |
716 | #endif |
674 | |
717 | |
|
|
718 | ev_ref (EV_A); /* child watcher */ |
|
|
719 | ev_signal_stop (EV_A_ &childev); |
|
|
720 | |
|
|
721 | ev_ref (EV_A); /* signal watcher */ |
675 | ev_io_stop (&sigev); |
722 | ev_io_stop (EV_A_ &sigev); |
|
|
723 | |
|
|
724 | close (sigpipe [0]); sigpipe [0] = 0; |
|
|
725 | close (sigpipe [1]); sigpipe [1] = 0; |
|
|
726 | |
|
|
727 | loop_destroy (EV_A); |
|
|
728 | } |
|
|
729 | |
|
|
730 | void |
|
|
731 | ev_default_fork (EV_P) |
|
|
732 | { |
|
|
733 | loop_fork (EV_A); |
|
|
734 | |
|
|
735 | ev_io_stop (EV_A_ &sigev); |
676 | close (sigpipe [0]); |
736 | close (sigpipe [0]); |
677 | close (sigpipe [1]); |
737 | close (sigpipe [1]); |
678 | pipe (sigpipe); |
738 | pipe (sigpipe); |
|
|
739 | |
|
|
740 | ev_ref (EV_A); /* signal watcher */ |
679 | siginit (); |
741 | siginit (EV_A); |
680 | } |
742 | } |
681 | |
743 | |
682 | /*****************************************************************************/ |
744 | /*****************************************************************************/ |
683 | |
745 | |
684 | static void |
746 | static void |
… | |
… | |
714 | downheap ((WT *)timers, timercnt, 0); |
776 | downheap ((WT *)timers, timercnt, 0); |
715 | } |
777 | } |
716 | else |
778 | else |
717 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
779 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
718 | |
780 | |
719 | event ((W)w, EV_TIMEOUT); |
781 | event (EV_A_ (W)w, EV_TIMEOUT); |
720 | } |
782 | } |
721 | } |
783 | } |
722 | |
784 | |
723 | static void |
785 | static void |
724 | periodics_reify (EV_P) |
786 | periodics_reify (EV_P) |
… | |
… | |
740 | event (EV_A_ (W)w, EV_PERIODIC); |
802 | event (EV_A_ (W)w, EV_PERIODIC); |
741 | } |
803 | } |
742 | } |
804 | } |
743 | |
805 | |
744 | static void |
806 | static void |
745 | periodics_reschedule (EV_P_ ev_tstamp diff) |
807 | periodics_reschedule (EV_P) |
746 | { |
808 | { |
747 | int i; |
809 | int i; |
748 | |
810 | |
749 | /* adjust periodics after time jump */ |
811 | /* adjust periodics after time jump */ |
750 | for (i = 0; i < periodiccnt; ++i) |
812 | for (i = 0; i < periodiccnt; ++i) |
… | |
… | |
771 | { |
833 | { |
772 | mn_now = get_clock (); |
834 | mn_now = get_clock (); |
773 | |
835 | |
774 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
836 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
775 | { |
837 | { |
776 | rt_now = mn_now + diff; |
838 | rt_now = rtmn_diff + mn_now; |
777 | return 0; |
839 | return 0; |
778 | } |
840 | } |
779 | else |
841 | else |
780 | { |
842 | { |
781 | now_floor = mn_now; |
843 | now_floor = mn_now; |
… | |
… | |
792 | #if EV_USE_MONOTONIC |
854 | #if EV_USE_MONOTONIC |
793 | if (expect_true (have_monotonic)) |
855 | if (expect_true (have_monotonic)) |
794 | { |
856 | { |
795 | if (time_update_monotonic (EV_A)) |
857 | if (time_update_monotonic (EV_A)) |
796 | { |
858 | { |
797 | ev_tstamp odiff = diff; |
859 | ev_tstamp odiff = rtmn_diff; |
798 | |
860 | |
799 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
861 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
800 | { |
862 | { |
801 | diff = rt_now - mn_now; |
863 | rtmn_diff = rt_now - mn_now; |
802 | |
864 | |
803 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
865 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
804 | return; /* all is well */ |
866 | return; /* all is well */ |
805 | |
867 | |
806 | rt_now = ev_time (); |
868 | rt_now = ev_time (); |
807 | mn_now = get_clock (); |
869 | mn_now = get_clock (); |
808 | now_floor = mn_now; |
870 | now_floor = mn_now; |
809 | } |
871 | } |
810 | |
872 | |
811 | periodics_reschedule (EV_A_ diff - odiff); |
873 | periodics_reschedule (EV_A); |
812 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
874 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
875 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
813 | } |
876 | } |
814 | } |
877 | } |
815 | else |
878 | else |
816 | #endif |
879 | #endif |
817 | { |
880 | { |
818 | rt_now = ev_time (); |
881 | rt_now = ev_time (); |
819 | |
882 | |
820 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
883 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
821 | { |
884 | { |
822 | periodics_reschedule (EV_A_ rt_now - mn_now); |
885 | periodics_reschedule (EV_A); |
823 | |
886 | |
824 | /* adjust timers. this is easy, as the offset is the same for all */ |
887 | /* adjust timers. this is easy, as the offset is the same for all */ |
825 | for (i = 0; i < timercnt; ++i) |
888 | for (i = 0; i < timercnt; ++i) |
826 | timers [i]->at += diff; |
889 | timers [i]->at += rt_now - mn_now; |
827 | } |
890 | } |
828 | |
891 | |
829 | mn_now = rt_now; |
892 | mn_now = rt_now; |
830 | } |
893 | } |
831 | } |
894 | } |
… | |
… | |
1094 | } |
1157 | } |
1095 | |
1158 | |
1096 | ev_stop (EV_A_ (W)w); |
1159 | ev_stop (EV_A_ (W)w); |
1097 | } |
1160 | } |
1098 | |
1161 | |
|
|
1162 | void |
|
|
1163 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1164 | { |
|
|
1165 | if (ev_is_active (w)) |
|
|
1166 | return; |
|
|
1167 | |
|
|
1168 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1169 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1170 | idles [idlecnt - 1] = w; |
|
|
1171 | } |
|
|
1172 | |
|
|
1173 | void |
|
|
1174 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1175 | { |
|
|
1176 | ev_clear_pending (EV_A_ (W)w); |
|
|
1177 | if (ev_is_active (w)) |
|
|
1178 | return; |
|
|
1179 | |
|
|
1180 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
1181 | ev_stop (EV_A_ (W)w); |
|
|
1182 | } |
|
|
1183 | |
|
|
1184 | void |
|
|
1185 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1186 | { |
|
|
1187 | if (ev_is_active (w)) |
|
|
1188 | return; |
|
|
1189 | |
|
|
1190 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1191 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1192 | prepares [preparecnt - 1] = w; |
|
|
1193 | } |
|
|
1194 | |
|
|
1195 | void |
|
|
1196 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1197 | { |
|
|
1198 | ev_clear_pending (EV_A_ (W)w); |
|
|
1199 | if (ev_is_active (w)) |
|
|
1200 | return; |
|
|
1201 | |
|
|
1202 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1203 | ev_stop (EV_A_ (W)w); |
|
|
1204 | } |
|
|
1205 | |
|
|
1206 | void |
|
|
1207 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1208 | { |
|
|
1209 | if (ev_is_active (w)) |
|
|
1210 | return; |
|
|
1211 | |
|
|
1212 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1213 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1214 | checks [checkcnt - 1] = w; |
|
|
1215 | } |
|
|
1216 | |
|
|
1217 | void |
|
|
1218 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1219 | { |
|
|
1220 | ev_clear_pending (EV_A_ (W)w); |
|
|
1221 | if (ev_is_active (w)) |
|
|
1222 | return; |
|
|
1223 | |
|
|
1224 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1225 | ev_stop (EV_A_ (W)w); |
|
|
1226 | } |
|
|
1227 | |
1099 | #ifndef SA_RESTART |
1228 | #ifndef SA_RESTART |
1100 | # define SA_RESTART 0 |
1229 | # define SA_RESTART 0 |
1101 | #endif |
1230 | #endif |
1102 | |
1231 | |
1103 | void |
1232 | void |
1104 | ev_signal_start (EV_P_ struct ev_signal *w) |
1233 | ev_signal_start (EV_P_ struct ev_signal *w) |
1105 | { |
1234 | { |
|
|
1235 | #if EV_MULTIPLICITY |
|
|
1236 | assert (("signal watchers are only supported in the default loop", loop == default_loop)); |
|
|
1237 | #endif |
1106 | if (ev_is_active (w)) |
1238 | if (ev_is_active (w)) |
1107 | return; |
1239 | return; |
1108 | |
1240 | |
1109 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1241 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1110 | |
1242 | |
… | |
… | |
1135 | if (!signals [w->signum - 1].head) |
1267 | if (!signals [w->signum - 1].head) |
1136 | signal (w->signum, SIG_DFL); |
1268 | signal (w->signum, SIG_DFL); |
1137 | } |
1269 | } |
1138 | |
1270 | |
1139 | void |
1271 | void |
1140 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1141 | { |
|
|
1142 | if (ev_is_active (w)) |
|
|
1143 | return; |
|
|
1144 | |
|
|
1145 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1146 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1147 | idles [idlecnt - 1] = w; |
|
|
1148 | } |
|
|
1149 | |
|
|
1150 | void |
|
|
1151 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1152 | { |
|
|
1153 | ev_clear_pending (EV_A_ (W)w); |
|
|
1154 | if (ev_is_active (w)) |
|
|
1155 | return; |
|
|
1156 | |
|
|
1157 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
1158 | ev_stop (EV_A_ (W)w); |
|
|
1159 | } |
|
|
1160 | |
|
|
1161 | void |
|
|
1162 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1163 | { |
|
|
1164 | if (ev_is_active (w)) |
|
|
1165 | return; |
|
|
1166 | |
|
|
1167 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1168 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1169 | prepares [preparecnt - 1] = w; |
|
|
1170 | } |
|
|
1171 | |
|
|
1172 | void |
|
|
1173 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1174 | { |
|
|
1175 | ev_clear_pending (EV_A_ (W)w); |
|
|
1176 | if (ev_is_active (w)) |
|
|
1177 | return; |
|
|
1178 | |
|
|
1179 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1180 | ev_stop (EV_A_ (W)w); |
|
|
1181 | } |
|
|
1182 | |
|
|
1183 | void |
|
|
1184 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1185 | { |
|
|
1186 | if (ev_is_active (w)) |
|
|
1187 | return; |
|
|
1188 | |
|
|
1189 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1190 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1191 | checks [checkcnt - 1] = w; |
|
|
1192 | } |
|
|
1193 | |
|
|
1194 | void |
|
|
1195 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1196 | { |
|
|
1197 | ev_clear_pending (EV_A_ (W)w); |
|
|
1198 | if (ev_is_active (w)) |
|
|
1199 | return; |
|
|
1200 | |
|
|
1201 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1202 | ev_stop (EV_A_ (W)w); |
|
|
1203 | } |
|
|
1204 | |
|
|
1205 | void |
|
|
1206 | ev_child_start (EV_P_ struct ev_child *w) |
1272 | ev_child_start (EV_P_ struct ev_child *w) |
1207 | { |
1273 | { |
|
|
1274 | #if EV_MULTIPLICITY |
|
|
1275 | assert (("child watchers are only supported in the default loop", loop == default_loop)); |
|
|
1276 | #endif |
1208 | if (ev_is_active (w)) |
1277 | if (ev_is_active (w)) |
1209 | return; |
1278 | return; |
1210 | |
1279 | |
1211 | ev_start (EV_A_ (W)w, 1); |
1280 | ev_start (EV_A_ (W)w, 1); |
1212 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1281 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
… | |
… | |
1284 | ev_timer_start (EV_A_ &once->to); |
1353 | ev_timer_start (EV_A_ &once->to); |
1285 | } |
1354 | } |
1286 | } |
1355 | } |
1287 | } |
1356 | } |
1288 | |
1357 | |
1289 | /*****************************************************************************/ |
|
|
1290 | |
|
|
1291 | #if 0 |
|
|
1292 | |
|
|
1293 | struct ev_io wio; |
|
|
1294 | |
|
|
1295 | static void |
|
|
1296 | sin_cb (struct ev_io *w, int revents) |
|
|
1297 | { |
|
|
1298 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
|
|
1299 | } |
|
|
1300 | |
|
|
1301 | static void |
|
|
1302 | ocb (struct ev_timer *w, int revents) |
|
|
1303 | { |
|
|
1304 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
|
|
1305 | ev_timer_stop (w); |
|
|
1306 | ev_timer_start (w); |
|
|
1307 | } |
|
|
1308 | |
|
|
1309 | static void |
|
|
1310 | scb (struct ev_signal *w, int revents) |
|
|
1311 | { |
|
|
1312 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
1313 | ev_io_stop (&wio); |
|
|
1314 | ev_io_start (&wio); |
|
|
1315 | } |
|
|
1316 | |
|
|
1317 | static void |
|
|
1318 | gcb (struct ev_signal *w, int revents) |
|
|
1319 | { |
|
|
1320 | fprintf (stderr, "generic %x\n", revents); |
|
|
1321 | |
|
|
1322 | } |
|
|
1323 | |
|
|
1324 | int main (void) |
|
|
1325 | { |
|
|
1326 | ev_init (0); |
|
|
1327 | |
|
|
1328 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
|
|
1329 | ev_io_start (&wio); |
|
|
1330 | |
|
|
1331 | struct ev_timer t[10000]; |
|
|
1332 | |
|
|
1333 | #if 0 |
|
|
1334 | int i; |
|
|
1335 | for (i = 0; i < 10000; ++i) |
|
|
1336 | { |
|
|
1337 | struct ev_timer *w = t + i; |
|
|
1338 | ev_watcher_init (w, ocb, i); |
|
|
1339 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
|
|
1340 | ev_timer_start (w); |
|
|
1341 | if (drand48 () < 0.5) |
|
|
1342 | ev_timer_stop (w); |
|
|
1343 | } |
|
|
1344 | #endif |
|
|
1345 | |
|
|
1346 | struct ev_timer t1; |
|
|
1347 | ev_timer_init (&t1, ocb, 5, 10); |
|
|
1348 | ev_timer_start (&t1); |
|
|
1349 | |
|
|
1350 | struct ev_signal sig; |
|
|
1351 | ev_signal_init (&sig, scb, SIGQUIT); |
|
|
1352 | ev_signal_start (&sig); |
|
|
1353 | |
|
|
1354 | struct ev_check cw; |
|
|
1355 | ev_check_init (&cw, gcb); |
|
|
1356 | ev_check_start (&cw); |
|
|
1357 | |
|
|
1358 | struct ev_idle iw; |
|
|
1359 | ev_idle_init (&iw, gcb); |
|
|
1360 | ev_idle_start (&iw); |
|
|
1361 | |
|
|
1362 | ev_loop (0); |
|
|
1363 | |
|
|
1364 | return 0; |
|
|
1365 | } |
|
|
1366 | |
|
|
1367 | #endif |
|
|
1368 | |
|
|
1369 | |
|
|
1370 | |
|
|
1371 | |
|
|