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.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
125static int pendingmax, pendingcnt; 153static int pendingmax, pendingcnt;
126 154
127static void 155static void
128event (W w, int events) 156event (W w, int events)
129{ 157{
158 if (w->active)
159 {
130 w->pending = ++pendingcnt; 160 w->pending = ++pendingcnt;
131 array_needsize (pendings, pendingmax, pendingcnt, ); 161 array_needsize (pendings, pendingmax, pendingcnt, );
132 pendings [pendingcnt - 1].w = w; 162 pendings [pendingcnt - 1].w = w;
133 pendings [pendingcnt - 1].events = events; 163 pendings [pendingcnt - 1].events = events;
164 }
134} 165}
135 166
136static void 167static void
137fd_event (int fd, int events) 168fd_event (int fd, int events)
138{ 169{
153{ 184{
154 int i; 185 int i;
155 186
156 for (i = 0; i < eventcnt; ++i) 187 for (i = 0; i < eventcnt; ++i)
157 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);
158} 202}
159 203
160/*****************************************************************************/ 204/*****************************************************************************/
161 205
162static struct ev_timer **timers; 206static struct ev_timer **timers;
279 323
280/*****************************************************************************/ 324/*****************************************************************************/
281 325
282static struct ev_idle **idles; 326static struct ev_idle **idles;
283static int idlemax, idlecnt; 327static int idlemax, idlecnt;
328
329static struct ev_prepare **prepares;
330static int preparemax, preparecnt;
284 331
285static struct ev_check **checks; 332static struct ev_check **checks;
286static int checkmax, checkcnt; 333static int checkmax, checkcnt;
287 334
288/*****************************************************************************/ 335/*****************************************************************************/
383} 430}
384 431
385static void 432static void
386call_pending () 433call_pending ()
387{ 434{
388 int i; 435 while (pendingcnt)
389
390 for (i = 0; i < pendingcnt; ++i)
391 { 436 {
392 ANPENDING *p = pendings + i; 437 ANPENDING *p = pendings + --pendingcnt;
393 438
394 if (p->w) 439 if (p->w)
395 { 440 {
396 p->w->pending = 0; 441 p->w->pending = 0;
397 p->w->cb (p->w, p->events); 442 p->w->cb (p->w, p->events);
398 } 443 }
399 } 444 }
400
401 pendingcnt = 0;
402} 445}
403 446
404static void 447static void
405timers_reify () 448timers_reify ()
406{ 449{
407 while (timercnt && timers [0]->at <= now) 450 while (timercnt && timers [0]->at <= now)
408 { 451 {
409 struct ev_timer *w = timers [0]; 452 struct ev_timer *w = timers [0];
453
454 event ((W)w, EV_TIMEOUT);
410 455
411 /* first reschedule or stop timer */ 456 /* first reschedule or stop timer */
412 if (w->repeat) 457 if (w->repeat)
413 { 458 {
414 w->at = now + w->repeat; 459 w->at = now + w->repeat;
415 assert (("timer timeout in the past, negative repeat?", w->at > now)); 460 assert (("timer timeout in the past, negative repeat?", w->at > now));
416 downheap ((WT *)timers, timercnt, 0); 461 downheap ((WT *)timers, timercnt, 0);
417 } 462 }
418 else 463 else
419 evtimer_stop (w); /* nonrepeating: stop timer */ 464 evtimer_stop (w); /* nonrepeating: stop timer */
420
421 event ((W)w, EV_TIMEOUT);
422 } 465 }
423} 466}
424 467
425static void 468static void
426periodics_reify () 469periodics_reify ()
513void ev_loop (int flags) 556void ev_loop (int flags)
514{ 557{
515 double block; 558 double block;
516 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 559 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
517 560
518 if (checkcnt)
519 {
520 queue_events ((W *)checks, checkcnt, EV_CHECK);
521 call_pending ();
522 }
523
524 do 561 do
525 { 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
526 /* update fd-related kernel structures */ 570 /* update fd-related kernel structures */
527 fd_reify (); 571 fd_reify ();
528 572
529 /* calculate blocking time */ 573 /* calculate blocking time */
530 574
531 /* 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 */
532 ev_now = ev_time (); 577 ev_now = ev_time ();
533 578
534 if (flags & EVLOOP_NONBLOCK || idlecnt) 579 if (flags & EVLOOP_NONBLOCK || idlecnt)
535 block = 0.; 580 block = 0.;
536 else 581 else
556 601
557 /* update ev_now, do magic */ 602 /* update ev_now, do magic */
558 time_update (); 603 time_update ();
559 604
560 /* queue pending timers and reschedule them */ 605 /* queue pending timers and reschedule them */
606 timers_reify (); /* relative timers called last */
561 periodics_reify (); /* absolute timers first */ 607 periodics_reify (); /* absolute timers called first */
562 timers_reify (); /* relative timers second */
563 608
564 /* queue idle watchers unless io or timers are pending */ 609 /* queue idle watchers unless io or timers are pending */
565 if (!pendingcnt) 610 if (!pendingcnt)
566 queue_events ((W *)idles, idlecnt, EV_IDLE); 611 queue_events ((W *)idles, idlecnt, EV_IDLE);
567 612
568 /* queue check and possibly idle watchers */ 613 /* queue check watchers, to be executed first */
614 if (checkcnt)
569 queue_events ((W *)checks, checkcnt, EV_CHECK); 615 queue_events ((W *)checks, checkcnt, EV_CHECK);
570 616
571 call_pending (); 617 call_pending ();
572 } 618 }
573 while (!ev_loop_done); 619 while (!ev_loop_done);
574 620
599 head = &(*head)->next; 645 head = &(*head)->next;
600 } 646 }
601} 647}
602 648
603static 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
604ev_start (W w, int active) 660ev_start (W w, int active)
605{ 661{
606 w->pending = 0;
607 w->active = active; 662 w->active = active;
608} 663}
609 664
610static void 665static void
611ev_stop (W w) 666ev_stop (W w)
612{ 667{
613 if (w->pending)
614 pendings [w->pending - 1].w = 0;
615
616 w->active = 0; 668 w->active = 0;
617} 669}
618 670
619/*****************************************************************************/ 671/*****************************************************************************/
620 672
636} 688}
637 689
638void 690void
639evio_stop (struct ev_io *w) 691evio_stop (struct ev_io *w)
640{ 692{
693 ev_clear ((W)w);
641 if (!ev_is_active (w)) 694 if (!ev_is_active (w))
642 return; 695 return;
643 696
644 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 697 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
645 ev_stop ((W)w); 698 ev_stop ((W)w);
646 699
647 ++fdchangecnt; 700 ++fdchangecnt;
648 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 701 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
649 fdchanges [fdchangecnt - 1] = w->fd; 702 fdchanges [fdchangecnt - 1] = w->fd;
650} 703}
651
652 704
653void 705void
654evtimer_start (struct ev_timer *w) 706evtimer_start (struct ev_timer *w)
655{ 707{
656 if (ev_is_active (w)) 708 if (ev_is_active (w))
667} 719}
668 720
669void 721void
670evtimer_stop (struct ev_timer *w) 722evtimer_stop (struct ev_timer *w)
671{ 723{
724 ev_clear ((W)w);
672 if (!ev_is_active (w)) 725 if (!ev_is_active (w))
673 return; 726 return;
674 727
675 if (w->active < timercnt--) 728 if (w->active < timercnt--)
676 { 729 {
719} 772}
720 773
721void 774void
722evperiodic_stop (struct ev_periodic *w) 775evperiodic_stop (struct ev_periodic *w)
723{ 776{
777 ev_clear ((W)w);
724 if (!ev_is_active (w)) 778 if (!ev_is_active (w))
725 return; 779 return;
726 780
727 if (w->active < periodiccnt--) 781 if (w->active < periodiccnt--)
728 { 782 {
754} 808}
755 809
756void 810void
757evsignal_stop (struct ev_signal *w) 811evsignal_stop (struct ev_signal *w)
758{ 812{
813 ev_clear ((W)w);
759 if (!ev_is_active (w)) 814 if (!ev_is_active (w))
760 return; 815 return;
761 816
762 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 817 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
763 ev_stop ((W)w); 818 ev_stop ((W)w);
776 idles [idlecnt - 1] = w; 831 idles [idlecnt - 1] = w;
777} 832}
778 833
779void evidle_stop (struct ev_idle *w) 834void evidle_stop (struct ev_idle *w)
780{ 835{
836 ev_clear ((W)w);
837 if (ev_is_active (w))
838 return;
839
781 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];
782 ev_stop ((W)w); 861 ev_stop ((W)w);
783} 862}
784 863
785void evcheck_start (struct ev_check *w) 864void evcheck_start (struct ev_check *w)
786{ 865{
792 checks [checkcnt - 1] = w; 871 checks [checkcnt - 1] = w;
793} 872}
794 873
795void evcheck_stop (struct ev_check *w) 874void evcheck_stop (struct ev_check *w)
796{ 875{
876 ev_clear ((W)w);
877 if (ev_is_active (w))
878 return;
879
797 checks [w->active - 1] = checks [--checkcnt]; 880 checks [w->active - 1] = checks [--checkcnt];
798 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 }
799} 947}
800 948
801/*****************************************************************************/ 949/*****************************************************************************/
802 950
803#if 0 951#if 0

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines