… | |
… | |
58 | |
58 | |
59 | #ifndef EV_USE_SELECT |
59 | #ifndef EV_USE_SELECT |
60 | # define EV_USE_SELECT 1 |
60 | # define EV_USE_SELECT 1 |
61 | #endif |
61 | #endif |
62 | |
62 | |
63 | #ifndef EV_USEV_POLL |
63 | #ifndef EV_USE_POLL |
64 | # define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ |
64 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
65 | #endif |
65 | #endif |
66 | |
66 | |
67 | #ifndef EV_USE_EPOLL |
67 | #ifndef EV_USE_EPOLL |
68 | # define EV_USE_EPOLL 0 |
68 | # define EV_USE_EPOLL 0 |
69 | #endif |
69 | #endif |
… | |
… | |
113 | |
113 | |
114 | typedef struct ev_watcher *W; |
114 | typedef struct ev_watcher *W; |
115 | typedef struct ev_watcher_list *WL; |
115 | typedef struct ev_watcher_list *WL; |
116 | typedef struct ev_watcher_time *WT; |
116 | typedef struct ev_watcher_time *WT; |
117 | |
117 | |
118 | static ev_tstamp now_floor, mn_now, diff; /* monotonic clock */ |
118 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
119 | static ev_tstamp rt_now; |
|
|
120 | static int method; |
|
|
121 | |
119 | |
122 | static int have_monotonic; /* runtime */ |
120 | /*****************************************************************************/ |
123 | |
121 | |
124 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
122 | typedef struct |
125 | static void (*method_modify)(EV_P_ int fd, int oev, int nev); |
123 | { |
126 | static void (*method_poll)(EV_P_ ev_tstamp timeout); |
124 | struct ev_watcher_list *head; |
|
|
125 | unsigned char events; |
|
|
126 | unsigned char reify; |
|
|
127 | } ANFD; |
127 | |
128 | |
128 | static int activecnt; /* number of active events */ |
129 | typedef struct |
|
|
130 | { |
|
|
131 | W w; |
|
|
132 | int events; |
|
|
133 | } ANPENDING; |
129 | |
134 | |
130 | #if EV_USE_SELECT |
135 | #if EV_MULTIPLICITY |
131 | static unsigned char *vec_ri, *vec_ro, *vec_wi, *vec_wo; |
|
|
132 | static int vec_max; |
|
|
133 | #endif |
|
|
134 | |
136 | |
135 | #if EV_USEV_POLL |
137 | struct ev_loop |
136 | static struct pollfd *polls; |
138 | { |
137 | static int pollmax, pollcnt; |
139 | # define VAR(name,decl) decl; |
138 | static int *pollidxs; /* maps fds into structure indices */ |
140 | # include "ev_vars.h" |
139 | static int pollidxmax; |
141 | }; |
140 | #endif |
142 | # undef VAR |
|
|
143 | # include "ev_wrap.h" |
141 | |
144 | |
142 | #if EV_USE_EPOLL |
145 | #else |
143 | static int epoll_fd = -1; |
|
|
144 | |
146 | |
145 | static struct epoll_event *events; |
147 | # define VAR(name,decl) static decl; |
146 | static int eventmax; |
148 | # include "ev_vars.h" |
147 | #endif |
149 | # undef VAR |
148 | |
150 | |
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 |
151 | #endif |
156 | |
152 | |
157 | /*****************************************************************************/ |
153 | /*****************************************************************************/ |
158 | |
154 | |
159 | inline ev_tstamp |
155 | inline ev_tstamp |
… | |
… | |
208 | cur = newcnt; \ |
204 | cur = newcnt; \ |
209 | } |
205 | } |
210 | |
206 | |
211 | /*****************************************************************************/ |
207 | /*****************************************************************************/ |
212 | |
208 | |
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 |
209 | static void |
224 | anfds_init (ANFD *base, int count) |
210 | anfds_init (ANFD *base, int count) |
225 | { |
211 | { |
226 | while (count--) |
212 | while (count--) |
227 | { |
213 | { |
… | |
… | |
230 | base->reify = 0; |
216 | base->reify = 0; |
231 | |
217 | |
232 | ++base; |
218 | ++base; |
233 | } |
219 | } |
234 | } |
220 | } |
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 | |
221 | |
245 | static void |
222 | static void |
246 | event (EV_P_ W w, int events) |
223 | event (EV_P_ W w, int events) |
247 | { |
224 | { |
248 | if (w->pending) |
225 | if (w->pending) |
… | |
… | |
280 | event (EV_A_ (W)w, ev); |
257 | event (EV_A_ (W)w, ev); |
281 | } |
258 | } |
282 | } |
259 | } |
283 | |
260 | |
284 | /*****************************************************************************/ |
261 | /*****************************************************************************/ |
285 | |
|
|
286 | static int *fdchanges; |
|
|
287 | static int fdchangemax, fdchangecnt; |
|
|
288 | |
262 | |
289 | static void |
263 | static void |
290 | fd_reify (EV_P) |
264 | fd_reify (EV_P) |
291 | { |
265 | { |
292 | int i; |
266 | int i; |
… | |
… | |
364 | fd_kill (EV_A_ fd); |
338 | fd_kill (EV_A_ fd); |
365 | return; |
339 | return; |
366 | } |
340 | } |
367 | } |
341 | } |
368 | |
342 | |
|
|
343 | /* susually called after fork if method needs to re-arm all fds from scratch */ |
|
|
344 | static void |
|
|
345 | fd_rearm_all (EV_P) |
|
|
346 | { |
|
|
347 | int fd; |
|
|
348 | |
|
|
349 | /* this should be highly optimised to not do anything but set a flag */ |
|
|
350 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
351 | if (anfds [fd].events) |
|
|
352 | { |
|
|
353 | anfds [fd].events = 0; |
|
|
354 | fd_change (fd); |
|
|
355 | } |
|
|
356 | } |
|
|
357 | |
369 | /*****************************************************************************/ |
358 | /*****************************************************************************/ |
370 | |
359 | |
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 |
360 | static void |
378 | upheap (WT *timers, int k) |
361 | upheap (WT *heap, int k) |
379 | { |
362 | { |
380 | WT w = timers [k]; |
363 | WT w = heap [k]; |
381 | |
364 | |
382 | while (k && timers [k >> 1]->at > w->at) |
365 | while (k && heap [k >> 1]->at > w->at) |
383 | { |
366 | { |
384 | timers [k] = timers [k >> 1]; |
367 | heap [k] = heap [k >> 1]; |
385 | timers [k]->active = k + 1; |
368 | heap [k]->active = k + 1; |
386 | k >>= 1; |
369 | k >>= 1; |
387 | } |
370 | } |
388 | |
371 | |
389 | timers [k] = w; |
372 | heap [k] = w; |
390 | timers [k]->active = k + 1; |
373 | heap [k]->active = k + 1; |
391 | |
374 | |
392 | } |
375 | } |
393 | |
376 | |
394 | static void |
377 | static void |
395 | downheap (WT *timers, int N, int k) |
378 | downheap (WT *heap, int N, int k) |
396 | { |
379 | { |
397 | WT w = timers [k]; |
380 | WT w = heap [k]; |
398 | |
381 | |
399 | while (k < (N >> 1)) |
382 | while (k < (N >> 1)) |
400 | { |
383 | { |
401 | int j = k << 1; |
384 | int j = k << 1; |
402 | |
385 | |
403 | if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
386 | if (j + 1 < N && heap [j]->at > heap [j + 1]->at) |
404 | ++j; |
387 | ++j; |
405 | |
388 | |
406 | if (w->at <= timers [j]->at) |
389 | if (w->at <= heap [j]->at) |
407 | break; |
390 | break; |
408 | |
391 | |
409 | timers [k] = timers [j]; |
392 | heap [k] = heap [j]; |
410 | timers [k]->active = k + 1; |
393 | heap [k]->active = k + 1; |
411 | k = j; |
394 | k = j; |
412 | } |
395 | } |
413 | |
396 | |
414 | timers [k] = w; |
397 | heap [k] = w; |
415 | timers [k]->active = k + 1; |
398 | heap [k]->active = k + 1; |
416 | } |
399 | } |
417 | |
400 | |
418 | /*****************************************************************************/ |
401 | /*****************************************************************************/ |
419 | |
402 | |
420 | typedef struct |
403 | typedef struct |
… | |
… | |
486 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
469 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
487 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
470 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
488 | #endif |
471 | #endif |
489 | |
472 | |
490 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
473 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
491 | ev_io_start (&sigev); |
474 | ev_io_start (EV_A_ &sigev); |
492 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
475 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
493 | } |
476 | } |
494 | |
477 | |
495 | /*****************************************************************************/ |
478 | /*****************************************************************************/ |
496 | |
479 | |
497 | static struct ev_idle **idles; |
480 | #ifndef WIN32 |
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 | |
481 | |
508 | static struct ev_child *childs [PID_HASHSIZE]; |
482 | static struct ev_child *childs [PID_HASHSIZE]; |
509 | static struct ev_signal childev; |
483 | static struct ev_signal childev; |
510 | |
|
|
511 | #ifndef WIN32 |
|
|
512 | |
484 | |
513 | #ifndef WCONTINUED |
485 | #ifndef WCONTINUED |
514 | # define WCONTINUED 0 |
486 | # define WCONTINUED 0 |
515 | #endif |
487 | #endif |
516 | |
488 | |
… | |
… | |
552 | # include "ev_kqueue.c" |
524 | # include "ev_kqueue.c" |
553 | #endif |
525 | #endif |
554 | #if EV_USE_EPOLL |
526 | #if EV_USE_EPOLL |
555 | # include "ev_epoll.c" |
527 | # include "ev_epoll.c" |
556 | #endif |
528 | #endif |
557 | #if EV_USEV_POLL |
529 | #if EV_USE_POLL |
558 | # include "ev_poll.c" |
530 | # include "ev_poll.c" |
559 | #endif |
531 | #endif |
560 | #if EV_USE_SELECT |
532 | #if EV_USE_SELECT |
561 | # include "ev_select.c" |
533 | # include "ev_select.c" |
562 | #endif |
534 | #endif |
… | |
… | |
589 | ev_method (EV_P) |
561 | ev_method (EV_P) |
590 | { |
562 | { |
591 | return method; |
563 | return method; |
592 | } |
564 | } |
593 | |
565 | |
594 | int |
566 | static void |
595 | ev_init (EV_P_ int methods) |
567 | loop_init (EV_P_ int methods) |
596 | { |
568 | { |
597 | if (!method) |
569 | if (!method) |
598 | { |
570 | { |
599 | #if EV_USE_MONOTONIC |
571 | #if EV_USE_MONOTONIC |
600 | { |
572 | { |
… | |
… | |
605 | #endif |
577 | #endif |
606 | |
578 | |
607 | rt_now = ev_time (); |
579 | rt_now = ev_time (); |
608 | mn_now = get_clock (); |
580 | mn_now = get_clock (); |
609 | now_floor = mn_now; |
581 | now_floor = mn_now; |
610 | diff = rt_now - mn_now; |
582 | rtmn_diff = rt_now - mn_now; |
611 | |
|
|
612 | if (pipe (sigpipe)) |
|
|
613 | return 0; |
|
|
614 | |
583 | |
615 | if (methods == EVMETHOD_AUTO) |
584 | if (methods == EVMETHOD_AUTO) |
616 | if (!enable_secure () && getenv ("LIBmethodS")) |
585 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
617 | methods = atoi (getenv ("LIBmethodS")); |
586 | methods = atoi (getenv ("LIBEV_METHODS")); |
618 | else |
587 | else |
619 | methods = EVMETHOD_ANY; |
588 | methods = EVMETHOD_ANY; |
620 | |
589 | |
621 | method = 0; |
590 | method = 0; |
622 | #if EV_USE_KQUEUE |
591 | #if EV_USE_KQUEUE |
623 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
592 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
624 | #endif |
593 | #endif |
625 | #if EV_USE_EPOLL |
594 | #if EV_USE_EPOLL |
626 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
595 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
627 | #endif |
596 | #endif |
628 | #if EV_USEV_POLL |
597 | #if EV_USE_POLL |
629 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
598 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
630 | #endif |
599 | #endif |
631 | #if EV_USE_SELECT |
600 | #if EV_USE_SELECT |
632 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
601 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
633 | #endif |
602 | #endif |
|
|
603 | } |
|
|
604 | } |
634 | |
605 | |
|
|
606 | void |
|
|
607 | loop_destroy (EV_P) |
|
|
608 | { |
|
|
609 | #if EV_USE_KQUEUE |
|
|
610 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
|
|
611 | #endif |
|
|
612 | #if EV_USE_EPOLL |
|
|
613 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
|
|
614 | #endif |
|
|
615 | #if EV_USE_POLL |
|
|
616 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
|
|
617 | #endif |
|
|
618 | #if EV_USE_SELECT |
|
|
619 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
|
|
620 | #endif |
|
|
621 | |
|
|
622 | method = 0; |
|
|
623 | /*TODO*/ |
|
|
624 | } |
|
|
625 | |
|
|
626 | void |
|
|
627 | loop_fork (EV_P) |
|
|
628 | { |
|
|
629 | /*TODO*/ |
|
|
630 | #if EV_USE_EPOLL |
|
|
631 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
|
|
632 | #endif |
|
|
633 | #if EV_USE_KQUEUE |
|
|
634 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
|
|
635 | #endif |
|
|
636 | } |
|
|
637 | |
|
|
638 | #if EV_MULTIPLICITY |
|
|
639 | struct ev_loop * |
|
|
640 | ev_loop_new (int methods) |
|
|
641 | { |
|
|
642 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
|
|
643 | |
|
|
644 | loop_init (EV_A_ methods); |
|
|
645 | |
|
|
646 | if (ev_methods (EV_A)) |
|
|
647 | return loop; |
|
|
648 | |
|
|
649 | return 0; |
|
|
650 | } |
|
|
651 | |
|
|
652 | void |
|
|
653 | ev_loop_destroy (EV_P) |
|
|
654 | { |
|
|
655 | loop_destroy (EV_A); |
|
|
656 | free (loop); |
|
|
657 | } |
|
|
658 | |
|
|
659 | void |
|
|
660 | ev_loop_fork (EV_P) |
|
|
661 | { |
|
|
662 | loop_fork (EV_A); |
|
|
663 | } |
|
|
664 | |
|
|
665 | #endif |
|
|
666 | |
|
|
667 | #if EV_MULTIPLICITY |
|
|
668 | struct ev_loop default_loop_struct; |
|
|
669 | static struct ev_loop *default_loop; |
|
|
670 | |
|
|
671 | struct ev_loop * |
|
|
672 | #else |
|
|
673 | static int default_loop; |
|
|
674 | |
|
|
675 | int |
|
|
676 | #endif |
|
|
677 | ev_default_loop (int methods) |
|
|
678 | { |
|
|
679 | if (sigpipe [0] == sigpipe [1]) |
|
|
680 | if (pipe (sigpipe)) |
|
|
681 | return 0; |
|
|
682 | |
|
|
683 | if (!default_loop) |
|
|
684 | { |
|
|
685 | #if EV_MULTIPLICITY |
|
|
686 | struct ev_loop *loop = default_loop = &default_loop_struct; |
|
|
687 | #else |
|
|
688 | default_loop = 1; |
|
|
689 | #endif |
|
|
690 | |
|
|
691 | loop_init (EV_A_ methods); |
|
|
692 | |
635 | if (method) |
693 | if (ev_method (EV_A)) |
636 | { |
694 | { |
637 | ev_watcher_init (&sigev, sigcb); |
695 | ev_watcher_init (&sigev, sigcb); |
638 | ev_set_priority (&sigev, EV_MAXPRI); |
696 | ev_set_priority (&sigev, EV_MAXPRI); |
639 | siginit (EV_A); |
697 | siginit (EV_A); |
640 | |
698 | |
… | |
… | |
643 | ev_set_priority (&childev, EV_MAXPRI); |
701 | ev_set_priority (&childev, EV_MAXPRI); |
644 | ev_signal_start (EV_A_ &childev); |
702 | ev_signal_start (EV_A_ &childev); |
645 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
703 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
646 | #endif |
704 | #endif |
647 | } |
705 | } |
|
|
706 | else |
|
|
707 | default_loop = 0; |
648 | } |
708 | } |
649 | |
709 | |
650 | return method; |
710 | return default_loop; |
651 | } |
711 | } |
652 | |
712 | |
653 | /*****************************************************************************/ |
|
|
654 | |
|
|
655 | void |
713 | void |
656 | ev_fork_prepare (void) |
714 | ev_default_destroy (void) |
657 | { |
715 | { |
658 | /* nop */ |
716 | #if EV_MULTIPLICITY |
659 | } |
717 | 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 |
718 | #endif |
674 | |
719 | |
|
|
720 | ev_ref (EV_A); /* child watcher */ |
|
|
721 | ev_signal_stop (EV_A_ &childev); |
|
|
722 | |
|
|
723 | ev_ref (EV_A); /* signal watcher */ |
675 | ev_io_stop (&sigev); |
724 | ev_io_stop (EV_A_ &sigev); |
|
|
725 | |
|
|
726 | close (sigpipe [0]); sigpipe [0] = 0; |
|
|
727 | close (sigpipe [1]); sigpipe [1] = 0; |
|
|
728 | |
|
|
729 | loop_destroy (EV_A); |
|
|
730 | } |
|
|
731 | |
|
|
732 | void |
|
|
733 | ev_default_fork (EV_P) |
|
|
734 | { |
|
|
735 | loop_fork (EV_A); |
|
|
736 | |
|
|
737 | ev_io_stop (EV_A_ &sigev); |
676 | close (sigpipe [0]); |
738 | close (sigpipe [0]); |
677 | close (sigpipe [1]); |
739 | close (sigpipe [1]); |
678 | pipe (sigpipe); |
740 | pipe (sigpipe); |
|
|
741 | |
|
|
742 | ev_ref (EV_A); /* signal watcher */ |
679 | siginit (); |
743 | siginit (EV_A); |
680 | } |
744 | } |
681 | |
745 | |
682 | /*****************************************************************************/ |
746 | /*****************************************************************************/ |
683 | |
747 | |
684 | static void |
748 | static void |
… | |
… | |
714 | downheap ((WT *)timers, timercnt, 0); |
778 | downheap ((WT *)timers, timercnt, 0); |
715 | } |
779 | } |
716 | else |
780 | else |
717 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
781 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
718 | |
782 | |
719 | event ((W)w, EV_TIMEOUT); |
783 | event (EV_A_ (W)w, EV_TIMEOUT); |
720 | } |
784 | } |
721 | } |
785 | } |
722 | |
786 | |
723 | static void |
787 | static void |
724 | periodics_reify (EV_P) |
788 | periodics_reify (EV_P) |
… | |
… | |
740 | event (EV_A_ (W)w, EV_PERIODIC); |
804 | event (EV_A_ (W)w, EV_PERIODIC); |
741 | } |
805 | } |
742 | } |
806 | } |
743 | |
807 | |
744 | static void |
808 | static void |
745 | periodics_reschedule (EV_P_ ev_tstamp diff) |
809 | periodics_reschedule (EV_P) |
746 | { |
810 | { |
747 | int i; |
811 | int i; |
748 | |
812 | |
749 | /* adjust periodics after time jump */ |
813 | /* adjust periodics after time jump */ |
750 | for (i = 0; i < periodiccnt; ++i) |
814 | for (i = 0; i < periodiccnt; ++i) |
… | |
… | |
771 | { |
835 | { |
772 | mn_now = get_clock (); |
836 | mn_now = get_clock (); |
773 | |
837 | |
774 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
838 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
775 | { |
839 | { |
776 | rt_now = mn_now + diff; |
840 | rt_now = rtmn_diff + mn_now; |
777 | return 0; |
841 | return 0; |
778 | } |
842 | } |
779 | else |
843 | else |
780 | { |
844 | { |
781 | now_floor = mn_now; |
845 | now_floor = mn_now; |
… | |
… | |
792 | #if EV_USE_MONOTONIC |
856 | #if EV_USE_MONOTONIC |
793 | if (expect_true (have_monotonic)) |
857 | if (expect_true (have_monotonic)) |
794 | { |
858 | { |
795 | if (time_update_monotonic (EV_A)) |
859 | if (time_update_monotonic (EV_A)) |
796 | { |
860 | { |
797 | ev_tstamp odiff = diff; |
861 | ev_tstamp odiff = rtmn_diff; |
798 | |
862 | |
799 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
863 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
800 | { |
864 | { |
801 | diff = rt_now - mn_now; |
865 | rtmn_diff = rt_now - mn_now; |
802 | |
866 | |
803 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
867 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
804 | return; /* all is well */ |
868 | return; /* all is well */ |
805 | |
869 | |
806 | rt_now = ev_time (); |
870 | rt_now = ev_time (); |
807 | mn_now = get_clock (); |
871 | mn_now = get_clock (); |
808 | now_floor = mn_now; |
872 | now_floor = mn_now; |
809 | } |
873 | } |
810 | |
874 | |
811 | periodics_reschedule (EV_A_ diff - odiff); |
875 | periodics_reschedule (EV_A); |
812 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
876 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
877 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
813 | } |
878 | } |
814 | } |
879 | } |
815 | else |
880 | else |
816 | #endif |
881 | #endif |
817 | { |
882 | { |
818 | rt_now = ev_time (); |
883 | rt_now = ev_time (); |
819 | |
884 | |
820 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
885 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
821 | { |
886 | { |
822 | periodics_reschedule (EV_A_ rt_now - mn_now); |
887 | periodics_reschedule (EV_A); |
823 | |
888 | |
824 | /* adjust timers. this is easy, as the offset is the same for all */ |
889 | /* adjust timers. this is easy, as the offset is the same for all */ |
825 | for (i = 0; i < timercnt; ++i) |
890 | for (i = 0; i < timercnt; ++i) |
826 | timers [i]->at += diff; |
891 | timers [i]->at += rt_now - mn_now; |
827 | } |
892 | } |
828 | |
893 | |
829 | mn_now = rt_now; |
894 | mn_now = rt_now; |
830 | } |
895 | } |
831 | } |
896 | } |
… | |
… | |
1094 | } |
1159 | } |
1095 | |
1160 | |
1096 | ev_stop (EV_A_ (W)w); |
1161 | ev_stop (EV_A_ (W)w); |
1097 | } |
1162 | } |
1098 | |
1163 | |
|
|
1164 | void |
|
|
1165 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1166 | { |
|
|
1167 | if (ev_is_active (w)) |
|
|
1168 | return; |
|
|
1169 | |
|
|
1170 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1171 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1172 | idles [idlecnt - 1] = w; |
|
|
1173 | } |
|
|
1174 | |
|
|
1175 | void |
|
|
1176 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1177 | { |
|
|
1178 | ev_clear_pending (EV_A_ (W)w); |
|
|
1179 | if (ev_is_active (w)) |
|
|
1180 | return; |
|
|
1181 | |
|
|
1182 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
1183 | ev_stop (EV_A_ (W)w); |
|
|
1184 | } |
|
|
1185 | |
|
|
1186 | void |
|
|
1187 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1188 | { |
|
|
1189 | if (ev_is_active (w)) |
|
|
1190 | return; |
|
|
1191 | |
|
|
1192 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1193 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1194 | prepares [preparecnt - 1] = w; |
|
|
1195 | } |
|
|
1196 | |
|
|
1197 | void |
|
|
1198 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1199 | { |
|
|
1200 | ev_clear_pending (EV_A_ (W)w); |
|
|
1201 | if (ev_is_active (w)) |
|
|
1202 | return; |
|
|
1203 | |
|
|
1204 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1205 | ev_stop (EV_A_ (W)w); |
|
|
1206 | } |
|
|
1207 | |
|
|
1208 | void |
|
|
1209 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1210 | { |
|
|
1211 | if (ev_is_active (w)) |
|
|
1212 | return; |
|
|
1213 | |
|
|
1214 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1215 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1216 | checks [checkcnt - 1] = w; |
|
|
1217 | } |
|
|
1218 | |
|
|
1219 | void |
|
|
1220 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1221 | { |
|
|
1222 | ev_clear_pending (EV_A_ (W)w); |
|
|
1223 | if (ev_is_active (w)) |
|
|
1224 | return; |
|
|
1225 | |
|
|
1226 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1227 | ev_stop (EV_A_ (W)w); |
|
|
1228 | } |
|
|
1229 | |
1099 | #ifndef SA_RESTART |
1230 | #ifndef SA_RESTART |
1100 | # define SA_RESTART 0 |
1231 | # define SA_RESTART 0 |
1101 | #endif |
1232 | #endif |
1102 | |
1233 | |
1103 | void |
1234 | void |
1104 | ev_signal_start (EV_P_ struct ev_signal *w) |
1235 | ev_signal_start (EV_P_ struct ev_signal *w) |
1105 | { |
1236 | { |
|
|
1237 | #if EV_MULTIPLICITY |
|
|
1238 | assert (("signal watchers are only supported in the default loop", loop == default_loop)); |
|
|
1239 | #endif |
1106 | if (ev_is_active (w)) |
1240 | if (ev_is_active (w)) |
1107 | return; |
1241 | return; |
1108 | |
1242 | |
1109 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1243 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1110 | |
1244 | |
… | |
… | |
1135 | if (!signals [w->signum - 1].head) |
1269 | if (!signals [w->signum - 1].head) |
1136 | signal (w->signum, SIG_DFL); |
1270 | signal (w->signum, SIG_DFL); |
1137 | } |
1271 | } |
1138 | |
1272 | |
1139 | void |
1273 | 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) |
1274 | ev_child_start (EV_P_ struct ev_child *w) |
1207 | { |
1275 | { |
|
|
1276 | #if EV_MULTIPLICITY |
|
|
1277 | assert (("child watchers are only supported in the default loop", loop == default_loop)); |
|
|
1278 | #endif |
1208 | if (ev_is_active (w)) |
1279 | if (ev_is_active (w)) |
1209 | return; |
1280 | return; |
1210 | |
1281 | |
1211 | ev_start (EV_A_ (W)w, 1); |
1282 | ev_start (EV_A_ (W)w, 1); |
1212 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1283 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
… | |
… | |
1284 | ev_timer_start (EV_A_ &once->to); |
1355 | ev_timer_start (EV_A_ &once->to); |
1285 | } |
1356 | } |
1286 | } |
1357 | } |
1287 | } |
1358 | } |
1288 | |
1359 | |
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 | |
|
|