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

Comparing libev/ev.c (file contents):
Revision 1.14 by root, Wed Oct 31 11:52:12 2007 UTC vs.
Revision 1.19 by root, Wed Oct 31 17:55:55 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>
11#include <sys/time.h> 41#include <sys/time.h>
12#include <time.h> 42#include <time.h>
13
14#define HAVE_EPOLL 1
15 43
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
83 111
84#define array_needsize(base,cur,cnt,init) \ 112#define array_needsize(base,cur,cnt,init) \
85 if ((cnt) > cur) \ 113 if ((cnt) > cur) \
86 { \ 114 { \
87 int newcnt = cur ? cur << 1 : 16; \ 115 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)); \ 116 base = realloc (base, sizeof (*base) * (newcnt)); \
90 init (base + cur, newcnt - cur); \ 117 init (base + cur, newcnt - cur); \
91 cur = newcnt; \ 118 cur = newcnt; \
92 } 119 }
93 120
126static int pendingmax, pendingcnt; 153static int pendingmax, pendingcnt;
127 154
128static void 155static void
129event (W w, int events) 156event (W w, int events)
130{ 157{
158 if (w->active)
159 {
131 w->pending = ++pendingcnt; 160 w->pending = ++pendingcnt;
132 array_needsize (pendings, pendingmax, pendingcnt, ); 161 array_needsize (pendings, pendingmax, pendingcnt, );
133 pendings [pendingcnt - 1].w = w; 162 pendings [pendingcnt - 1].w = w;
134 pendings [pendingcnt - 1].events = events; 163 pendings [pendingcnt - 1].events = events;
164 }
135} 165}
136 166
137static void 167static void
138fd_event (int fd, int events) 168fd_event (int fd, int events)
139{ 169{
154{ 184{
155 int i; 185 int i;
156 186
157 for (i = 0; i < eventcnt; ++i) 187 for (i = 0; i < eventcnt; ++i)
158 event (events [i], type); 188 event (events [i], type);
189}
190
191/* called on EBADF to verify fds */
192static void
193fd_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);
159} 202}
160 203
161/*****************************************************************************/ 204/*****************************************************************************/
162 205
163static struct ev_timer **timers; 206static struct ev_timer **timers;
384} 427}
385 428
386static void 429static void
387call_pending () 430call_pending ()
388{ 431{
389 int i; 432 while (pendingcnt)
390
391 for (i = 0; i < pendingcnt; ++i)
392 { 433 {
393 ANPENDING *p = pendings + i; 434 ANPENDING *p = pendings + --pendingcnt;
394 435
395 if (p->w) 436 if (p->w)
396 { 437 {
397 p->w->pending = 0; 438 p->w->pending = 0;
398 p->w->cb (p->w, p->events); 439 p->w->cb (p->w, p->events);
399 } 440 }
400 } 441 }
401
402 pendingcnt = 0;
403} 442}
404 443
405static void 444static void
406timers_reify () 445timers_reify ()
407{ 446{
408 while (timercnt && timers [0]->at <= now) 447 while (timercnt && timers [0]->at <= now)
409 { 448 {
410 struct ev_timer *w = timers [0]; 449 struct ev_timer *w = timers [0];
450
451 event ((W)w, EV_TIMEOUT);
411 452
412 /* first reschedule or stop timer */ 453 /* first reschedule or stop timer */
413 if (w->repeat) 454 if (w->repeat)
414 { 455 {
415 w->at = now + w->repeat; 456 w->at = now + w->repeat;
416 assert (("timer timeout in the past, negative repeat?", w->at > now)); 457 assert (("timer timeout in the past, negative repeat?", w->at > now));
417 downheap ((WT *)timers, timercnt, 0); 458 downheap ((WT *)timers, timercnt, 0);
418 } 459 }
419 else 460 else
420 evtimer_stop (w); /* nonrepeating: stop timer */ 461 evtimer_stop (w); /* nonrepeating: stop timer */
421
422 event ((W)w, EV_TIMEOUT);
423 } 462 }
424} 463}
425 464
426static void 465static void
427periodics_reify () 466periodics_reify ()
600 head = &(*head)->next; 639 head = &(*head)->next;
601 } 640 }
602} 641}
603 642
604static void 643static void
644ev_clear (W w)
645{
646 if (w->pending)
647 {
648 pendings [w->pending - 1].w = 0;
649 w->pending = 0;
650 }
651}
652
653static void
605ev_start (W w, int active) 654ev_start (W w, int active)
606{ 655{
607 w->pending = 0;
608 w->active = active; 656 w->active = active;
609} 657}
610 658
611static void 659static void
612ev_stop (W w) 660ev_stop (W w)
613{ 661{
614 if (w->pending)
615 pendings [w->pending - 1].w = 0;
616
617 w->active = 0; 662 w->active = 0;
618} 663}
619 664
620/*****************************************************************************/ 665/*****************************************************************************/
621 666
637} 682}
638 683
639void 684void
640evio_stop (struct ev_io *w) 685evio_stop (struct ev_io *w)
641{ 686{
687 ev_clear ((W)w);
642 if (!ev_is_active (w)) 688 if (!ev_is_active (w))
643 return; 689 return;
644 690
645 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 691 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
646 ev_stop ((W)w); 692 ev_stop ((W)w);
647 693
648 ++fdchangecnt; 694 ++fdchangecnt;
649 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 695 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
650 fdchanges [fdchangecnt - 1] = w->fd; 696 fdchanges [fdchangecnt - 1] = w->fd;
651} 697}
652
653 698
654void 699void
655evtimer_start (struct ev_timer *w) 700evtimer_start (struct ev_timer *w)
656{ 701{
657 if (ev_is_active (w)) 702 if (ev_is_active (w))
668} 713}
669 714
670void 715void
671evtimer_stop (struct ev_timer *w) 716evtimer_stop (struct ev_timer *w)
672{ 717{
718 ev_clear ((W)w);
673 if (!ev_is_active (w)) 719 if (!ev_is_active (w))
674 return; 720 return;
675 721
676 if (w->active < timercnt--) 722 if (w->active < timercnt--)
677 { 723 {
720} 766}
721 767
722void 768void
723evperiodic_stop (struct ev_periodic *w) 769evperiodic_stop (struct ev_periodic *w)
724{ 770{
771 ev_clear ((W)w);
725 if (!ev_is_active (w)) 772 if (!ev_is_active (w))
726 return; 773 return;
727 774
728 if (w->active < periodiccnt--) 775 if (w->active < periodiccnt--)
729 { 776 {
755} 802}
756 803
757void 804void
758evsignal_stop (struct ev_signal *w) 805evsignal_stop (struct ev_signal *w)
759{ 806{
807 ev_clear ((W)w);
760 if (!ev_is_active (w)) 808 if (!ev_is_active (w))
761 return; 809 return;
762 810
763 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 811 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
764 ev_stop ((W)w); 812 ev_stop ((W)w);
777 idles [idlecnt - 1] = w; 825 idles [idlecnt - 1] = w;
778} 826}
779 827
780void evidle_stop (struct ev_idle *w) 828void evidle_stop (struct ev_idle *w)
781{ 829{
830 ev_clear ((W)w);
831 if (ev_is_active (w))
832 return;
833
782 idles [w->active - 1] = idles [--idlecnt]; 834 idles [w->active - 1] = idles [--idlecnt];
783 ev_stop ((W)w); 835 ev_stop ((W)w);
784} 836}
785 837
786void evcheck_start (struct ev_check *w) 838void evcheck_start (struct ev_check *w)
793 checks [checkcnt - 1] = w; 845 checks [checkcnt - 1] = w;
794} 846}
795 847
796void evcheck_stop (struct ev_check *w) 848void evcheck_stop (struct ev_check *w)
797{ 849{
850 ev_clear ((W)w);
851 if (ev_is_active (w))
852 return;
853
798 checks [w->active - 1] = checks [--checkcnt]; 854 checks [w->active - 1] = checks [--checkcnt];
799 ev_stop ((W)w); 855 ev_stop ((W)w);
856}
857
858/*****************************************************************************/
859
860struct ev_once
861{
862 struct ev_io io;
863 struct ev_timer to;
864 void (*cb)(int revents, void *arg);
865 void *arg;
866};
867
868static void
869once_cb (struct ev_once *once, int revents)
870{
871 void (*cb)(int revents, void *arg) = once->cb;
872 void *arg = once->arg;
873
874 evio_stop (&once->io);
875 evtimer_stop (&once->to);
876 free (once);
877
878 cb (revents, arg);
879}
880
881static void
882once_cb_io (struct ev_io *w, int revents)
883{
884 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
885}
886
887static void
888once_cb_to (struct ev_timer *w, int revents)
889{
890 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
891}
892
893void
894ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
895{
896 struct ev_once *once = malloc (sizeof (struct ev_once));
897
898 if (!once)
899 cb (EV_ERROR, arg);
900 else
901 {
902 once->cb = cb;
903 once->arg = arg;
904
905 evw_init (&once->io, once_cb_io);
906
907 if (fd >= 0)
908 {
909 evio_set (&once->io, fd, events);
910 evio_start (&once->io);
911 }
912
913 evw_init (&once->to, once_cb_to);
914
915 if (timeout >= 0.)
916 {
917 evtimer_set (&once->to, timeout, 0.);
918 evtimer_start (&once->to);
919 }
920 }
800} 921}
801 922
802/*****************************************************************************/ 923/*****************************************************************************/
803 924
804#if 0 925#if 0

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines