1 | #include <math.h> |
1 | #include <math.h> |
2 | #include <stdlib.h> |
2 | #include <stdlib.h> |
3 | #include <unistd.h> |
3 | #include <unistd.h> |
4 | #include <fcntl.h> |
4 | #include <fcntl.h> |
5 | #include <signal.h> |
5 | #include <signal.h> |
|
|
6 | #include <stddef.h> |
6 | |
7 | |
7 | #include <stdio.h> |
8 | #include <stdio.h> |
8 | |
9 | |
9 | #include <assert.h> |
10 | #include <assert.h> |
10 | #include <errno.h> |
11 | #include <errno.h> |
11 | #include <sys/time.h> |
12 | #include <sys/time.h> |
12 | #include <time.h> |
13 | #include <time.h> |
13 | |
14 | |
|
|
15 | #define HAVE_EPOLL 1 |
|
|
16 | |
14 | #ifndef HAVE_MONOTONIC |
17 | #ifndef HAVE_MONOTONIC |
15 | # ifdef CLOCK_MONOTONIC |
18 | # ifdef CLOCK_MONOTONIC |
16 | # define HAVE_MONOTONIC 1 |
19 | # define HAVE_MONOTONIC 1 |
17 | # endif |
20 | # endif |
18 | #endif |
21 | #endif |
… | |
… | |
32 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
35 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
33 | #define MAX_BLOCKTIME 60. |
36 | #define MAX_BLOCKTIME 60. |
34 | |
37 | |
35 | #include "ev.h" |
38 | #include "ev.h" |
36 | |
39 | |
37 | struct ev_watcher { |
|
|
38 | EV_WATCHER (ev_watcher); |
|
|
39 | }; |
|
|
40 | |
|
|
41 | struct ev_watcher_list { |
|
|
42 | EV_WATCHER_LIST (ev_watcher_list); |
|
|
43 | }; |
|
|
44 | |
|
|
45 | typedef struct ev_watcher *W; |
40 | typedef struct ev_watcher *W; |
46 | typedef struct ev_watcher_list *WL; |
41 | typedef struct ev_watcher_list *WL; |
|
|
42 | typedef struct ev_watcher_time *WT; |
47 | |
43 | |
48 | static ev_tstamp now, diff; /* monotonic clock */ |
44 | static ev_tstamp now, diff; /* monotonic clock */ |
49 | ev_tstamp ev_now; |
45 | ev_tstamp ev_now; |
50 | int ev_method; |
46 | int ev_method; |
51 | |
47 | |
… | |
… | |
88 | |
84 | |
89 | #define array_needsize(base,cur,cnt,init) \ |
85 | #define array_needsize(base,cur,cnt,init) \ |
90 | if ((cnt) > cur) \ |
86 | if ((cnt) > cur) \ |
91 | { \ |
87 | { \ |
92 | int newcnt = cur ? cur << 1 : 16; \ |
88 | int newcnt = cur ? cur << 1 : 16; \ |
93 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
|
|
94 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
89 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
95 | init (base + cur, newcnt - cur); \ |
90 | init (base + cur, newcnt - cur); \ |
96 | cur = newcnt; \ |
91 | cur = newcnt; \ |
97 | } |
92 | } |
98 | |
93 | |
… | |
… | |
131 | static int pendingmax, pendingcnt; |
126 | static int pendingmax, pendingcnt; |
132 | |
127 | |
133 | static void |
128 | static void |
134 | event (W w, int events) |
129 | event (W w, int events) |
135 | { |
130 | { |
|
|
131 | if (w->active) |
|
|
132 | { |
136 | w->pending = ++pendingcnt; |
133 | w->pending = ++pendingcnt; |
137 | array_needsize (pendings, pendingmax, pendingcnt, ); |
134 | array_needsize (pendings, pendingmax, pendingcnt, ); |
138 | pendings [pendingcnt - 1].w = w; |
135 | pendings [pendingcnt - 1].w = w; |
139 | pendings [pendingcnt - 1].events = events; |
136 | pendings [pendingcnt - 1].events = events; |
|
|
137 | } |
140 | } |
138 | } |
141 | |
139 | |
142 | static void |
140 | static void |
143 | fd_event (int fd, int events) |
141 | fd_event (int fd, int events) |
144 | { |
142 | { |
… | |
… | |
163 | event (events [i], type); |
161 | event (events [i], type); |
164 | } |
162 | } |
165 | |
163 | |
166 | /*****************************************************************************/ |
164 | /*****************************************************************************/ |
167 | |
165 | |
168 | static struct ev_timer **atimers; |
166 | static struct ev_timer **timers; |
169 | static int atimermax, atimercnt; |
167 | static int timermax, timercnt; |
170 | |
168 | |
171 | static struct ev_timer **rtimers; |
169 | static struct ev_periodic **periodics; |
172 | static int rtimermax, rtimercnt; |
170 | static int periodicmax, periodiccnt; |
173 | |
171 | |
174 | static void |
172 | static void |
175 | upheap (struct ev_timer **timers, int k) |
173 | upheap (WT *timers, int k) |
176 | { |
174 | { |
177 | struct ev_timer *w = timers [k]; |
175 | WT w = timers [k]; |
178 | |
176 | |
179 | while (k && timers [k >> 1]->at > w->at) |
177 | while (k && timers [k >> 1]->at > w->at) |
180 | { |
178 | { |
181 | timers [k] = timers [k >> 1]; |
179 | timers [k] = timers [k >> 1]; |
182 | timers [k]->active = k + 1; |
180 | timers [k]->active = k + 1; |
… | |
… | |
187 | timers [k]->active = k + 1; |
185 | timers [k]->active = k + 1; |
188 | |
186 | |
189 | } |
187 | } |
190 | |
188 | |
191 | static void |
189 | static void |
192 | downheap (struct ev_timer **timers, int N, int k) |
190 | downheap (WT *timers, int N, int k) |
193 | { |
191 | { |
194 | struct ev_timer *w = timers [k]; |
192 | WT w = timers [k]; |
195 | |
193 | |
196 | while (k < (N >> 1)) |
194 | while (k < (N >> 1)) |
197 | { |
195 | { |
198 | int j = k << 1; |
196 | int j = k << 1; |
199 | |
197 | |
… | |
… | |
325 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
323 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
326 | #endif |
324 | #endif |
327 | |
325 | |
328 | if (ev_method) |
326 | if (ev_method) |
329 | { |
327 | { |
330 | evw_init (&sigev, sigcb, 0); |
328 | evw_init (&sigev, sigcb); |
331 | siginit (); |
329 | siginit (); |
332 | } |
330 | } |
333 | |
331 | |
334 | return ev_method; |
332 | return ev_method; |
335 | } |
333 | } |
… | |
… | |
406 | |
404 | |
407 | pendingcnt = 0; |
405 | pendingcnt = 0; |
408 | } |
406 | } |
409 | |
407 | |
410 | static void |
408 | static void |
411 | timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) |
409 | timers_reify () |
412 | { |
410 | { |
413 | while (timercnt && timers [0]->at <= now) |
411 | while (timercnt && timers [0]->at <= now) |
414 | { |
412 | { |
415 | struct ev_timer *w = timers [0]; |
413 | struct ev_timer *w = timers [0]; |
|
|
414 | |
|
|
415 | event ((W)w, EV_TIMEOUT); |
416 | |
416 | |
417 | /* first reschedule or stop timer */ |
417 | /* first reschedule or stop timer */ |
418 | if (w->repeat) |
418 | if (w->repeat) |
419 | { |
419 | { |
420 | if (w->is_abs) |
|
|
421 | w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
422 | else |
|
|
423 | w->at = now + w->repeat; |
420 | w->at = now + w->repeat; |
424 | |
421 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
425 | assert (w->at > now); |
|
|
426 | |
|
|
427 | downheap (timers, timercnt, 0); |
422 | downheap ((WT *)timers, timercnt, 0); |
428 | } |
423 | } |
429 | else |
424 | else |
430 | { |
|
|
431 | evtimer_stop (w); /* nonrepeating: stop timer */ |
425 | evtimer_stop (w); /* nonrepeating: stop timer */ |
432 | --timercnt; /* maybe pass by reference instead? */ |
426 | } |
|
|
427 | } |
|
|
428 | |
|
|
429 | static void |
|
|
430 | periodics_reify () |
|
|
431 | { |
|
|
432 | while (periodiccnt && periodics [0]->at <= ev_now) |
|
|
433 | { |
|
|
434 | struct ev_periodic *w = periodics [0]; |
|
|
435 | |
|
|
436 | /* first reschedule or stop timer */ |
|
|
437 | if (w->interval) |
433 | } |
438 | { |
|
|
439 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
|
|
440 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
|
|
441 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
442 | } |
|
|
443 | else |
|
|
444 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
434 | |
445 | |
435 | event ((W)w, EV_TIMEOUT); |
446 | event ((W)w, EV_TIMEOUT); |
436 | } |
447 | } |
437 | } |
448 | } |
438 | |
449 | |
439 | static void |
450 | static void |
|
|
451 | periodics_reschedule (ev_tstamp diff) |
|
|
452 | { |
|
|
453 | int i; |
|
|
454 | |
|
|
455 | /* adjust periodics after time jump */ |
|
|
456 | for (i = 0; i < periodiccnt; ++i) |
|
|
457 | { |
|
|
458 | struct ev_periodic *w = periodics [i]; |
|
|
459 | |
|
|
460 | if (w->interval) |
|
|
461 | { |
|
|
462 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
463 | |
|
|
464 | if (fabs (diff) >= 1e-4) |
|
|
465 | { |
|
|
466 | evperiodic_stop (w); |
|
|
467 | evperiodic_start (w); |
|
|
468 | |
|
|
469 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
470 | } |
|
|
471 | } |
|
|
472 | } |
|
|
473 | } |
|
|
474 | |
|
|
475 | static void |
440 | time_update () |
476 | time_update () |
441 | { |
477 | { |
442 | int i; |
478 | int i; |
|
|
479 | |
443 | ev_now = ev_time (); |
480 | ev_now = ev_time (); |
444 | |
481 | |
445 | if (have_monotonic) |
482 | if (have_monotonic) |
446 | { |
483 | { |
447 | ev_tstamp odiff = diff; |
484 | ev_tstamp odiff = diff; |
448 | |
485 | |
449 | /* detecting time jumps is much more difficult */ |
|
|
450 | for (i = 2; --i; ) /* loop a few times, before making important decisions */ |
486 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
451 | { |
487 | { |
452 | now = get_clock (); |
488 | now = get_clock (); |
453 | diff = ev_now - now; |
489 | diff = ev_now - now; |
454 | |
490 | |
455 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
491 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
456 | return; /* all is well */ |
492 | return; /* all is well */ |
457 | |
493 | |
458 | ev_now = ev_time (); |
494 | ev_now = ev_time (); |
459 | } |
495 | } |
460 | |
496 | |
461 | /* time jump detected, reschedule atimers */ |
497 | periodics_reschedule (diff - odiff); |
462 | for (i = 0; i < atimercnt; ++i) |
498 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
463 | { |
|
|
464 | struct ev_timer *w = atimers [i]; |
|
|
465 | w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
466 | } |
|
|
467 | } |
499 | } |
468 | else |
500 | else |
469 | { |
501 | { |
470 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
502 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
471 | /* time jump detected, adjust rtimers */ |
503 | { |
|
|
504 | periodics_reschedule (ev_now - now); |
|
|
505 | |
|
|
506 | /* adjust timers. this is easy, as the offset is the same for all */ |
472 | for (i = 0; i < rtimercnt; ++i) |
507 | for (i = 0; i < timercnt; ++i) |
473 | rtimers [i]->at += ev_now - now; |
508 | timers [i]->at += diff; |
|
|
509 | } |
474 | |
510 | |
475 | now = ev_now; |
511 | now = ev_now; |
476 | } |
512 | } |
477 | } |
513 | } |
478 | |
514 | |
479 | int ev_loop_done; |
515 | int ev_loop_done; |
480 | |
516 | |
481 | void ev_loop (int flags) |
517 | void ev_loop (int flags) |
482 | { |
518 | { |
483 | double block; |
519 | double block; |
484 | ev_loop_done = flags & EVLOOP_ONESHOT; |
520 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
485 | |
521 | |
486 | if (checkcnt) |
522 | if (checkcnt) |
487 | { |
523 | { |
488 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
524 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
489 | call_pending (); |
525 | call_pending (); |
… | |
… | |
493 | { |
529 | { |
494 | /* update fd-related kernel structures */ |
530 | /* update fd-related kernel structures */ |
495 | fd_reify (); |
531 | fd_reify (); |
496 | |
532 | |
497 | /* calculate blocking time */ |
533 | /* calculate blocking time */ |
|
|
534 | |
|
|
535 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
|
|
536 | ev_now = ev_time (); |
|
|
537 | |
498 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
538 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
499 | block = 0.; |
539 | block = 0.; |
500 | else |
540 | else |
501 | { |
541 | { |
502 | block = MAX_BLOCKTIME; |
542 | block = MAX_BLOCKTIME; |
503 | |
543 | |
504 | if (rtimercnt) |
544 | if (timercnt) |
505 | { |
545 | { |
506 | ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; |
546 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
507 | if (block > to) block = to; |
547 | if (block > to) block = to; |
508 | } |
548 | } |
509 | |
549 | |
510 | if (atimercnt) |
550 | if (periodiccnt) |
511 | { |
551 | { |
512 | ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; |
552 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
513 | if (block > to) block = to; |
553 | if (block > to) block = to; |
514 | } |
554 | } |
515 | |
555 | |
516 | if (block < 0.) block = 0.; |
556 | if (block < 0.) block = 0.; |
517 | } |
557 | } |
… | |
… | |
520 | |
560 | |
521 | /* update ev_now, do magic */ |
561 | /* update ev_now, do magic */ |
522 | time_update (); |
562 | time_update (); |
523 | |
563 | |
524 | /* queue pending timers and reschedule them */ |
564 | /* queue pending timers and reschedule them */ |
525 | /* absolute timers first */ |
565 | periodics_reify (); /* absolute timers first */ |
526 | timers_reify (atimers, atimercnt, ev_now); |
|
|
527 | /* relative timers second */ |
566 | timers_reify (); /* relative timers second */ |
528 | timers_reify (rtimers, rtimercnt, now); |
|
|
529 | |
567 | |
530 | /* queue idle watchers unless io or timers are pending */ |
568 | /* queue idle watchers unless io or timers are pending */ |
531 | if (!pendingcnt) |
569 | if (!pendingcnt) |
532 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
570 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
533 | |
571 | |
… | |
… | |
535 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
573 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
536 | |
574 | |
537 | call_pending (); |
575 | call_pending (); |
538 | } |
576 | } |
539 | while (!ev_loop_done); |
577 | while (!ev_loop_done); |
|
|
578 | |
|
|
579 | if (ev_loop_done != 2) |
|
|
580 | ev_loop_done = 0; |
540 | } |
581 | } |
541 | |
582 | |
542 | /*****************************************************************************/ |
583 | /*****************************************************************************/ |
543 | |
584 | |
544 | static void |
585 | static void |
… | |
… | |
562 | head = &(*head)->next; |
603 | head = &(*head)->next; |
563 | } |
604 | } |
564 | } |
605 | } |
565 | |
606 | |
566 | static void |
607 | static void |
|
|
608 | ev_clear (W w) |
|
|
609 | { |
|
|
610 | if (w->pending) |
|
|
611 | { |
|
|
612 | pendings [w->pending - 1].w = 0; |
|
|
613 | w->pending = 0; |
|
|
614 | } |
|
|
615 | } |
|
|
616 | |
|
|
617 | static void |
567 | ev_start (W w, int active) |
618 | ev_start (W w, int active) |
568 | { |
619 | { |
569 | w->pending = 0; |
|
|
570 | w->active = active; |
620 | w->active = active; |
571 | } |
621 | } |
572 | |
622 | |
573 | static void |
623 | static void |
574 | ev_stop (W w) |
624 | ev_stop (W w) |
575 | { |
625 | { |
576 | if (w->pending) |
|
|
577 | pendings [w->pending - 1].w = 0; |
|
|
578 | |
|
|
579 | w->active = 0; |
626 | w->active = 0; |
580 | } |
627 | } |
581 | |
628 | |
582 | /*****************************************************************************/ |
629 | /*****************************************************************************/ |
583 | |
630 | |
… | |
… | |
599 | } |
646 | } |
600 | |
647 | |
601 | void |
648 | void |
602 | evio_stop (struct ev_io *w) |
649 | evio_stop (struct ev_io *w) |
603 | { |
650 | { |
|
|
651 | ev_clear ((W)w); |
604 | if (!ev_is_active (w)) |
652 | if (!ev_is_active (w)) |
605 | return; |
653 | return; |
606 | |
654 | |
607 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
655 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
608 | ev_stop ((W)w); |
656 | ev_stop ((W)w); |
… | |
… | |
616 | evtimer_start (struct ev_timer *w) |
664 | evtimer_start (struct ev_timer *w) |
617 | { |
665 | { |
618 | if (ev_is_active (w)) |
666 | if (ev_is_active (w)) |
619 | return; |
667 | return; |
620 | |
668 | |
621 | if (w->is_abs) |
669 | w->at += now; |
622 | { |
|
|
623 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
|
|
624 | if (w->repeat) |
|
|
625 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
|
|
626 | |
670 | |
|
|
671 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
|
|
672 | |
627 | ev_start ((W)w, ++atimercnt); |
673 | ev_start ((W)w, ++timercnt); |
628 | array_needsize (atimers, atimermax, atimercnt, ); |
674 | array_needsize (timers, timermax, timercnt, ); |
629 | atimers [atimercnt - 1] = w; |
675 | timers [timercnt - 1] = w; |
630 | upheap (atimers, atimercnt - 1); |
676 | upheap ((WT *)timers, timercnt - 1); |
631 | } |
|
|
632 | else |
|
|
633 | { |
|
|
634 | w->at += now; |
|
|
635 | |
|
|
636 | ev_start ((W)w, ++rtimercnt); |
|
|
637 | array_needsize (rtimers, rtimermax, rtimercnt, ); |
|
|
638 | rtimers [rtimercnt - 1] = w; |
|
|
639 | upheap (rtimers, rtimercnt - 1); |
|
|
640 | } |
|
|
641 | |
|
|
642 | } |
677 | } |
643 | |
678 | |
644 | void |
679 | void |
645 | evtimer_stop (struct ev_timer *w) |
680 | evtimer_stop (struct ev_timer *w) |
646 | { |
681 | { |
|
|
682 | ev_clear ((W)w); |
647 | if (!ev_is_active (w)) |
683 | if (!ev_is_active (w)) |
648 | return; |
684 | return; |
649 | |
685 | |
650 | if (w->is_abs) |
|
|
651 | { |
|
|
652 | if (w->active < atimercnt--) |
686 | if (w->active < timercnt--) |
653 | { |
687 | { |
654 | atimers [w->active - 1] = atimers [atimercnt]; |
688 | timers [w->active - 1] = timers [timercnt]; |
|
|
689 | downheap ((WT *)timers, timercnt, w->active - 1); |
|
|
690 | } |
|
|
691 | |
|
|
692 | w->at = w->repeat; |
|
|
693 | |
|
|
694 | ev_stop ((W)w); |
|
|
695 | } |
|
|
696 | |
|
|
697 | void |
|
|
698 | evtimer_again (struct ev_timer *w) |
|
|
699 | { |
|
|
700 | if (ev_is_active (w)) |
|
|
701 | { |
|
|
702 | if (w->repeat) |
|
|
703 | { |
|
|
704 | w->at = now + w->repeat; |
655 | downheap (atimers, atimercnt, w->active - 1); |
705 | downheap ((WT *)timers, timercnt, w->active - 1); |
656 | } |
|
|
657 | } |
|
|
658 | else |
|
|
659 | { |
|
|
660 | if (w->active < rtimercnt--) |
|
|
661 | { |
706 | } |
662 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
707 | else |
663 | downheap (rtimers, rtimercnt, w->active - 1); |
708 | evtimer_stop (w); |
664 | } |
709 | } |
|
|
710 | else if (w->repeat) |
|
|
711 | evtimer_start (w); |
|
|
712 | } |
|
|
713 | |
|
|
714 | void |
|
|
715 | evperiodic_start (struct ev_periodic *w) |
|
|
716 | { |
|
|
717 | if (ev_is_active (w)) |
|
|
718 | return; |
|
|
719 | |
|
|
720 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
|
|
721 | |
|
|
722 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
|
|
723 | if (w->interval) |
|
|
724 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
725 | |
|
|
726 | ev_start ((W)w, ++periodiccnt); |
|
|
727 | array_needsize (periodics, periodicmax, periodiccnt, ); |
|
|
728 | periodics [periodiccnt - 1] = w; |
|
|
729 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
730 | } |
|
|
731 | |
|
|
732 | void |
|
|
733 | evperiodic_stop (struct ev_periodic *w) |
|
|
734 | { |
|
|
735 | ev_clear ((W)w); |
|
|
736 | if (!ev_is_active (w)) |
|
|
737 | return; |
|
|
738 | |
|
|
739 | if (w->active < periodiccnt--) |
|
|
740 | { |
|
|
741 | periodics [w->active - 1] = periodics [periodiccnt]; |
|
|
742 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
665 | } |
743 | } |
666 | |
744 | |
667 | ev_stop ((W)w); |
745 | ev_stop ((W)w); |
668 | } |
746 | } |
669 | |
747 | |
… | |
… | |
688 | } |
766 | } |
689 | |
767 | |
690 | void |
768 | void |
691 | evsignal_stop (struct ev_signal *w) |
769 | evsignal_stop (struct ev_signal *w) |
692 | { |
770 | { |
|
|
771 | ev_clear ((W)w); |
693 | if (!ev_is_active (w)) |
772 | if (!ev_is_active (w)) |
694 | return; |
773 | return; |
695 | |
774 | |
696 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
775 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
697 | ev_stop ((W)w); |
776 | ev_stop ((W)w); |
… | |
… | |
710 | idles [idlecnt - 1] = w; |
789 | idles [idlecnt - 1] = w; |
711 | } |
790 | } |
712 | |
791 | |
713 | void evidle_stop (struct ev_idle *w) |
792 | void evidle_stop (struct ev_idle *w) |
714 | { |
793 | { |
|
|
794 | ev_clear ((W)w); |
|
|
795 | if (ev_is_active (w)) |
|
|
796 | return; |
|
|
797 | |
715 | idles [w->active - 1] = idles [--idlecnt]; |
798 | idles [w->active - 1] = idles [--idlecnt]; |
716 | ev_stop ((W)w); |
799 | ev_stop ((W)w); |
717 | } |
800 | } |
718 | |
801 | |
719 | void evcheck_start (struct ev_check *w) |
802 | void evcheck_start (struct ev_check *w) |
… | |
… | |
726 | checks [checkcnt - 1] = w; |
809 | checks [checkcnt - 1] = w; |
727 | } |
810 | } |
728 | |
811 | |
729 | void evcheck_stop (struct ev_check *w) |
812 | void evcheck_stop (struct ev_check *w) |
730 | { |
813 | { |
|
|
814 | ev_clear ((W)w); |
|
|
815 | if (ev_is_active (w)) |
|
|
816 | return; |
|
|
817 | |
731 | checks [w->active - 1] = checks [--checkcnt]; |
818 | checks [w->active - 1] = checks [--checkcnt]; |
732 | ev_stop ((W)w); |
819 | ev_stop ((W)w); |
733 | } |
820 | } |
734 | |
821 | |
735 | /*****************************************************************************/ |
822 | /*****************************************************************************/ |
736 | |
823 | |
|
|
824 | struct ev_once |
|
|
825 | { |
|
|
826 | struct ev_io io; |
|
|
827 | struct ev_timer to; |
|
|
828 | void (*cb)(int revents, void *arg); |
|
|
829 | void *arg; |
|
|
830 | }; |
|
|
831 | |
|
|
832 | static void |
|
|
833 | once_cb (struct ev_once *once, int revents) |
|
|
834 | { |
|
|
835 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
836 | void *arg = once->arg; |
|
|
837 | |
|
|
838 | evio_stop (&once->io); |
|
|
839 | evtimer_stop (&once->to); |
|
|
840 | free (once); |
|
|
841 | |
|
|
842 | cb (revents, arg); |
|
|
843 | } |
|
|
844 | |
|
|
845 | static void |
|
|
846 | once_cb_io (struct ev_io *w, int revents) |
|
|
847 | { |
|
|
848 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
849 | } |
|
|
850 | |
|
|
851 | static void |
|
|
852 | once_cb_to (struct ev_timer *w, int revents) |
|
|
853 | { |
|
|
854 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
855 | } |
|
|
856 | |
|
|
857 | void |
|
|
858 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
859 | { |
|
|
860 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
861 | |
|
|
862 | if (!once) |
|
|
863 | cb (EV_ERROR, arg); |
|
|
864 | else |
|
|
865 | { |
|
|
866 | once->cb = cb; |
|
|
867 | once->arg = arg; |
|
|
868 | |
|
|
869 | evw_init (&once->io, once_cb_io); |
|
|
870 | |
|
|
871 | if (fd >= 0) |
|
|
872 | { |
|
|
873 | evio_set (&once->io, fd, events); |
|
|
874 | evio_start (&once->io); |
|
|
875 | } |
|
|
876 | |
|
|
877 | evw_init (&once->to, once_cb_to); |
|
|
878 | |
|
|
879 | if (timeout >= 0.) |
|
|
880 | { |
|
|
881 | evtimer_set (&once->to, timeout, 0.); |
|
|
882 | evtimer_start (&once->to); |
|
|
883 | } |
|
|
884 | } |
|
|
885 | } |
|
|
886 | |
|
|
887 | /*****************************************************************************/ |
|
|
888 | |
737 | #if 0 |
889 | #if 0 |
|
|
890 | |
|
|
891 | struct ev_io wio; |
738 | |
892 | |
739 | static void |
893 | static void |
740 | sin_cb (struct ev_io *w, int revents) |
894 | sin_cb (struct ev_io *w, int revents) |
741 | { |
895 | { |
742 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
896 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
… | |
… | |
752 | |
906 | |
753 | static void |
907 | static void |
754 | scb (struct ev_signal *w, int revents) |
908 | scb (struct ev_signal *w, int revents) |
755 | { |
909 | { |
756 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
910 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
911 | evio_stop (&wio); |
|
|
912 | evio_start (&wio); |
757 | } |
913 | } |
758 | |
914 | |
759 | static void |
915 | static void |
760 | gcb (struct ev_signal *w, int revents) |
916 | gcb (struct ev_signal *w, int revents) |
761 | { |
917 | { |
762 | fprintf (stderr, "generic %x\n", revents); |
918 | fprintf (stderr, "generic %x\n", revents); |
|
|
919 | |
763 | } |
920 | } |
764 | |
921 | |
765 | int main (void) |
922 | int main (void) |
766 | { |
923 | { |
767 | struct ev_io sin; |
|
|
768 | |
|
|
769 | ev_init (0); |
924 | ev_init (0); |
770 | |
925 | |
771 | evw_init (&sin, sin_cb, 55); |
|
|
772 | evio_set (&sin, 0, EV_READ); |
926 | evio_init (&wio, sin_cb, 0, EV_READ); |
773 | evio_start (&sin); |
927 | evio_start (&wio); |
774 | |
928 | |
775 | struct ev_timer t[10000]; |
929 | struct ev_timer t[10000]; |
776 | |
930 | |
777 | #if 0 |
931 | #if 0 |
778 | int i; |
932 | int i; |
779 | for (i = 0; i < 10000; ++i) |
933 | for (i = 0; i < 10000; ++i) |
780 | { |
934 | { |
781 | struct ev_timer *w = t + i; |
935 | struct ev_timer *w = t + i; |
782 | evw_init (w, ocb, i); |
936 | evw_init (w, ocb, i); |
783 | evtimer_set_abs (w, drand48 (), 0.99775533); |
937 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
784 | evtimer_start (w); |
938 | evtimer_start (w); |
785 | if (drand48 () < 0.5) |
939 | if (drand48 () < 0.5) |
786 | evtimer_stop (w); |
940 | evtimer_stop (w); |
787 | } |
941 | } |
788 | #endif |
942 | #endif |
789 | |
943 | |
790 | struct ev_timer t1; |
944 | struct ev_timer t1; |
791 | evw_init (&t1, ocb, 0); |
945 | evtimer_init (&t1, ocb, 5, 10); |
792 | evtimer_set_abs (&t1, 5, 10); |
|
|
793 | evtimer_start (&t1); |
946 | evtimer_start (&t1); |
794 | |
947 | |
795 | struct ev_signal sig; |
948 | struct ev_signal sig; |
796 | evw_init (&sig, scb, 65535); |
|
|
797 | evsignal_set (&sig, SIGQUIT); |
949 | evsignal_init (&sig, scb, SIGQUIT); |
798 | evsignal_start (&sig); |
950 | evsignal_start (&sig); |
799 | |
951 | |
800 | struct ev_check cw; |
952 | struct ev_check cw; |
801 | evw_init (&cw, gcb, 0); |
953 | evcheck_init (&cw, gcb); |
802 | evcheck_start (&cw); |
954 | evcheck_start (&cw); |
803 | |
955 | |
804 | struct ev_idle iw; |
956 | struct ev_idle iw; |
805 | evw_init (&iw, gcb, 0); |
957 | evidle_init (&iw, gcb); |
806 | evidle_start (&iw); |
958 | evidle_start (&iw); |
807 | |
959 | |
808 | ev_loop (0); |
960 | ev_loop (0); |
809 | |
961 | |
810 | return 0; |
962 | return 0; |