ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.13 by root, Wed Oct 31 10:50:05 2007 UTC vs.
Revision 1.18 by root, Wed Oct 31 16:29:52 2007 UTC

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>
83 113
84#define array_needsize(base,cur,cnt,init) \ 114#define array_needsize(base,cur,cnt,init) \
85 if ((cnt) > cur) \ 115 if ((cnt) > cur) \
86 { \ 116 { \
87 int newcnt = cur ? cur << 1 : 16; \ 117 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)); \ 118 base = realloc (base, sizeof (*base) * (newcnt)); \
90 init (base + cur, newcnt - cur); \ 119 init (base + cur, newcnt - cur); \
91 cur = newcnt; \ 120 cur = newcnt; \
92 } 121 }
93 122
126static int pendingmax, pendingcnt; 155static int pendingmax, pendingcnt;
127 156
128static void 157static void
129event (W w, int events) 158event (W w, int events)
130{ 159{
160 if (w->active)
161 {
131 w->pending = ++pendingcnt; 162 w->pending = ++pendingcnt;
132 array_needsize (pendings, pendingmax, pendingcnt, ); 163 array_needsize (pendings, pendingmax, pendingcnt, );
133 pendings [pendingcnt - 1].w = w; 164 pendings [pendingcnt - 1].w = w;
134 pendings [pendingcnt - 1].events = events; 165 pendings [pendingcnt - 1].events = events;
166 }
135} 167}
136 168
137static void 169static void
138fd_event (int fd, int events) 170fd_event (int fd, int events)
139{ 171{
384} 416}
385 417
386static void 418static void
387call_pending () 419call_pending ()
388{ 420{
389 int i; 421 while (pendingcnt)
390
391 for (i = 0; i < pendingcnt; ++i)
392 { 422 {
393 ANPENDING *p = pendings + i; 423 ANPENDING *p = pendings + --pendingcnt;
394 424
395 if (p->w) 425 if (p->w)
396 { 426 {
397 p->w->pending = 0; 427 p->w->pending = 0;
398 p->w->cb (p->w, p->events); 428 p->w->cb (p->w, p->events);
399 } 429 }
400 } 430 }
401
402 pendingcnt = 0;
403} 431}
404 432
405static void 433static void
406timers_reify () 434timers_reify ()
407{ 435{
408 while (timercnt && timers [0]->at <= now) 436 while (timercnt && timers [0]->at <= now)
409 { 437 {
410 struct ev_timer *w = timers [0]; 438 struct ev_timer *w = timers [0];
439
440 event ((W)w, EV_TIMEOUT);
411 441
412 /* first reschedule or stop timer */ 442 /* first reschedule or stop timer */
413 if (w->repeat) 443 if (w->repeat)
414 { 444 {
415 w->at = now + w->repeat; 445 w->at = now + w->repeat;
416 assert (("timer timeout in the past, negative repeat?", w->at > now)); 446 assert (("timer timeout in the past, negative repeat?", w->at > now));
417 downheap ((WT *)timers, timercnt, 0); 447 downheap ((WT *)timers, timercnt, 0);
418 } 448 }
419 else 449 else
420 evtimer_stop (w); /* nonrepeating: stop timer */ 450 evtimer_stop (w); /* nonrepeating: stop timer */
421
422 event ((W)w, EV_TIMEOUT);
423 } 451 }
424} 452}
425 453
426static void 454static void
427periodics_reify () 455periodics_reify ()
538 { 566 {
539 block = MAX_BLOCKTIME; 567 block = MAX_BLOCKTIME;
540 568
541 if (timercnt) 569 if (timercnt)
542 { 570 {
543 ev_tstamp to = timers [0]->at - get_clock () + method_fudge; 571 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
544 if (block > to) block = to; 572 if (block > to) block = to;
545 } 573 }
546 574
547 if (periodiccnt) 575 if (periodiccnt)
548 { 576 {
600 head = &(*head)->next; 628 head = &(*head)->next;
601 } 629 }
602} 630}
603 631
604static void 632static void
633ev_clear (W w)
634{
635 if (w->pending)
636 {
637 pendings [w->pending - 1].w = 0;
638 w->pending = 0;
639 }
640}
641
642static void
605ev_start (W w, int active) 643ev_start (W w, int active)
606{ 644{
607 w->pending = 0;
608 w->active = active; 645 w->active = active;
609} 646}
610 647
611static void 648static void
612ev_stop (W w) 649ev_stop (W w)
613{ 650{
614 if (w->pending)
615 pendings [w->pending - 1].w = 0;
616
617 w->active = 0; 651 w->active = 0;
618} 652}
619 653
620/*****************************************************************************/ 654/*****************************************************************************/
621 655
637} 671}
638 672
639void 673void
640evio_stop (struct ev_io *w) 674evio_stop (struct ev_io *w)
641{ 675{
676 ev_clear ((W)w);
642 if (!ev_is_active (w)) 677 if (!ev_is_active (w))
643 return; 678 return;
644 679
645 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 680 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
646 ev_stop ((W)w); 681 ev_stop ((W)w);
647 682
648 ++fdchangecnt; 683 ++fdchangecnt;
649 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 684 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
650 fdchanges [fdchangecnt - 1] = w->fd; 685 fdchanges [fdchangecnt - 1] = w->fd;
651} 686}
652
653 687
654void 688void
655evtimer_start (struct ev_timer *w) 689evtimer_start (struct ev_timer *w)
656{ 690{
657 if (ev_is_active (w)) 691 if (ev_is_active (w))
668} 702}
669 703
670void 704void
671evtimer_stop (struct ev_timer *w) 705evtimer_stop (struct ev_timer *w)
672{ 706{
707 ev_clear ((W)w);
673 if (!ev_is_active (w)) 708 if (!ev_is_active (w))
674 return; 709 return;
675 710
676 if (w->active < timercnt--) 711 if (w->active < timercnt--)
677 { 712 {
678 timers [w->active - 1] = timers [timercnt]; 713 timers [w->active - 1] = timers [timercnt];
679 downheap ((WT *)timers, timercnt, w->active - 1); 714 downheap ((WT *)timers, timercnt, w->active - 1);
680 } 715 }
681 716
717 w->at = w->repeat;
718
682 ev_stop ((W)w); 719 ev_stop ((W)w);
720}
721
722void
723evtimer_again (struct ev_timer *w)
724{
725 if (ev_is_active (w))
726 {
727 if (w->repeat)
728 {
729 w->at = now + w->repeat;
730 downheap ((WT *)timers, timercnt, w->active - 1);
731 }
732 else
733 evtimer_stop (w);
734 }
735 else if (w->repeat)
736 evtimer_start (w);
683} 737}
684 738
685void 739void
686evperiodic_start (struct ev_periodic *w) 740evperiodic_start (struct ev_periodic *w)
687{ 741{
701} 755}
702 756
703void 757void
704evperiodic_stop (struct ev_periodic *w) 758evperiodic_stop (struct ev_periodic *w)
705{ 759{
760 ev_clear ((W)w);
706 if (!ev_is_active (w)) 761 if (!ev_is_active (w))
707 return; 762 return;
708 763
709 if (w->active < periodiccnt--) 764 if (w->active < periodiccnt--)
710 { 765 {
736} 791}
737 792
738void 793void
739evsignal_stop (struct ev_signal *w) 794evsignal_stop (struct ev_signal *w)
740{ 795{
796 ev_clear ((W)w);
741 if (!ev_is_active (w)) 797 if (!ev_is_active (w))
742 return; 798 return;
743 799
744 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 800 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
745 ev_stop ((W)w); 801 ev_stop ((W)w);
758 idles [idlecnt - 1] = w; 814 idles [idlecnt - 1] = w;
759} 815}
760 816
761void evidle_stop (struct ev_idle *w) 817void evidle_stop (struct ev_idle *w)
762{ 818{
819 ev_clear ((W)w);
820 if (ev_is_active (w))
821 return;
822
763 idles [w->active - 1] = idles [--idlecnt]; 823 idles [w->active - 1] = idles [--idlecnt];
764 ev_stop ((W)w); 824 ev_stop ((W)w);
765} 825}
766 826
767void evcheck_start (struct ev_check *w) 827void evcheck_start (struct ev_check *w)
774 checks [checkcnt - 1] = w; 834 checks [checkcnt - 1] = w;
775} 835}
776 836
777void evcheck_stop (struct ev_check *w) 837void evcheck_stop (struct ev_check *w)
778{ 838{
839 ev_clear ((W)w);
840 if (ev_is_active (w))
841 return;
842
779 checks [w->active - 1] = checks [--checkcnt]; 843 checks [w->active - 1] = checks [--checkcnt];
780 ev_stop ((W)w); 844 ev_stop ((W)w);
845}
846
847/*****************************************************************************/
848
849struct ev_once
850{
851 struct ev_io io;
852 struct ev_timer to;
853 void (*cb)(int revents, void *arg);
854 void *arg;
855};
856
857static void
858once_cb (struct ev_once *once, int revents)
859{
860 void (*cb)(int revents, void *arg) = once->cb;
861 void *arg = once->arg;
862
863 evio_stop (&once->io);
864 evtimer_stop (&once->to);
865 free (once);
866
867 cb (revents, arg);
868}
869
870static void
871once_cb_io (struct ev_io *w, int revents)
872{
873 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
874}
875
876static void
877once_cb_to (struct ev_timer *w, int revents)
878{
879 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
880}
881
882void
883ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
884{
885 struct ev_once *once = malloc (sizeof (struct ev_once));
886
887 if (!once)
888 cb (EV_ERROR, arg);
889 else
890 {
891 once->cb = cb;
892 once->arg = arg;
893
894 evw_init (&once->io, once_cb_io);
895
896 if (fd >= 0)
897 {
898 evio_set (&once->io, fd, events);
899 evio_start (&once->io);
900 }
901
902 evw_init (&once->to, once_cb_to);
903
904 if (timeout >= 0.)
905 {
906 evtimer_set (&once->to, timeout, 0.);
907 evtimer_start (&once->to);
908 }
909 }
781} 910}
782 911
783/*****************************************************************************/ 912/*****************************************************************************/
784 913
785#if 0 914#if 0

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines