… | |
… | |
9 | #include <assert.h> |
9 | #include <assert.h> |
10 | #include <errno.h> |
10 | #include <errno.h> |
11 | #include <sys/time.h> |
11 | #include <sys/time.h> |
12 | #include <time.h> |
12 | #include <time.h> |
13 | |
13 | |
|
|
14 | #ifndef HAVE_MONOTONIC |
14 | #ifdef CLOCK_MONOTONIC |
15 | # ifdef CLOCK_MONOTONIC |
15 | # define HAVE_MONOTONIC 1 |
16 | # define HAVE_MONOTONIC 1 |
16 | #endif |
17 | # endif |
|
|
18 | #endif |
17 | |
19 | |
18 | #define HAVE_REALTIME 1 |
20 | #ifndef HAVE_SELECT |
19 | #define HAVE_EPOLL 1 |
|
|
20 | #define HAVE_SELECT 1 |
21 | # define HAVE_SELECT 1 |
|
|
22 | #endif |
|
|
23 | |
|
|
24 | #ifndef HAVE_EPOLL |
|
|
25 | # define HAVE_EPOLL 0 |
|
|
26 | #endif |
|
|
27 | |
|
|
28 | #ifndef HAVE_REALTIME |
|
|
29 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
|
|
30 | #endif |
21 | |
31 | |
22 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
32 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
23 | #define MAX_BLOCKTIME 60. |
33 | #define MAX_BLOCKTIME 60. |
24 | |
34 | |
25 | #include "ev.h" |
35 | #include "ev.h" |
… | |
… | |
29 | }; |
39 | }; |
30 | |
40 | |
31 | struct ev_watcher_list { |
41 | struct ev_watcher_list { |
32 | EV_WATCHER_LIST (ev_watcher_list); |
42 | EV_WATCHER_LIST (ev_watcher_list); |
33 | }; |
43 | }; |
|
|
44 | |
|
|
45 | typedef struct ev_watcher *W; |
|
|
46 | typedef struct ev_watcher_list *WL; |
34 | |
47 | |
35 | static ev_tstamp now, diff; /* monotonic clock */ |
48 | static ev_tstamp now, diff; /* monotonic clock */ |
36 | ev_tstamp ev_now; |
49 | ev_tstamp ev_now; |
37 | int ev_method; |
50 | int ev_method; |
38 | |
51 | |
… | |
… | |
108 | } |
121 | } |
109 | } |
122 | } |
110 | |
123 | |
111 | typedef struct |
124 | typedef struct |
112 | { |
125 | { |
113 | struct ev_watcher *w; |
126 | W w; |
114 | int events; |
127 | int events; |
115 | } ANPENDING; |
128 | } ANPENDING; |
116 | |
129 | |
117 | static ANPENDING *pendings; |
130 | static ANPENDING *pendings; |
118 | static int pendingmax, pendingcnt; |
131 | static int pendingmax, pendingcnt; |
119 | |
132 | |
120 | static void |
133 | static void |
121 | event (struct ev_watcher *w, int events) |
134 | event (W w, int events) |
122 | { |
135 | { |
123 | w->pending = ++pendingcnt; |
136 | w->pending = ++pendingcnt; |
124 | array_needsize (pendings, pendingmax, pendingcnt, ); |
137 | array_needsize (pendings, pendingmax, pendingcnt, ); |
125 | pendings [pendingcnt - 1].w = w; |
138 | pendings [pendingcnt - 1].w = w; |
126 | pendings [pendingcnt - 1].events = events; |
139 | pendings [pendingcnt - 1].events = events; |
… | |
… | |
135 | for (w = anfd->head; w; w = w->next) |
148 | for (w = anfd->head; w; w = w->next) |
136 | { |
149 | { |
137 | int ev = w->events & events; |
150 | int ev = w->events & events; |
138 | |
151 | |
139 | if (ev) |
152 | if (ev) |
140 | event ((struct ev_watcher *)w, ev); |
153 | event ((W)w, ev); |
141 | } |
154 | } |
142 | } |
155 | } |
143 | |
156 | |
144 | static void |
157 | static void |
145 | queue_events (struct ev_watcher **events, int eventcnt, int type) |
158 | queue_events (W *events, int eventcnt, int type) |
146 | { |
159 | { |
147 | int i; |
160 | int i; |
148 | |
161 | |
149 | for (i = 0; i < eventcnt; ++i) |
162 | for (i = 0; i < eventcnt; ++i) |
150 | event (events [i], type); |
163 | event (events [i], type); |
… | |
… | |
250 | if (signals [sig].gotsig) |
263 | if (signals [sig].gotsig) |
251 | { |
264 | { |
252 | signals [sig].gotsig = 0; |
265 | signals [sig].gotsig = 0; |
253 | |
266 | |
254 | for (w = signals [sig].head; w; w = w->next) |
267 | for (w = signals [sig].head; w; w = w->next) |
255 | event ((struct ev_watcher *)w, EV_SIGNAL); |
268 | event ((W)w, EV_SIGNAL); |
256 | } |
269 | } |
257 | } |
270 | } |
258 | |
271 | |
259 | static void |
272 | static void |
260 | siginit (void) |
273 | siginit (void) |
… | |
… | |
415 | { |
428 | { |
416 | evtimer_stop (w); /* nonrepeating: stop timer */ |
429 | evtimer_stop (w); /* nonrepeating: stop timer */ |
417 | --timercnt; /* maybe pass by reference instead? */ |
430 | --timercnt; /* maybe pass by reference instead? */ |
418 | } |
431 | } |
419 | |
432 | |
420 | event ((struct ev_watcher *)w, EV_TIMEOUT); |
433 | event ((W)w, EV_TIMEOUT); |
421 | } |
434 | } |
422 | } |
435 | } |
423 | |
436 | |
424 | static void |
437 | static void |
425 | time_update () |
438 | time_update () |
… | |
… | |
468 | double block; |
481 | double block; |
469 | ev_loop_done = flags & EVLOOP_ONESHOT; |
482 | ev_loop_done = flags & EVLOOP_ONESHOT; |
470 | |
483 | |
471 | if (checkcnt) |
484 | if (checkcnt) |
472 | { |
485 | { |
473 | queue_events (checks, checkcnt, EV_CHECK); |
486 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
474 | call_pending (); |
487 | call_pending (); |
475 | } |
488 | } |
476 | |
489 | |
477 | do |
490 | do |
478 | { |
491 | { |
… | |
… | |
512 | /* relative timers second */ |
525 | /* relative timers second */ |
513 | timers_reify (rtimers, rtimercnt, now); |
526 | timers_reify (rtimers, rtimercnt, now); |
514 | |
527 | |
515 | /* queue idle watchers unless io or timers are pending */ |
528 | /* queue idle watchers unless io or timers are pending */ |
516 | if (!pendingcnt) |
529 | if (!pendingcnt) |
517 | queue_events (idles, idlecnt, EV_IDLE); |
530 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
518 | |
531 | |
519 | /* queue check and possibly idle watchers */ |
532 | /* queue check and possibly idle watchers */ |
520 | queue_events (checks, checkcnt, EV_CHECK); |
533 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
521 | |
534 | |
522 | call_pending (); |
535 | call_pending (); |
523 | } |
536 | } |
524 | while (!ev_loop_done); |
537 | while (!ev_loop_done); |
525 | } |
538 | } |
526 | |
539 | |
527 | /*****************************************************************************/ |
540 | /*****************************************************************************/ |
528 | |
541 | |
529 | static void |
542 | static void |
530 | wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
543 | wlist_add (WL *head, WL elem) |
531 | { |
544 | { |
532 | elem->next = *head; |
545 | elem->next = *head; |
533 | *head = elem; |
546 | *head = elem; |
534 | } |
547 | } |
535 | |
548 | |
536 | static void |
549 | static void |
537 | wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
550 | wlist_del (WL *head, WL elem) |
538 | { |
551 | { |
539 | while (*head) |
552 | while (*head) |
540 | { |
553 | { |
541 | if (*head == elem) |
554 | if (*head == elem) |
542 | { |
555 | { |
… | |
… | |
547 | head = &(*head)->next; |
560 | head = &(*head)->next; |
548 | } |
561 | } |
549 | } |
562 | } |
550 | |
563 | |
551 | static void |
564 | static void |
552 | ev_start (struct ev_watcher *w, int active) |
565 | ev_start (W w, int active) |
553 | { |
566 | { |
554 | w->pending = 0; |
567 | w->pending = 0; |
555 | w->active = active; |
568 | w->active = active; |
556 | } |
569 | } |
557 | |
570 | |
558 | static void |
571 | static void |
559 | ev_stop (struct ev_watcher *w) |
572 | ev_stop (W w) |
560 | { |
573 | { |
561 | if (w->pending) |
574 | if (w->pending) |
562 | pendings [w->pending - 1].w = 0; |
575 | pendings [w->pending - 1].w = 0; |
563 | |
576 | |
564 | w->active = 0; |
577 | w->active = 0; |
… | |
… | |
573 | if (ev_is_active (w)) |
586 | if (ev_is_active (w)) |
574 | return; |
587 | return; |
575 | |
588 | |
576 | int fd = w->fd; |
589 | int fd = w->fd; |
577 | |
590 | |
578 | ev_start ((struct ev_watcher *)w, 1); |
591 | ev_start ((W)w, 1); |
579 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
592 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
580 | wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); |
593 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
581 | |
594 | |
582 | ++fdchangecnt; |
595 | ++fdchangecnt; |
583 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
596 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
584 | fdchanges [fdchangecnt - 1] = fd; |
597 | fdchanges [fdchangecnt - 1] = fd; |
585 | } |
598 | } |
… | |
… | |
588 | evio_stop (struct ev_io *w) |
601 | evio_stop (struct ev_io *w) |
589 | { |
602 | { |
590 | if (!ev_is_active (w)) |
603 | if (!ev_is_active (w)) |
591 | return; |
604 | return; |
592 | |
605 | |
593 | wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); |
606 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
594 | ev_stop ((struct ev_watcher *)w); |
607 | ev_stop ((W)w); |
595 | |
608 | |
596 | ++fdchangecnt; |
609 | ++fdchangecnt; |
597 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
610 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
598 | fdchanges [fdchangecnt - 1] = w->fd; |
611 | fdchanges [fdchangecnt - 1] = w->fd; |
599 | } |
612 | } |
… | |
… | |
608 | { |
621 | { |
609 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
622 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
610 | if (w->repeat) |
623 | if (w->repeat) |
611 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
624 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
612 | |
625 | |
613 | ev_start ((struct ev_watcher *)w, ++atimercnt); |
626 | ev_start ((W)w, ++atimercnt); |
614 | array_needsize (atimers, atimermax, atimercnt, ); |
627 | array_needsize (atimers, atimermax, atimercnt, ); |
615 | atimers [atimercnt - 1] = w; |
628 | atimers [atimercnt - 1] = w; |
616 | upheap (atimers, atimercnt - 1); |
629 | upheap (atimers, atimercnt - 1); |
617 | } |
630 | } |
618 | else |
631 | else |
619 | { |
632 | { |
620 | w->at += now; |
633 | w->at += now; |
621 | |
634 | |
622 | ev_start ((struct ev_watcher *)w, ++rtimercnt); |
635 | ev_start ((W)w, ++rtimercnt); |
623 | array_needsize (rtimers, rtimermax, rtimercnt, ); |
636 | array_needsize (rtimers, rtimermax, rtimercnt, ); |
624 | rtimers [rtimercnt - 1] = w; |
637 | rtimers [rtimercnt - 1] = w; |
625 | upheap (rtimers, rtimercnt - 1); |
638 | upheap (rtimers, rtimercnt - 1); |
626 | } |
639 | } |
627 | |
640 | |
… | |
… | |
648 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
661 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
649 | downheap (rtimers, rtimercnt, w->active - 1); |
662 | downheap (rtimers, rtimercnt, w->active - 1); |
650 | } |
663 | } |
651 | } |
664 | } |
652 | |
665 | |
653 | ev_stop ((struct ev_watcher *)w); |
666 | ev_stop ((W)w); |
654 | } |
667 | } |
655 | |
668 | |
656 | void |
669 | void |
657 | evsignal_start (struct ev_signal *w) |
670 | evsignal_start (struct ev_signal *w) |
658 | { |
671 | { |
659 | if (ev_is_active (w)) |
672 | if (ev_is_active (w)) |
660 | return; |
673 | return; |
661 | |
674 | |
662 | ev_start ((struct ev_watcher *)w, 1); |
675 | ev_start ((W)w, 1); |
663 | array_needsize (signals, signalmax, w->signum, signals_init); |
676 | array_needsize (signals, signalmax, w->signum, signals_init); |
664 | wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); |
677 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
665 | |
678 | |
666 | if (!w->next) |
679 | if (!w->next) |
667 | { |
680 | { |
668 | struct sigaction sa; |
681 | struct sigaction sa; |
669 | sa.sa_handler = sighandler; |
682 | sa.sa_handler = sighandler; |
… | |
… | |
677 | evsignal_stop (struct ev_signal *w) |
690 | evsignal_stop (struct ev_signal *w) |
678 | { |
691 | { |
679 | if (!ev_is_active (w)) |
692 | if (!ev_is_active (w)) |
680 | return; |
693 | return; |
681 | |
694 | |
682 | wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); |
695 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
683 | ev_stop ((struct ev_watcher *)w); |
696 | ev_stop ((W)w); |
684 | |
697 | |
685 | if (!signals [w->signum - 1].head) |
698 | if (!signals [w->signum - 1].head) |
686 | signal (w->signum, SIG_DFL); |
699 | signal (w->signum, SIG_DFL); |
687 | } |
700 | } |
688 | |
701 | |
689 | void evidle_start (struct ev_idle *w) |
702 | void evidle_start (struct ev_idle *w) |
690 | { |
703 | { |
691 | if (ev_is_active (w)) |
704 | if (ev_is_active (w)) |
692 | return; |
705 | return; |
693 | |
706 | |
694 | ev_start ((struct ev_watcher *)w, ++idlecnt); |
707 | ev_start ((W)w, ++idlecnt); |
695 | array_needsize (idles, idlemax, idlecnt, ); |
708 | array_needsize (idles, idlemax, idlecnt, ); |
696 | idles [idlecnt - 1] = w; |
709 | idles [idlecnt - 1] = w; |
697 | } |
710 | } |
698 | |
711 | |
699 | void evidle_stop (struct ev_idle *w) |
712 | void evidle_stop (struct ev_idle *w) |
700 | { |
713 | { |
701 | idles [w->active - 1] = idles [--idlecnt]; |
714 | idles [w->active - 1] = idles [--idlecnt]; |
702 | ev_stop ((struct ev_watcher *)w); |
715 | ev_stop ((W)w); |
703 | } |
716 | } |
704 | |
717 | |
705 | void evcheck_start (struct ev_check *w) |
718 | void evcheck_start (struct ev_check *w) |
706 | { |
719 | { |
707 | if (ev_is_active (w)) |
720 | if (ev_is_active (w)) |
708 | return; |
721 | return; |
709 | |
722 | |
710 | ev_start ((struct ev_watcher *)w, ++checkcnt); |
723 | ev_start ((W)w, ++checkcnt); |
711 | array_needsize (checks, checkmax, checkcnt, ); |
724 | array_needsize (checks, checkmax, checkcnt, ); |
712 | checks [checkcnt - 1] = w; |
725 | checks [checkcnt - 1] = w; |
713 | } |
726 | } |
714 | |
727 | |
715 | void evcheck_stop (struct ev_check *w) |
728 | void evcheck_stop (struct ev_check *w) |
716 | { |
729 | { |
717 | checks [w->active - 1] = checks [--checkcnt]; |
730 | checks [w->active - 1] = checks [--checkcnt]; |
718 | ev_stop ((struct ev_watcher *)w); |
731 | ev_stop ((W)w); |
719 | } |
732 | } |
720 | |
733 | |
721 | /*****************************************************************************/ |
734 | /*****************************************************************************/ |
|
|
735 | |
722 | #if 1 |
736 | #if 0 |
723 | |
737 | |
724 | static void |
738 | static void |
725 | sin_cb (struct ev_io *w, int revents) |
739 | sin_cb (struct ev_io *w, int revents) |
726 | { |
740 | { |
727 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
741 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |