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.17 by root, Wed Oct 31 14:44:15 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{
407{ 439{
408 while (timercnt && timers [0]->at <= now) 440 while (timercnt && timers [0]->at <= now)
409 { 441 {
410 struct ev_timer *w = timers [0]; 442 struct ev_timer *w = timers [0];
411 443
444 event ((W)w, EV_TIMEOUT);
445
412 /* first reschedule or stop timer */ 446 /* first reschedule or stop timer */
413 if (w->repeat) 447 if (w->repeat)
414 { 448 {
415 w->at = now + w->repeat; 449 w->at = now + w->repeat;
416 assert (("timer timeout in the past, negative repeat?", w->at > now)); 450 assert (("timer timeout in the past, negative repeat?", w->at > now));
417 downheap ((WT *)timers, timercnt, 0); 451 downheap ((WT *)timers, timercnt, 0);
418 } 452 }
419 else 453 else
420 evtimer_stop (w); /* nonrepeating: stop timer */ 454 evtimer_stop (w); /* nonrepeating: stop timer */
421
422 event ((W)w, EV_TIMEOUT);
423 } 455 }
424} 456}
425 457
426static void 458static void
427periodics_reify () 459periodics_reify ()
538 { 570 {
539 block = MAX_BLOCKTIME; 571 block = MAX_BLOCKTIME;
540 572
541 if (timercnt) 573 if (timercnt)
542 { 574 {
543 ev_tstamp to = timers [0]->at - get_clock () + method_fudge; 575 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
544 if (block > to) block = to; 576 if (block > to) block = to;
545 } 577 }
546 578
547 if (periodiccnt) 579 if (periodiccnt)
548 { 580 {
600 head = &(*head)->next; 632 head = &(*head)->next;
601 } 633 }
602} 634}
603 635
604static void 636static void
637ev_clear (W w)
638{
639 if (w->pending)
640 {
641 pendings [w->pending - 1].w = 0;
642 w->pending = 0;
643 }
644}
645
646static void
605ev_start (W w, int active) 647ev_start (W w, int active)
606{ 648{
607 w->pending = 0;
608 w->active = active; 649 w->active = active;
609} 650}
610 651
611static void 652static void
612ev_stop (W w) 653ev_stop (W w)
613{ 654{
614 if (w->pending)
615 pendings [w->pending - 1].w = 0;
616
617 w->active = 0; 655 w->active = 0;
618} 656}
619 657
620/*****************************************************************************/ 658/*****************************************************************************/
621 659
637} 675}
638 676
639void 677void
640evio_stop (struct ev_io *w) 678evio_stop (struct ev_io *w)
641{ 679{
680 ev_clear ((W)w);
642 if (!ev_is_active (w)) 681 if (!ev_is_active (w))
643 return; 682 return;
644 683
645 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 684 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
646 ev_stop ((W)w); 685 ev_stop ((W)w);
647 686
648 ++fdchangecnt; 687 ++fdchangecnt;
649 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 688 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
650 fdchanges [fdchangecnt - 1] = w->fd; 689 fdchanges [fdchangecnt - 1] = w->fd;
651} 690}
652
653 691
654void 692void
655evtimer_start (struct ev_timer *w) 693evtimer_start (struct ev_timer *w)
656{ 694{
657 if (ev_is_active (w)) 695 if (ev_is_active (w))
668} 706}
669 707
670void 708void
671evtimer_stop (struct ev_timer *w) 709evtimer_stop (struct ev_timer *w)
672{ 710{
711 ev_clear ((W)w);
673 if (!ev_is_active (w)) 712 if (!ev_is_active (w))
674 return; 713 return;
675 714
676 if (w->active < timercnt--) 715 if (w->active < timercnt--)
677 { 716 {
678 timers [w->active - 1] = timers [timercnt]; 717 timers [w->active - 1] = timers [timercnt];
679 downheap ((WT *)timers, timercnt, w->active - 1); 718 downheap ((WT *)timers, timercnt, w->active - 1);
680 } 719 }
681 720
721 w->at = w->repeat;
722
682 ev_stop ((W)w); 723 ev_stop ((W)w);
724}
725
726void
727evtimer_again (struct ev_timer *w)
728{
729 if (ev_is_active (w))
730 {
731 if (w->repeat)
732 {
733 w->at = now + w->repeat;
734 downheap ((WT *)timers, timercnt, w->active - 1);
735 }
736 else
737 evtimer_stop (w);
738 }
739 else if (w->repeat)
740 evtimer_start (w);
683} 741}
684 742
685void 743void
686evperiodic_start (struct ev_periodic *w) 744evperiodic_start (struct ev_periodic *w)
687{ 745{
701} 759}
702 760
703void 761void
704evperiodic_stop (struct ev_periodic *w) 762evperiodic_stop (struct ev_periodic *w)
705{ 763{
764 ev_clear ((W)w);
706 if (!ev_is_active (w)) 765 if (!ev_is_active (w))
707 return; 766 return;
708 767
709 if (w->active < periodiccnt--) 768 if (w->active < periodiccnt--)
710 { 769 {
736} 795}
737 796
738void 797void
739evsignal_stop (struct ev_signal *w) 798evsignal_stop (struct ev_signal *w)
740{ 799{
800 ev_clear ((W)w);
741 if (!ev_is_active (w)) 801 if (!ev_is_active (w))
742 return; 802 return;
743 803
744 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 804 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
745 ev_stop ((W)w); 805 ev_stop ((W)w);
758 idles [idlecnt - 1] = w; 818 idles [idlecnt - 1] = w;
759} 819}
760 820
761void evidle_stop (struct ev_idle *w) 821void evidle_stop (struct ev_idle *w)
762{ 822{
823 ev_clear ((W)w);
824 if (ev_is_active (w))
825 return;
826
763 idles [w->active - 1] = idles [--idlecnt]; 827 idles [w->active - 1] = idles [--idlecnt];
764 ev_stop ((W)w); 828 ev_stop ((W)w);
765} 829}
766 830
767void evcheck_start (struct ev_check *w) 831void evcheck_start (struct ev_check *w)
774 checks [checkcnt - 1] = w; 838 checks [checkcnt - 1] = w;
775} 839}
776 840
777void evcheck_stop (struct ev_check *w) 841void evcheck_stop (struct ev_check *w)
778{ 842{
843 ev_clear ((W)w);
844 if (ev_is_active (w))
845 return;
846
779 checks [w->active - 1] = checks [--checkcnt]; 847 checks [w->active - 1] = checks [--checkcnt];
780 ev_stop ((W)w); 848 ev_stop ((W)w);
849}
850
851/*****************************************************************************/
852
853struct ev_once
854{
855 struct ev_io io;
856 struct ev_timer to;
857 void (*cb)(int revents, void *arg);
858 void *arg;
859};
860
861static void
862once_cb (struct ev_once *once, int revents)
863{
864 void (*cb)(int revents, void *arg) = once->cb;
865 void *arg = once->arg;
866
867 evio_stop (&once->io);
868 evtimer_stop (&once->to);
869 free (once);
870
871 cb (revents, arg);
872}
873
874static void
875once_cb_io (struct ev_io *w, int revents)
876{
877 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
878}
879
880static void
881once_cb_to (struct ev_timer *w, int revents)
882{
883 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
884}
885
886void
887ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
888{
889 struct ev_once *once = malloc (sizeof (struct ev_once));
890
891 if (!once)
892 cb (EV_ERROR, arg);
893 else
894 {
895 once->cb = cb;
896 once->arg = arg;
897
898 evw_init (&once->io, once_cb_io);
899
900 if (fd >= 0)
901 {
902 evio_set (&once->io, fd, events);
903 evio_start (&once->io);
904 }
905
906 evw_init (&once->to, once_cb_to);
907
908 if (timeout >= 0.)
909 {
910 evtimer_set (&once->to, timeout, 0.);
911 evtimer_start (&once->to);
912 }
913 }
781} 914}
782 915
783/*****************************************************************************/ 916/*****************************************************************************/
784 917
785#if 0 918#if 0

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines