|
|
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> |
|
|
41 | #include <sys/types.h> |
|
|
42 | #include <sys/wait.h> |
11 | #include <sys/time.h> |
43 | #include <sys/time.h> |
12 | #include <time.h> |
44 | #include <time.h> |
13 | |
|
|
14 | #define HAVE_EPOLL 1 |
|
|
15 | |
45 | |
16 | #ifndef HAVE_MONOTONIC |
46 | #ifndef HAVE_MONOTONIC |
17 | # ifdef CLOCK_MONOTONIC |
47 | # ifdef CLOCK_MONOTONIC |
18 | # define HAVE_MONOTONIC 1 |
48 | # define HAVE_MONOTONIC 1 |
19 | # endif |
49 | # endif |
… | |
… | |
31 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
32 | #endif |
62 | #endif |
33 | |
63 | |
34 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
64 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
35 | #define MAX_BLOCKTIME 60. |
65 | #define MAX_BLOCKTIME 60. |
|
|
66 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
36 | |
67 | |
37 | #include "ev.h" |
68 | #include "ev.h" |
38 | |
69 | |
39 | typedef struct ev_watcher *W; |
70 | typedef struct ev_watcher *W; |
40 | typedef struct ev_watcher_list *WL; |
71 | typedef struct ev_watcher_list *WL; |
… | |
… | |
83 | |
114 | |
84 | #define array_needsize(base,cur,cnt,init) \ |
115 | #define array_needsize(base,cur,cnt,init) \ |
85 | if ((cnt) > cur) \ |
116 | if ((cnt) > cur) \ |
86 | { \ |
117 | { \ |
87 | int newcnt = cur ? cur << 1 : 16; \ |
118 | int newcnt = cur ? cur << 1 : 16; \ |
88 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
|
|
89 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
119 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
90 | init (base + cur, newcnt - cur); \ |
120 | init (base + cur, newcnt - cur); \ |
91 | cur = newcnt; \ |
121 | cur = newcnt; \ |
92 | } |
122 | } |
93 | |
123 | |
… | |
… | |
126 | static int pendingmax, pendingcnt; |
156 | static int pendingmax, pendingcnt; |
127 | |
157 | |
128 | static void |
158 | static void |
129 | event (W w, int events) |
159 | event (W w, int events) |
130 | { |
160 | { |
|
|
161 | if (w->active) |
|
|
162 | { |
131 | w->pending = ++pendingcnt; |
163 | w->pending = ++pendingcnt; |
132 | array_needsize (pendings, pendingmax, pendingcnt, ); |
164 | array_needsize (pendings, pendingmax, pendingcnt, ); |
133 | pendings [pendingcnt - 1].w = w; |
165 | pendings [pendingcnt - 1].w = w; |
134 | pendings [pendingcnt - 1].events = events; |
166 | pendings [pendingcnt - 1].events = events; |
|
|
167 | } |
135 | } |
168 | } |
136 | |
169 | |
137 | static void |
170 | static void |
138 | fd_event (int fd, int events) |
171 | fd_event (int fd, int events) |
139 | { |
172 | { |
… | |
… | |
154 | { |
187 | { |
155 | int i; |
188 | int i; |
156 | |
189 | |
157 | for (i = 0; i < eventcnt; ++i) |
190 | for (i = 0; i < eventcnt; ++i) |
158 | event (events [i], type); |
191 | event (events [i], type); |
|
|
192 | } |
|
|
193 | |
|
|
194 | /* called on EBADF to verify fds */ |
|
|
195 | static void |
|
|
196 | fd_recheck () |
|
|
197 | { |
|
|
198 | int fd; |
|
|
199 | |
|
|
200 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
201 | if (anfds [fd].wev) |
|
|
202 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
|
|
203 | while (anfds [fd].head) |
|
|
204 | evio_stop (anfds [fd].head); |
159 | } |
205 | } |
160 | |
206 | |
161 | /*****************************************************************************/ |
207 | /*****************************************************************************/ |
162 | |
208 | |
163 | static struct ev_timer **timers; |
209 | static struct ev_timer **timers; |
… | |
… | |
281 | /*****************************************************************************/ |
327 | /*****************************************************************************/ |
282 | |
328 | |
283 | static struct ev_idle **idles; |
329 | static struct ev_idle **idles; |
284 | static int idlemax, idlecnt; |
330 | static int idlemax, idlecnt; |
285 | |
331 | |
|
|
332 | static struct ev_prepare **prepares; |
|
|
333 | static int preparemax, preparecnt; |
|
|
334 | |
286 | static struct ev_check **checks; |
335 | static struct ev_check **checks; |
287 | static int checkmax, checkcnt; |
336 | static int checkmax, checkcnt; |
|
|
337 | |
|
|
338 | /*****************************************************************************/ |
|
|
339 | |
|
|
340 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
341 | static struct ev_signal childev; |
|
|
342 | |
|
|
343 | #ifndef WCONTINUED |
|
|
344 | # define WCONTINUED 0 |
|
|
345 | #endif |
|
|
346 | |
|
|
347 | static void |
|
|
348 | childcb (struct ev_signal *sw, int revents) |
|
|
349 | { |
|
|
350 | struct ev_child *w; |
|
|
351 | int pid, status; |
|
|
352 | |
|
|
353 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
|
|
354 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
355 | if (w->pid == pid || w->pid == -1) |
|
|
356 | { |
|
|
357 | w->status = status; |
|
|
358 | event ((W)w, EV_CHILD); |
|
|
359 | } |
|
|
360 | } |
288 | |
361 | |
289 | /*****************************************************************************/ |
362 | /*****************************************************************************/ |
290 | |
363 | |
291 | #if HAVE_EPOLL |
364 | #if HAVE_EPOLL |
292 | # include "ev_epoll.c" |
365 | # include "ev_epoll.c" |
… | |
… | |
322 | |
395 | |
323 | if (ev_method) |
396 | if (ev_method) |
324 | { |
397 | { |
325 | evw_init (&sigev, sigcb); |
398 | evw_init (&sigev, sigcb); |
326 | siginit (); |
399 | siginit (); |
|
|
400 | |
|
|
401 | evsignal_init (&childev, childcb, SIGCHLD); |
|
|
402 | evsignal_start (&childev); |
327 | } |
403 | } |
328 | |
404 | |
329 | return ev_method; |
405 | return ev_method; |
330 | } |
406 | } |
331 | |
407 | |
… | |
… | |
384 | } |
460 | } |
385 | |
461 | |
386 | static void |
462 | static void |
387 | call_pending () |
463 | call_pending () |
388 | { |
464 | { |
389 | int i; |
465 | while (pendingcnt) |
390 | |
|
|
391 | for (i = 0; i < pendingcnt; ++i) |
|
|
392 | { |
466 | { |
393 | ANPENDING *p = pendings + i; |
467 | ANPENDING *p = pendings + --pendingcnt; |
394 | |
468 | |
395 | if (p->w) |
469 | if (p->w) |
396 | { |
470 | { |
397 | p->w->pending = 0; |
471 | p->w->pending = 0; |
398 | p->w->cb (p->w, p->events); |
472 | p->w->cb (p->w, p->events); |
399 | } |
473 | } |
400 | } |
474 | } |
401 | |
|
|
402 | pendingcnt = 0; |
|
|
403 | } |
475 | } |
404 | |
476 | |
405 | static void |
477 | static void |
406 | timers_reify () |
478 | timers_reify () |
407 | { |
479 | { |
408 | while (timercnt && timers [0]->at <= now) |
480 | while (timercnt && timers [0]->at <= now) |
409 | { |
481 | { |
410 | struct ev_timer *w = timers [0]; |
482 | struct ev_timer *w = timers [0]; |
|
|
483 | |
|
|
484 | event ((W)w, EV_TIMEOUT); |
411 | |
485 | |
412 | /* first reschedule or stop timer */ |
486 | /* first reschedule or stop timer */ |
413 | if (w->repeat) |
487 | if (w->repeat) |
414 | { |
488 | { |
415 | w->at = now + w->repeat; |
489 | w->at = now + w->repeat; |
416 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
490 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
417 | downheap ((WT *)timers, timercnt, 0); |
491 | downheap ((WT *)timers, timercnt, 0); |
418 | } |
492 | } |
419 | else |
493 | else |
420 | evtimer_stop (w); /* nonrepeating: stop timer */ |
494 | evtimer_stop (w); /* nonrepeating: stop timer */ |
421 | |
|
|
422 | event ((W)w, EV_TIMEOUT); |
|
|
423 | } |
495 | } |
424 | } |
496 | } |
425 | |
497 | |
426 | static void |
498 | static void |
427 | periodics_reify () |
499 | periodics_reify () |
… | |
… | |
514 | void ev_loop (int flags) |
586 | void ev_loop (int flags) |
515 | { |
587 | { |
516 | double block; |
588 | double block; |
517 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
589 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
518 | |
590 | |
519 | if (checkcnt) |
|
|
520 | { |
|
|
521 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
522 | call_pending (); |
|
|
523 | } |
|
|
524 | |
|
|
525 | do |
591 | do |
526 | { |
592 | { |
|
|
593 | /* queue check watchers (and execute them) */ |
|
|
594 | if (preparecnt) |
|
|
595 | { |
|
|
596 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
|
|
597 | call_pending (); |
|
|
598 | } |
|
|
599 | |
527 | /* update fd-related kernel structures */ |
600 | /* update fd-related kernel structures */ |
528 | fd_reify (); |
601 | fd_reify (); |
529 | |
602 | |
530 | /* calculate blocking time */ |
603 | /* calculate blocking time */ |
531 | |
604 | |
532 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
605 | /* we only need this for !monotonic clockor timers, but as we basically |
|
|
606 | always have timers, we just calculate it always */ |
533 | ev_now = ev_time (); |
607 | ev_now = ev_time (); |
534 | |
608 | |
535 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
609 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
536 | block = 0.; |
610 | block = 0.; |
537 | else |
611 | else |
… | |
… | |
557 | |
631 | |
558 | /* update ev_now, do magic */ |
632 | /* update ev_now, do magic */ |
559 | time_update (); |
633 | time_update (); |
560 | |
634 | |
561 | /* queue pending timers and reschedule them */ |
635 | /* queue pending timers and reschedule them */ |
|
|
636 | timers_reify (); /* relative timers called last */ |
562 | periodics_reify (); /* absolute timers first */ |
637 | periodics_reify (); /* absolute timers called first */ |
563 | timers_reify (); /* relative timers second */ |
|
|
564 | |
638 | |
565 | /* queue idle watchers unless io or timers are pending */ |
639 | /* queue idle watchers unless io or timers are pending */ |
566 | if (!pendingcnt) |
640 | if (!pendingcnt) |
567 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
641 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
568 | |
642 | |
569 | /* queue check and possibly idle watchers */ |
643 | /* queue check watchers, to be executed first */ |
|
|
644 | if (checkcnt) |
570 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
645 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
571 | |
646 | |
572 | call_pending (); |
647 | call_pending (); |
573 | } |
648 | } |
574 | while (!ev_loop_done); |
649 | while (!ev_loop_done); |
575 | |
650 | |
… | |
… | |
600 | head = &(*head)->next; |
675 | head = &(*head)->next; |
601 | } |
676 | } |
602 | } |
677 | } |
603 | |
678 | |
604 | static void |
679 | static void |
|
|
680 | ev_clear (W w) |
|
|
681 | { |
|
|
682 | if (w->pending) |
|
|
683 | { |
|
|
684 | pendings [w->pending - 1].w = 0; |
|
|
685 | w->pending = 0; |
|
|
686 | } |
|
|
687 | } |
|
|
688 | |
|
|
689 | static void |
605 | ev_start (W w, int active) |
690 | ev_start (W w, int active) |
606 | { |
691 | { |
607 | w->pending = 0; |
|
|
608 | w->active = active; |
692 | w->active = active; |
609 | } |
693 | } |
610 | |
694 | |
611 | static void |
695 | static void |
612 | ev_stop (W w) |
696 | ev_stop (W w) |
613 | { |
697 | { |
614 | if (w->pending) |
|
|
615 | pendings [w->pending - 1].w = 0; |
|
|
616 | |
|
|
617 | w->active = 0; |
698 | w->active = 0; |
618 | } |
699 | } |
619 | |
700 | |
620 | /*****************************************************************************/ |
701 | /*****************************************************************************/ |
621 | |
702 | |
… | |
… | |
637 | } |
718 | } |
638 | |
719 | |
639 | void |
720 | void |
640 | evio_stop (struct ev_io *w) |
721 | evio_stop (struct ev_io *w) |
641 | { |
722 | { |
|
|
723 | ev_clear ((W)w); |
642 | if (!ev_is_active (w)) |
724 | if (!ev_is_active (w)) |
643 | return; |
725 | return; |
644 | |
726 | |
645 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
727 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
646 | ev_stop ((W)w); |
728 | ev_stop ((W)w); |
647 | |
729 | |
648 | ++fdchangecnt; |
730 | ++fdchangecnt; |
649 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
731 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
650 | fdchanges [fdchangecnt - 1] = w->fd; |
732 | fdchanges [fdchangecnt - 1] = w->fd; |
651 | } |
733 | } |
652 | |
|
|
653 | |
734 | |
654 | void |
735 | void |
655 | evtimer_start (struct ev_timer *w) |
736 | evtimer_start (struct ev_timer *w) |
656 | { |
737 | { |
657 | if (ev_is_active (w)) |
738 | if (ev_is_active (w)) |
… | |
… | |
668 | } |
749 | } |
669 | |
750 | |
670 | void |
751 | void |
671 | evtimer_stop (struct ev_timer *w) |
752 | evtimer_stop (struct ev_timer *w) |
672 | { |
753 | { |
|
|
754 | ev_clear ((W)w); |
673 | if (!ev_is_active (w)) |
755 | if (!ev_is_active (w)) |
674 | return; |
756 | return; |
675 | |
757 | |
676 | if (w->active < timercnt--) |
758 | if (w->active < timercnt--) |
677 | { |
759 | { |
… | |
… | |
720 | } |
802 | } |
721 | |
803 | |
722 | void |
804 | void |
723 | evperiodic_stop (struct ev_periodic *w) |
805 | evperiodic_stop (struct ev_periodic *w) |
724 | { |
806 | { |
|
|
807 | ev_clear ((W)w); |
725 | if (!ev_is_active (w)) |
808 | if (!ev_is_active (w)) |
726 | return; |
809 | return; |
727 | |
810 | |
728 | if (w->active < periodiccnt--) |
811 | if (w->active < periodiccnt--) |
729 | { |
812 | { |
… | |
… | |
755 | } |
838 | } |
756 | |
839 | |
757 | void |
840 | void |
758 | evsignal_stop (struct ev_signal *w) |
841 | evsignal_stop (struct ev_signal *w) |
759 | { |
842 | { |
|
|
843 | ev_clear ((W)w); |
760 | if (!ev_is_active (w)) |
844 | if (!ev_is_active (w)) |
761 | return; |
845 | return; |
762 | |
846 | |
763 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
847 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
764 | ev_stop ((W)w); |
848 | ev_stop ((W)w); |
… | |
… | |
777 | idles [idlecnt - 1] = w; |
861 | idles [idlecnt - 1] = w; |
778 | } |
862 | } |
779 | |
863 | |
780 | void evidle_stop (struct ev_idle *w) |
864 | void evidle_stop (struct ev_idle *w) |
781 | { |
865 | { |
|
|
866 | ev_clear ((W)w); |
|
|
867 | if (ev_is_active (w)) |
|
|
868 | return; |
|
|
869 | |
782 | idles [w->active - 1] = idles [--idlecnt]; |
870 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
871 | ev_stop ((W)w); |
|
|
872 | } |
|
|
873 | |
|
|
874 | void evprepare_start (struct ev_prepare *w) |
|
|
875 | { |
|
|
876 | if (ev_is_active (w)) |
|
|
877 | return; |
|
|
878 | |
|
|
879 | ev_start ((W)w, ++preparecnt); |
|
|
880 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
881 | prepares [preparecnt - 1] = w; |
|
|
882 | } |
|
|
883 | |
|
|
884 | void evprepare_stop (struct ev_prepare *w) |
|
|
885 | { |
|
|
886 | ev_clear ((W)w); |
|
|
887 | if (ev_is_active (w)) |
|
|
888 | return; |
|
|
889 | |
|
|
890 | prepares [w->active - 1] = prepares [--preparecnt]; |
783 | ev_stop ((W)w); |
891 | ev_stop ((W)w); |
784 | } |
892 | } |
785 | |
893 | |
786 | void evcheck_start (struct ev_check *w) |
894 | void evcheck_start (struct ev_check *w) |
787 | { |
895 | { |
… | |
… | |
793 | checks [checkcnt - 1] = w; |
901 | checks [checkcnt - 1] = w; |
794 | } |
902 | } |
795 | |
903 | |
796 | void evcheck_stop (struct ev_check *w) |
904 | void evcheck_stop (struct ev_check *w) |
797 | { |
905 | { |
|
|
906 | ev_clear ((W)w); |
|
|
907 | if (ev_is_active (w)) |
|
|
908 | return; |
|
|
909 | |
798 | checks [w->active - 1] = checks [--checkcnt]; |
910 | checks [w->active - 1] = checks [--checkcnt]; |
799 | ev_stop ((W)w); |
911 | ev_stop ((W)w); |
|
|
912 | } |
|
|
913 | |
|
|
914 | void evchild_start (struct ev_child *w) |
|
|
915 | { |
|
|
916 | if (ev_is_active (w)) |
|
|
917 | return; |
|
|
918 | |
|
|
919 | ev_start ((W)w, 1); |
|
|
920 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
921 | } |
|
|
922 | |
|
|
923 | void evchild_stop (struct ev_child *w) |
|
|
924 | { |
|
|
925 | ev_clear ((W)w); |
|
|
926 | if (ev_is_active (w)) |
|
|
927 | return; |
|
|
928 | |
|
|
929 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
930 | ev_stop ((W)w); |
|
|
931 | } |
|
|
932 | |
|
|
933 | /*****************************************************************************/ |
|
|
934 | |
|
|
935 | struct ev_once |
|
|
936 | { |
|
|
937 | struct ev_io io; |
|
|
938 | struct ev_timer to; |
|
|
939 | void (*cb)(int revents, void *arg); |
|
|
940 | void *arg; |
|
|
941 | }; |
|
|
942 | |
|
|
943 | static void |
|
|
944 | once_cb (struct ev_once *once, int revents) |
|
|
945 | { |
|
|
946 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
947 | void *arg = once->arg; |
|
|
948 | |
|
|
949 | evio_stop (&once->io); |
|
|
950 | evtimer_stop (&once->to); |
|
|
951 | free (once); |
|
|
952 | |
|
|
953 | cb (revents, arg); |
|
|
954 | } |
|
|
955 | |
|
|
956 | static void |
|
|
957 | once_cb_io (struct ev_io *w, int revents) |
|
|
958 | { |
|
|
959 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
960 | } |
|
|
961 | |
|
|
962 | static void |
|
|
963 | once_cb_to (struct ev_timer *w, int revents) |
|
|
964 | { |
|
|
965 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
966 | } |
|
|
967 | |
|
|
968 | void |
|
|
969 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
970 | { |
|
|
971 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
972 | |
|
|
973 | if (!once) |
|
|
974 | cb (EV_ERROR, arg); |
|
|
975 | else |
|
|
976 | { |
|
|
977 | once->cb = cb; |
|
|
978 | once->arg = arg; |
|
|
979 | |
|
|
980 | evw_init (&once->io, once_cb_io); |
|
|
981 | |
|
|
982 | if (fd >= 0) |
|
|
983 | { |
|
|
984 | evio_set (&once->io, fd, events); |
|
|
985 | evio_start (&once->io); |
|
|
986 | } |
|
|
987 | |
|
|
988 | evw_init (&once->to, once_cb_to); |
|
|
989 | |
|
|
990 | if (timeout >= 0.) |
|
|
991 | { |
|
|
992 | evtimer_set (&once->to, timeout, 0.); |
|
|
993 | evtimer_start (&once->to); |
|
|
994 | } |
|
|
995 | } |
800 | } |
996 | } |
801 | |
997 | |
802 | /*****************************************************************************/ |
998 | /*****************************************************************************/ |
803 | |
999 | |
804 | #if 0 |
1000 | #if 0 |