|
|
1 | /* |
|
|
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
|
|
3 | * All rights reserved. |
|
|
4 | * |
|
|
5 | * Redistribution and use in source and binary forms, with or without |
|
|
6 | * modification, are permitted provided that the following conditions are |
|
|
7 | * met: |
|
|
8 | * |
|
|
9 | * * Redistributions of source code must retain the above copyright |
|
|
10 | * notice, this list of conditions and the following disclaimer. |
|
|
11 | * |
|
|
12 | * * Redistributions in binary form must reproduce the above |
|
|
13 | * copyright notice, this list of conditions and the following |
|
|
14 | * disclaimer in the documentation and/or other materials provided |
|
|
15 | * with the distribution. |
|
|
16 | * |
|
|
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
|
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
|
19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
|
20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
|
21 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
|
22 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
|
23 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
|
27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
28 | */ |
|
|
29 | |
1 | #include <math.h> |
30 | #include <math.h> |
2 | #include <stdlib.h> |
31 | #include <stdlib.h> |
3 | #include <unistd.h> |
32 | #include <unistd.h> |
4 | #include <fcntl.h> |
33 | #include <fcntl.h> |
5 | #include <signal.h> |
34 | #include <signal.h> |
|
|
35 | #include <stddef.h> |
6 | |
36 | |
7 | #include <stdio.h> |
37 | #include <stdio.h> |
8 | |
38 | |
9 | #include <assert.h> |
39 | #include <assert.h> |
10 | #include <errno.h> |
40 | #include <errno.h> |
11 | #include <sys/time.h> |
41 | #include <sys/time.h> |
12 | #include <time.h> |
42 | #include <time.h> |
13 | |
43 | |
14 | #define HAVE_EPOLL 1 |
|
|
15 | |
|
|
16 | #ifndef HAVE_MONOTONIC |
44 | #ifndef HAVE_MONOTONIC |
17 | # ifdef CLOCK_MONOTONIC |
45 | # ifdef CLOCK_MONOTONIC |
18 | # define HAVE_MONOTONIC 1 |
46 | # define HAVE_MONOTONIC 1 |
19 | # endif |
47 | # endif |
20 | #endif |
48 | #endif |
… | |
… | |
33 | |
61 | |
34 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
62 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
35 | #define MAX_BLOCKTIME 60. |
63 | #define MAX_BLOCKTIME 60. |
36 | |
64 | |
37 | #include "ev.h" |
65 | #include "ev.h" |
38 | |
|
|
39 | struct ev_watcher { |
|
|
40 | EV_WATCHER (ev_watcher); |
|
|
41 | }; |
|
|
42 | |
|
|
43 | struct ev_watcher_list { |
|
|
44 | EV_WATCHER_LIST (ev_watcher_list); |
|
|
45 | }; |
|
|
46 | |
|
|
47 | struct ev_watcher_time { |
|
|
48 | EV_WATCHER_TIME (ev_watcher_time); |
|
|
49 | }; |
|
|
50 | |
66 | |
51 | typedef struct ev_watcher *W; |
67 | typedef struct ev_watcher *W; |
52 | typedef struct ev_watcher_list *WL; |
68 | typedef struct ev_watcher_list *WL; |
53 | typedef struct ev_watcher_time *WT; |
69 | typedef struct ev_watcher_time *WT; |
54 | |
70 | |
… | |
… | |
95 | |
111 | |
96 | #define array_needsize(base,cur,cnt,init) \ |
112 | #define array_needsize(base,cur,cnt,init) \ |
97 | if ((cnt) > cur) \ |
113 | if ((cnt) > cur) \ |
98 | { \ |
114 | { \ |
99 | int newcnt = cur ? cur << 1 : 16; \ |
115 | int newcnt = cur ? cur << 1 : 16; \ |
100 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
|
|
101 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
116 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
102 | init (base + cur, newcnt - cur); \ |
117 | init (base + cur, newcnt - cur); \ |
103 | cur = newcnt; \ |
118 | cur = newcnt; \ |
104 | } |
119 | } |
105 | |
120 | |
… | |
… | |
138 | static int pendingmax, pendingcnt; |
153 | static int pendingmax, pendingcnt; |
139 | |
154 | |
140 | static void |
155 | static void |
141 | event (W w, int events) |
156 | event (W w, int events) |
142 | { |
157 | { |
|
|
158 | if (w->active) |
|
|
159 | { |
143 | w->pending = ++pendingcnt; |
160 | w->pending = ++pendingcnt; |
144 | array_needsize (pendings, pendingmax, pendingcnt, ); |
161 | array_needsize (pendings, pendingmax, pendingcnt, ); |
145 | pendings [pendingcnt - 1].w = w; |
162 | pendings [pendingcnt - 1].w = w; |
146 | pendings [pendingcnt - 1].events = events; |
163 | pendings [pendingcnt - 1].events = events; |
|
|
164 | } |
147 | } |
165 | } |
148 | |
166 | |
149 | static void |
167 | static void |
150 | fd_event (int fd, int events) |
168 | fd_event (int fd, int events) |
151 | { |
169 | { |
… | |
… | |
166 | { |
184 | { |
167 | int i; |
185 | int i; |
168 | |
186 | |
169 | for (i = 0; i < eventcnt; ++i) |
187 | for (i = 0; i < eventcnt; ++i) |
170 | event (events [i], type); |
188 | event (events [i], type); |
|
|
189 | } |
|
|
190 | |
|
|
191 | /* called on EBADF to verify fds */ |
|
|
192 | static void |
|
|
193 | fd_recheck () |
|
|
194 | { |
|
|
195 | int fd; |
|
|
196 | |
|
|
197 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
198 | if (anfds [fd].wev) |
|
|
199 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
|
|
200 | while (anfds [fd].head) |
|
|
201 | evio_stop (anfds [fd].head); |
171 | } |
202 | } |
172 | |
203 | |
173 | /*****************************************************************************/ |
204 | /*****************************************************************************/ |
174 | |
205 | |
175 | static struct ev_timer **timers; |
206 | static struct ev_timer **timers; |
… | |
… | |
396 | } |
427 | } |
397 | |
428 | |
398 | static void |
429 | static void |
399 | call_pending () |
430 | call_pending () |
400 | { |
431 | { |
401 | int i; |
432 | while (pendingcnt) |
402 | |
|
|
403 | for (i = 0; i < pendingcnt; ++i) |
|
|
404 | { |
433 | { |
405 | ANPENDING *p = pendings + i; |
434 | ANPENDING *p = pendings + --pendingcnt; |
406 | |
435 | |
407 | if (p->w) |
436 | if (p->w) |
408 | { |
437 | { |
409 | p->w->pending = 0; |
438 | p->w->pending = 0; |
410 | p->w->cb (p->w, p->events); |
439 | p->w->cb (p->w, p->events); |
411 | } |
440 | } |
412 | } |
441 | } |
413 | |
|
|
414 | pendingcnt = 0; |
|
|
415 | } |
442 | } |
416 | |
443 | |
417 | static void |
444 | static void |
418 | timers_reify () |
445 | timers_reify () |
419 | { |
446 | { |
420 | while (timercnt && timers [0]->at <= now) |
447 | while (timercnt && timers [0]->at <= now) |
421 | { |
448 | { |
422 | struct ev_timer *w = timers [0]; |
449 | struct ev_timer *w = timers [0]; |
|
|
450 | |
|
|
451 | event ((W)w, EV_TIMEOUT); |
423 | |
452 | |
424 | /* first reschedule or stop timer */ |
453 | /* first reschedule or stop timer */ |
425 | if (w->repeat) |
454 | if (w->repeat) |
426 | { |
455 | { |
427 | w->at = now + w->repeat; |
456 | w->at = now + w->repeat; |
428 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
457 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
429 | downheap ((WT *)timers, timercnt, 0); |
458 | downheap ((WT *)timers, timercnt, 0); |
430 | } |
459 | } |
431 | else |
460 | else |
432 | evtimer_stop (w); /* nonrepeating: stop timer */ |
461 | evtimer_stop (w); /* nonrepeating: stop timer */ |
433 | |
|
|
434 | event ((W)w, EV_TIMEOUT); |
|
|
435 | } |
462 | } |
436 | } |
463 | } |
437 | |
464 | |
438 | static void |
465 | static void |
439 | periodics_reify () |
466 | periodics_reify () |
… | |
… | |
455 | event ((W)w, EV_TIMEOUT); |
482 | event ((W)w, EV_TIMEOUT); |
456 | } |
483 | } |
457 | } |
484 | } |
458 | |
485 | |
459 | static void |
486 | static void |
460 | time_jump (ev_tstamp diff) |
487 | periodics_reschedule (ev_tstamp diff) |
461 | { |
488 | { |
462 | int i; |
489 | int i; |
463 | |
490 | |
464 | /* adjust periodics */ |
491 | /* adjust periodics after time jump */ |
465 | for (i = 0; i < periodiccnt; ++i) |
492 | for (i = 0; i < periodiccnt; ++i) |
466 | { |
493 | { |
467 | struct ev_periodic *w = periodics [i]; |
494 | struct ev_periodic *w = periodics [i]; |
468 | |
495 | |
469 | if (w->interval) |
496 | if (w->interval) |
… | |
… | |
477 | |
504 | |
478 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
505 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
479 | } |
506 | } |
480 | } |
507 | } |
481 | } |
508 | } |
482 | |
|
|
483 | /* adjust timers. this is easy, as the offset is the same for all */ |
|
|
484 | for (i = 0; i < timercnt; ++i) |
|
|
485 | timers [i]->at += diff; |
|
|
486 | } |
509 | } |
487 | |
510 | |
488 | static void |
511 | static void |
489 | time_update () |
512 | time_update () |
490 | { |
513 | { |
… | |
… | |
505 | return; /* all is well */ |
528 | return; /* all is well */ |
506 | |
529 | |
507 | ev_now = ev_time (); |
530 | ev_now = ev_time (); |
508 | } |
531 | } |
509 | |
532 | |
510 | time_jump (diff - odiff); |
533 | periodics_reschedule (diff - odiff); |
|
|
534 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
511 | } |
535 | } |
512 | else |
536 | else |
513 | { |
537 | { |
514 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
538 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
515 | time_jump (ev_now - now); |
539 | { |
|
|
540 | periodics_reschedule (ev_now - now); |
|
|
541 | |
|
|
542 | /* adjust timers. this is easy, as the offset is the same for all */ |
|
|
543 | for (i = 0; i < timercnt; ++i) |
|
|
544 | timers [i]->at += diff; |
|
|
545 | } |
516 | |
546 | |
517 | now = ev_now; |
547 | now = ev_now; |
518 | } |
548 | } |
519 | } |
549 | } |
520 | |
550 | |
521 | int ev_loop_done; |
551 | int ev_loop_done; |
522 | |
552 | |
523 | void ev_loop (int flags) |
553 | void ev_loop (int flags) |
524 | { |
554 | { |
525 | double block; |
555 | double block; |
526 | ev_loop_done = flags & EVLOOP_ONESHOT; |
556 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
527 | |
557 | |
528 | if (checkcnt) |
558 | if (checkcnt) |
529 | { |
559 | { |
530 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
560 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
531 | call_pending (); |
561 | call_pending (); |
… | |
… | |
547 | { |
577 | { |
548 | block = MAX_BLOCKTIME; |
578 | block = MAX_BLOCKTIME; |
549 | |
579 | |
550 | if (timercnt) |
580 | if (timercnt) |
551 | { |
581 | { |
552 | ev_tstamp to = timers [0]->at - get_clock () + method_fudge; |
582 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
553 | if (block > to) block = to; |
583 | if (block > to) block = to; |
554 | } |
584 | } |
555 | |
585 | |
556 | if (periodiccnt) |
586 | if (periodiccnt) |
557 | { |
587 | { |
… | |
… | |
579 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
609 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
580 | |
610 | |
581 | call_pending (); |
611 | call_pending (); |
582 | } |
612 | } |
583 | while (!ev_loop_done); |
613 | while (!ev_loop_done); |
|
|
614 | |
|
|
615 | if (ev_loop_done != 2) |
|
|
616 | ev_loop_done = 0; |
584 | } |
617 | } |
585 | |
618 | |
586 | /*****************************************************************************/ |
619 | /*****************************************************************************/ |
587 | |
620 | |
588 | static void |
621 | static void |
… | |
… | |
606 | head = &(*head)->next; |
639 | head = &(*head)->next; |
607 | } |
640 | } |
608 | } |
641 | } |
609 | |
642 | |
610 | static void |
643 | static void |
|
|
644 | ev_clear (W w) |
|
|
645 | { |
|
|
646 | if (w->pending) |
|
|
647 | { |
|
|
648 | pendings [w->pending - 1].w = 0; |
|
|
649 | w->pending = 0; |
|
|
650 | } |
|
|
651 | } |
|
|
652 | |
|
|
653 | static void |
611 | ev_start (W w, int active) |
654 | ev_start (W w, int active) |
612 | { |
655 | { |
613 | w->pending = 0; |
|
|
614 | w->active = active; |
656 | w->active = active; |
615 | } |
657 | } |
616 | |
658 | |
617 | static void |
659 | static void |
618 | ev_stop (W w) |
660 | ev_stop (W w) |
619 | { |
661 | { |
620 | if (w->pending) |
|
|
621 | pendings [w->pending - 1].w = 0; |
|
|
622 | |
|
|
623 | w->active = 0; |
662 | w->active = 0; |
624 | } |
663 | } |
625 | |
664 | |
626 | /*****************************************************************************/ |
665 | /*****************************************************************************/ |
627 | |
666 | |
… | |
… | |
643 | } |
682 | } |
644 | |
683 | |
645 | void |
684 | void |
646 | evio_stop (struct ev_io *w) |
685 | evio_stop (struct ev_io *w) |
647 | { |
686 | { |
|
|
687 | ev_clear ((W)w); |
648 | if (!ev_is_active (w)) |
688 | if (!ev_is_active (w)) |
649 | return; |
689 | return; |
650 | |
690 | |
651 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
691 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
652 | ev_stop ((W)w); |
692 | ev_stop ((W)w); |
… | |
… | |
654 | ++fdchangecnt; |
694 | ++fdchangecnt; |
655 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
695 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
656 | fdchanges [fdchangecnt - 1] = w->fd; |
696 | fdchanges [fdchangecnt - 1] = w->fd; |
657 | } |
697 | } |
658 | |
698 | |
659 | |
|
|
660 | void |
699 | void |
661 | evtimer_start (struct ev_timer *w) |
700 | evtimer_start (struct ev_timer *w) |
662 | { |
701 | { |
663 | if (ev_is_active (w)) |
702 | if (ev_is_active (w)) |
664 | return; |
703 | return; |
665 | |
704 | |
666 | w->at += now; |
705 | w->at += now; |
|
|
706 | |
|
|
707 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
667 | |
708 | |
668 | ev_start ((W)w, ++timercnt); |
709 | ev_start ((W)w, ++timercnt); |
669 | array_needsize (timers, timermax, timercnt, ); |
710 | array_needsize (timers, timermax, timercnt, ); |
670 | timers [timercnt - 1] = w; |
711 | timers [timercnt - 1] = w; |
671 | upheap ((WT *)timers, timercnt - 1); |
712 | upheap ((WT *)timers, timercnt - 1); |
672 | } |
713 | } |
673 | |
714 | |
674 | void |
715 | void |
675 | evtimer_stop (struct ev_timer *w) |
716 | evtimer_stop (struct ev_timer *w) |
676 | { |
717 | { |
|
|
718 | ev_clear ((W)w); |
677 | if (!ev_is_active (w)) |
719 | if (!ev_is_active (w)) |
678 | return; |
720 | return; |
679 | |
721 | |
680 | if (w->active < timercnt--) |
722 | if (w->active < timercnt--) |
681 | { |
723 | { |
682 | timers [w->active - 1] = timers [timercnt]; |
724 | timers [w->active - 1] = timers [timercnt]; |
683 | downheap ((WT *)timers, timercnt, w->active - 1); |
725 | downheap ((WT *)timers, timercnt, w->active - 1); |
684 | } |
726 | } |
685 | |
727 | |
|
|
728 | w->at = w->repeat; |
|
|
729 | |
686 | ev_stop ((W)w); |
730 | ev_stop ((W)w); |
|
|
731 | } |
|
|
732 | |
|
|
733 | void |
|
|
734 | evtimer_again (struct ev_timer *w) |
|
|
735 | { |
|
|
736 | if (ev_is_active (w)) |
|
|
737 | { |
|
|
738 | if (w->repeat) |
|
|
739 | { |
|
|
740 | w->at = now + w->repeat; |
|
|
741 | downheap ((WT *)timers, timercnt, w->active - 1); |
|
|
742 | } |
|
|
743 | else |
|
|
744 | evtimer_stop (w); |
|
|
745 | } |
|
|
746 | else if (w->repeat) |
|
|
747 | evtimer_start (w); |
687 | } |
748 | } |
688 | |
749 | |
689 | void |
750 | void |
690 | evperiodic_start (struct ev_periodic *w) |
751 | evperiodic_start (struct ev_periodic *w) |
691 | { |
752 | { |
692 | if (ev_is_active (w)) |
753 | if (ev_is_active (w)) |
693 | return; |
754 | return; |
|
|
755 | |
|
|
756 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
694 | |
757 | |
695 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
758 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
696 | if (w->interval) |
759 | if (w->interval) |
697 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
760 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
698 | |
761 | |
… | |
… | |
703 | } |
766 | } |
704 | |
767 | |
705 | void |
768 | void |
706 | evperiodic_stop (struct ev_periodic *w) |
769 | evperiodic_stop (struct ev_periodic *w) |
707 | { |
770 | { |
|
|
771 | ev_clear ((W)w); |
708 | if (!ev_is_active (w)) |
772 | if (!ev_is_active (w)) |
709 | return; |
773 | return; |
710 | |
774 | |
711 | if (w->active < periodiccnt--) |
775 | if (w->active < periodiccnt--) |
712 | { |
776 | { |
… | |
… | |
738 | } |
802 | } |
739 | |
803 | |
740 | void |
804 | void |
741 | evsignal_stop (struct ev_signal *w) |
805 | evsignal_stop (struct ev_signal *w) |
742 | { |
806 | { |
|
|
807 | ev_clear ((W)w); |
743 | if (!ev_is_active (w)) |
808 | if (!ev_is_active (w)) |
744 | return; |
809 | return; |
745 | |
810 | |
746 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
811 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
747 | ev_stop ((W)w); |
812 | ev_stop ((W)w); |
… | |
… | |
760 | idles [idlecnt - 1] = w; |
825 | idles [idlecnt - 1] = w; |
761 | } |
826 | } |
762 | |
827 | |
763 | void evidle_stop (struct ev_idle *w) |
828 | void evidle_stop (struct ev_idle *w) |
764 | { |
829 | { |
|
|
830 | ev_clear ((W)w); |
|
|
831 | if (ev_is_active (w)) |
|
|
832 | return; |
|
|
833 | |
765 | idles [w->active - 1] = idles [--idlecnt]; |
834 | idles [w->active - 1] = idles [--idlecnt]; |
766 | ev_stop ((W)w); |
835 | ev_stop ((W)w); |
767 | } |
836 | } |
768 | |
837 | |
769 | void evcheck_start (struct ev_check *w) |
838 | void evcheck_start (struct ev_check *w) |
… | |
… | |
776 | checks [checkcnt - 1] = w; |
845 | checks [checkcnt - 1] = w; |
777 | } |
846 | } |
778 | |
847 | |
779 | void evcheck_stop (struct ev_check *w) |
848 | void evcheck_stop (struct ev_check *w) |
780 | { |
849 | { |
|
|
850 | ev_clear ((W)w); |
|
|
851 | if (ev_is_active (w)) |
|
|
852 | return; |
|
|
853 | |
781 | checks [w->active - 1] = checks [--checkcnt]; |
854 | checks [w->active - 1] = checks [--checkcnt]; |
782 | ev_stop ((W)w); |
855 | ev_stop ((W)w); |
783 | } |
856 | } |
784 | |
857 | |
785 | /*****************************************************************************/ |
858 | /*****************************************************************************/ |
786 | |
859 | |
|
|
860 | struct ev_once |
|
|
861 | { |
|
|
862 | struct ev_io io; |
|
|
863 | struct ev_timer to; |
|
|
864 | void (*cb)(int revents, void *arg); |
|
|
865 | void *arg; |
|
|
866 | }; |
|
|
867 | |
|
|
868 | static void |
|
|
869 | once_cb (struct ev_once *once, int revents) |
|
|
870 | { |
|
|
871 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
872 | void *arg = once->arg; |
|
|
873 | |
|
|
874 | evio_stop (&once->io); |
|
|
875 | evtimer_stop (&once->to); |
|
|
876 | free (once); |
|
|
877 | |
|
|
878 | cb (revents, arg); |
|
|
879 | } |
|
|
880 | |
|
|
881 | static void |
|
|
882 | once_cb_io (struct ev_io *w, int revents) |
|
|
883 | { |
|
|
884 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
885 | } |
|
|
886 | |
|
|
887 | static void |
|
|
888 | once_cb_to (struct ev_timer *w, int revents) |
|
|
889 | { |
|
|
890 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
891 | } |
|
|
892 | |
|
|
893 | void |
|
|
894 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
895 | { |
|
|
896 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
897 | |
|
|
898 | if (!once) |
|
|
899 | cb (EV_ERROR, arg); |
|
|
900 | else |
|
|
901 | { |
|
|
902 | once->cb = cb; |
|
|
903 | once->arg = arg; |
|
|
904 | |
|
|
905 | evw_init (&once->io, once_cb_io); |
|
|
906 | |
|
|
907 | if (fd >= 0) |
|
|
908 | { |
|
|
909 | evio_set (&once->io, fd, events); |
|
|
910 | evio_start (&once->io); |
|
|
911 | } |
|
|
912 | |
|
|
913 | evw_init (&once->to, once_cb_to); |
|
|
914 | |
|
|
915 | if (timeout >= 0.) |
|
|
916 | { |
|
|
917 | evtimer_set (&once->to, timeout, 0.); |
|
|
918 | evtimer_start (&once->to); |
|
|
919 | } |
|
|
920 | } |
|
|
921 | } |
|
|
922 | |
|
|
923 | /*****************************************************************************/ |
|
|
924 | |
787 | #if 1 |
925 | #if 0 |
788 | |
926 | |
789 | struct ev_io wio; |
927 | struct ev_io wio; |
790 | |
928 | |
791 | static void |
929 | static void |
792 | sin_cb (struct ev_io *w, int revents) |
930 | sin_cb (struct ev_io *w, int revents) |