… | |
… | |
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 int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
|
|
119 | |
118 | /*****************************************************************************/ |
120 | /*****************************************************************************/ |
119 | |
121 | |
120 | typedef struct |
122 | typedef struct |
121 | { |
123 | { |
122 | struct ev_watcher_list *head; |
124 | struct ev_watcher_list *head; |
… | |
… | |
128 | { |
130 | { |
129 | W w; |
131 | W w; |
130 | int events; |
132 | int events; |
131 | } ANPENDING; |
133 | } ANPENDING; |
132 | |
134 | |
133 | #ifdef EV_MULTIPLICITY |
135 | #if EV_MULTIPLICITY |
|
|
136 | |
134 | struct ev_loop |
137 | struct ev_loop |
135 | { |
138 | { |
136 | # define VAR(name,decl) decl |
139 | # define VAR(name,decl) decl; |
137 | # include "ev_vars.h" |
140 | # include "ev_vars.h" |
138 | }; |
141 | }; |
|
|
142 | # undef VAR |
|
|
143 | # include "ev_wrap.h" |
|
|
144 | |
139 | #else |
145 | #else |
|
|
146 | |
140 | # define VAR(name,decl) static decl |
147 | # define VAR(name,decl) static decl; |
141 | # include "ev_vars.h" |
148 | # include "ev_vars.h" |
142 | #endif |
|
|
143 | #undef VAR |
149 | # undef VAR |
|
|
150 | |
|
|
151 | #endif |
144 | |
152 | |
145 | /*****************************************************************************/ |
153 | /*****************************************************************************/ |
146 | |
154 | |
147 | inline ev_tstamp |
155 | inline ev_tstamp |
148 | ev_time (void) |
156 | ev_time (void) |
… | |
… | |
330 | fd_kill (EV_A_ fd); |
338 | fd_kill (EV_A_ fd); |
331 | return; |
339 | return; |
332 | } |
340 | } |
333 | } |
341 | } |
334 | |
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 | |
335 | /*****************************************************************************/ |
358 | /*****************************************************************************/ |
336 | |
359 | |
337 | static void |
360 | static void |
338 | upheap (WT *timers, int k) |
361 | upheap (WT *heap, int k) |
339 | { |
362 | { |
340 | WT w = timers [k]; |
363 | WT w = heap [k]; |
341 | |
364 | |
342 | while (k && timers [k >> 1]->at > w->at) |
365 | while (k && heap [k >> 1]->at > w->at) |
343 | { |
366 | { |
344 | timers [k] = timers [k >> 1]; |
367 | heap [k] = heap [k >> 1]; |
345 | timers [k]->active = k + 1; |
368 | heap [k]->active = k + 1; |
346 | k >>= 1; |
369 | k >>= 1; |
347 | } |
370 | } |
348 | |
371 | |
349 | timers [k] = w; |
372 | heap [k] = w; |
350 | timers [k]->active = k + 1; |
373 | heap [k]->active = k + 1; |
351 | |
374 | |
352 | } |
375 | } |
353 | |
376 | |
354 | static void |
377 | static void |
355 | downheap (WT *timers, int N, int k) |
378 | downheap (WT *heap, int N, int k) |
356 | { |
379 | { |
357 | WT w = timers [k]; |
380 | WT w = heap [k]; |
358 | |
381 | |
359 | while (k < (N >> 1)) |
382 | while (k < (N >> 1)) |
360 | { |
383 | { |
361 | int j = k << 1; |
384 | int j = k << 1; |
362 | |
385 | |
363 | if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
386 | if (j + 1 < N && heap [j]->at > heap [j + 1]->at) |
364 | ++j; |
387 | ++j; |
365 | |
388 | |
366 | if (w->at <= timers [j]->at) |
389 | if (w->at <= heap [j]->at) |
367 | break; |
390 | break; |
368 | |
391 | |
369 | timers [k] = timers [j]; |
392 | heap [k] = heap [j]; |
370 | timers [k]->active = k + 1; |
393 | heap [k]->active = k + 1; |
371 | k = j; |
394 | k = j; |
372 | } |
395 | } |
373 | |
396 | |
374 | timers [k] = w; |
397 | heap [k] = w; |
375 | timers [k]->active = k + 1; |
398 | heap [k]->active = k + 1; |
376 | } |
399 | } |
377 | |
400 | |
378 | /*****************************************************************************/ |
401 | /*****************************************************************************/ |
379 | |
402 | |
380 | typedef struct |
403 | typedef struct |
… | |
… | |
386 | static ANSIG *signals; |
409 | static ANSIG *signals; |
387 | static int signalmax; |
410 | static int signalmax; |
388 | |
411 | |
389 | static int sigpipe [2]; |
412 | static int sigpipe [2]; |
390 | static sig_atomic_t volatile gotsig; |
413 | static sig_atomic_t volatile gotsig; |
|
|
414 | static struct ev_io sigev; |
391 | |
415 | |
392 | static void |
416 | static void |
393 | signals_init (ANSIG *base, int count) |
417 | signals_init (ANSIG *base, int count) |
394 | { |
418 | { |
395 | while (count--) |
419 | while (count--) |
… | |
… | |
445 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
469 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
446 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
470 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
447 | #endif |
471 | #endif |
448 | |
472 | |
449 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
473 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
450 | ev_io_start (&sigev); |
474 | ev_io_start (EV_A_ &sigev); |
451 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
475 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
452 | } |
476 | } |
453 | |
477 | |
454 | /*****************************************************************************/ |
478 | /*****************************************************************************/ |
455 | |
479 | |
456 | #ifndef WIN32 |
480 | #ifndef WIN32 |
|
|
481 | |
|
|
482 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
483 | static struct ev_signal childev; |
457 | |
484 | |
458 | #ifndef WCONTINUED |
485 | #ifndef WCONTINUED |
459 | # define WCONTINUED 0 |
486 | # define WCONTINUED 0 |
460 | #endif |
487 | #endif |
461 | |
488 | |
… | |
… | |
497 | # include "ev_kqueue.c" |
524 | # include "ev_kqueue.c" |
498 | #endif |
525 | #endif |
499 | #if EV_USE_EPOLL |
526 | #if EV_USE_EPOLL |
500 | # include "ev_epoll.c" |
527 | # include "ev_epoll.c" |
501 | #endif |
528 | #endif |
502 | #if EV_USEV_POLL |
529 | #if EV_USE_POLL |
503 | # include "ev_poll.c" |
530 | # include "ev_poll.c" |
504 | #endif |
531 | #endif |
505 | #if EV_USE_SELECT |
532 | #if EV_USE_SELECT |
506 | # include "ev_select.c" |
533 | # include "ev_select.c" |
507 | #endif |
534 | #endif |
… | |
… | |
534 | ev_method (EV_P) |
561 | ev_method (EV_P) |
535 | { |
562 | { |
536 | return method; |
563 | return method; |
537 | } |
564 | } |
538 | |
565 | |
539 | int |
566 | static void |
540 | ev_init (EV_P_ int methods) |
567 | loop_init (EV_P_ int methods) |
541 | { |
568 | { |
542 | #ifdef EV_MULTIPLICITY |
|
|
543 | memset (loop, 0, sizeof (struct ev_loop)); |
|
|
544 | #endif |
|
|
545 | |
|
|
546 | if (!method) |
569 | if (!method) |
547 | { |
570 | { |
548 | #if EV_USE_MONOTONIC |
571 | #if EV_USE_MONOTONIC |
549 | { |
572 | { |
550 | struct timespec ts; |
573 | struct timespec ts; |
… | |
… | |
554 | #endif |
577 | #endif |
555 | |
578 | |
556 | rt_now = ev_time (); |
579 | rt_now = ev_time (); |
557 | mn_now = get_clock (); |
580 | mn_now = get_clock (); |
558 | now_floor = mn_now; |
581 | now_floor = mn_now; |
559 | diff = rt_now - mn_now; |
582 | rtmn_diff = rt_now - mn_now; |
560 | |
|
|
561 | if (pipe (sigpipe)) |
|
|
562 | return 0; |
|
|
563 | |
583 | |
564 | if (methods == EVMETHOD_AUTO) |
584 | if (methods == EVMETHOD_AUTO) |
565 | if (!enable_secure () && getenv ("LIBmethodS")) |
585 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
566 | methods = atoi (getenv ("LIBmethodS")); |
586 | methods = atoi (getenv ("LIBEV_METHODS")); |
567 | else |
587 | else |
568 | methods = EVMETHOD_ANY; |
588 | methods = EVMETHOD_ANY; |
569 | |
589 | |
570 | method = 0; |
590 | method = 0; |
571 | #if EV_USE_KQUEUE |
591 | #if EV_USE_KQUEUE |
572 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
592 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
573 | #endif |
593 | #endif |
574 | #if EV_USE_EPOLL |
594 | #if EV_USE_EPOLL |
575 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
595 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
576 | #endif |
596 | #endif |
577 | #if EV_USEV_POLL |
597 | #if EV_USE_POLL |
578 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
598 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
579 | #endif |
599 | #endif |
580 | #if EV_USE_SELECT |
600 | #if EV_USE_SELECT |
581 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
601 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
582 | #endif |
602 | #endif |
|
|
603 | } |
|
|
604 | } |
583 | |
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 | |
584 | if (method) |
693 | if (ev_method (EV_A)) |
585 | { |
694 | { |
586 | ev_watcher_init (&sigev, sigcb); |
695 | ev_watcher_init (&sigev, sigcb); |
587 | ev_set_priority (&sigev, EV_MAXPRI); |
696 | ev_set_priority (&sigev, EV_MAXPRI); |
588 | siginit (EV_A); |
697 | siginit (EV_A); |
589 | |
698 | |
… | |
… | |
592 | ev_set_priority (&childev, EV_MAXPRI); |
701 | ev_set_priority (&childev, EV_MAXPRI); |
593 | ev_signal_start (EV_A_ &childev); |
702 | ev_signal_start (EV_A_ &childev); |
594 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
703 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
595 | #endif |
704 | #endif |
596 | } |
705 | } |
|
|
706 | else |
|
|
707 | default_loop = 0; |
597 | } |
708 | } |
598 | |
709 | |
599 | return method; |
710 | return default_loop; |
600 | } |
711 | } |
601 | |
712 | |
602 | /*****************************************************************************/ |
|
|
603 | |
|
|
604 | void |
713 | void |
605 | ev_fork_prepare (void) |
714 | ev_default_destroy (void) |
606 | { |
715 | { |
607 | /* nop */ |
716 | #if EV_MULTIPLICITY |
608 | } |
717 | struct ev_loop *loop = default_loop; |
609 | |
|
|
610 | void |
|
|
611 | ev_fork_parent (void) |
|
|
612 | { |
|
|
613 | /* nop */ |
|
|
614 | } |
|
|
615 | |
|
|
616 | void |
|
|
617 | ev_fork_child (void) |
|
|
618 | { |
|
|
619 | #if EV_USE_EPOLL |
|
|
620 | if (method == EVMETHOD_EPOLL) |
|
|
621 | epoll_postfork_child (); |
|
|
622 | #endif |
718 | #endif |
623 | |
719 | |
|
|
720 | ev_ref (EV_A); /* child watcher */ |
|
|
721 | ev_signal_stop (EV_A_ &childev); |
|
|
722 | |
|
|
723 | ev_ref (EV_A); /* signal watcher */ |
624 | 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); |
625 | close (sigpipe [0]); |
738 | close (sigpipe [0]); |
626 | close (sigpipe [1]); |
739 | close (sigpipe [1]); |
627 | pipe (sigpipe); |
740 | pipe (sigpipe); |
|
|
741 | |
|
|
742 | ev_ref (EV_A); /* signal watcher */ |
628 | siginit (); |
743 | siginit (EV_A); |
629 | } |
744 | } |
630 | |
745 | |
631 | /*****************************************************************************/ |
746 | /*****************************************************************************/ |
632 | |
747 | |
633 | static void |
748 | static void |
… | |
… | |
663 | downheap ((WT *)timers, timercnt, 0); |
778 | downheap ((WT *)timers, timercnt, 0); |
664 | } |
779 | } |
665 | else |
780 | else |
666 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
781 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
667 | |
782 | |
668 | event ((W)w, EV_TIMEOUT); |
783 | event (EV_A_ (W)w, EV_TIMEOUT); |
669 | } |
784 | } |
670 | } |
785 | } |
671 | |
786 | |
672 | static void |
787 | static void |
673 | periodics_reify (EV_P) |
788 | periodics_reify (EV_P) |
… | |
… | |
689 | event (EV_A_ (W)w, EV_PERIODIC); |
804 | event (EV_A_ (W)w, EV_PERIODIC); |
690 | } |
805 | } |
691 | } |
806 | } |
692 | |
807 | |
693 | static void |
808 | static void |
694 | periodics_reschedule (EV_P_ ev_tstamp diff) |
809 | periodics_reschedule (EV_P) |
695 | { |
810 | { |
696 | int i; |
811 | int i; |
697 | |
812 | |
698 | /* adjust periodics after time jump */ |
813 | /* adjust periodics after time jump */ |
699 | for (i = 0; i < periodiccnt; ++i) |
814 | for (i = 0; i < periodiccnt; ++i) |
… | |
… | |
720 | { |
835 | { |
721 | mn_now = get_clock (); |
836 | mn_now = get_clock (); |
722 | |
837 | |
723 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
838 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
724 | { |
839 | { |
725 | rt_now = mn_now + diff; |
840 | rt_now = rtmn_diff + mn_now; |
726 | return 0; |
841 | return 0; |
727 | } |
842 | } |
728 | else |
843 | else |
729 | { |
844 | { |
730 | now_floor = mn_now; |
845 | now_floor = mn_now; |
… | |
… | |
741 | #if EV_USE_MONOTONIC |
856 | #if EV_USE_MONOTONIC |
742 | if (expect_true (have_monotonic)) |
857 | if (expect_true (have_monotonic)) |
743 | { |
858 | { |
744 | if (time_update_monotonic (EV_A)) |
859 | if (time_update_monotonic (EV_A)) |
745 | { |
860 | { |
746 | ev_tstamp odiff = diff; |
861 | ev_tstamp odiff = rtmn_diff; |
747 | |
862 | |
748 | 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 */ |
749 | { |
864 | { |
750 | diff = rt_now - mn_now; |
865 | rtmn_diff = rt_now - mn_now; |
751 | |
866 | |
752 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
867 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
753 | return; /* all is well */ |
868 | return; /* all is well */ |
754 | |
869 | |
755 | rt_now = ev_time (); |
870 | rt_now = ev_time (); |
756 | mn_now = get_clock (); |
871 | mn_now = get_clock (); |
757 | now_floor = mn_now; |
872 | now_floor = mn_now; |
758 | } |
873 | } |
759 | |
874 | |
760 | periodics_reschedule (EV_A_ diff - odiff); |
875 | periodics_reschedule (EV_A); |
761 | /* 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) */ |
762 | } |
878 | } |
763 | } |
879 | } |
764 | else |
880 | else |
765 | #endif |
881 | #endif |
766 | { |
882 | { |
767 | rt_now = ev_time (); |
883 | rt_now = ev_time (); |
768 | |
884 | |
769 | 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)) |
770 | { |
886 | { |
771 | periodics_reschedule (EV_A_ rt_now - mn_now); |
887 | periodics_reschedule (EV_A); |
772 | |
888 | |
773 | /* 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 */ |
774 | for (i = 0; i < timercnt; ++i) |
890 | for (i = 0; i < timercnt; ++i) |
775 | timers [i]->at += diff; |
891 | timers [i]->at += rt_now - mn_now; |
776 | } |
892 | } |
777 | |
893 | |
778 | mn_now = rt_now; |
894 | mn_now = rt_now; |
779 | } |
895 | } |
780 | } |
896 | } |
… | |
… | |
1043 | } |
1159 | } |
1044 | |
1160 | |
1045 | ev_stop (EV_A_ (W)w); |
1161 | ev_stop (EV_A_ (W)w); |
1046 | } |
1162 | } |
1047 | |
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 | |
1048 | #ifndef SA_RESTART |
1230 | #ifndef SA_RESTART |
1049 | # define SA_RESTART 0 |
1231 | # define SA_RESTART 0 |
1050 | #endif |
1232 | #endif |
1051 | |
1233 | |
1052 | void |
1234 | void |
1053 | ev_signal_start (EV_P_ struct ev_signal *w) |
1235 | ev_signal_start (EV_P_ struct ev_signal *w) |
1054 | { |
1236 | { |
|
|
1237 | #if EV_MULTIPLICITY |
|
|
1238 | assert (("signal watchers are only supported in the default loop", loop == default_loop)); |
|
|
1239 | #endif |
1055 | if (ev_is_active (w)) |
1240 | if (ev_is_active (w)) |
1056 | return; |
1241 | return; |
1057 | |
1242 | |
1058 | 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)); |
1059 | |
1244 | |
… | |
… | |
1084 | if (!signals [w->signum - 1].head) |
1269 | if (!signals [w->signum - 1].head) |
1085 | signal (w->signum, SIG_DFL); |
1270 | signal (w->signum, SIG_DFL); |
1086 | } |
1271 | } |
1087 | |
1272 | |
1088 | void |
1273 | void |
1089 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1090 | { |
|
|
1091 | if (ev_is_active (w)) |
|
|
1092 | return; |
|
|
1093 | |
|
|
1094 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1095 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1096 | idles [idlecnt - 1] = w; |
|
|
1097 | } |
|
|
1098 | |
|
|
1099 | void |
|
|
1100 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1101 | { |
|
|
1102 | ev_clear_pending (EV_A_ (W)w); |
|
|
1103 | if (ev_is_active (w)) |
|
|
1104 | return; |
|
|
1105 | |
|
|
1106 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
1107 | ev_stop (EV_A_ (W)w); |
|
|
1108 | } |
|
|
1109 | |
|
|
1110 | void |
|
|
1111 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1112 | { |
|
|
1113 | if (ev_is_active (w)) |
|
|
1114 | return; |
|
|
1115 | |
|
|
1116 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1117 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1118 | prepares [preparecnt - 1] = w; |
|
|
1119 | } |
|
|
1120 | |
|
|
1121 | void |
|
|
1122 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1123 | { |
|
|
1124 | ev_clear_pending (EV_A_ (W)w); |
|
|
1125 | if (ev_is_active (w)) |
|
|
1126 | return; |
|
|
1127 | |
|
|
1128 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1129 | ev_stop (EV_A_ (W)w); |
|
|
1130 | } |
|
|
1131 | |
|
|
1132 | void |
|
|
1133 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1134 | { |
|
|
1135 | if (ev_is_active (w)) |
|
|
1136 | return; |
|
|
1137 | |
|
|
1138 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1139 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1140 | checks [checkcnt - 1] = w; |
|
|
1141 | } |
|
|
1142 | |
|
|
1143 | void |
|
|
1144 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1145 | { |
|
|
1146 | ev_clear_pending (EV_A_ (W)w); |
|
|
1147 | if (ev_is_active (w)) |
|
|
1148 | return; |
|
|
1149 | |
|
|
1150 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1151 | ev_stop (EV_A_ (W)w); |
|
|
1152 | } |
|
|
1153 | |
|
|
1154 | void |
|
|
1155 | ev_child_start (EV_P_ struct ev_child *w) |
1274 | ev_child_start (EV_P_ struct ev_child *w) |
1156 | { |
1275 | { |
|
|
1276 | #if EV_MULTIPLICITY |
|
|
1277 | assert (("child watchers are only supported in the default loop", loop == default_loop)); |
|
|
1278 | #endif |
1157 | if (ev_is_active (w)) |
1279 | if (ev_is_active (w)) |
1158 | return; |
1280 | return; |
1159 | |
1281 | |
1160 | ev_start (EV_A_ (W)w, 1); |
1282 | ev_start (EV_A_ (W)w, 1); |
1161 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1283 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
… | |
… | |
1233 | ev_timer_start (EV_A_ &once->to); |
1355 | ev_timer_start (EV_A_ &once->to); |
1234 | } |
1356 | } |
1235 | } |
1357 | } |
1236 | } |
1358 | } |
1237 | |
1359 | |
1238 | /*****************************************************************************/ |
|
|
1239 | |
|
|
1240 | #if 0 |
|
|
1241 | |
|
|
1242 | struct ev_io wio; |
|
|
1243 | |
|
|
1244 | static void |
|
|
1245 | sin_cb (struct ev_io *w, int revents) |
|
|
1246 | { |
|
|
1247 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
|
|
1248 | } |
|
|
1249 | |
|
|
1250 | static void |
|
|
1251 | ocb (struct ev_timer *w, int revents) |
|
|
1252 | { |
|
|
1253 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
|
|
1254 | ev_timer_stop (w); |
|
|
1255 | ev_timer_start (w); |
|
|
1256 | } |
|
|
1257 | |
|
|
1258 | static void |
|
|
1259 | scb (struct ev_signal *w, int revents) |
|
|
1260 | { |
|
|
1261 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
1262 | ev_io_stop (&wio); |
|
|
1263 | ev_io_start (&wio); |
|
|
1264 | } |
|
|
1265 | |
|
|
1266 | static void |
|
|
1267 | gcb (struct ev_signal *w, int revents) |
|
|
1268 | { |
|
|
1269 | fprintf (stderr, "generic %x\n", revents); |
|
|
1270 | |
|
|
1271 | } |
|
|
1272 | |
|
|
1273 | int main (void) |
|
|
1274 | { |
|
|
1275 | ev_init (0); |
|
|
1276 | |
|
|
1277 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
|
|
1278 | ev_io_start (&wio); |
|
|
1279 | |
|
|
1280 | struct ev_timer t[10000]; |
|
|
1281 | |
|
|
1282 | #if 0 |
|
|
1283 | int i; |
|
|
1284 | for (i = 0; i < 10000; ++i) |
|
|
1285 | { |
|
|
1286 | struct ev_timer *w = t + i; |
|
|
1287 | ev_watcher_init (w, ocb, i); |
|
|
1288 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
|
|
1289 | ev_timer_start (w); |
|
|
1290 | if (drand48 () < 0.5) |
|
|
1291 | ev_timer_stop (w); |
|
|
1292 | } |
|
|
1293 | #endif |
|
|
1294 | |
|
|
1295 | struct ev_timer t1; |
|
|
1296 | ev_timer_init (&t1, ocb, 5, 10); |
|
|
1297 | ev_timer_start (&t1); |
|
|
1298 | |
|
|
1299 | struct ev_signal sig; |
|
|
1300 | ev_signal_init (&sig, scb, SIGQUIT); |
|
|
1301 | ev_signal_start (&sig); |
|
|
1302 | |
|
|
1303 | struct ev_check cw; |
|
|
1304 | ev_check_init (&cw, gcb); |
|
|
1305 | ev_check_start (&cw); |
|
|
1306 | |
|
|
1307 | struct ev_idle iw; |
|
|
1308 | ev_idle_init (&iw, gcb); |
|
|
1309 | ev_idle_start (&iw); |
|
|
1310 | |
|
|
1311 | ev_loop (0); |
|
|
1312 | |
|
|
1313 | return 0; |
|
|
1314 | } |
|
|
1315 | |
|
|
1316 | #endif |
|
|
1317 | |
|
|
1318 | |
|
|
1319 | |
|
|
1320 | |
|
|