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

Comparing libev/ev.c (file contents):
Revision 1.12 by root, Wed Oct 31 09:23:17 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 43
14#define HAVE_EPOLL 1
15
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
20#endif 48#endif
33 61
34#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 62#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
35#define MAX_BLOCKTIME 60. 63#define MAX_BLOCKTIME 60.
36 64
37#include "ev.h" 65#include "ev.h"
38
39struct ev_watcher {
40 EV_WATCHER (ev_watcher);
41};
42
43struct ev_watcher_list {
44 EV_WATCHER_LIST (ev_watcher_list);
45};
46
47struct ev_watcher_time {
48 EV_WATCHER_TIME (ev_watcher_time);
49};
50 66
51typedef struct ev_watcher *W; 67typedef struct ev_watcher *W;
52typedef struct ev_watcher_list *WL; 68typedef struct ev_watcher_list *WL;
53typedef struct ev_watcher_time *WT; 69typedef struct ev_watcher_time *WT;
54 70
95 111
96#define array_needsize(base,cur,cnt,init) \ 112#define array_needsize(base,cur,cnt,init) \
97 if ((cnt) > cur) \ 113 if ((cnt) > cur) \
98 { \ 114 { \
99 int newcnt = cur ? cur << 1 : 16; \ 115 int newcnt = cur ? cur << 1 : 16; \
100 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\
101 base = realloc (base, sizeof (*base) * (newcnt)); \ 116 base = realloc (base, sizeof (*base) * (newcnt)); \
102 init (base + cur, newcnt - cur); \ 117 init (base + cur, newcnt - cur); \
103 cur = newcnt; \ 118 cur = newcnt; \
104 } 119 }
105 120
138static int pendingmax, pendingcnt; 153static int pendingmax, pendingcnt;
139 154
140static void 155static void
141event (W w, int events) 156event (W w, int events)
142{ 157{
158 if (w->active)
159 {
143 w->pending = ++pendingcnt; 160 w->pending = ++pendingcnt;
144 array_needsize (pendings, pendingmax, pendingcnt, ); 161 array_needsize (pendings, pendingmax, pendingcnt, );
145 pendings [pendingcnt - 1].w = w; 162 pendings [pendingcnt - 1].w = w;
146 pendings [pendingcnt - 1].events = events; 163 pendings [pendingcnt - 1].events = events;
164 }
147} 165}
148 166
149static void 167static void
150fd_event (int fd, int events) 168fd_event (int fd, int events)
151{ 169{
166{ 184{
167 int i; 185 int i;
168 186
169 for (i = 0; i < eventcnt; ++i) 187 for (i = 0; i < eventcnt; ++i)
170 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);
171} 202}
172 203
173/*****************************************************************************/ 204/*****************************************************************************/
174 205
175static struct ev_timer **timers; 206static struct ev_timer **timers;
292 323
293/*****************************************************************************/ 324/*****************************************************************************/
294 325
295static struct ev_idle **idles; 326static struct ev_idle **idles;
296static int idlemax, idlecnt; 327static int idlemax, idlecnt;
328
329static struct ev_prepare **prepares;
330static int preparemax, preparecnt;
297 331
298static struct ev_check **checks; 332static struct ev_check **checks;
299static int checkmax, checkcnt; 333static int checkmax, checkcnt;
300 334
301/*****************************************************************************/ 335/*****************************************************************************/
396} 430}
397 431
398static void 432static void
399call_pending () 433call_pending ()
400{ 434{
401 int i; 435 while (pendingcnt)
402
403 for (i = 0; i < pendingcnt; ++i)
404 { 436 {
405 ANPENDING *p = pendings + i; 437 ANPENDING *p = pendings + --pendingcnt;
406 438
407 if (p->w) 439 if (p->w)
408 { 440 {
409 p->w->pending = 0; 441 p->w->pending = 0;
410 p->w->cb (p->w, p->events); 442 p->w->cb (p->w, p->events);
411 } 443 }
412 } 444 }
413
414 pendingcnt = 0;
415} 445}
416 446
417static void 447static void
418timers_reify () 448timers_reify ()
419{ 449{
420 while (timercnt && timers [0]->at <= now) 450 while (timercnt && timers [0]->at <= now)
421 { 451 {
422 struct ev_timer *w = timers [0]; 452 struct ev_timer *w = timers [0];
453
454 event ((W)w, EV_TIMEOUT);
423 455
424 /* first reschedule or stop timer */ 456 /* first reschedule or stop timer */
425 if (w->repeat) 457 if (w->repeat)
426 { 458 {
427 w->at = now + w->repeat; 459 w->at = now + w->repeat;
428 assert (("timer timeout in the past, negative repeat?", w->at > now)); 460 assert (("timer timeout in the past, negative repeat?", w->at > now));
429 downheap ((WT *)timers, timercnt, 0); 461 downheap ((WT *)timers, timercnt, 0);
430 } 462 }
431 else 463 else
432 evtimer_stop (w); /* nonrepeating: stop timer */ 464 evtimer_stop (w); /* nonrepeating: stop timer */
433
434 event ((W)w, EV_TIMEOUT);
435 } 465 }
436} 466}
437 467
438static void 468static void
439periodics_reify () 469periodics_reify ()
455 event ((W)w, EV_TIMEOUT); 485 event ((W)w, EV_TIMEOUT);
456 } 486 }
457} 487}
458 488
459static void 489static void
460time_jump (ev_tstamp diff) 490periodics_reschedule (ev_tstamp diff)
461{ 491{
462 int i; 492 int i;
463 493
464 /* adjust periodics */ 494 /* adjust periodics after time jump */
465 for (i = 0; i < periodiccnt; ++i) 495 for (i = 0; i < periodiccnt; ++i)
466 { 496 {
467 struct ev_periodic *w = periodics [i]; 497 struct ev_periodic *w = periodics [i];
468 498
469 if (w->interval) 499 if (w->interval)
477 507
478 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 508 i = 0; /* restart loop, inefficient, but time jumps should be rare */
479 } 509 }
480 } 510 }
481 } 511 }
482
483 /* adjust timers. this is easy, as the offset is the same for all */
484 for (i = 0; i < timercnt; ++i)
485 timers [i]->at += diff;
486} 512}
487 513
488static void 514static void
489time_update () 515time_update ()
490{ 516{
505 return; /* all is well */ 531 return; /* all is well */
506 532
507 ev_now = ev_time (); 533 ev_now = ev_time ();
508 } 534 }
509 535
510 time_jump (diff - odiff); 536 periodics_reschedule (diff - odiff);
537 /* no timer adjustment, as the monotonic clock doesn't jump */
511 } 538 }
512 else 539 else
513 { 540 {
514 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 541 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
515 time_jump (ev_now - now); 542 {
543 periodics_reschedule (ev_now - now);
544
545 /* adjust timers. this is easy, as the offset is the same for all */
546 for (i = 0; i < timercnt; ++i)
547 timers [i]->at += diff;
548 }
516 549
517 now = ev_now; 550 now = ev_now;
518 } 551 }
519} 552}
520 553
521int ev_loop_done; 554int ev_loop_done;
522 555
523void ev_loop (int flags) 556void ev_loop (int flags)
524{ 557{
525 double block; 558 double block;
526 ev_loop_done = flags & EVLOOP_ONESHOT; 559 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
527
528 if (checkcnt)
529 {
530 queue_events ((W *)checks, checkcnt, EV_CHECK);
531 call_pending ();
532 }
533 560
534 do 561 do
535 { 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
536 /* update fd-related kernel structures */ 570 /* update fd-related kernel structures */
537 fd_reify (); 571 fd_reify ();
538 572
539 /* calculate blocking time */ 573 /* calculate blocking time */
540 574
541 /* 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 */
542 ev_now = ev_time (); 577 ev_now = ev_time ();
543 578
544 if (flags & EVLOOP_NONBLOCK || idlecnt) 579 if (flags & EVLOOP_NONBLOCK || idlecnt)
545 block = 0.; 580 block = 0.;
546 else 581 else
547 { 582 {
548 block = MAX_BLOCKTIME; 583 block = MAX_BLOCKTIME;
549 584
550 if (timercnt) 585 if (timercnt)
551 { 586 {
552 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;
553 if (block > to) block = to; 588 if (block > to) block = to;
554 } 589 }
555 590
556 if (periodiccnt) 591 if (periodiccnt)
557 { 592 {
566 601
567 /* update ev_now, do magic */ 602 /* update ev_now, do magic */
568 time_update (); 603 time_update ();
569 604
570 /* queue pending timers and reschedule them */ 605 /* queue pending timers and reschedule them */
606 timers_reify (); /* relative timers called last */
571 periodics_reify (); /* absolute timers first */ 607 periodics_reify (); /* absolute timers called first */
572 timers_reify (); /* relative timers second */
573 608
574 /* queue idle watchers unless io or timers are pending */ 609 /* queue idle watchers unless io or timers are pending */
575 if (!pendingcnt) 610 if (!pendingcnt)
576 queue_events ((W *)idles, idlecnt, EV_IDLE); 611 queue_events ((W *)idles, idlecnt, EV_IDLE);
577 612
578 /* queue check and possibly idle watchers */ 613 /* queue check watchers, to be executed first */
614 if (checkcnt)
579 queue_events ((W *)checks, checkcnt, EV_CHECK); 615 queue_events ((W *)checks, checkcnt, EV_CHECK);
580 616
581 call_pending (); 617 call_pending ();
582 } 618 }
583 while (!ev_loop_done); 619 while (!ev_loop_done);
620
621 if (ev_loop_done != 2)
622 ev_loop_done = 0;
584} 623}
585 624
586/*****************************************************************************/ 625/*****************************************************************************/
587 626
588static void 627static void
606 head = &(*head)->next; 645 head = &(*head)->next;
607 } 646 }
608} 647}
609 648
610static 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
611ev_start (W w, int active) 660ev_start (W w, int active)
612{ 661{
613 w->pending = 0;
614 w->active = active; 662 w->active = active;
615} 663}
616 664
617static void 665static void
618ev_stop (W w) 666ev_stop (W w)
619{ 667{
620 if (w->pending)
621 pendings [w->pending - 1].w = 0;
622
623 w->active = 0; 668 w->active = 0;
624} 669}
625 670
626/*****************************************************************************/ 671/*****************************************************************************/
627 672
643} 688}
644 689
645void 690void
646evio_stop (struct ev_io *w) 691evio_stop (struct ev_io *w)
647{ 692{
693 ev_clear ((W)w);
648 if (!ev_is_active (w)) 694 if (!ev_is_active (w))
649 return; 695 return;
650 696
651 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 697 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
652 ev_stop ((W)w); 698 ev_stop ((W)w);
654 ++fdchangecnt; 700 ++fdchangecnt;
655 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 701 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
656 fdchanges [fdchangecnt - 1] = w->fd; 702 fdchanges [fdchangecnt - 1] = w->fd;
657} 703}
658 704
659
660void 705void
661evtimer_start (struct ev_timer *w) 706evtimer_start (struct ev_timer *w)
662{ 707{
663 if (ev_is_active (w)) 708 if (ev_is_active (w))
664 return; 709 return;
665 710
666 w->at += now; 711 w->at += now;
712
713 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
667 714
668 ev_start ((W)w, ++timercnt); 715 ev_start ((W)w, ++timercnt);
669 array_needsize (timers, timermax, timercnt, ); 716 array_needsize (timers, timermax, timercnt, );
670 timers [timercnt - 1] = w; 717 timers [timercnt - 1] = w;
671 upheap ((WT *)timers, timercnt - 1); 718 upheap ((WT *)timers, timercnt - 1);
672} 719}
673 720
674void 721void
675evtimer_stop (struct ev_timer *w) 722evtimer_stop (struct ev_timer *w)
676{ 723{
724 ev_clear ((W)w);
677 if (!ev_is_active (w)) 725 if (!ev_is_active (w))
678 return; 726 return;
679 727
680 if (w->active < timercnt--) 728 if (w->active < timercnt--)
681 { 729 {
682 timers [w->active - 1] = timers [timercnt]; 730 timers [w->active - 1] = timers [timercnt];
683 downheap ((WT *)timers, timercnt, w->active - 1); 731 downheap ((WT *)timers, timercnt, w->active - 1);
684 } 732 }
685 733
734 w->at = w->repeat;
735
686 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);
687} 754}
688 755
689void 756void
690evperiodic_start (struct ev_periodic *w) 757evperiodic_start (struct ev_periodic *w)
691{ 758{
692 if (ev_is_active (w)) 759 if (ev_is_active (w))
693 return; 760 return;
761
762 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
694 763
695 /* this formula differs from the one in periodic_reify because we do not always round up */ 764 /* this formula differs from the one in periodic_reify because we do not always round up */
696 if (w->interval) 765 if (w->interval)
697 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 766 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
698 767
703} 772}
704 773
705void 774void
706evperiodic_stop (struct ev_periodic *w) 775evperiodic_stop (struct ev_periodic *w)
707{ 776{
777 ev_clear ((W)w);
708 if (!ev_is_active (w)) 778 if (!ev_is_active (w))
709 return; 779 return;
710 780
711 if (w->active < periodiccnt--) 781 if (w->active < periodiccnt--)
712 { 782 {
738} 808}
739 809
740void 810void
741evsignal_stop (struct ev_signal *w) 811evsignal_stop (struct ev_signal *w)
742{ 812{
813 ev_clear ((W)w);
743 if (!ev_is_active (w)) 814 if (!ev_is_active (w))
744 return; 815 return;
745 816
746 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 817 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
747 ev_stop ((W)w); 818 ev_stop ((W)w);
760 idles [idlecnt - 1] = w; 831 idles [idlecnt - 1] = w;
761} 832}
762 833
763void evidle_stop (struct ev_idle *w) 834void evidle_stop (struct ev_idle *w)
764{ 835{
836 ev_clear ((W)w);
837 if (ev_is_active (w))
838 return;
839
765 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];
766 ev_stop ((W)w); 861 ev_stop ((W)w);
767} 862}
768 863
769void evcheck_start (struct ev_check *w) 864void evcheck_start (struct ev_check *w)
770{ 865{
776 checks [checkcnt - 1] = w; 871 checks [checkcnt - 1] = w;
777} 872}
778 873
779void evcheck_stop (struct ev_check *w) 874void evcheck_stop (struct ev_check *w)
780{ 875{
876 ev_clear ((W)w);
877 if (ev_is_active (w))
878 return;
879
781 checks [w->active - 1] = checks [--checkcnt]; 880 checks [w->active - 1] = checks [--checkcnt];
782 ev_stop ((W)w); 881 ev_stop ((W)w);
783} 882}
784 883
785/*****************************************************************************/ 884/*****************************************************************************/
786 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 }
947}
948
949/*****************************************************************************/
950
787#if 1 951#if 0
788 952
789struct ev_io wio; 953struct ev_io wio;
790 954
791static void 955static void
792sin_cb (struct ev_io *w, int revents) 956sin_cb (struct ev_io *w, int revents)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines