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.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
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 (checkcnt)
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
556 600
557 /* update ev_now, do magic */ 601 /* update ev_now, do magic */
558 time_update (); 602 time_update ();
559 603
560 /* queue pending timers and reschedule them */ 604 /* queue pending timers and reschedule them */
605 timers_reify (); /* relative timers called last */
561 periodics_reify (); /* absolute timers first */ 606 periodics_reify (); /* absolute timers called first */
562 timers_reify (); /* relative timers second */
563 607
564 /* queue idle watchers unless io or timers are pending */ 608 /* queue idle watchers unless io or timers are pending */
565 if (!pendingcnt) 609 if (!pendingcnt)
566 queue_events ((W *)idles, idlecnt, EV_IDLE); 610 queue_events ((W *)idles, idlecnt, EV_IDLE);
567 611
568 /* queue check and possibly idle watchers */ 612 /* queue check watchers, to be executed first */
613 if (checkcnt)
569 queue_events ((W *)checks, checkcnt, EV_CHECK); 614 queue_events ((W *)checks, checkcnt, EV_CHECK);
570 615
571 call_pending (); 616 call_pending ();
572 } 617 }
573 while (!ev_loop_done); 618 while (!ev_loop_done);
574 619
599 head = &(*head)->next; 644 head = &(*head)->next;
600 } 645 }
601} 646}
602 647
603static 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
604ev_start (W w, int active) 659ev_start (W w, int active)
605{ 660{
606 w->pending = 0;
607 w->active = active; 661 w->active = active;
608} 662}
609 663
610static void 664static void
611ev_stop (W w) 665ev_stop (W w)
612{ 666{
613 if (w->pending)
614 pendings [w->pending - 1].w = 0;
615
616 w->active = 0; 667 w->active = 0;
617} 668}
618 669
619/*****************************************************************************/ 670/*****************************************************************************/
620 671
636} 687}
637 688
638void 689void
639evio_stop (struct ev_io *w) 690evio_stop (struct ev_io *w)
640{ 691{
692 ev_clear ((W)w);
641 if (!ev_is_active (w)) 693 if (!ev_is_active (w))
642 return; 694 return;
643 695
644 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 696 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
645 ev_stop ((W)w); 697 ev_stop ((W)w);
646 698
647 ++fdchangecnt; 699 ++fdchangecnt;
648 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 700 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
649 fdchanges [fdchangecnt - 1] = w->fd; 701 fdchanges [fdchangecnt - 1] = w->fd;
650} 702}
651
652 703
653void 704void
654evtimer_start (struct ev_timer *w) 705evtimer_start (struct ev_timer *w)
655{ 706{
656 if (ev_is_active (w)) 707 if (ev_is_active (w))
667} 718}
668 719
669void 720void
670evtimer_stop (struct ev_timer *w) 721evtimer_stop (struct ev_timer *w)
671{ 722{
723 ev_clear ((W)w);
672 if (!ev_is_active (w)) 724 if (!ev_is_active (w))
673 return; 725 return;
674 726
675 if (w->active < timercnt--) 727 if (w->active < timercnt--)
676 { 728 {
719} 771}
720 772
721void 773void
722evperiodic_stop (struct ev_periodic *w) 774evperiodic_stop (struct ev_periodic *w)
723{ 775{
776 ev_clear ((W)w);
724 if (!ev_is_active (w)) 777 if (!ev_is_active (w))
725 return; 778 return;
726 779
727 if (w->active < periodiccnt--) 780 if (w->active < periodiccnt--)
728 { 781 {
754} 807}
755 808
756void 809void
757evsignal_stop (struct ev_signal *w) 810evsignal_stop (struct ev_signal *w)
758{ 811{
812 ev_clear ((W)w);
759 if (!ev_is_active (w)) 813 if (!ev_is_active (w))
760 return; 814 return;
761 815
762 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 816 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
763 ev_stop ((W)w); 817 ev_stop ((W)w);
776 idles [idlecnt - 1] = w; 830 idles [idlecnt - 1] = w;
777} 831}
778 832
779void evidle_stop (struct ev_idle *w) 833void evidle_stop (struct ev_idle *w)
780{ 834{
835 ev_clear ((W)w);
836 if (ev_is_active (w))
837 return;
838
781 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];
782 ev_stop ((W)w); 860 ev_stop ((W)w);
783} 861}
784 862
785void evcheck_start (struct ev_check *w) 863void evcheck_start (struct ev_check *w)
786{ 864{
792 checks [checkcnt - 1] = w; 870 checks [checkcnt - 1] = w;
793} 871}
794 872
795void evcheck_stop (struct ev_check *w) 873void evcheck_stop (struct ev_check *w)
796{ 874{
875 ev_clear ((W)w);
876 if (ev_is_active (w))
877 return;
878
797 checks [w->active - 1] = checks [--checkcnt]; 879 checks [w->active - 1] = checks [--checkcnt];
798 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 }
799} 946}
800 947
801/*****************************************************************************/ 948/*****************************************************************************/
802 949
803#if 0 950#if 0

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines