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

Comparing libev/ev.c (file contents):
Revision 1.15 by root, Wed Oct 31 11:56:34 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>
125static int pendingmax, pendingcnt; 155static int pendingmax, pendingcnt;
126 156
127static void 157static void
128event (W w, int events) 158event (W w, int events)
129{ 159{
160 if (w->active)
161 {
130 w->pending = ++pendingcnt; 162 w->pending = ++pendingcnt;
131 array_needsize (pendings, pendingmax, pendingcnt, ); 163 array_needsize (pendings, pendingmax, pendingcnt, );
132 pendings [pendingcnt - 1].w = w; 164 pendings [pendingcnt - 1].w = w;
133 pendings [pendingcnt - 1].events = events; 165 pendings [pendingcnt - 1].events = events;
166 }
134} 167}
135 168
136static void 169static void
137fd_event (int fd, int events) 170fd_event (int fd, int events)
138{ 171{
406{ 439{
407 while (timercnt && timers [0]->at <= now) 440 while (timercnt && timers [0]->at <= now)
408 { 441 {
409 struct ev_timer *w = timers [0]; 442 struct ev_timer *w = timers [0];
410 443
444 event ((W)w, EV_TIMEOUT);
445
411 /* first reschedule or stop timer */ 446 /* first reschedule or stop timer */
412 if (w->repeat) 447 if (w->repeat)
413 { 448 {
414 w->at = now + w->repeat; 449 w->at = now + w->repeat;
415 assert (("timer timeout in the past, negative repeat?", w->at > now)); 450 assert (("timer timeout in the past, negative repeat?", w->at > now));
416 downheap ((WT *)timers, timercnt, 0); 451 downheap ((WT *)timers, timercnt, 0);
417 } 452 }
418 else 453 else
419 evtimer_stop (w); /* nonrepeating: stop timer */ 454 evtimer_stop (w); /* nonrepeating: stop timer */
420
421 event ((W)w, EV_TIMEOUT);
422 } 455 }
423} 456}
424 457
425static void 458static void
426periodics_reify () 459periodics_reify ()
599 head = &(*head)->next; 632 head = &(*head)->next;
600 } 633 }
601} 634}
602 635
603static 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
604ev_start (W w, int active) 647ev_start (W w, int active)
605{ 648{
606 w->pending = 0;
607 w->active = active; 649 w->active = active;
608} 650}
609 651
610static void 652static void
611ev_stop (W w) 653ev_stop (W w)
612{ 654{
613 if (w->pending)
614 pendings [w->pending - 1].w = 0;
615
616 w->active = 0; 655 w->active = 0;
617} 656}
618 657
619/*****************************************************************************/ 658/*****************************************************************************/
620 659
636} 675}
637 676
638void 677void
639evio_stop (struct ev_io *w) 678evio_stop (struct ev_io *w)
640{ 679{
680 ev_clear ((W)w);
641 if (!ev_is_active (w)) 681 if (!ev_is_active (w))
642 return; 682 return;
643 683
644 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 684 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
645 ev_stop ((W)w); 685 ev_stop ((W)w);
646 686
647 ++fdchangecnt; 687 ++fdchangecnt;
648 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 688 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
649 fdchanges [fdchangecnt - 1] = w->fd; 689 fdchanges [fdchangecnt - 1] = w->fd;
650} 690}
651
652 691
653void 692void
654evtimer_start (struct ev_timer *w) 693evtimer_start (struct ev_timer *w)
655{ 694{
656 if (ev_is_active (w)) 695 if (ev_is_active (w))
667} 706}
668 707
669void 708void
670evtimer_stop (struct ev_timer *w) 709evtimer_stop (struct ev_timer *w)
671{ 710{
711 ev_clear ((W)w);
672 if (!ev_is_active (w)) 712 if (!ev_is_active (w))
673 return; 713 return;
674 714
675 if (w->active < timercnt--) 715 if (w->active < timercnt--)
676 { 716 {
719} 759}
720 760
721void 761void
722evperiodic_stop (struct ev_periodic *w) 762evperiodic_stop (struct ev_periodic *w)
723{ 763{
764 ev_clear ((W)w);
724 if (!ev_is_active (w)) 765 if (!ev_is_active (w))
725 return; 766 return;
726 767
727 if (w->active < periodiccnt--) 768 if (w->active < periodiccnt--)
728 { 769 {
754} 795}
755 796
756void 797void
757evsignal_stop (struct ev_signal *w) 798evsignal_stop (struct ev_signal *w)
758{ 799{
800 ev_clear ((W)w);
759 if (!ev_is_active (w)) 801 if (!ev_is_active (w))
760 return; 802 return;
761 803
762 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 804 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
763 ev_stop ((W)w); 805 ev_stop ((W)w);
776 idles [idlecnt - 1] = w; 818 idles [idlecnt - 1] = w;
777} 819}
778 820
779void evidle_stop (struct ev_idle *w) 821void evidle_stop (struct ev_idle *w)
780{ 822{
823 ev_clear ((W)w);
824 if (ev_is_active (w))
825 return;
826
781 idles [w->active - 1] = idles [--idlecnt]; 827 idles [w->active - 1] = idles [--idlecnt];
782 ev_stop ((W)w); 828 ev_stop ((W)w);
783} 829}
784 830
785void evcheck_start (struct ev_check *w) 831void evcheck_start (struct ev_check *w)
792 checks [checkcnt - 1] = w; 838 checks [checkcnt - 1] = w;
793} 839}
794 840
795void evcheck_stop (struct ev_check *w) 841void evcheck_stop (struct ev_check *w)
796{ 842{
843 ev_clear ((W)w);
844 if (ev_is_active (w))
845 return;
846
797 checks [w->active - 1] = checks [--checkcnt]; 847 checks [w->active - 1] = checks [--checkcnt];
798 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 }
799} 914}
800 915
801/*****************************************************************************/ 916/*****************************************************************************/
802 917
803#if 0 918#if 0

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines