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 | } |
336 | |
334 | |
337 | /*****************************************************************************/ |
335 | /*****************************************************************************/ |
338 | |
336 | |
339 | void ev_prefork (void) |
337 | void ev_prefork (void) |
340 | { |
338 | { |
|
|
339 | /* nop */ |
341 | } |
340 | } |
342 | |
341 | |
343 | void ev_postfork_parent (void) |
342 | void ev_postfork_parent (void) |
344 | { |
343 | { |
|
|
344 | /* nop */ |
345 | } |
345 | } |
346 | |
346 | |
347 | void ev_postfork_child (void) |
347 | void ev_postfork_child (void) |
348 | { |
348 | { |
349 | #if HAVE_EPOLL |
349 | #if HAVE_EPOLL |
… | |
… | |
404 | |
404 | |
405 | pendingcnt = 0; |
405 | pendingcnt = 0; |
406 | } |
406 | } |
407 | |
407 | |
408 | static void |
408 | static void |
409 | timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) |
409 | timers_reify () |
410 | { |
410 | { |
411 | while (timercnt && timers [0]->at <= now) |
411 | while (timercnt && timers [0]->at <= now) |
412 | { |
412 | { |
413 | struct ev_timer *w = timers [0]; |
413 | struct ev_timer *w = timers [0]; |
|
|
414 | |
|
|
415 | event ((W)w, EV_TIMEOUT); |
414 | |
416 | |
415 | /* first reschedule or stop timer */ |
417 | /* first reschedule or stop timer */ |
416 | if (w->repeat) |
418 | if (w->repeat) |
417 | { |
419 | { |
418 | if (w->is_abs) |
|
|
419 | w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
420 | else |
|
|
421 | w->at = now + w->repeat; |
420 | w->at = now + w->repeat; |
422 | |
421 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
423 | assert (w->at > now); |
|
|
424 | |
|
|
425 | downheap (timers, timercnt, 0); |
422 | downheap ((WT *)timers, timercnt, 0); |
426 | } |
423 | } |
427 | else |
424 | else |
428 | { |
|
|
429 | evtimer_stop (w); /* nonrepeating: stop timer */ |
425 | evtimer_stop (w); /* nonrepeating: stop timer */ |
430 | --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) |
431 | } |
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 */ |
432 | |
445 | |
433 | event ((W)w, EV_TIMEOUT); |
446 | event ((W)w, EV_TIMEOUT); |
434 | } |
447 | } |
435 | } |
448 | } |
436 | |
449 | |
437 | 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 |
438 | time_update () |
476 | time_update () |
439 | { |
477 | { |
440 | int i; |
478 | int i; |
|
|
479 | |
441 | ev_now = ev_time (); |
480 | ev_now = ev_time (); |
442 | |
481 | |
443 | if (have_monotonic) |
482 | if (have_monotonic) |
444 | { |
483 | { |
445 | ev_tstamp odiff = diff; |
484 | ev_tstamp odiff = diff; |
446 | |
485 | |
447 | /* detecting time jumps is much more difficult */ |
|
|
448 | 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 */ |
449 | { |
487 | { |
450 | now = get_clock (); |
488 | now = get_clock (); |
451 | diff = ev_now - now; |
489 | diff = ev_now - now; |
452 | |
490 | |
453 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
491 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
454 | return; /* all is well */ |
492 | return; /* all is well */ |
455 | |
493 | |
456 | ev_now = ev_time (); |
494 | ev_now = ev_time (); |
457 | } |
495 | } |
458 | |
496 | |
459 | /* time jump detected, reschedule atimers */ |
497 | periodics_reschedule (diff - odiff); |
460 | for (i = 0; i < atimercnt; ++i) |
498 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
461 | { |
|
|
462 | struct ev_timer *w = atimers [i]; |
|
|
463 | w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
464 | } |
|
|
465 | } |
499 | } |
466 | else |
500 | else |
467 | { |
501 | { |
468 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
502 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
469 | /* 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 */ |
470 | for (i = 0; i < rtimercnt; ++i) |
507 | for (i = 0; i < timercnt; ++i) |
471 | rtimers [i]->at += ev_now - now; |
508 | timers [i]->at += diff; |
|
|
509 | } |
472 | |
510 | |
473 | now = ev_now; |
511 | now = ev_now; |
474 | } |
512 | } |
475 | } |
513 | } |
476 | |
514 | |
477 | int ev_loop_done; |
515 | int ev_loop_done; |
478 | |
516 | |
479 | void ev_loop (int flags) |
517 | void ev_loop (int flags) |
480 | { |
518 | { |
481 | double block; |
519 | double block; |
482 | ev_loop_done = flags & EVLOOP_ONESHOT; |
520 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
483 | |
521 | |
484 | if (checkcnt) |
522 | if (checkcnt) |
485 | { |
523 | { |
486 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
524 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
487 | call_pending (); |
525 | call_pending (); |
… | |
… | |
491 | { |
529 | { |
492 | /* update fd-related kernel structures */ |
530 | /* update fd-related kernel structures */ |
493 | fd_reify (); |
531 | fd_reify (); |
494 | |
532 | |
495 | /* 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 | |
496 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
538 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
497 | block = 0.; |
539 | block = 0.; |
498 | else |
540 | else |
499 | { |
541 | { |
500 | block = MAX_BLOCKTIME; |
542 | block = MAX_BLOCKTIME; |
501 | |
543 | |
502 | if (rtimercnt) |
544 | if (timercnt) |
503 | { |
545 | { |
504 | 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; |
505 | if (block > to) block = to; |
547 | if (block > to) block = to; |
506 | } |
548 | } |
507 | |
549 | |
508 | if (atimercnt) |
550 | if (periodiccnt) |
509 | { |
551 | { |
510 | ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; |
552 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
511 | if (block > to) block = to; |
553 | if (block > to) block = to; |
512 | } |
554 | } |
513 | |
555 | |
514 | if (block < 0.) block = 0.; |
556 | if (block < 0.) block = 0.; |
515 | } |
557 | } |
… | |
… | |
518 | |
560 | |
519 | /* update ev_now, do magic */ |
561 | /* update ev_now, do magic */ |
520 | time_update (); |
562 | time_update (); |
521 | |
563 | |
522 | /* queue pending timers and reschedule them */ |
564 | /* queue pending timers and reschedule them */ |
523 | /* absolute timers first */ |
565 | periodics_reify (); /* absolute timers first */ |
524 | timers_reify (atimers, atimercnt, ev_now); |
|
|
525 | /* relative timers second */ |
566 | timers_reify (); /* relative timers second */ |
526 | timers_reify (rtimers, rtimercnt, now); |
|
|
527 | |
567 | |
528 | /* queue idle watchers unless io or timers are pending */ |
568 | /* queue idle watchers unless io or timers are pending */ |
529 | if (!pendingcnt) |
569 | if (!pendingcnt) |
530 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
570 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
531 | |
571 | |
… | |
… | |
533 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
573 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
534 | |
574 | |
535 | call_pending (); |
575 | call_pending (); |
536 | } |
576 | } |
537 | while (!ev_loop_done); |
577 | while (!ev_loop_done); |
|
|
578 | |
|
|
579 | if (ev_loop_done != 2) |
|
|
580 | ev_loop_done = 0; |
538 | } |
581 | } |
539 | |
582 | |
540 | /*****************************************************************************/ |
583 | /*****************************************************************************/ |
541 | |
584 | |
542 | static void |
585 | static void |
… | |
… | |
560 | head = &(*head)->next; |
603 | head = &(*head)->next; |
561 | } |
604 | } |
562 | } |
605 | } |
563 | |
606 | |
564 | 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 |
565 | ev_start (W w, int active) |
618 | ev_start (W w, int active) |
566 | { |
619 | { |
567 | w->pending = 0; |
|
|
568 | w->active = active; |
620 | w->active = active; |
569 | } |
621 | } |
570 | |
622 | |
571 | static void |
623 | static void |
572 | ev_stop (W w) |
624 | ev_stop (W w) |
573 | { |
625 | { |
574 | if (w->pending) |
|
|
575 | pendings [w->pending - 1].w = 0; |
|
|
576 | |
|
|
577 | w->active = 0; |
626 | w->active = 0; |
578 | /* nop */ |
|
|
579 | } |
627 | } |
580 | |
628 | |
581 | /*****************************************************************************/ |
629 | /*****************************************************************************/ |
582 | |
630 | |
583 | void |
631 | void |
… | |
… | |
598 | } |
646 | } |
599 | |
647 | |
600 | void |
648 | void |
601 | evio_stop (struct ev_io *w) |
649 | evio_stop (struct ev_io *w) |
602 | { |
650 | { |
|
|
651 | ev_clear ((W)w); |
603 | if (!ev_is_active (w)) |
652 | if (!ev_is_active (w)) |
604 | return; |
653 | return; |
605 | |
654 | |
606 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
655 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
607 | ev_stop ((W)w); |
656 | ev_stop ((W)w); |
… | |
… | |
615 | evtimer_start (struct ev_timer *w) |
664 | evtimer_start (struct ev_timer *w) |
616 | { |
665 | { |
617 | if (ev_is_active (w)) |
666 | if (ev_is_active (w)) |
618 | return; |
667 | return; |
619 | |
668 | |
620 | if (w->is_abs) |
669 | w->at += now; |
621 | { |
|
|
622 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
|
|
623 | if (w->repeat) |
|
|
624 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
|
|
625 | |
670 | |
|
|
671 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
|
|
672 | |
626 | ev_start ((W)w, ++atimercnt); |
673 | ev_start ((W)w, ++timercnt); |
627 | array_needsize (atimers, atimermax, atimercnt, ); |
674 | array_needsize (timers, timermax, timercnt, ); |
628 | atimers [atimercnt - 1] = w; |
675 | timers [timercnt - 1] = w; |
629 | upheap (atimers, atimercnt - 1); |
676 | upheap ((WT *)timers, timercnt - 1); |
630 | } |
|
|
631 | else |
|
|
632 | { |
|
|
633 | w->at += now; |
|
|
634 | |
|
|
635 | ev_start ((W)w, ++rtimercnt); |
|
|
636 | array_needsize (rtimers, rtimermax, rtimercnt, ); |
|
|
637 | rtimers [rtimercnt - 1] = w; |
|
|
638 | upheap (rtimers, rtimercnt - 1); |
|
|
639 | } |
|
|
640 | |
|
|
641 | } |
677 | } |
642 | |
678 | |
643 | void |
679 | void |
644 | evtimer_stop (struct ev_timer *w) |
680 | evtimer_stop (struct ev_timer *w) |
645 | { |
681 | { |
|
|
682 | ev_clear ((W)w); |
646 | if (!ev_is_active (w)) |
683 | if (!ev_is_active (w)) |
647 | return; |
684 | return; |
648 | |
685 | |
649 | if (w->is_abs) |
|
|
650 | { |
|
|
651 | if (w->active < atimercnt--) |
686 | if (w->active < timercnt--) |
652 | { |
687 | { |
653 | 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; |
654 | downheap (atimers, atimercnt, w->active - 1); |
705 | downheap ((WT *)timers, timercnt, w->active - 1); |
655 | } |
|
|
656 | } |
|
|
657 | else |
|
|
658 | { |
|
|
659 | if (w->active < rtimercnt--) |
|
|
660 | { |
706 | } |
661 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
707 | else |
662 | downheap (rtimers, rtimercnt, w->active - 1); |
708 | evtimer_stop (w); |
663 | } |
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); |
664 | } |
743 | } |
665 | |
744 | |
666 | ev_stop ((W)w); |
745 | ev_stop ((W)w); |
667 | } |
746 | } |
668 | |
747 | |
… | |
… | |
687 | } |
766 | } |
688 | |
767 | |
689 | void |
768 | void |
690 | evsignal_stop (struct ev_signal *w) |
769 | evsignal_stop (struct ev_signal *w) |
691 | { |
770 | { |
|
|
771 | ev_clear ((W)w); |
692 | if (!ev_is_active (w)) |
772 | if (!ev_is_active (w)) |
693 | return; |
773 | return; |
694 | |
774 | |
695 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
775 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
696 | ev_stop ((W)w); |
776 | ev_stop ((W)w); |
… | |
… | |
709 | idles [idlecnt - 1] = w; |
789 | idles [idlecnt - 1] = w; |
710 | } |
790 | } |
711 | |
791 | |
712 | void evidle_stop (struct ev_idle *w) |
792 | void evidle_stop (struct ev_idle *w) |
713 | { |
793 | { |
|
|
794 | ev_clear ((W)w); |
|
|
795 | if (ev_is_active (w)) |
|
|
796 | return; |
|
|
797 | |
714 | idles [w->active - 1] = idles [--idlecnt]; |
798 | idles [w->active - 1] = idles [--idlecnt]; |
715 | ev_stop ((W)w); |
799 | ev_stop ((W)w); |
716 | } |
800 | } |
717 | |
801 | |
718 | void evcheck_start (struct ev_check *w) |
802 | void evcheck_start (struct ev_check *w) |
… | |
… | |
725 | checks [checkcnt - 1] = w; |
809 | checks [checkcnt - 1] = w; |
726 | } |
810 | } |
727 | |
811 | |
728 | void evcheck_stop (struct ev_check *w) |
812 | void evcheck_stop (struct ev_check *w) |
729 | { |
813 | { |
|
|
814 | ev_clear ((W)w); |
|
|
815 | if (ev_is_active (w)) |
|
|
816 | return; |
|
|
817 | |
730 | checks [w->active - 1] = checks [--checkcnt]; |
818 | checks [w->active - 1] = checks [--checkcnt]; |
731 | ev_stop ((W)w); |
819 | ev_stop ((W)w); |
732 | } |
820 | } |
733 | |
821 | |
734 | /*****************************************************************************/ |
822 | /*****************************************************************************/ |
735 | |
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 | |
736 | #if 0 |
889 | #if 0 |
|
|
890 | |
|
|
891 | struct ev_io wio; |
737 | |
892 | |
738 | static void |
893 | static void |
739 | sin_cb (struct ev_io *w, int revents) |
894 | sin_cb (struct ev_io *w, int revents) |
740 | { |
895 | { |
741 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
896 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
… | |
… | |
751 | |
906 | |
752 | static void |
907 | static void |
753 | scb (struct ev_signal *w, int revents) |
908 | scb (struct ev_signal *w, int revents) |
754 | { |
909 | { |
755 | 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); |
756 | } |
913 | } |
757 | |
914 | |
758 | static void |
915 | static void |
759 | gcb (struct ev_signal *w, int revents) |
916 | gcb (struct ev_signal *w, int revents) |
760 | { |
917 | { |
761 | fprintf (stderr, "generic %x\n", revents); |
918 | fprintf (stderr, "generic %x\n", revents); |
|
|
919 | |
762 | } |
920 | } |
763 | |
921 | |
764 | int main (void) |
922 | int main (void) |
765 | { |
923 | { |
766 | struct ev_io sin; |
|
|
767 | |
|
|
768 | ev_init (0); |
924 | ev_init (0); |
769 | |
925 | |
770 | evw_init (&sin, sin_cb, 55); |
|
|
771 | evio_set (&sin, 0, EV_READ); |
926 | evio_init (&wio, sin_cb, 0, EV_READ); |
772 | evio_start (&sin); |
927 | evio_start (&wio); |
773 | |
928 | |
774 | struct ev_timer t[10000]; |
929 | struct ev_timer t[10000]; |
775 | |
930 | |
776 | #if 0 |
931 | #if 0 |
777 | int i; |
932 | int i; |
778 | for (i = 0; i < 10000; ++i) |
933 | for (i = 0; i < 10000; ++i) |
779 | { |
934 | { |
780 | struct ev_timer *w = t + i; |
935 | struct ev_timer *w = t + i; |
781 | evw_init (w, ocb, i); |
936 | evw_init (w, ocb, i); |
782 | evtimer_set_abs (w, drand48 (), 0.99775533); |
937 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
783 | evtimer_start (w); |
938 | evtimer_start (w); |
784 | if (drand48 () < 0.5) |
939 | if (drand48 () < 0.5) |
785 | evtimer_stop (w); |
940 | evtimer_stop (w); |
786 | } |
941 | } |
787 | #endif |
942 | #endif |
788 | |
943 | |
789 | struct ev_timer t1; |
944 | struct ev_timer t1; |
790 | evw_init (&t1, ocb, 0); |
945 | evtimer_init (&t1, ocb, 5, 10); |
791 | evtimer_set_abs (&t1, 5, 10); |
|
|
792 | evtimer_start (&t1); |
946 | evtimer_start (&t1); |
793 | |
947 | |
794 | struct ev_signal sig; |
948 | struct ev_signal sig; |
795 | evw_init (&sig, scb, 65535); |
|
|
796 | evsignal_set (&sig, SIGQUIT); |
949 | evsignal_init (&sig, scb, SIGQUIT); |
797 | evsignal_start (&sig); |
950 | evsignal_start (&sig); |
798 | |
951 | |
799 | struct ev_check cw; |
952 | struct ev_check cw; |
800 | evw_init (&cw, gcb, 0); |
953 | evcheck_init (&cw, gcb); |
801 | evcheck_start (&cw); |
954 | evcheck_start (&cw); |
802 | |
955 | |
803 | struct ev_idle iw; |
956 | struct ev_idle iw; |
804 | evw_init (&iw, gcb, 0); |
957 | evidle_init (&iw, gcb); |
805 | evidle_start (&iw); |
958 | evidle_start (&iw); |
806 | |
959 | |
807 | ev_loop (0); |
960 | ev_loop (0); |
808 | |
961 | |
809 | return 0; |
962 | return 0; |