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.21 by root, Wed Oct 31 18:37:38 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 (preparecnt)
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
532 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ 575 /* we only need this for !monotonic clockor timers, but as we basically
576 always have timers, we just calculate it always */
533 ev_now = ev_time (); 577 ev_now = ev_time ();
534 578
535 if (flags & EVLOOP_NONBLOCK || idlecnt) 579 if (flags & EVLOOP_NONBLOCK || idlecnt)
536 block = 0.; 580 block = 0.;
537 else 581 else
538 { 582 {
539 block = MAX_BLOCKTIME; 583 block = MAX_BLOCKTIME;
540 584
541 if (timercnt) 585 if (timercnt)
542 { 586 {
543 ev_tstamp to = timers [0]->at - get_clock () + method_fudge; 587 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
544 if (block > to) block = to; 588 if (block > to) block = to;
545 } 589 }
546 590
547 if (periodiccnt) 591 if (periodiccnt)
548 { 592 {
557 601
558 /* update ev_now, do magic */ 602 /* update ev_now, do magic */
559 time_update (); 603 time_update ();
560 604
561 /* queue pending timers and reschedule them */ 605 /* queue pending timers and reschedule them */
606 timers_reify (); /* relative timers called last */
562 periodics_reify (); /* absolute timers first */ 607 periodics_reify (); /* absolute timers called first */
563 timers_reify (); /* relative timers second */
564 608
565 /* queue idle watchers unless io or timers are pending */ 609 /* queue idle watchers unless io or timers are pending */
566 if (!pendingcnt) 610 if (!pendingcnt)
567 queue_events ((W *)idles, idlecnt, EV_IDLE); 611 queue_events ((W *)idles, idlecnt, EV_IDLE);
568 612
569 /* queue check and possibly idle watchers */ 613 /* queue check watchers, to be executed first */
614 if (checkcnt)
570 queue_events ((W *)checks, checkcnt, EV_CHECK); 615 queue_events ((W *)checks, checkcnt, EV_CHECK);
571 616
572 call_pending (); 617 call_pending ();
573 } 618 }
574 while (!ev_loop_done); 619 while (!ev_loop_done);
575 620
600 head = &(*head)->next; 645 head = &(*head)->next;
601 } 646 }
602} 647}
603 648
604static void 649static void
650ev_clear (W w)
651{
652 if (w->pending)
653 {
654 pendings [w->pending - 1].w = 0;
655 w->pending = 0;
656 }
657}
658
659static void
605ev_start (W w, int active) 660ev_start (W w, int active)
606{ 661{
607 w->pending = 0;
608 w->active = active; 662 w->active = active;
609} 663}
610 664
611static void 665static void
612ev_stop (W w) 666ev_stop (W w)
613{ 667{
614 if (w->pending)
615 pendings [w->pending - 1].w = 0;
616
617 w->active = 0; 668 w->active = 0;
618} 669}
619 670
620/*****************************************************************************/ 671/*****************************************************************************/
621 672
637} 688}
638 689
639void 690void
640evio_stop (struct ev_io *w) 691evio_stop (struct ev_io *w)
641{ 692{
693 ev_clear ((W)w);
642 if (!ev_is_active (w)) 694 if (!ev_is_active (w))
643 return; 695 return;
644 696
645 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 697 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
646 ev_stop ((W)w); 698 ev_stop ((W)w);
647 699
648 ++fdchangecnt; 700 ++fdchangecnt;
649 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 701 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
650 fdchanges [fdchangecnt - 1] = w->fd; 702 fdchanges [fdchangecnt - 1] = w->fd;
651} 703}
652
653 704
654void 705void
655evtimer_start (struct ev_timer *w) 706evtimer_start (struct ev_timer *w)
656{ 707{
657 if (ev_is_active (w)) 708 if (ev_is_active (w))
668} 719}
669 720
670void 721void
671evtimer_stop (struct ev_timer *w) 722evtimer_stop (struct ev_timer *w)
672{ 723{
724 ev_clear ((W)w);
673 if (!ev_is_active (w)) 725 if (!ev_is_active (w))
674 return; 726 return;
675 727
676 if (w->active < timercnt--) 728 if (w->active < timercnt--)
677 { 729 {
678 timers [w->active - 1] = timers [timercnt]; 730 timers [w->active - 1] = timers [timercnt];
679 downheap ((WT *)timers, timercnt, w->active - 1); 731 downheap ((WT *)timers, timercnt, w->active - 1);
680 } 732 }
681 733
734 w->at = w->repeat;
735
682 ev_stop ((W)w); 736 ev_stop ((W)w);
737}
738
739void
740evtimer_again (struct ev_timer *w)
741{
742 if (ev_is_active (w))
743 {
744 if (w->repeat)
745 {
746 w->at = now + w->repeat;
747 downheap ((WT *)timers, timercnt, w->active - 1);
748 }
749 else
750 evtimer_stop (w);
751 }
752 else if (w->repeat)
753 evtimer_start (w);
683} 754}
684 755
685void 756void
686evperiodic_start (struct ev_periodic *w) 757evperiodic_start (struct ev_periodic *w)
687{ 758{
701} 772}
702 773
703void 774void
704evperiodic_stop (struct ev_periodic *w) 775evperiodic_stop (struct ev_periodic *w)
705{ 776{
777 ev_clear ((W)w);
706 if (!ev_is_active (w)) 778 if (!ev_is_active (w))
707 return; 779 return;
708 780
709 if (w->active < periodiccnt--) 781 if (w->active < periodiccnt--)
710 { 782 {
736} 808}
737 809
738void 810void
739evsignal_stop (struct ev_signal *w) 811evsignal_stop (struct ev_signal *w)
740{ 812{
813 ev_clear ((W)w);
741 if (!ev_is_active (w)) 814 if (!ev_is_active (w))
742 return; 815 return;
743 816
744 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 817 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
745 ev_stop ((W)w); 818 ev_stop ((W)w);
758 idles [idlecnt - 1] = w; 831 idles [idlecnt - 1] = w;
759} 832}
760 833
761void evidle_stop (struct ev_idle *w) 834void evidle_stop (struct ev_idle *w)
762{ 835{
836 ev_clear ((W)w);
837 if (ev_is_active (w))
838 return;
839
763 idles [w->active - 1] = idles [--idlecnt]; 840 idles [w->active - 1] = idles [--idlecnt];
841 ev_stop ((W)w);
842}
843
844void evprepare_start (struct ev_prepare *w)
845{
846 if (ev_is_active (w))
847 return;
848
849 ev_start ((W)w, ++preparecnt);
850 array_needsize (prepares, preparemax, preparecnt, );
851 prepares [preparecnt - 1] = w;
852}
853
854void evprepare_stop (struct ev_prepare *w)
855{
856 ev_clear ((W)w);
857 if (ev_is_active (w))
858 return;
859
860 prepares [w->active - 1] = prepares [--preparecnt];
764 ev_stop ((W)w); 861 ev_stop ((W)w);
765} 862}
766 863
767void evcheck_start (struct ev_check *w) 864void evcheck_start (struct ev_check *w)
768{ 865{
774 checks [checkcnt - 1] = w; 871 checks [checkcnt - 1] = w;
775} 872}
776 873
777void evcheck_stop (struct ev_check *w) 874void evcheck_stop (struct ev_check *w)
778{ 875{
876 ev_clear ((W)w);
877 if (ev_is_active (w))
878 return;
879
779 checks [w->active - 1] = checks [--checkcnt]; 880 checks [w->active - 1] = checks [--checkcnt];
780 ev_stop ((W)w); 881 ev_stop ((W)w);
882}
883
884/*****************************************************************************/
885
886struct ev_once
887{
888 struct ev_io io;
889 struct ev_timer to;
890 void (*cb)(int revents, void *arg);
891 void *arg;
892};
893
894static void
895once_cb (struct ev_once *once, int revents)
896{
897 void (*cb)(int revents, void *arg) = once->cb;
898 void *arg = once->arg;
899
900 evio_stop (&once->io);
901 evtimer_stop (&once->to);
902 free (once);
903
904 cb (revents, arg);
905}
906
907static void
908once_cb_io (struct ev_io *w, int revents)
909{
910 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
911}
912
913static void
914once_cb_to (struct ev_timer *w, int revents)
915{
916 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
917}
918
919void
920ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
921{
922 struct ev_once *once = malloc (sizeof (struct ev_once));
923
924 if (!once)
925 cb (EV_ERROR, arg);
926 else
927 {
928 once->cb = cb;
929 once->arg = arg;
930
931 evw_init (&once->io, once_cb_io);
932
933 if (fd >= 0)
934 {
935 evio_set (&once->io, fd, events);
936 evio_start (&once->io);
937 }
938
939 evw_init (&once->to, once_cb_to);
940
941 if (timeout >= 0.)
942 {
943 evtimer_set (&once->to, timeout, 0.);
944 evtimer_start (&once->to);
945 }
946 }
781} 947}
782 948
783/*****************************************************************************/ 949/*****************************************************************************/
784 950
785#if 0 951#if 0

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines