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.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>
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{
383} 416}
384 417
385static void 418static void
386call_pending () 419call_pending ()
387{ 420{
388 int i; 421 while (pendingcnt)
389
390 for (i = 0; i < pendingcnt; ++i)
391 { 422 {
392 ANPENDING *p = pendings + i; 423 ANPENDING *p = pendings + --pendingcnt;
393 424
394 if (p->w) 425 if (p->w)
395 { 426 {
396 p->w->pending = 0; 427 p->w->pending = 0;
397 p->w->cb (p->w, p->events); 428 p->w->cb (p->w, p->events);
398 } 429 }
399 } 430 }
400
401 pendingcnt = 0;
402} 431}
403 432
404static void 433static void
405timers_reify () 434timers_reify ()
406{ 435{
407 while (timercnt && timers [0]->at <= now) 436 while (timercnt && timers [0]->at <= now)
408 { 437 {
409 struct ev_timer *w = timers [0]; 438 struct ev_timer *w = timers [0];
439
440 event ((W)w, EV_TIMEOUT);
410 441
411 /* first reschedule or stop timer */ 442 /* first reschedule or stop timer */
412 if (w->repeat) 443 if (w->repeat)
413 { 444 {
414 w->at = now + w->repeat; 445 w->at = now + w->repeat;
415 assert (("timer timeout in the past, negative repeat?", w->at > now)); 446 assert (("timer timeout in the past, negative repeat?", w->at > now));
416 downheap ((WT *)timers, timercnt, 0); 447 downheap ((WT *)timers, timercnt, 0);
417 } 448 }
418 else 449 else
419 evtimer_stop (w); /* nonrepeating: stop timer */ 450 evtimer_stop (w); /* nonrepeating: stop timer */
420
421 event ((W)w, EV_TIMEOUT);
422 } 451 }
423} 452}
424 453
425static void 454static void
426periodics_reify () 455periodics_reify ()
599 head = &(*head)->next; 628 head = &(*head)->next;
600 } 629 }
601} 630}
602 631
603static 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
604ev_start (W w, int active) 643ev_start (W w, int active)
605{ 644{
606 w->pending = 0;
607 w->active = active; 645 w->active = active;
608} 646}
609 647
610static void 648static void
611ev_stop (W w) 649ev_stop (W w)
612{ 650{
613 if (w->pending)
614 pendings [w->pending - 1].w = 0;
615
616 w->active = 0; 651 w->active = 0;
617} 652}
618 653
619/*****************************************************************************/ 654/*****************************************************************************/
620 655
636} 671}
637 672
638void 673void
639evio_stop (struct ev_io *w) 674evio_stop (struct ev_io *w)
640{ 675{
676 ev_clear ((W)w);
641 if (!ev_is_active (w)) 677 if (!ev_is_active (w))
642 return; 678 return;
643 679
644 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 680 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
645 ev_stop ((W)w); 681 ev_stop ((W)w);
646 682
647 ++fdchangecnt; 683 ++fdchangecnt;
648 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 684 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
649 fdchanges [fdchangecnt - 1] = w->fd; 685 fdchanges [fdchangecnt - 1] = w->fd;
650} 686}
651
652 687
653void 688void
654evtimer_start (struct ev_timer *w) 689evtimer_start (struct ev_timer *w)
655{ 690{
656 if (ev_is_active (w)) 691 if (ev_is_active (w))
667} 702}
668 703
669void 704void
670evtimer_stop (struct ev_timer *w) 705evtimer_stop (struct ev_timer *w)
671{ 706{
707 ev_clear ((W)w);
672 if (!ev_is_active (w)) 708 if (!ev_is_active (w))
673 return; 709 return;
674 710
675 if (w->active < timercnt--) 711 if (w->active < timercnt--)
676 { 712 {
719} 755}
720 756
721void 757void
722evperiodic_stop (struct ev_periodic *w) 758evperiodic_stop (struct ev_periodic *w)
723{ 759{
760 ev_clear ((W)w);
724 if (!ev_is_active (w)) 761 if (!ev_is_active (w))
725 return; 762 return;
726 763
727 if (w->active < periodiccnt--) 764 if (w->active < periodiccnt--)
728 { 765 {
754} 791}
755 792
756void 793void
757evsignal_stop (struct ev_signal *w) 794evsignal_stop (struct ev_signal *w)
758{ 795{
796 ev_clear ((W)w);
759 if (!ev_is_active (w)) 797 if (!ev_is_active (w))
760 return; 798 return;
761 799
762 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 800 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
763 ev_stop ((W)w); 801 ev_stop ((W)w);
776 idles [idlecnt - 1] = w; 814 idles [idlecnt - 1] = w;
777} 815}
778 816
779void evidle_stop (struct ev_idle *w) 817void evidle_stop (struct ev_idle *w)
780{ 818{
819 ev_clear ((W)w);
820 if (ev_is_active (w))
821 return;
822
781 idles [w->active - 1] = idles [--idlecnt]; 823 idles [w->active - 1] = idles [--idlecnt];
782 ev_stop ((W)w); 824 ev_stop ((W)w);
783} 825}
784 826
785void evcheck_start (struct ev_check *w) 827void evcheck_start (struct ev_check *w)
792 checks [checkcnt - 1] = w; 834 checks [checkcnt - 1] = w;
793} 835}
794 836
795void evcheck_stop (struct ev_check *w) 837void evcheck_stop (struct ev_check *w)
796{ 838{
839 ev_clear ((W)w);
840 if (ev_is_active (w))
841 return;
842
797 checks [w->active - 1] = checks [--checkcnt]; 843 checks [w->active - 1] = checks [--checkcnt];
798 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 }
799} 910}
800 911
801/*****************************************************************************/ 912/*****************************************************************************/
802 913
803#if 0 914#if 0

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines