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.14 by root, Wed Oct 31 11:52:12 2007 UTC

33 33
34#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) */
35#define MAX_BLOCKTIME 60. 35#define MAX_BLOCKTIME 60.
36 36
37#include "ev.h" 37#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 38
51typedef struct ev_watcher *W; 39typedef struct ev_watcher *W;
52typedef struct ev_watcher_list *WL; 40typedef struct ev_watcher_list *WL;
53typedef struct ev_watcher_time *WT; 41typedef struct ev_watcher_time *WT;
54 42
455 event ((W)w, EV_TIMEOUT); 443 event ((W)w, EV_TIMEOUT);
456 } 444 }
457} 445}
458 446
459static void 447static void
460time_jump (ev_tstamp diff) 448periodics_reschedule (ev_tstamp diff)
461{ 449{
462 int i; 450 int i;
463 451
464 /* adjust periodics */ 452 /* adjust periodics after time jump */
465 for (i = 0; i < periodiccnt; ++i) 453 for (i = 0; i < periodiccnt; ++i)
466 { 454 {
467 struct ev_periodic *w = periodics [i]; 455 struct ev_periodic *w = periodics [i];
468 456
469 if (w->interval) 457 if (w->interval)
477 465
478 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 466 i = 0; /* restart loop, inefficient, but time jumps should be rare */
479 } 467 }
480 } 468 }
481 } 469 }
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} 470}
487 471
488static void 472static void
489time_update () 473time_update ()
490{ 474{
505 return; /* all is well */ 489 return; /* all is well */
506 490
507 ev_now = ev_time (); 491 ev_now = ev_time ();
508 } 492 }
509 493
510 time_jump (diff - odiff); 494 periodics_reschedule (diff - odiff);
495 /* no timer adjustment, as the monotonic clock doesn't jump */
511 } 496 }
512 else 497 else
513 { 498 {
514 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 499 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
515 time_jump (ev_now - now); 500 {
501 periodics_reschedule (ev_now - now);
502
503 /* adjust timers. this is easy, as the offset is the same for all */
504 for (i = 0; i < timercnt; ++i)
505 timers [i]->at += diff;
506 }
516 507
517 now = ev_now; 508 now = ev_now;
518 } 509 }
519} 510}
520 511
521int ev_loop_done; 512int ev_loop_done;
522 513
523void ev_loop (int flags) 514void ev_loop (int flags)
524{ 515{
525 double block; 516 double block;
526 ev_loop_done = flags & EVLOOP_ONESHOT; 517 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
527 518
528 if (checkcnt) 519 if (checkcnt)
529 { 520 {
530 queue_events ((W *)checks, checkcnt, EV_CHECK); 521 queue_events ((W *)checks, checkcnt, EV_CHECK);
531 call_pending (); 522 call_pending ();
547 { 538 {
548 block = MAX_BLOCKTIME; 539 block = MAX_BLOCKTIME;
549 540
550 if (timercnt) 541 if (timercnt)
551 { 542 {
552 ev_tstamp to = timers [0]->at - get_clock () + method_fudge; 543 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
553 if (block > to) block = to; 544 if (block > to) block = to;
554 } 545 }
555 546
556 if (periodiccnt) 547 if (periodiccnt)
557 { 548 {
579 queue_events ((W *)checks, checkcnt, EV_CHECK); 570 queue_events ((W *)checks, checkcnt, EV_CHECK);
580 571
581 call_pending (); 572 call_pending ();
582 } 573 }
583 while (!ev_loop_done); 574 while (!ev_loop_done);
575
576 if (ev_loop_done != 2)
577 ev_loop_done = 0;
584} 578}
585 579
586/*****************************************************************************/ 580/*****************************************************************************/
587 581
588static void 582static void
662{ 656{
663 if (ev_is_active (w)) 657 if (ev_is_active (w))
664 return; 658 return;
665 659
666 w->at += now; 660 w->at += now;
661
662 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
667 663
668 ev_start ((W)w, ++timercnt); 664 ev_start ((W)w, ++timercnt);
669 array_needsize (timers, timermax, timercnt, ); 665 array_needsize (timers, timermax, timercnt, );
670 timers [timercnt - 1] = w; 666 timers [timercnt - 1] = w;
671 upheap ((WT *)timers, timercnt - 1); 667 upheap ((WT *)timers, timercnt - 1);
681 { 677 {
682 timers [w->active - 1] = timers [timercnt]; 678 timers [w->active - 1] = timers [timercnt];
683 downheap ((WT *)timers, timercnt, w->active - 1); 679 downheap ((WT *)timers, timercnt, w->active - 1);
684 } 680 }
685 681
682 w->at = w->repeat;
683
686 ev_stop ((W)w); 684 ev_stop ((W)w);
685}
686
687void
688evtimer_again (struct ev_timer *w)
689{
690 if (ev_is_active (w))
691 {
692 if (w->repeat)
693 {
694 w->at = now + w->repeat;
695 downheap ((WT *)timers, timercnt, w->active - 1);
696 }
697 else
698 evtimer_stop (w);
699 }
700 else if (w->repeat)
701 evtimer_start (w);
687} 702}
688 703
689void 704void
690evperiodic_start (struct ev_periodic *w) 705evperiodic_start (struct ev_periodic *w)
691{ 706{
692 if (ev_is_active (w)) 707 if (ev_is_active (w))
693 return; 708 return;
709
710 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
694 711
695 /* this formula differs from the one in periodic_reify because we do not always round up */ 712 /* this formula differs from the one in periodic_reify because we do not always round up */
696 if (w->interval) 713 if (w->interval)
697 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 714 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
698 715
782 ev_stop ((W)w); 799 ev_stop ((W)w);
783} 800}
784 801
785/*****************************************************************************/ 802/*****************************************************************************/
786 803
787#if 1 804#if 0
788 805
789struct ev_io wio; 806struct ev_io wio;
790 807
791static void 808static void
792sin_cb (struct ev_io *w, int revents) 809sin_cb (struct ev_io *w, int revents)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines