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.20 by root, Wed Oct 31 18:28:00 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;
280 323
281/*****************************************************************************/ 324/*****************************************************************************/
282 325
283static struct ev_idle **idles; 326static struct ev_idle **idles;
284static int idlemax, idlecnt; 327static int idlemax, idlecnt;
328
329static struct ev_prepare **prepares;
330static int preparemax, preparecnt;
285 331
286static struct ev_check **checks; 332static struct ev_check **checks;
287static int checkmax, checkcnt; 333static int checkmax, checkcnt;
288 334
289/*****************************************************************************/ 335/*****************************************************************************/
384} 430}
385 431
386static void 432static void
387call_pending () 433call_pending ()
388{ 434{
389 int i; 435 while (pendingcnt)
390
391 for (i = 0; i < pendingcnt; ++i)
392 { 436 {
393 ANPENDING *p = pendings + i; 437 ANPENDING *p = pendings + --pendingcnt;
394 438
395 if (p->w) 439 if (p->w)
396 { 440 {
397 p->w->pending = 0; 441 p->w->pending = 0;
398 p->w->cb (p->w, p->events); 442 p->w->cb (p->w, p->events);
399 } 443 }
400 } 444 }
401
402 pendingcnt = 0;
403} 445}
404 446
405static void 447static void
406timers_reify () 448timers_reify ()
407{ 449{
408 while (timercnt && timers [0]->at <= now) 450 while (timercnt && timers [0]->at <= now)
409 { 451 {
410 struct ev_timer *w = timers [0]; 452 struct ev_timer *w = timers [0];
453
454 event ((W)w, EV_TIMEOUT);
411 455
412 /* first reschedule or stop timer */ 456 /* first reschedule or stop timer */
413 if (w->repeat) 457 if (w->repeat)
414 { 458 {
415 w->at = now + w->repeat; 459 w->at = now + w->repeat;
416 assert (("timer timeout in the past, negative repeat?", w->at > now)); 460 assert (("timer timeout in the past, negative repeat?", w->at > now));
417 downheap ((WT *)timers, timercnt, 0); 461 downheap ((WT *)timers, timercnt, 0);
418 } 462 }
419 else 463 else
420 evtimer_stop (w); /* nonrepeating: stop timer */ 464 evtimer_stop (w); /* nonrepeating: stop timer */
421
422 event ((W)w, EV_TIMEOUT);
423 } 465 }
424} 466}
425 467
426static void 468static void
427periodics_reify () 469periodics_reify ()
514void ev_loop (int flags) 556void ev_loop (int flags)
515{ 557{
516 double block; 558 double block;
517 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 559 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
518 560
519 if (checkcnt)
520 {
521 queue_events ((W *)checks, checkcnt, EV_CHECK);
522 call_pending ();
523 }
524
525 do 561 do
526 { 562 {
563 /* queue check watchers (and execute them) */
564 if (checkcnt)
565 {
566 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
567 call_pending ();
568 }
569
527 /* update fd-related kernel structures */ 570 /* update fd-related kernel structures */
528 fd_reify (); 571 fd_reify ();
529 572
530 /* calculate blocking time */ 573 /* calculate blocking time */
531 574
557 600
558 /* update ev_now, do magic */ 601 /* update ev_now, do magic */
559 time_update (); 602 time_update ();
560 603
561 /* queue pending timers and reschedule them */ 604 /* queue pending timers and reschedule them */
605 timers_reify (); /* relative timers called last */
562 periodics_reify (); /* absolute timers first */ 606 periodics_reify (); /* absolute timers called first */
563 timers_reify (); /* relative timers second */
564 607
565 /* queue idle watchers unless io or timers are pending */ 608 /* queue idle watchers unless io or timers are pending */
566 if (!pendingcnt) 609 if (!pendingcnt)
567 queue_events ((W *)idles, idlecnt, EV_IDLE); 610 queue_events ((W *)idles, idlecnt, EV_IDLE);
568 611
569 /* queue check and possibly idle watchers */ 612 /* queue check watchers, to be executed first */
613 if (checkcnt)
570 queue_events ((W *)checks, checkcnt, EV_CHECK); 614 queue_events ((W *)checks, checkcnt, EV_CHECK);
571 615
572 call_pending (); 616 call_pending ();
573 } 617 }
574 while (!ev_loop_done); 618 while (!ev_loop_done);
575 619
600 head = &(*head)->next; 644 head = &(*head)->next;
601 } 645 }
602} 646}
603 647
604static void 648static void
649ev_clear (W w)
650{
651 if (w->pending)
652 {
653 pendings [w->pending - 1].w = 0;
654 w->pending = 0;
655 }
656}
657
658static void
605ev_start (W w, int active) 659ev_start (W w, int active)
606{ 660{
607 w->pending = 0;
608 w->active = active; 661 w->active = active;
609} 662}
610 663
611static void 664static void
612ev_stop (W w) 665ev_stop (W w)
613{ 666{
614 if (w->pending)
615 pendings [w->pending - 1].w = 0;
616
617 w->active = 0; 667 w->active = 0;
618} 668}
619 669
620/*****************************************************************************/ 670/*****************************************************************************/
621 671
637} 687}
638 688
639void 689void
640evio_stop (struct ev_io *w) 690evio_stop (struct ev_io *w)
641{ 691{
692 ev_clear ((W)w);
642 if (!ev_is_active (w)) 693 if (!ev_is_active (w))
643 return; 694 return;
644 695
645 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 696 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
646 ev_stop ((W)w); 697 ev_stop ((W)w);
647 698
648 ++fdchangecnt; 699 ++fdchangecnt;
649 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 700 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
650 fdchanges [fdchangecnt - 1] = w->fd; 701 fdchanges [fdchangecnt - 1] = w->fd;
651} 702}
652
653 703
654void 704void
655evtimer_start (struct ev_timer *w) 705evtimer_start (struct ev_timer *w)
656{ 706{
657 if (ev_is_active (w)) 707 if (ev_is_active (w))
668} 718}
669 719
670void 720void
671evtimer_stop (struct ev_timer *w) 721evtimer_stop (struct ev_timer *w)
672{ 722{
723 ev_clear ((W)w);
673 if (!ev_is_active (w)) 724 if (!ev_is_active (w))
674 return; 725 return;
675 726
676 if (w->active < timercnt--) 727 if (w->active < timercnt--)
677 { 728 {
720} 771}
721 772
722void 773void
723evperiodic_stop (struct ev_periodic *w) 774evperiodic_stop (struct ev_periodic *w)
724{ 775{
776 ev_clear ((W)w);
725 if (!ev_is_active (w)) 777 if (!ev_is_active (w))
726 return; 778 return;
727 779
728 if (w->active < periodiccnt--) 780 if (w->active < periodiccnt--)
729 { 781 {
755} 807}
756 808
757void 809void
758evsignal_stop (struct ev_signal *w) 810evsignal_stop (struct ev_signal *w)
759{ 811{
812 ev_clear ((W)w);
760 if (!ev_is_active (w)) 813 if (!ev_is_active (w))
761 return; 814 return;
762 815
763 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 816 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
764 ev_stop ((W)w); 817 ev_stop ((W)w);
777 idles [idlecnt - 1] = w; 830 idles [idlecnt - 1] = w;
778} 831}
779 832
780void evidle_stop (struct ev_idle *w) 833void evidle_stop (struct ev_idle *w)
781{ 834{
835 ev_clear ((W)w);
836 if (ev_is_active (w))
837 return;
838
782 idles [w->active - 1] = idles [--idlecnt]; 839 idles [w->active - 1] = idles [--idlecnt];
840 ev_stop ((W)w);
841}
842
843void evprepare_start (struct ev_prepare *w)
844{
845 if (ev_is_active (w))
846 return;
847
848 ev_start ((W)w, ++preparecnt);
849 array_needsize (prepares, preparemax, preparecnt, );
850 prepares [preparecnt - 1] = w;
851}
852
853void evprepare_stop (struct ev_prepare *w)
854{
855 ev_clear ((W)w);
856 if (ev_is_active (w))
857 return;
858
859 prepares [w->active - 1] = prepares [--preparecnt];
783 ev_stop ((W)w); 860 ev_stop ((W)w);
784} 861}
785 862
786void evcheck_start (struct ev_check *w) 863void evcheck_start (struct ev_check *w)
787{ 864{
793 checks [checkcnt - 1] = w; 870 checks [checkcnt - 1] = w;
794} 871}
795 872
796void evcheck_stop (struct ev_check *w) 873void evcheck_stop (struct ev_check *w)
797{ 874{
875 ev_clear ((W)w);
876 if (ev_is_active (w))
877 return;
878
798 checks [w->active - 1] = checks [--checkcnt]; 879 checks [w->active - 1] = checks [--checkcnt];
799 ev_stop ((W)w); 880 ev_stop ((W)w);
881}
882
883/*****************************************************************************/
884
885struct ev_once
886{
887 struct ev_io io;
888 struct ev_timer to;
889 void (*cb)(int revents, void *arg);
890 void *arg;
891};
892
893static void
894once_cb (struct ev_once *once, int revents)
895{
896 void (*cb)(int revents, void *arg) = once->cb;
897 void *arg = once->arg;
898
899 evio_stop (&once->io);
900 evtimer_stop (&once->to);
901 free (once);
902
903 cb (revents, arg);
904}
905
906static void
907once_cb_io (struct ev_io *w, int revents)
908{
909 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
910}
911
912static void
913once_cb_to (struct ev_timer *w, int revents)
914{
915 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
916}
917
918void
919ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
920{
921 struct ev_once *once = malloc (sizeof (struct ev_once));
922
923 if (!once)
924 cb (EV_ERROR, arg);
925 else
926 {
927 once->cb = cb;
928 once->arg = arg;
929
930 evw_init (&once->io, once_cb_io);
931
932 if (fd >= 0)
933 {
934 evio_set (&once->io, fd, events);
935 evio_start (&once->io);
936 }
937
938 evw_init (&once->to, once_cb_to);
939
940 if (timeout >= 0.)
941 {
942 evtimer_set (&once->to, timeout, 0.);
943 evtimer_start (&once->to);
944 }
945 }
800} 946}
801 947
802/*****************************************************************************/ 948/*****************************************************************************/
803 949
804#if 0 950#if 0

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines