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

Comparing libev/ev.c (file contents):
Revision 1.11 by root, Wed Oct 31 07:40:49 2007 UTC vs.
Revision 1.15 by root, Wed Oct 31 11:56:34 2007 UTC

9#include <assert.h> 9#include <assert.h>
10#include <errno.h> 10#include <errno.h>
11#include <sys/time.h> 11#include <sys/time.h>
12#include <time.h> 12#include <time.h>
13 13
14#define HAVE_EPOLL 1
15
14#ifndef HAVE_MONOTONIC 16#ifndef HAVE_MONOTONIC
15# ifdef CLOCK_MONOTONIC 17# ifdef CLOCK_MONOTONIC
16# define HAVE_MONOTONIC 1 18# define HAVE_MONOTONIC 1
17# endif 19# endif
18#endif 20#endif
32#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 34#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
33#define MAX_BLOCKTIME 60. 35#define MAX_BLOCKTIME 60.
34 36
35#include "ev.h" 37#include "ev.h"
36 38
37struct ev_watcher {
38 EV_WATCHER (ev_watcher);
39};
40
41struct ev_watcher_list {
42 EV_WATCHER_LIST (ev_watcher_list);
43};
44
45typedef struct ev_watcher *W; 39typedef struct ev_watcher *W;
46typedef struct ev_watcher_list *WL; 40typedef struct ev_watcher_list *WL;
41typedef struct ev_watcher_time *WT;
47 42
48static ev_tstamp now, diff; /* monotonic clock */ 43static ev_tstamp now, diff; /* monotonic clock */
49ev_tstamp ev_now; 44ev_tstamp ev_now;
50int ev_method; 45int ev_method;
51 46
88 83
89#define array_needsize(base,cur,cnt,init) \ 84#define array_needsize(base,cur,cnt,init) \
90 if ((cnt) > cur) \ 85 if ((cnt) > cur) \
91 { \ 86 { \
92 int newcnt = cur ? cur << 1 : 16; \ 87 int newcnt = cur ? cur << 1 : 16; \
93 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\
94 base = realloc (base, sizeof (*base) * (newcnt)); \ 88 base = realloc (base, sizeof (*base) * (newcnt)); \
95 init (base + cur, newcnt - cur); \ 89 init (base + cur, newcnt - cur); \
96 cur = newcnt; \ 90 cur = newcnt; \
97 } 91 }
98 92
163 event (events [i], type); 157 event (events [i], type);
164} 158}
165 159
166/*****************************************************************************/ 160/*****************************************************************************/
167 161
168static struct ev_timer **atimers; 162static struct ev_timer **timers;
169static int atimermax, atimercnt; 163static int timermax, timercnt;
170 164
171static struct ev_timer **rtimers; 165static struct ev_periodic **periodics;
172static int rtimermax, rtimercnt; 166static int periodicmax, periodiccnt;
173 167
174static void 168static void
175upheap (struct ev_timer **timers, int k) 169upheap (WT *timers, int k)
176{ 170{
177 struct ev_timer *w = timers [k]; 171 WT w = timers [k];
178 172
179 while (k && timers [k >> 1]->at > w->at) 173 while (k && timers [k >> 1]->at > w->at)
180 { 174 {
181 timers [k] = timers [k >> 1]; 175 timers [k] = timers [k >> 1];
182 timers [k]->active = k + 1; 176 timers [k]->active = k + 1;
187 timers [k]->active = k + 1; 181 timers [k]->active = k + 1;
188 182
189} 183}
190 184
191static void 185static void
192downheap (struct ev_timer **timers, int N, int k) 186downheap (WT *timers, int N, int k)
193{ 187{
194 struct ev_timer *w = timers [k]; 188 WT w = timers [k];
195 189
196 while (k < (N >> 1)) 190 while (k < (N >> 1))
197 { 191 {
198 int j = k << 1; 192 int j = k << 1;
199 193
325 if (ev_method == EVMETHOD_NONE) select_init (flags); 319 if (ev_method == EVMETHOD_NONE) select_init (flags);
326#endif 320#endif
327 321
328 if (ev_method) 322 if (ev_method)
329 { 323 {
330 evw_init (&sigev, sigcb, 0); 324 evw_init (&sigev, sigcb);
331 siginit (); 325 siginit ();
332 } 326 }
333 327
334 return ev_method; 328 return ev_method;
335} 329}
406 400
407 pendingcnt = 0; 401 pendingcnt = 0;
408} 402}
409 403
410static void 404static void
411timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) 405timers_reify ()
412{ 406{
413 while (timercnt && timers [0]->at <= now) 407 while (timercnt && timers [0]->at <= now)
414 { 408 {
415 struct ev_timer *w = timers [0]; 409 struct ev_timer *w = timers [0];
416 410
417 /* first reschedule or stop timer */ 411 /* first reschedule or stop timer */
418 if (w->repeat) 412 if (w->repeat)
419 { 413 {
420 if (w->is_abs)
421 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
422 else
423 w->at = now + w->repeat; 414 w->at = now + w->repeat;
424 415 assert (("timer timeout in the past, negative repeat?", w->at > now));
425 assert (w->at > now);
426
427 downheap (timers, timercnt, 0); 416 downheap ((WT *)timers, timercnt, 0);
428 } 417 }
429 else 418 else
430 {
431 evtimer_stop (w); /* nonrepeating: stop timer */ 419 evtimer_stop (w); /* nonrepeating: stop timer */
432 --timercnt; /* maybe pass by reference instead? */
433 }
434 420
435 event ((W)w, EV_TIMEOUT); 421 event ((W)w, EV_TIMEOUT);
436 } 422 }
437} 423}
438 424
439static void 425static void
426periodics_reify ()
427{
428 while (periodiccnt && periodics [0]->at <= ev_now)
429 {
430 struct ev_periodic *w = periodics [0];
431
432 /* first reschedule or stop timer */
433 if (w->interval)
434 {
435 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
436 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
437 downheap ((WT *)periodics, periodiccnt, 0);
438 }
439 else
440 evperiodic_stop (w); /* nonrepeating: stop timer */
441
442 event ((W)w, EV_TIMEOUT);
443 }
444}
445
446static void
447periodics_reschedule (ev_tstamp diff)
448{
449 int i;
450
451 /* adjust periodics after time jump */
452 for (i = 0; i < periodiccnt; ++i)
453 {
454 struct ev_periodic *w = periodics [i];
455
456 if (w->interval)
457 {
458 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
459
460 if (fabs (diff) >= 1e-4)
461 {
462 evperiodic_stop (w);
463 evperiodic_start (w);
464
465 i = 0; /* restart loop, inefficient, but time jumps should be rare */
466 }
467 }
468 }
469}
470
471static void
440time_update () 472time_update ()
441{ 473{
442 int i; 474 int i;
475
443 ev_now = ev_time (); 476 ev_now = ev_time ();
444 477
445 if (have_monotonic) 478 if (have_monotonic)
446 { 479 {
447 ev_tstamp odiff = diff; 480 ev_tstamp odiff = diff;
448 481
449 /* detecting time jumps is much more difficult */
450 for (i = 2; --i; ) /* loop a few times, before making important decisions */ 482 for (i = 4; --i; ) /* loop a few times, before making important decisions */
451 { 483 {
452 now = get_clock (); 484 now = get_clock ();
453 diff = ev_now - now; 485 diff = ev_now - now;
454 486
455 if (fabs (odiff - diff) < MIN_TIMEJUMP) 487 if (fabs (odiff - diff) < MIN_TIMEJUMP)
456 return; /* all is well */ 488 return; /* all is well */
457 489
458 ev_now = ev_time (); 490 ev_now = ev_time ();
459 } 491 }
460 492
461 /* time jump detected, reschedule atimers */ 493 periodics_reschedule (diff - odiff);
462 for (i = 0; i < atimercnt; ++i) 494 /* no timer adjustment, as the monotonic clock doesn't jump */
463 {
464 struct ev_timer *w = atimers [i];
465 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
466 }
467 } 495 }
468 else 496 else
469 { 497 {
470 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 498 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
471 /* time jump detected, adjust rtimers */ 499 {
500 periodics_reschedule (ev_now - now);
501
502 /* adjust timers. this is easy, as the offset is the same for all */
472 for (i = 0; i < rtimercnt; ++i) 503 for (i = 0; i < timercnt; ++i)
473 rtimers [i]->at += ev_now - now; 504 timers [i]->at += diff;
505 }
474 506
475 now = ev_now; 507 now = ev_now;
476 } 508 }
477} 509}
478 510
479int ev_loop_done; 511int ev_loop_done;
480 512
481void ev_loop (int flags) 513void ev_loop (int flags)
482{ 514{
483 double block; 515 double block;
484 ev_loop_done = flags & EVLOOP_ONESHOT; 516 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
485 517
486 if (checkcnt) 518 if (checkcnt)
487 { 519 {
488 queue_events ((W *)checks, checkcnt, EV_CHECK); 520 queue_events ((W *)checks, checkcnt, EV_CHECK);
489 call_pending (); 521 call_pending ();
493 { 525 {
494 /* update fd-related kernel structures */ 526 /* update fd-related kernel structures */
495 fd_reify (); 527 fd_reify ();
496 528
497 /* calculate blocking time */ 529 /* calculate blocking time */
530
531 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */
532 ev_now = ev_time ();
533
498 if (flags & EVLOOP_NONBLOCK || idlecnt) 534 if (flags & EVLOOP_NONBLOCK || idlecnt)
499 block = 0.; 535 block = 0.;
500 else 536 else
501 { 537 {
502 block = MAX_BLOCKTIME; 538 block = MAX_BLOCKTIME;
503 539
504 if (rtimercnt) 540 if (timercnt)
505 { 541 {
506 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; 542 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
507 if (block > to) block = to; 543 if (block > to) block = to;
508 } 544 }
509 545
510 if (atimercnt) 546 if (periodiccnt)
511 { 547 {
512 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; 548 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
513 if (block > to) block = to; 549 if (block > to) block = to;
514 } 550 }
515 551
516 if (block < 0.) block = 0.; 552 if (block < 0.) block = 0.;
517 } 553 }
520 556
521 /* update ev_now, do magic */ 557 /* update ev_now, do magic */
522 time_update (); 558 time_update ();
523 559
524 /* queue pending timers and reschedule them */ 560 /* queue pending timers and reschedule them */
525 /* absolute timers first */ 561 periodics_reify (); /* absolute timers first */
526 timers_reify (atimers, atimercnt, ev_now);
527 /* relative timers second */ 562 timers_reify (); /* relative timers second */
528 timers_reify (rtimers, rtimercnt, now);
529 563
530 /* queue idle watchers unless io or timers are pending */ 564 /* queue idle watchers unless io or timers are pending */
531 if (!pendingcnt) 565 if (!pendingcnt)
532 queue_events ((W *)idles, idlecnt, EV_IDLE); 566 queue_events ((W *)idles, idlecnt, EV_IDLE);
533 567
535 queue_events ((W *)checks, checkcnt, EV_CHECK); 569 queue_events ((W *)checks, checkcnt, EV_CHECK);
536 570
537 call_pending (); 571 call_pending ();
538 } 572 }
539 while (!ev_loop_done); 573 while (!ev_loop_done);
574
575 if (ev_loop_done != 2)
576 ev_loop_done = 0;
540} 577}
541 578
542/*****************************************************************************/ 579/*****************************************************************************/
543 580
544static void 581static void
610 ++fdchangecnt; 647 ++fdchangecnt;
611 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 648 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
612 fdchanges [fdchangecnt - 1] = w->fd; 649 fdchanges [fdchangecnt - 1] = w->fd;
613} 650}
614 651
652
615void 653void
616evtimer_start (struct ev_timer *w) 654evtimer_start (struct ev_timer *w)
617{ 655{
618 if (ev_is_active (w)) 656 if (ev_is_active (w))
619 return; 657 return;
620 658
621 if (w->is_abs) 659 w->at += now;
622 {
623 /* this formula differs from the one in timer_reify becuse we do not round up */
624 if (w->repeat)
625 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
626 660
661 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
662
627 ev_start ((W)w, ++atimercnt); 663 ev_start ((W)w, ++timercnt);
628 array_needsize (atimers, atimermax, atimercnt, ); 664 array_needsize (timers, timermax, timercnt, );
629 atimers [atimercnt - 1] = w; 665 timers [timercnt - 1] = w;
630 upheap (atimers, atimercnt - 1); 666 upheap ((WT *)timers, timercnt - 1);
631 }
632 else
633 {
634 w->at += now;
635
636 ev_start ((W)w, ++rtimercnt);
637 array_needsize (rtimers, rtimermax, rtimercnt, );
638 rtimers [rtimercnt - 1] = w;
639 upheap (rtimers, rtimercnt - 1);
640 }
641
642} 667}
643 668
644void 669void
645evtimer_stop (struct ev_timer *w) 670evtimer_stop (struct ev_timer *w)
646{ 671{
647 if (!ev_is_active (w)) 672 if (!ev_is_active (w))
648 return; 673 return;
649 674
650 if (w->is_abs)
651 {
652 if (w->active < atimercnt--) 675 if (w->active < timercnt--)
653 { 676 {
654 atimers [w->active - 1] = atimers [atimercnt]; 677 timers [w->active - 1] = timers [timercnt];
678 downheap ((WT *)timers, timercnt, w->active - 1);
679 }
680
681 w->at = w->repeat;
682
683 ev_stop ((W)w);
684}
685
686void
687evtimer_again (struct ev_timer *w)
688{
689 if (ev_is_active (w))
690 {
691 if (w->repeat)
692 {
693 w->at = now + w->repeat;
655 downheap (atimers, atimercnt, w->active - 1); 694 downheap ((WT *)timers, timercnt, w->active - 1);
656 }
657 }
658 else
659 {
660 if (w->active < rtimercnt--)
661 { 695 }
662 rtimers [w->active - 1] = rtimers [rtimercnt]; 696 else
663 downheap (rtimers, rtimercnt, w->active - 1); 697 evtimer_stop (w);
664 } 698 }
699 else if (w->repeat)
700 evtimer_start (w);
701}
702
703void
704evperiodic_start (struct ev_periodic *w)
705{
706 if (ev_is_active (w))
707 return;
708
709 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
710
711 /* this formula differs from the one in periodic_reify because we do not always round up */
712 if (w->interval)
713 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
714
715 ev_start ((W)w, ++periodiccnt);
716 array_needsize (periodics, periodicmax, periodiccnt, );
717 periodics [periodiccnt - 1] = w;
718 upheap ((WT *)periodics, periodiccnt - 1);
719}
720
721void
722evperiodic_stop (struct ev_periodic *w)
723{
724 if (!ev_is_active (w))
725 return;
726
727 if (w->active < periodiccnt--)
728 {
729 periodics [w->active - 1] = periodics [periodiccnt];
730 downheap ((WT *)periodics, periodiccnt, w->active - 1);
665 } 731 }
666 732
667 ev_stop ((W)w); 733 ev_stop ((W)w);
668} 734}
669 735
734 800
735/*****************************************************************************/ 801/*****************************************************************************/
736 802
737#if 0 803#if 0
738 804
805struct ev_io wio;
806
739static void 807static void
740sin_cb (struct ev_io *w, int revents) 808sin_cb (struct ev_io *w, int revents)
741{ 809{
742 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 810 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
743} 811}
752 820
753static void 821static void
754scb (struct ev_signal *w, int revents) 822scb (struct ev_signal *w, int revents)
755{ 823{
756 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 824 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
825 evio_stop (&wio);
826 evio_start (&wio);
757} 827}
758 828
759static void 829static void
760gcb (struct ev_signal *w, int revents) 830gcb (struct ev_signal *w, int revents)
761{ 831{
762 fprintf (stderr, "generic %x\n", revents); 832 fprintf (stderr, "generic %x\n", revents);
833
763} 834}
764 835
765int main (void) 836int main (void)
766{ 837{
767 struct ev_io sin;
768
769 ev_init (0); 838 ev_init (0);
770 839
771 evw_init (&sin, sin_cb, 55);
772 evio_set (&sin, 0, EV_READ); 840 evio_init (&wio, sin_cb, 0, EV_READ);
773 evio_start (&sin); 841 evio_start (&wio);
774 842
775 struct ev_timer t[10000]; 843 struct ev_timer t[10000];
776 844
777#if 0 845#if 0
778 int i; 846 int i;
779 for (i = 0; i < 10000; ++i) 847 for (i = 0; i < 10000; ++i)
780 { 848 {
781 struct ev_timer *w = t + i; 849 struct ev_timer *w = t + i;
782 evw_init (w, ocb, i); 850 evw_init (w, ocb, i);
783 evtimer_set_abs (w, drand48 (), 0.99775533); 851 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
784 evtimer_start (w); 852 evtimer_start (w);
785 if (drand48 () < 0.5) 853 if (drand48 () < 0.5)
786 evtimer_stop (w); 854 evtimer_stop (w);
787 } 855 }
788#endif 856#endif
789 857
790 struct ev_timer t1; 858 struct ev_timer t1;
791 evw_init (&t1, ocb, 0); 859 evtimer_init (&t1, ocb, 5, 10);
792 evtimer_set_abs (&t1, 5, 10);
793 evtimer_start (&t1); 860 evtimer_start (&t1);
794 861
795 struct ev_signal sig; 862 struct ev_signal sig;
796 evw_init (&sig, scb, 65535);
797 evsignal_set (&sig, SIGQUIT); 863 evsignal_init (&sig, scb, SIGQUIT);
798 evsignal_start (&sig); 864 evsignal_start (&sig);
799 865
800 struct ev_check cw; 866 struct ev_check cw;
801 evw_init (&cw, gcb, 0); 867 evcheck_init (&cw, gcb);
802 evcheck_start (&cw); 868 evcheck_start (&cw);
803 869
804 struct ev_idle iw; 870 struct ev_idle iw;
805 evw_init (&iw, gcb, 0); 871 evidle_init (&iw, gcb);
806 evidle_start (&iw); 872 evidle_start (&iw);
807 873
808 ev_loop (0); 874 ev_loop (0);
809 875
810 return 0; 876 return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines