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

Comparing libev/ev.c (file contents):
Revision 1.10 by root, Wed Oct 31 07:36:03 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}
336 330
337/*****************************************************************************/ 331/*****************************************************************************/
338 332
339void ev_prefork (void) 333void ev_prefork (void)
340{ 334{
335 /* nop */
341} 336}
342 337
343void ev_postfork_parent (void) 338void ev_postfork_parent (void)
344{ 339{
340 /* nop */
345} 341}
346 342
347void ev_postfork_child (void) 343void ev_postfork_child (void)
348{ 344{
349#if HAVE_EPOLL 345#if HAVE_EPOLL
404 400
405 pendingcnt = 0; 401 pendingcnt = 0;
406} 402}
407 403
408static void 404static void
409timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) 405timers_reify ()
410{ 406{
411 while (timercnt && timers [0]->at <= now) 407 while (timercnt && timers [0]->at <= now)
412 { 408 {
413 struct ev_timer *w = timers [0]; 409 struct ev_timer *w = timers [0];
414 410
415 /* first reschedule or stop timer */ 411 /* first reschedule or stop timer */
416 if (w->repeat) 412 if (w->repeat)
417 { 413 {
418 if (w->is_abs)
419 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
420 else
421 w->at = now + w->repeat; 414 w->at = now + w->repeat;
422 415 assert (("timer timeout in the past, negative repeat?", w->at > now));
423 assert (w->at > now);
424
425 downheap (timers, timercnt, 0); 416 downheap ((WT *)timers, timercnt, 0);
426 } 417 }
427 else 418 else
428 {
429 evtimer_stop (w); /* nonrepeating: stop timer */ 419 evtimer_stop (w); /* nonrepeating: stop timer */
430 --timercnt; /* maybe pass by reference instead? */
431 }
432 420
433 event ((W)w, EV_TIMEOUT); 421 event ((W)w, EV_TIMEOUT);
434 } 422 }
435} 423}
436 424
437static 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
438time_update () 472time_update ()
439{ 473{
440 int i; 474 int i;
475
441 ev_now = ev_time (); 476 ev_now = ev_time ();
442 477
443 if (have_monotonic) 478 if (have_monotonic)
444 { 479 {
445 ev_tstamp odiff = diff; 480 ev_tstamp odiff = diff;
446 481
447 /* detecting time jumps is much more difficult */
448 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 */
449 { 483 {
450 now = get_clock (); 484 now = get_clock ();
451 diff = ev_now - now; 485 diff = ev_now - now;
452 486
453 if (fabs (odiff - diff) < MIN_TIMEJUMP) 487 if (fabs (odiff - diff) < MIN_TIMEJUMP)
454 return; /* all is well */ 488 return; /* all is well */
455 489
456 ev_now = ev_time (); 490 ev_now = ev_time ();
457 } 491 }
458 492
459 /* time jump detected, reschedule atimers */ 493 periodics_reschedule (diff - odiff);
460 for (i = 0; i < atimercnt; ++i) 494 /* no timer adjustment, as the monotonic clock doesn't jump */
461 {
462 struct ev_timer *w = atimers [i];
463 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
464 }
465 } 495 }
466 else 496 else
467 { 497 {
468 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 498 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
469 /* 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 */
470 for (i = 0; i < rtimercnt; ++i) 503 for (i = 0; i < timercnt; ++i)
471 rtimers [i]->at += ev_now - now; 504 timers [i]->at += diff;
505 }
472 506
473 now = ev_now; 507 now = ev_now;
474 } 508 }
475} 509}
476 510
477int ev_loop_done; 511int ev_loop_done;
478 512
479void ev_loop (int flags) 513void ev_loop (int flags)
480{ 514{
481 double block; 515 double block;
482 ev_loop_done = flags & EVLOOP_ONESHOT; 516 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
483 517
484 if (checkcnt) 518 if (checkcnt)
485 { 519 {
486 queue_events ((W *)checks, checkcnt, EV_CHECK); 520 queue_events ((W *)checks, checkcnt, EV_CHECK);
487 call_pending (); 521 call_pending ();
491 { 525 {
492 /* update fd-related kernel structures */ 526 /* update fd-related kernel structures */
493 fd_reify (); 527 fd_reify ();
494 528
495 /* 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
496 if (flags & EVLOOP_NONBLOCK || idlecnt) 534 if (flags & EVLOOP_NONBLOCK || idlecnt)
497 block = 0.; 535 block = 0.;
498 else 536 else
499 { 537 {
500 block = MAX_BLOCKTIME; 538 block = MAX_BLOCKTIME;
501 539
502 if (rtimercnt) 540 if (timercnt)
503 { 541 {
504 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;
505 if (block > to) block = to; 543 if (block > to) block = to;
506 } 544 }
507 545
508 if (atimercnt) 546 if (periodiccnt)
509 { 547 {
510 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; 548 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
511 if (block > to) block = to; 549 if (block > to) block = to;
512 } 550 }
513 551
514 if (block < 0.) block = 0.; 552 if (block < 0.) block = 0.;
515 } 553 }
518 556
519 /* update ev_now, do magic */ 557 /* update ev_now, do magic */
520 time_update (); 558 time_update ();
521 559
522 /* queue pending timers and reschedule them */ 560 /* queue pending timers and reschedule them */
523 /* absolute timers first */ 561 periodics_reify (); /* absolute timers first */
524 timers_reify (atimers, atimercnt, ev_now);
525 /* relative timers second */ 562 timers_reify (); /* relative timers second */
526 timers_reify (rtimers, rtimercnt, now);
527 563
528 /* queue idle watchers unless io or timers are pending */ 564 /* queue idle watchers unless io or timers are pending */
529 if (!pendingcnt) 565 if (!pendingcnt)
530 queue_events ((W *)idles, idlecnt, EV_IDLE); 566 queue_events ((W *)idles, idlecnt, EV_IDLE);
531 567
533 queue_events ((W *)checks, checkcnt, EV_CHECK); 569 queue_events ((W *)checks, checkcnt, EV_CHECK);
534 570
535 call_pending (); 571 call_pending ();
536 } 572 }
537 while (!ev_loop_done); 573 while (!ev_loop_done);
574
575 if (ev_loop_done != 2)
576 ev_loop_done = 0;
538} 577}
539 578
540/*****************************************************************************/ 579/*****************************************************************************/
541 580
542static void 581static void
573{ 612{
574 if (w->pending) 613 if (w->pending)
575 pendings [w->pending - 1].w = 0; 614 pendings [w->pending - 1].w = 0;
576 615
577 w->active = 0; 616 w->active = 0;
578 /* nop */
579} 617}
580 618
581/*****************************************************************************/ 619/*****************************************************************************/
582 620
583void 621void
609 ++fdchangecnt; 647 ++fdchangecnt;
610 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 648 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
611 fdchanges [fdchangecnt - 1] = w->fd; 649 fdchanges [fdchangecnt - 1] = w->fd;
612} 650}
613 651
652
614void 653void
615evtimer_start (struct ev_timer *w) 654evtimer_start (struct ev_timer *w)
616{ 655{
617 if (ev_is_active (w)) 656 if (ev_is_active (w))
618 return; 657 return;
619 658
620 if (w->is_abs) 659 w->at += now;
621 {
622 /* this formula differs from the one in timer_reify becuse we do not round up */
623 if (w->repeat)
624 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
625 660
661 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
662
626 ev_start ((W)w, ++atimercnt); 663 ev_start ((W)w, ++timercnt);
627 array_needsize (atimers, atimermax, atimercnt, ); 664 array_needsize (timers, timermax, timercnt, );
628 atimers [atimercnt - 1] = w; 665 timers [timercnt - 1] = w;
629 upheap (atimers, atimercnt - 1); 666 upheap ((WT *)timers, timercnt - 1);
630 }
631 else
632 {
633 w->at += now;
634
635 ev_start ((W)w, ++rtimercnt);
636 array_needsize (rtimers, rtimermax, rtimercnt, );
637 rtimers [rtimercnt - 1] = w;
638 upheap (rtimers, rtimercnt - 1);
639 }
640
641} 667}
642 668
643void 669void
644evtimer_stop (struct ev_timer *w) 670evtimer_stop (struct ev_timer *w)
645{ 671{
646 if (!ev_is_active (w)) 672 if (!ev_is_active (w))
647 return; 673 return;
648 674
649 if (w->is_abs)
650 {
651 if (w->active < atimercnt--) 675 if (w->active < timercnt--)
652 { 676 {
653 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;
654 downheap (atimers, atimercnt, w->active - 1); 694 downheap ((WT *)timers, timercnt, w->active - 1);
655 }
656 }
657 else
658 {
659 if (w->active < rtimercnt--)
660 { 695 }
661 rtimers [w->active - 1] = rtimers [rtimercnt]; 696 else
662 downheap (rtimers, rtimercnt, w->active - 1); 697 evtimer_stop (w);
663 } 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);
664 } 731 }
665 732
666 ev_stop ((W)w); 733 ev_stop ((W)w);
667} 734}
668 735
733 800
734/*****************************************************************************/ 801/*****************************************************************************/
735 802
736#if 0 803#if 0
737 804
805struct ev_io wio;
806
738static void 807static void
739sin_cb (struct ev_io *w, int revents) 808sin_cb (struct ev_io *w, int revents)
740{ 809{
741 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 810 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
742} 811}
751 820
752static void 821static void
753scb (struct ev_signal *w, int revents) 822scb (struct ev_signal *w, int revents)
754{ 823{
755 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);
756} 827}
757 828
758static void 829static void
759gcb (struct ev_signal *w, int revents) 830gcb (struct ev_signal *w, int revents)
760{ 831{
761 fprintf (stderr, "generic %x\n", revents); 832 fprintf (stderr, "generic %x\n", revents);
833
762} 834}
763 835
764int main (void) 836int main (void)
765{ 837{
766 struct ev_io sin;
767
768 ev_init (0); 838 ev_init (0);
769 839
770 evw_init (&sin, sin_cb, 55);
771 evio_set (&sin, 0, EV_READ); 840 evio_init (&wio, sin_cb, 0, EV_READ);
772 evio_start (&sin); 841 evio_start (&wio);
773 842
774 struct ev_timer t[10000]; 843 struct ev_timer t[10000];
775 844
776#if 0 845#if 0
777 int i; 846 int i;
778 for (i = 0; i < 10000; ++i) 847 for (i = 0; i < 10000; ++i)
779 { 848 {
780 struct ev_timer *w = t + i; 849 struct ev_timer *w = t + i;
781 evw_init (w, ocb, i); 850 evw_init (w, ocb, i);
782 evtimer_set_abs (w, drand48 (), 0.99775533); 851 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
783 evtimer_start (w); 852 evtimer_start (w);
784 if (drand48 () < 0.5) 853 if (drand48 () < 0.5)
785 evtimer_stop (w); 854 evtimer_stop (w);
786 } 855 }
787#endif 856#endif
788 857
789 struct ev_timer t1; 858 struct ev_timer t1;
790 evw_init (&t1, ocb, 0); 859 evtimer_init (&t1, ocb, 5, 10);
791 evtimer_set_abs (&t1, 5, 10);
792 evtimer_start (&t1); 860 evtimer_start (&t1);
793 861
794 struct ev_signal sig; 862 struct ev_signal sig;
795 evw_init (&sig, scb, 65535);
796 evsignal_set (&sig, SIGQUIT); 863 evsignal_init (&sig, scb, SIGQUIT);
797 evsignal_start (&sig); 864 evsignal_start (&sig);
798 865
799 struct ev_check cw; 866 struct ev_check cw;
800 evw_init (&cw, gcb, 0); 867 evcheck_init (&cw, gcb);
801 evcheck_start (&cw); 868 evcheck_start (&cw);
802 869
803 struct ev_idle iw; 870 struct ev_idle iw;
804 evw_init (&iw, gcb, 0); 871 evidle_init (&iw, gcb);
805 evidle_start (&iw); 872 evidle_start (&iw);
806 873
807 ev_loop (0); 874 ev_loop (0);
808 875
809 return 0; 876 return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines