|
|
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; |
… | |
… | |
292 | |
323 | |
293 | /*****************************************************************************/ |
324 | /*****************************************************************************/ |
294 | |
325 | |
295 | static struct ev_idle **idles; |
326 | static struct ev_idle **idles; |
296 | static int idlemax, idlecnt; |
327 | static int idlemax, idlecnt; |
|
|
328 | |
|
|
329 | static struct ev_prepare **prepares; |
|
|
330 | static int preparemax, preparecnt; |
297 | |
331 | |
298 | static struct ev_check **checks; |
332 | static struct ev_check **checks; |
299 | static int checkmax, checkcnt; |
333 | static int checkmax, checkcnt; |
300 | |
334 | |
301 | /*****************************************************************************/ |
335 | /*****************************************************************************/ |
… | |
… | |
396 | } |
430 | } |
397 | |
431 | |
398 | static void |
432 | static void |
399 | call_pending () |
433 | call_pending () |
400 | { |
434 | { |
401 | int i; |
435 | while (pendingcnt) |
402 | |
|
|
403 | for (i = 0; i < pendingcnt; ++i) |
|
|
404 | { |
436 | { |
405 | ANPENDING *p = pendings + i; |
437 | ANPENDING *p = pendings + --pendingcnt; |
406 | |
438 | |
407 | if (p->w) |
439 | if (p->w) |
408 | { |
440 | { |
409 | p->w->pending = 0; |
441 | p->w->pending = 0; |
410 | p->w->cb (p->w, p->events); |
442 | p->w->cb (p->w, p->events); |
411 | } |
443 | } |
412 | } |
444 | } |
413 | |
|
|
414 | pendingcnt = 0; |
|
|
415 | } |
445 | } |
416 | |
446 | |
417 | static void |
447 | static void |
418 | timers_reify () |
448 | timers_reify () |
419 | { |
449 | { |
420 | while (timercnt && timers [0]->at <= now) |
450 | while (timercnt && timers [0]->at <= now) |
421 | { |
451 | { |
422 | struct ev_timer *w = timers [0]; |
452 | struct ev_timer *w = timers [0]; |
|
|
453 | |
|
|
454 | event ((W)w, EV_TIMEOUT); |
423 | |
455 | |
424 | /* first reschedule or stop timer */ |
456 | /* first reschedule or stop timer */ |
425 | if (w->repeat) |
457 | if (w->repeat) |
426 | { |
458 | { |
427 | w->at = now + w->repeat; |
459 | w->at = now + w->repeat; |
428 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
460 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
429 | downheap ((WT *)timers, timercnt, 0); |
461 | downheap ((WT *)timers, timercnt, 0); |
430 | } |
462 | } |
431 | else |
463 | else |
432 | evtimer_stop (w); /* nonrepeating: stop timer */ |
464 | evtimer_stop (w); /* nonrepeating: stop timer */ |
433 | |
|
|
434 | event ((W)w, EV_TIMEOUT); |
|
|
435 | } |
465 | } |
436 | } |
466 | } |
437 | |
467 | |
438 | static void |
468 | static void |
439 | periodics_reify () |
469 | periodics_reify () |
… | |
… | |
455 | event ((W)w, EV_TIMEOUT); |
485 | event ((W)w, EV_TIMEOUT); |
456 | } |
486 | } |
457 | } |
487 | } |
458 | |
488 | |
459 | static void |
489 | static void |
460 | time_jump (ev_tstamp diff) |
490 | periodics_reschedule (ev_tstamp diff) |
461 | { |
491 | { |
462 | int i; |
492 | int i; |
463 | |
493 | |
464 | /* adjust periodics */ |
494 | /* adjust periodics after time jump */ |
465 | for (i = 0; i < periodiccnt; ++i) |
495 | for (i = 0; i < periodiccnt; ++i) |
466 | { |
496 | { |
467 | struct ev_periodic *w = periodics [i]; |
497 | struct ev_periodic *w = periodics [i]; |
468 | |
498 | |
469 | if (w->interval) |
499 | if (w->interval) |
… | |
… | |
477 | |
507 | |
478 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
508 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
479 | } |
509 | } |
480 | } |
510 | } |
481 | } |
511 | } |
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 | } |
512 | } |
487 | |
513 | |
488 | static void |
514 | static void |
489 | time_update () |
515 | time_update () |
490 | { |
516 | { |
… | |
… | |
505 | return; /* all is well */ |
531 | return; /* all is well */ |
506 | |
532 | |
507 | ev_now = ev_time (); |
533 | ev_now = ev_time (); |
508 | } |
534 | } |
509 | |
535 | |
510 | time_jump (diff - odiff); |
536 | periodics_reschedule (diff - odiff); |
|
|
537 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
511 | } |
538 | } |
512 | else |
539 | else |
513 | { |
540 | { |
514 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
541 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
515 | time_jump (ev_now - now); |
542 | { |
|
|
543 | periodics_reschedule (ev_now - now); |
|
|
544 | |
|
|
545 | /* adjust timers. this is easy, as the offset is the same for all */ |
|
|
546 | for (i = 0; i < timercnt; ++i) |
|
|
547 | timers [i]->at += diff; |
|
|
548 | } |
516 | |
549 | |
517 | now = ev_now; |
550 | now = ev_now; |
518 | } |
551 | } |
519 | } |
552 | } |
520 | |
553 | |
521 | int ev_loop_done; |
554 | int ev_loop_done; |
522 | |
555 | |
523 | void ev_loop (int flags) |
556 | void ev_loop (int flags) |
524 | { |
557 | { |
525 | double block; |
558 | double block; |
526 | ev_loop_done = flags & EVLOOP_ONESHOT; |
559 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
527 | |
|
|
528 | if (checkcnt) |
|
|
529 | { |
|
|
530 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
531 | call_pending (); |
|
|
532 | } |
|
|
533 | |
560 | |
534 | do |
561 | do |
535 | { |
562 | { |
|
|
563 | /* queue check watchers (and execute them) */ |
|
|
564 | if (preparecnt) |
|
|
565 | { |
|
|
566 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
|
|
567 | call_pending (); |
|
|
568 | } |
|
|
569 | |
536 | /* update fd-related kernel structures */ |
570 | /* update fd-related kernel structures */ |
537 | fd_reify (); |
571 | fd_reify (); |
538 | |
572 | |
539 | /* calculate blocking time */ |
573 | /* calculate blocking time */ |
540 | |
574 | |
541 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
575 | /* we only need this for !monotonic clockor timers, but as we basically |
|
|
576 | always have timers, we just calculate it always */ |
542 | ev_now = ev_time (); |
577 | ev_now = ev_time (); |
543 | |
578 | |
544 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
579 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
545 | block = 0.; |
580 | block = 0.; |
546 | else |
581 | else |
547 | { |
582 | { |
548 | block = MAX_BLOCKTIME; |
583 | block = MAX_BLOCKTIME; |
549 | |
584 | |
550 | if (timercnt) |
585 | if (timercnt) |
551 | { |
586 | { |
552 | ev_tstamp to = timers [0]->at - get_clock () + method_fudge; |
587 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
553 | if (block > to) block = to; |
588 | if (block > to) block = to; |
554 | } |
589 | } |
555 | |
590 | |
556 | if (periodiccnt) |
591 | if (periodiccnt) |
557 | { |
592 | { |
… | |
… | |
566 | |
601 | |
567 | /* update ev_now, do magic */ |
602 | /* update ev_now, do magic */ |
568 | time_update (); |
603 | time_update (); |
569 | |
604 | |
570 | /* queue pending timers and reschedule them */ |
605 | /* queue pending timers and reschedule them */ |
|
|
606 | timers_reify (); /* relative timers called last */ |
571 | periodics_reify (); /* absolute timers first */ |
607 | periodics_reify (); /* absolute timers called first */ |
572 | timers_reify (); /* relative timers second */ |
|
|
573 | |
608 | |
574 | /* queue idle watchers unless io or timers are pending */ |
609 | /* queue idle watchers unless io or timers are pending */ |
575 | if (!pendingcnt) |
610 | if (!pendingcnt) |
576 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
611 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
577 | |
612 | |
578 | /* queue check and possibly idle watchers */ |
613 | /* queue check watchers, to be executed first */ |
|
|
614 | if (checkcnt) |
579 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
615 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
580 | |
616 | |
581 | call_pending (); |
617 | call_pending (); |
582 | } |
618 | } |
583 | while (!ev_loop_done); |
619 | while (!ev_loop_done); |
|
|
620 | |
|
|
621 | if (ev_loop_done != 2) |
|
|
622 | ev_loop_done = 0; |
584 | } |
623 | } |
585 | |
624 | |
586 | /*****************************************************************************/ |
625 | /*****************************************************************************/ |
587 | |
626 | |
588 | static void |
627 | static void |
… | |
… | |
606 | head = &(*head)->next; |
645 | head = &(*head)->next; |
607 | } |
646 | } |
608 | } |
647 | } |
609 | |
648 | |
610 | static void |
649 | static void |
|
|
650 | ev_clear (W w) |
|
|
651 | { |
|
|
652 | if (w->pending) |
|
|
653 | { |
|
|
654 | pendings [w->pending - 1].w = 0; |
|
|
655 | w->pending = 0; |
|
|
656 | } |
|
|
657 | } |
|
|
658 | |
|
|
659 | static void |
611 | ev_start (W w, int active) |
660 | ev_start (W w, int active) |
612 | { |
661 | { |
613 | w->pending = 0; |
|
|
614 | w->active = active; |
662 | w->active = active; |
615 | } |
663 | } |
616 | |
664 | |
617 | static void |
665 | static void |
618 | ev_stop (W w) |
666 | ev_stop (W w) |
619 | { |
667 | { |
620 | if (w->pending) |
|
|
621 | pendings [w->pending - 1].w = 0; |
|
|
622 | |
|
|
623 | w->active = 0; |
668 | w->active = 0; |
624 | } |
669 | } |
625 | |
670 | |
626 | /*****************************************************************************/ |
671 | /*****************************************************************************/ |
627 | |
672 | |
… | |
… | |
643 | } |
688 | } |
644 | |
689 | |
645 | void |
690 | void |
646 | evio_stop (struct ev_io *w) |
691 | evio_stop (struct ev_io *w) |
647 | { |
692 | { |
|
|
693 | ev_clear ((W)w); |
648 | if (!ev_is_active (w)) |
694 | if (!ev_is_active (w)) |
649 | return; |
695 | return; |
650 | |
696 | |
651 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
697 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
652 | ev_stop ((W)w); |
698 | ev_stop ((W)w); |
… | |
… | |
654 | ++fdchangecnt; |
700 | ++fdchangecnt; |
655 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
701 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
656 | fdchanges [fdchangecnt - 1] = w->fd; |
702 | fdchanges [fdchangecnt - 1] = w->fd; |
657 | } |
703 | } |
658 | |
704 | |
659 | |
|
|
660 | void |
705 | void |
661 | evtimer_start (struct ev_timer *w) |
706 | evtimer_start (struct ev_timer *w) |
662 | { |
707 | { |
663 | if (ev_is_active (w)) |
708 | if (ev_is_active (w)) |
664 | return; |
709 | return; |
665 | |
710 | |
666 | w->at += now; |
711 | w->at += now; |
|
|
712 | |
|
|
713 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
667 | |
714 | |
668 | ev_start ((W)w, ++timercnt); |
715 | ev_start ((W)w, ++timercnt); |
669 | array_needsize (timers, timermax, timercnt, ); |
716 | array_needsize (timers, timermax, timercnt, ); |
670 | timers [timercnt - 1] = w; |
717 | timers [timercnt - 1] = w; |
671 | upheap ((WT *)timers, timercnt - 1); |
718 | upheap ((WT *)timers, timercnt - 1); |
672 | } |
719 | } |
673 | |
720 | |
674 | void |
721 | void |
675 | evtimer_stop (struct ev_timer *w) |
722 | evtimer_stop (struct ev_timer *w) |
676 | { |
723 | { |
|
|
724 | ev_clear ((W)w); |
677 | if (!ev_is_active (w)) |
725 | if (!ev_is_active (w)) |
678 | return; |
726 | return; |
679 | |
727 | |
680 | if (w->active < timercnt--) |
728 | if (w->active < timercnt--) |
681 | { |
729 | { |
682 | timers [w->active - 1] = timers [timercnt]; |
730 | timers [w->active - 1] = timers [timercnt]; |
683 | downheap ((WT *)timers, timercnt, w->active - 1); |
731 | downheap ((WT *)timers, timercnt, w->active - 1); |
684 | } |
732 | } |
685 | |
733 | |
|
|
734 | w->at = w->repeat; |
|
|
735 | |
686 | ev_stop ((W)w); |
736 | ev_stop ((W)w); |
|
|
737 | } |
|
|
738 | |
|
|
739 | void |
|
|
740 | evtimer_again (struct ev_timer *w) |
|
|
741 | { |
|
|
742 | if (ev_is_active (w)) |
|
|
743 | { |
|
|
744 | if (w->repeat) |
|
|
745 | { |
|
|
746 | w->at = now + w->repeat; |
|
|
747 | downheap ((WT *)timers, timercnt, w->active - 1); |
|
|
748 | } |
|
|
749 | else |
|
|
750 | evtimer_stop (w); |
|
|
751 | } |
|
|
752 | else if (w->repeat) |
|
|
753 | evtimer_start (w); |
687 | } |
754 | } |
688 | |
755 | |
689 | void |
756 | void |
690 | evperiodic_start (struct ev_periodic *w) |
757 | evperiodic_start (struct ev_periodic *w) |
691 | { |
758 | { |
692 | if (ev_is_active (w)) |
759 | if (ev_is_active (w)) |
693 | return; |
760 | return; |
|
|
761 | |
|
|
762 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
694 | |
763 | |
695 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
764 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
696 | if (w->interval) |
765 | if (w->interval) |
697 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
766 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
698 | |
767 | |
… | |
… | |
703 | } |
772 | } |
704 | |
773 | |
705 | void |
774 | void |
706 | evperiodic_stop (struct ev_periodic *w) |
775 | evperiodic_stop (struct ev_periodic *w) |
707 | { |
776 | { |
|
|
777 | ev_clear ((W)w); |
708 | if (!ev_is_active (w)) |
778 | if (!ev_is_active (w)) |
709 | return; |
779 | return; |
710 | |
780 | |
711 | if (w->active < periodiccnt--) |
781 | if (w->active < periodiccnt--) |
712 | { |
782 | { |
… | |
… | |
738 | } |
808 | } |
739 | |
809 | |
740 | void |
810 | void |
741 | evsignal_stop (struct ev_signal *w) |
811 | evsignal_stop (struct ev_signal *w) |
742 | { |
812 | { |
|
|
813 | ev_clear ((W)w); |
743 | if (!ev_is_active (w)) |
814 | if (!ev_is_active (w)) |
744 | return; |
815 | return; |
745 | |
816 | |
746 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
817 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
747 | ev_stop ((W)w); |
818 | ev_stop ((W)w); |
… | |
… | |
760 | idles [idlecnt - 1] = w; |
831 | idles [idlecnt - 1] = w; |
761 | } |
832 | } |
762 | |
833 | |
763 | void evidle_stop (struct ev_idle *w) |
834 | void evidle_stop (struct ev_idle *w) |
764 | { |
835 | { |
|
|
836 | ev_clear ((W)w); |
|
|
837 | if (ev_is_active (w)) |
|
|
838 | return; |
|
|
839 | |
765 | idles [w->active - 1] = idles [--idlecnt]; |
840 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
841 | ev_stop ((W)w); |
|
|
842 | } |
|
|
843 | |
|
|
844 | void evprepare_start (struct ev_prepare *w) |
|
|
845 | { |
|
|
846 | if (ev_is_active (w)) |
|
|
847 | return; |
|
|
848 | |
|
|
849 | ev_start ((W)w, ++preparecnt); |
|
|
850 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
851 | prepares [preparecnt - 1] = w; |
|
|
852 | } |
|
|
853 | |
|
|
854 | void evprepare_stop (struct ev_prepare *w) |
|
|
855 | { |
|
|
856 | ev_clear ((W)w); |
|
|
857 | if (ev_is_active (w)) |
|
|
858 | return; |
|
|
859 | |
|
|
860 | prepares [w->active - 1] = prepares [--preparecnt]; |
766 | ev_stop ((W)w); |
861 | ev_stop ((W)w); |
767 | } |
862 | } |
768 | |
863 | |
769 | void evcheck_start (struct ev_check *w) |
864 | void evcheck_start (struct ev_check *w) |
770 | { |
865 | { |
… | |
… | |
776 | checks [checkcnt - 1] = w; |
871 | checks [checkcnt - 1] = w; |
777 | } |
872 | } |
778 | |
873 | |
779 | void evcheck_stop (struct ev_check *w) |
874 | void evcheck_stop (struct ev_check *w) |
780 | { |
875 | { |
|
|
876 | ev_clear ((W)w); |
|
|
877 | if (ev_is_active (w)) |
|
|
878 | return; |
|
|
879 | |
781 | checks [w->active - 1] = checks [--checkcnt]; |
880 | checks [w->active - 1] = checks [--checkcnt]; |
782 | ev_stop ((W)w); |
881 | ev_stop ((W)w); |
783 | } |
882 | } |
784 | |
883 | |
785 | /*****************************************************************************/ |
884 | /*****************************************************************************/ |
786 | |
885 | |
|
|
886 | struct ev_once |
|
|
887 | { |
|
|
888 | struct ev_io io; |
|
|
889 | struct ev_timer to; |
|
|
890 | void (*cb)(int revents, void *arg); |
|
|
891 | void *arg; |
|
|
892 | }; |
|
|
893 | |
|
|
894 | static void |
|
|
895 | once_cb (struct ev_once *once, int revents) |
|
|
896 | { |
|
|
897 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
898 | void *arg = once->arg; |
|
|
899 | |
|
|
900 | evio_stop (&once->io); |
|
|
901 | evtimer_stop (&once->to); |
|
|
902 | free (once); |
|
|
903 | |
|
|
904 | cb (revents, arg); |
|
|
905 | } |
|
|
906 | |
|
|
907 | static void |
|
|
908 | once_cb_io (struct ev_io *w, int revents) |
|
|
909 | { |
|
|
910 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
911 | } |
|
|
912 | |
|
|
913 | static void |
|
|
914 | once_cb_to (struct ev_timer *w, int revents) |
|
|
915 | { |
|
|
916 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
917 | } |
|
|
918 | |
|
|
919 | void |
|
|
920 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
921 | { |
|
|
922 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
923 | |
|
|
924 | if (!once) |
|
|
925 | cb (EV_ERROR, arg); |
|
|
926 | else |
|
|
927 | { |
|
|
928 | once->cb = cb; |
|
|
929 | once->arg = arg; |
|
|
930 | |
|
|
931 | evw_init (&once->io, once_cb_io); |
|
|
932 | |
|
|
933 | if (fd >= 0) |
|
|
934 | { |
|
|
935 | evio_set (&once->io, fd, events); |
|
|
936 | evio_start (&once->io); |
|
|
937 | } |
|
|
938 | |
|
|
939 | evw_init (&once->to, once_cb_to); |
|
|
940 | |
|
|
941 | if (timeout >= 0.) |
|
|
942 | { |
|
|
943 | evtimer_set (&once->to, timeout, 0.); |
|
|
944 | evtimer_start (&once->to); |
|
|
945 | } |
|
|
946 | } |
|
|
947 | } |
|
|
948 | |
|
|
949 | /*****************************************************************************/ |
|
|
950 | |
787 | #if 1 |
951 | #if 0 |
788 | |
952 | |
789 | struct ev_io wio; |
953 | struct ev_io wio; |
790 | |
954 | |
791 | static void |
955 | static void |
792 | sin_cb (struct ev_io *w, int revents) |
956 | sin_cb (struct ev_io *w, int revents) |