… | |
… | |
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 |
… | |
… | |
426 | static ANSIG *signals; |
409 | static ANSIG *signals; |
427 | static int signalmax; |
410 | static int signalmax; |
428 | |
411 | |
429 | static int sigpipe [2]; |
412 | static int sigpipe [2]; |
430 | static sig_atomic_t volatile gotsig; |
413 | static sig_atomic_t volatile gotsig; |
431 | static struct ev_io sigev; |
|
|
432 | |
414 | |
433 | static void |
415 | static void |
434 | signals_init (ANSIG *base, int count) |
416 | signals_init (ANSIG *base, int count) |
435 | { |
417 | { |
436 | while (count--) |
418 | while (count--) |
… | |
… | |
486 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
468 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
487 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
469 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
488 | #endif |
470 | #endif |
489 | |
471 | |
490 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
472 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
491 | ev_io_start (&sigev); |
473 | ev_io_start (EV_A_ &sigev); |
|
|
474 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
492 | } |
475 | } |
493 | |
476 | |
494 | /*****************************************************************************/ |
477 | /*****************************************************************************/ |
495 | |
|
|
496 | static struct ev_idle **idles; |
|
|
497 | static int idlemax, idlecnt; |
|
|
498 | |
|
|
499 | static struct ev_prepare **prepares; |
|
|
500 | static int preparemax, preparecnt; |
|
|
501 | |
|
|
502 | static struct ev_check **checks; |
|
|
503 | static int checkmax, checkcnt; |
|
|
504 | |
|
|
505 | /*****************************************************************************/ |
|
|
506 | |
|
|
507 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
508 | static struct ev_signal childev; |
|
|
509 | |
478 | |
510 | #ifndef WIN32 |
479 | #ifndef WIN32 |
511 | |
480 | |
512 | #ifndef WCONTINUED |
481 | #ifndef WCONTINUED |
513 | # define WCONTINUED 0 |
482 | # define WCONTINUED 0 |
… | |
… | |
588 | ev_method (EV_P) |
557 | ev_method (EV_P) |
589 | { |
558 | { |
590 | return method; |
559 | return method; |
591 | } |
560 | } |
592 | |
561 | |
593 | int |
562 | static void |
594 | ev_init (EV_P_ int methods) |
563 | loop_init (EV_P_ int methods) |
595 | { |
564 | { |
596 | if (!method) |
565 | if (!method) |
597 | { |
566 | { |
598 | #if EV_USE_MONOTONIC |
567 | #if EV_USE_MONOTONIC |
599 | { |
568 | { |
… | |
… | |
604 | #endif |
573 | #endif |
605 | |
574 | |
606 | rt_now = ev_time (); |
575 | rt_now = ev_time (); |
607 | mn_now = get_clock (); |
576 | mn_now = get_clock (); |
608 | now_floor = mn_now; |
577 | now_floor = mn_now; |
609 | diff = rt_now - mn_now; |
578 | rtmn_diff = rt_now - mn_now; |
610 | |
|
|
611 | if (pipe (sigpipe)) |
|
|
612 | return 0; |
|
|
613 | |
579 | |
614 | if (methods == EVMETHOD_AUTO) |
580 | if (methods == EVMETHOD_AUTO) |
615 | if (!enable_secure () && getenv ("LIBmethodS")) |
581 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
616 | methods = atoi (getenv ("LIBmethodS")); |
582 | methods = atoi (getenv ("LIBEV_METHODS")); |
617 | else |
583 | else |
618 | methods = EVMETHOD_ANY; |
584 | methods = EVMETHOD_ANY; |
619 | |
585 | |
620 | method = 0; |
586 | method = 0; |
621 | #if EV_USE_KQUEUE |
587 | #if EV_USE_KQUEUE |
… | |
… | |
628 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
594 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
629 | #endif |
595 | #endif |
630 | #if EV_USE_SELECT |
596 | #if EV_USE_SELECT |
631 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
597 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
632 | #endif |
598 | #endif |
|
|
599 | } |
|
|
600 | } |
633 | |
601 | |
|
|
602 | void |
|
|
603 | loop_destroy (EV_P) |
|
|
604 | { |
|
|
605 | #if EV_USE_KQUEUE |
|
|
606 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
|
|
607 | #endif |
|
|
608 | #if EV_USE_EPOLL |
|
|
609 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
|
|
610 | #endif |
|
|
611 | #if EV_USEV_POLL |
|
|
612 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
|
|
613 | #endif |
|
|
614 | #if EV_USE_SELECT |
|
|
615 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
|
|
616 | #endif |
|
|
617 | |
|
|
618 | method = 0; |
|
|
619 | /*TODO*/ |
|
|
620 | } |
|
|
621 | |
|
|
622 | void |
|
|
623 | loop_fork (EV_P) |
|
|
624 | { |
|
|
625 | /*TODO*/ |
|
|
626 | #if EV_USE_EPOLL |
|
|
627 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
|
|
628 | #endif |
|
|
629 | #if EV_USE_KQUEUE |
|
|
630 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
|
|
631 | #endif |
|
|
632 | } |
|
|
633 | |
|
|
634 | #if EV_MULTIPLICITY |
|
|
635 | struct ev_loop * |
|
|
636 | ev_loop_new (int methods) |
|
|
637 | { |
|
|
638 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
|
|
639 | |
|
|
640 | loop_init (EV_A_ methods); |
|
|
641 | |
|
|
642 | if (ev_methods (EV_A)) |
|
|
643 | return loop; |
|
|
644 | |
|
|
645 | return 0; |
|
|
646 | } |
|
|
647 | |
|
|
648 | void |
|
|
649 | ev_loop_destroy (EV_P) |
|
|
650 | { |
|
|
651 | loop_destroy (EV_A); |
|
|
652 | free (loop); |
|
|
653 | } |
|
|
654 | |
|
|
655 | void |
|
|
656 | ev_loop_fork (EV_P) |
|
|
657 | { |
|
|
658 | loop_fork (EV_A); |
|
|
659 | } |
|
|
660 | |
|
|
661 | #endif |
|
|
662 | |
|
|
663 | #if EV_MULTIPLICITY |
|
|
664 | struct ev_loop default_loop_struct; |
|
|
665 | static struct ev_loop *default_loop; |
|
|
666 | |
|
|
667 | struct ev_loop * |
|
|
668 | #else |
|
|
669 | static int default_loop; |
|
|
670 | |
|
|
671 | int |
|
|
672 | #endif |
|
|
673 | ev_default_loop (int methods) |
|
|
674 | { |
|
|
675 | if (sigpipe [0] == sigpipe [1]) |
|
|
676 | if (pipe (sigpipe)) |
|
|
677 | return 0; |
|
|
678 | |
|
|
679 | if (!default_loop) |
|
|
680 | { |
|
|
681 | #if EV_MULTIPLICITY |
|
|
682 | struct ev_loop *loop = default_loop = &default_loop_struct; |
|
|
683 | #else |
|
|
684 | default_loop = 1; |
|
|
685 | #endif |
|
|
686 | |
|
|
687 | loop_init (EV_A_ methods); |
|
|
688 | |
634 | if (method) |
689 | if (ev_method (EV_A)) |
635 | { |
690 | { |
636 | ev_watcher_init (&sigev, sigcb); |
691 | ev_watcher_init (&sigev, sigcb); |
637 | ev_set_priority (&sigev, EV_MAXPRI); |
692 | ev_set_priority (&sigev, EV_MAXPRI); |
638 | siginit (EV_A); |
693 | siginit (EV_A); |
639 | |
694 | |
640 | #ifndef WIN32 |
695 | #ifndef WIN32 |
641 | ev_signal_init (&childev, childcb, SIGCHLD); |
696 | ev_signal_init (&childev, childcb, SIGCHLD); |
642 | ev_set_priority (&childev, EV_MAXPRI); |
697 | ev_set_priority (&childev, EV_MAXPRI); |
643 | ev_signal_start (EV_A_ &childev); |
698 | ev_signal_start (EV_A_ &childev); |
|
|
699 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
644 | #endif |
700 | #endif |
645 | } |
701 | } |
|
|
702 | else |
|
|
703 | default_loop = 0; |
646 | } |
704 | } |
647 | |
705 | |
648 | return method; |
706 | return default_loop; |
649 | } |
707 | } |
650 | |
708 | |
651 | /*****************************************************************************/ |
|
|
652 | |
|
|
653 | void |
709 | void |
654 | ev_fork_prepare (void) |
710 | ev_default_destroy (void) |
655 | { |
711 | { |
656 | /* nop */ |
712 | #if EV_MULTIPLICITY |
657 | } |
713 | struct ev_loop *loop = default_loop; |
658 | |
|
|
659 | void |
|
|
660 | ev_fork_parent (void) |
|
|
661 | { |
|
|
662 | /* nop */ |
|
|
663 | } |
|
|
664 | |
|
|
665 | void |
|
|
666 | ev_fork_child (void) |
|
|
667 | { |
|
|
668 | #if EV_USE_EPOLL |
|
|
669 | if (method == EVMETHOD_EPOLL) |
|
|
670 | epoll_postfork_child (); |
|
|
671 | #endif |
714 | #endif |
672 | |
715 | |
|
|
716 | ev_ref (EV_A); /* child watcher */ |
|
|
717 | ev_signal_stop (EV_A_ &childev); |
|
|
718 | |
|
|
719 | ev_ref (EV_A); /* signal watcher */ |
673 | ev_io_stop (&sigev); |
720 | ev_io_stop (EV_A_ &sigev); |
|
|
721 | |
|
|
722 | close (sigpipe [0]); sigpipe [0] = 0; |
|
|
723 | close (sigpipe [1]); sigpipe [1] = 0; |
|
|
724 | |
|
|
725 | loop_destroy (EV_A); |
|
|
726 | } |
|
|
727 | |
|
|
728 | void |
|
|
729 | ev_default_fork (EV_P) |
|
|
730 | { |
|
|
731 | loop_fork (EV_A); |
|
|
732 | |
|
|
733 | ev_io_stop (EV_A_ &sigev); |
674 | close (sigpipe [0]); |
734 | close (sigpipe [0]); |
675 | close (sigpipe [1]); |
735 | close (sigpipe [1]); |
676 | pipe (sigpipe); |
736 | pipe (sigpipe); |
|
|
737 | |
|
|
738 | ev_ref (EV_A); /* signal watcher */ |
677 | siginit (); |
739 | siginit (EV_A); |
678 | } |
740 | } |
679 | |
741 | |
680 | /*****************************************************************************/ |
742 | /*****************************************************************************/ |
681 | |
743 | |
682 | static void |
744 | static void |
… | |
… | |
712 | downheap ((WT *)timers, timercnt, 0); |
774 | downheap ((WT *)timers, timercnt, 0); |
713 | } |
775 | } |
714 | else |
776 | else |
715 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
777 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
716 | |
778 | |
717 | event ((W)w, EV_TIMEOUT); |
779 | event (EV_A_ (W)w, EV_TIMEOUT); |
718 | } |
780 | } |
719 | } |
781 | } |
720 | |
782 | |
721 | static void |
783 | static void |
722 | periodics_reify (EV_P) |
784 | periodics_reify (EV_P) |
… | |
… | |
738 | event (EV_A_ (W)w, EV_PERIODIC); |
800 | event (EV_A_ (W)w, EV_PERIODIC); |
739 | } |
801 | } |
740 | } |
802 | } |
741 | |
803 | |
742 | static void |
804 | static void |
743 | periodics_reschedule (EV_P_ ev_tstamp diff) |
805 | periodics_reschedule (EV_P) |
744 | { |
806 | { |
745 | int i; |
807 | int i; |
746 | |
808 | |
747 | /* adjust periodics after time jump */ |
809 | /* adjust periodics after time jump */ |
748 | for (i = 0; i < periodiccnt; ++i) |
810 | for (i = 0; i < periodiccnt; ++i) |
… | |
… | |
769 | { |
831 | { |
770 | mn_now = get_clock (); |
832 | mn_now = get_clock (); |
771 | |
833 | |
772 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
834 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
773 | { |
835 | { |
774 | rt_now = mn_now + diff; |
836 | rt_now = rtmn_diff + mn_now; |
775 | return 0; |
837 | return 0; |
776 | } |
838 | } |
777 | else |
839 | else |
778 | { |
840 | { |
779 | now_floor = mn_now; |
841 | now_floor = mn_now; |
… | |
… | |
790 | #if EV_USE_MONOTONIC |
852 | #if EV_USE_MONOTONIC |
791 | if (expect_true (have_monotonic)) |
853 | if (expect_true (have_monotonic)) |
792 | { |
854 | { |
793 | if (time_update_monotonic (EV_A)) |
855 | if (time_update_monotonic (EV_A)) |
794 | { |
856 | { |
795 | ev_tstamp odiff = diff; |
857 | ev_tstamp odiff = rtmn_diff; |
796 | |
858 | |
797 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
859 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
798 | { |
860 | { |
799 | diff = rt_now - mn_now; |
861 | rtmn_diff = rt_now - mn_now; |
800 | |
862 | |
801 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
863 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
802 | return; /* all is well */ |
864 | return; /* all is well */ |
803 | |
865 | |
804 | rt_now = ev_time (); |
866 | rt_now = ev_time (); |
805 | mn_now = get_clock (); |
867 | mn_now = get_clock (); |
806 | now_floor = mn_now; |
868 | now_floor = mn_now; |
807 | } |
869 | } |
808 | |
870 | |
809 | periodics_reschedule (EV_A_ diff - odiff); |
871 | periodics_reschedule (EV_A); |
810 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
872 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
873 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
811 | } |
874 | } |
812 | } |
875 | } |
813 | else |
876 | else |
814 | #endif |
877 | #endif |
815 | { |
878 | { |
816 | rt_now = ev_time (); |
879 | rt_now = ev_time (); |
817 | |
880 | |
818 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
881 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
819 | { |
882 | { |
820 | periodics_reschedule (EV_A_ rt_now - mn_now); |
883 | periodics_reschedule (EV_A); |
821 | |
884 | |
822 | /* adjust timers. this is easy, as the offset is the same for all */ |
885 | /* adjust timers. this is easy, as the offset is the same for all */ |
823 | for (i = 0; i < timercnt; ++i) |
886 | for (i = 0; i < timercnt; ++i) |
824 | timers [i]->at += diff; |
887 | timers [i]->at += rt_now - mn_now; |
825 | } |
888 | } |
826 | |
889 | |
827 | mn_now = rt_now; |
890 | mn_now = rt_now; |
828 | } |
891 | } |
829 | } |
892 | } |
… | |
… | |
911 | /* queue check watchers, to be executed first */ |
974 | /* queue check watchers, to be executed first */ |
912 | if (checkcnt) |
975 | if (checkcnt) |
913 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
976 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
914 | |
977 | |
915 | call_pending (EV_A); |
978 | call_pending (EV_A); |
916 | printf ("activecnt %d\n", activecnt);//D |
|
|
917 | } |
979 | } |
918 | while (activecnt && !loop_done); |
980 | while (activecnt && !loop_done); |
919 | |
981 | |
920 | if (loop_done != 2) |
982 | if (loop_done != 2) |
921 | loop_done = 0; |
983 | loop_done = 0; |
… | |
… | |
1093 | } |
1155 | } |
1094 | |
1156 | |
1095 | ev_stop (EV_A_ (W)w); |
1157 | ev_stop (EV_A_ (W)w); |
1096 | } |
1158 | } |
1097 | |
1159 | |
|
|
1160 | void |
|
|
1161 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1162 | { |
|
|
1163 | if (ev_is_active (w)) |
|
|
1164 | return; |
|
|
1165 | |
|
|
1166 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1167 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1168 | idles [idlecnt - 1] = w; |
|
|
1169 | } |
|
|
1170 | |
|
|
1171 | void |
|
|
1172 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1173 | { |
|
|
1174 | ev_clear_pending (EV_A_ (W)w); |
|
|
1175 | if (ev_is_active (w)) |
|
|
1176 | return; |
|
|
1177 | |
|
|
1178 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
1179 | ev_stop (EV_A_ (W)w); |
|
|
1180 | } |
|
|
1181 | |
|
|
1182 | void |
|
|
1183 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1184 | { |
|
|
1185 | if (ev_is_active (w)) |
|
|
1186 | return; |
|
|
1187 | |
|
|
1188 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1189 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1190 | prepares [preparecnt - 1] = w; |
|
|
1191 | } |
|
|
1192 | |
|
|
1193 | void |
|
|
1194 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1195 | { |
|
|
1196 | ev_clear_pending (EV_A_ (W)w); |
|
|
1197 | if (ev_is_active (w)) |
|
|
1198 | return; |
|
|
1199 | |
|
|
1200 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1201 | ev_stop (EV_A_ (W)w); |
|
|
1202 | } |
|
|
1203 | |
|
|
1204 | void |
|
|
1205 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1206 | { |
|
|
1207 | if (ev_is_active (w)) |
|
|
1208 | return; |
|
|
1209 | |
|
|
1210 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1211 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1212 | checks [checkcnt - 1] = w; |
|
|
1213 | } |
|
|
1214 | |
|
|
1215 | void |
|
|
1216 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1217 | { |
|
|
1218 | ev_clear_pending (EV_A_ (W)w); |
|
|
1219 | if (ev_is_active (w)) |
|
|
1220 | return; |
|
|
1221 | |
|
|
1222 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1223 | ev_stop (EV_A_ (W)w); |
|
|
1224 | } |
|
|
1225 | |
1098 | #ifndef SA_RESTART |
1226 | #ifndef SA_RESTART |
1099 | # define SA_RESTART 0 |
1227 | # define SA_RESTART 0 |
1100 | #endif |
1228 | #endif |
1101 | |
1229 | |
1102 | void |
1230 | void |
1103 | ev_signal_start (EV_P_ struct ev_signal *w) |
1231 | ev_signal_start (EV_P_ struct ev_signal *w) |
1104 | { |
1232 | { |
|
|
1233 | #if EV_MULTIPLICITY |
|
|
1234 | assert (("signal watchers are only supported in the default loop", loop == default_loop)); |
|
|
1235 | #endif |
1105 | if (ev_is_active (w)) |
1236 | if (ev_is_active (w)) |
1106 | return; |
1237 | return; |
1107 | |
1238 | |
1108 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1239 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1109 | |
1240 | |
… | |
… | |
1134 | if (!signals [w->signum - 1].head) |
1265 | if (!signals [w->signum - 1].head) |
1135 | signal (w->signum, SIG_DFL); |
1266 | signal (w->signum, SIG_DFL); |
1136 | } |
1267 | } |
1137 | |
1268 | |
1138 | void |
1269 | void |
1139 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1140 | { |
|
|
1141 | if (ev_is_active (w)) |
|
|
1142 | return; |
|
|
1143 | |
|
|
1144 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1145 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1146 | idles [idlecnt - 1] = w; |
|
|
1147 | } |
|
|
1148 | |
|
|
1149 | void |
|
|
1150 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1151 | { |
|
|
1152 | ev_clear_pending (EV_A_ (W)w); |
|
|
1153 | if (ev_is_active (w)) |
|
|
1154 | return; |
|
|
1155 | |
|
|
1156 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
1157 | ev_stop (EV_A_ (W)w); |
|
|
1158 | } |
|
|
1159 | |
|
|
1160 | void |
|
|
1161 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1162 | { |
|
|
1163 | if (ev_is_active (w)) |
|
|
1164 | return; |
|
|
1165 | |
|
|
1166 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1167 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1168 | prepares [preparecnt - 1] = w; |
|
|
1169 | } |
|
|
1170 | |
|
|
1171 | void |
|
|
1172 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1173 | { |
|
|
1174 | ev_clear_pending (EV_A_ (W)w); |
|
|
1175 | if (ev_is_active (w)) |
|
|
1176 | return; |
|
|
1177 | |
|
|
1178 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1179 | ev_stop (EV_A_ (W)w); |
|
|
1180 | } |
|
|
1181 | |
|
|
1182 | void |
|
|
1183 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1184 | { |
|
|
1185 | if (ev_is_active (w)) |
|
|
1186 | return; |
|
|
1187 | |
|
|
1188 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1189 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1190 | checks [checkcnt - 1] = w; |
|
|
1191 | } |
|
|
1192 | |
|
|
1193 | void |
|
|
1194 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1195 | { |
|
|
1196 | ev_clear_pending (EV_A_ (W)w); |
|
|
1197 | if (ev_is_active (w)) |
|
|
1198 | return; |
|
|
1199 | |
|
|
1200 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1201 | ev_stop (EV_A_ (W)w); |
|
|
1202 | } |
|
|
1203 | |
|
|
1204 | void |
|
|
1205 | ev_child_start (EV_P_ struct ev_child *w) |
1270 | ev_child_start (EV_P_ struct ev_child *w) |
1206 | { |
1271 | { |
|
|
1272 | #if EV_MULTIPLICITY |
|
|
1273 | assert (("child watchers are only supported in the default loop", loop == default_loop)); |
|
|
1274 | #endif |
1207 | if (ev_is_active (w)) |
1275 | if (ev_is_active (w)) |
1208 | return; |
1276 | return; |
1209 | |
1277 | |
1210 | ev_start (EV_A_ (W)w, 1); |
1278 | ev_start (EV_A_ (W)w, 1); |
1211 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1279 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
… | |
… | |
1283 | ev_timer_start (EV_A_ &once->to); |
1351 | ev_timer_start (EV_A_ &once->to); |
1284 | } |
1352 | } |
1285 | } |
1353 | } |
1286 | } |
1354 | } |
1287 | |
1355 | |
1288 | /*****************************************************************************/ |
|
|
1289 | |
|
|
1290 | #if 0 |
|
|
1291 | |
|
|
1292 | struct ev_io wio; |
|
|
1293 | |
|
|
1294 | static void |
|
|
1295 | sin_cb (struct ev_io *w, int revents) |
|
|
1296 | { |
|
|
1297 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
|
|
1298 | } |
|
|
1299 | |
|
|
1300 | static void |
|
|
1301 | ocb (struct ev_timer *w, int revents) |
|
|
1302 | { |
|
|
1303 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
|
|
1304 | ev_timer_stop (w); |
|
|
1305 | ev_timer_start (w); |
|
|
1306 | } |
|
|
1307 | |
|
|
1308 | static void |
|
|
1309 | scb (struct ev_signal *w, int revents) |
|
|
1310 | { |
|
|
1311 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
1312 | ev_io_stop (&wio); |
|
|
1313 | ev_io_start (&wio); |
|
|
1314 | } |
|
|
1315 | |
|
|
1316 | static void |
|
|
1317 | gcb (struct ev_signal *w, int revents) |
|
|
1318 | { |
|
|
1319 | fprintf (stderr, "generic %x\n", revents); |
|
|
1320 | |
|
|
1321 | } |
|
|
1322 | |
|
|
1323 | int main (void) |
|
|
1324 | { |
|
|
1325 | ev_init (0); |
|
|
1326 | |
|
|
1327 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
|
|
1328 | ev_io_start (&wio); |
|
|
1329 | |
|
|
1330 | struct ev_timer t[10000]; |
|
|
1331 | |
|
|
1332 | #if 0 |
|
|
1333 | int i; |
|
|
1334 | for (i = 0; i < 10000; ++i) |
|
|
1335 | { |
|
|
1336 | struct ev_timer *w = t + i; |
|
|
1337 | ev_watcher_init (w, ocb, i); |
|
|
1338 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
|
|
1339 | ev_timer_start (w); |
|
|
1340 | if (drand48 () < 0.5) |
|
|
1341 | ev_timer_stop (w); |
|
|
1342 | } |
|
|
1343 | #endif |
|
|
1344 | |
|
|
1345 | struct ev_timer t1; |
|
|
1346 | ev_timer_init (&t1, ocb, 5, 10); |
|
|
1347 | ev_timer_start (&t1); |
|
|
1348 | |
|
|
1349 | struct ev_signal sig; |
|
|
1350 | ev_signal_init (&sig, scb, SIGQUIT); |
|
|
1351 | ev_signal_start (&sig); |
|
|
1352 | |
|
|
1353 | struct ev_check cw; |
|
|
1354 | ev_check_init (&cw, gcb); |
|
|
1355 | ev_check_start (&cw); |
|
|
1356 | |
|
|
1357 | struct ev_idle iw; |
|
|
1358 | ev_idle_init (&iw, gcb); |
|
|
1359 | ev_idle_start (&iw); |
|
|
1360 | |
|
|
1361 | ev_loop (0); |
|
|
1362 | |
|
|
1363 | return 0; |
|
|
1364 | } |
|
|
1365 | |
|
|
1366 | #endif |
|
|
1367 | |
|
|
1368 | |
|
|
1369 | |
|
|
1370 | |
|
|