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

Comparing libev/ev.c (file contents):
Revision 1.39 by root, Thu Nov 1 17:17:32 2007 UTC vs.
Revision 1.40 by root, Fri Nov 2 11:02:23 2007 UTC

46#include <sys/types.h> 46#include <sys/types.h>
47#include <sys/wait.h> 47#include <sys/wait.h>
48#include <sys/time.h> 48#include <sys/time.h>
49#include <time.h> 49#include <time.h>
50 50
51/**/
52
51#ifndef EV_USE_MONOTONIC 53#ifndef EV_USE_MONOTONIC
52# define EV_USE_MONOTONIC 1 54# define EV_USE_MONOTONIC 1
53#endif 55#endif
56
57#ifndef EV_USE_SELECT
58# define EV_USE_SELECT 1
59#endif
60
61#ifndef EV_USE_EPOLL
62# define EV_USE_EPOLL 0
63#endif
64
65#ifndef EV_USE_REALTIME
66# define EV_USE_REALTIME 1
67#endif
68
69/**/
54 70
55#ifndef CLOCK_MONOTONIC 71#ifndef CLOCK_MONOTONIC
56# undef EV_USE_MONOTONIC 72# undef EV_USE_MONOTONIC
57# define EV_USE_MONOTONIC 0 73# define EV_USE_MONOTONIC 0
58#endif 74#endif
59 75
60#ifndef EV_USE_SELECT
61# define EV_USE_SELECT 1
62#endif
63
64#ifndef EV_USE_EPOLL
65# define EV_USE_EPOLL 0
66#endif
67
68#ifndef CLOCK_REALTIME 76#ifndef CLOCK_REALTIME
77# undef EV_USE_REALTIME
69# define EV_USE_REALTIME 0 78# define EV_USE_REALTIME 0
70#endif 79#endif
71#ifndef EV_USE_REALTIME 80
72# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ 81/**/
73#endif
74 82
75#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 83#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
76#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ 84#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
77#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 85#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
78#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ 86/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
79 87
80#include "ev.h" 88#include "ev.h"
89
90#if __GNUC__ >= 3
91# define expect(expr,value) __builtin_expect ((expr),(value))
92# define inline inline
93#else
94# define expect(expr,value) (expr)
95# define inline static
96#endif
97
98#define expect_false(expr) expect ((expr) != 0, 0)
99#define expect_true(expr) expect ((expr) != 0, 1)
81 100
82typedef struct ev_watcher *W; 101typedef struct ev_watcher *W;
83typedef struct ev_watcher_list *WL; 102typedef struct ev_watcher_list *WL;
84typedef struct ev_watcher_time *WT; 103typedef struct ev_watcher_time *WT;
85 104
86static ev_tstamp now, diff; /* monotonic clock */ 105static ev_tstamp now_floor, now, diff; /* monotonic clock */
87ev_tstamp ev_now; 106ev_tstamp ev_now;
88int ev_method; 107int ev_method;
89 108
90static int have_monotonic; /* runtime */ 109static int have_monotonic; /* runtime */
91 110
111 130
112static ev_tstamp 131static ev_tstamp
113get_clock (void) 132get_clock (void)
114{ 133{
115#if EV_USE_MONOTONIC 134#if EV_USE_MONOTONIC
116 if (have_monotonic) 135 if (expect_true (have_monotonic))
117 { 136 {
118 struct timespec ts; 137 struct timespec ts;
119 clock_gettime (CLOCK_MONOTONIC, &ts); 138 clock_gettime (CLOCK_MONOTONIC, &ts);
120 return ts.tv_sec + ts.tv_nsec * 1e-9; 139 return ts.tv_sec + ts.tv_nsec * 1e-9;
121 } 140 }
125} 144}
126 145
127#define array_roundsize(base,n) ((n) | 4 & ~3) 146#define array_roundsize(base,n) ((n) | 4 & ~3)
128 147
129#define array_needsize(base,cur,cnt,init) \ 148#define array_needsize(base,cur,cnt,init) \
130 if ((cnt) > cur) \ 149 if (expect_false ((cnt) > cur)) \
131 { \ 150 { \
132 int newcnt = cur; \ 151 int newcnt = cur; \
133 do \ 152 do \
134 { \ 153 { \
135 newcnt = array_roundsize (base, newcnt << 1); \ 154 newcnt = array_roundsize (base, newcnt << 1); \
463 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 482 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
464 have_monotonic = 1; 483 have_monotonic = 1;
465 } 484 }
466#endif 485#endif
467 486
468 ev_now = ev_time (); 487 ev_now = ev_time ();
469 now = get_clock (); 488 now = get_clock ();
489 now_floor = now;
470 diff = ev_now - now; 490 diff = ev_now - now;
471 491
472 if (pipe (sigpipe)) 492 if (pipe (sigpipe))
473 return 0; 493 return 0;
474 494
475 ev_method = EVMETHOD_NONE; 495 ev_method = EVMETHOD_NONE;
604 } 624 }
605 } 625 }
606 } 626 }
607} 627}
608 628
629static int
630time_update_monotonic (void)
631{
632 now = get_clock ();
633
634 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
635 {
636 ev_now = now + diff;
637 return 0;
638 }
639 else
640 {
641 now_floor = now;
642 ev_now = ev_time ();
643 return 1;
644 }
645}
646
609static void 647static void
610time_update (void) 648time_update (void)
611{ 649{
612 int i; 650 int i;
613 651
614 ev_now = ev_time (); 652#if EV_USE_MONOTONIC
615
616 if (have_monotonic) 653 if (expect_true (have_monotonic))
617 { 654 {
655 if (time_update_monotonic ())
656 {
618 ev_tstamp odiff = diff; 657 ev_tstamp odiff = diff;
619 658
620 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 659 for (i = 4; --i; ) /* loop a few times, before making important decisions */
621 { 660 {
622 now = get_clock ();
623 diff = ev_now - now; 661 diff = ev_now - now;
624 662
625 if (fabs (odiff - diff) < MIN_TIMEJUMP) 663 if (fabs (odiff - diff) < MIN_TIMEJUMP)
626 return; /* all is well */ 664 return; /* all is well */
627 665
628 ev_now = ev_time (); 666 ev_now = ev_time ();
667 now = get_clock ();
668 now_floor = now;
629 } 669 }
630 670
631 periodics_reschedule (diff - odiff); 671 periodics_reschedule (diff - odiff);
632 /* no timer adjustment, as the monotonic clock doesn't jump */ 672 /* no timer adjustment, as the monotonic clock doesn't jump */
673 }
633 } 674 }
634 else 675 else
676#endif
635 { 677 {
678 ev_now = ev_time ();
679
636 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 680 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
637 { 681 {
638 periodics_reschedule (ev_now - now); 682 periodics_reschedule (ev_now - now);
639 683
640 /* adjust timers. this is easy, as the offset is the same for all */ 684 /* adjust timers. this is easy, as the offset is the same for all */
641 for (i = 0; i < timercnt; ++i) 685 for (i = 0; i < timercnt; ++i)
654 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 698 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
655 699
656 do 700 do
657 { 701 {
658 /* queue check watchers (and execute them) */ 702 /* queue check watchers (and execute them) */
659 if (preparecnt) 703 if (expect_false (preparecnt))
660 { 704 {
661 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 705 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
662 call_pending (); 706 call_pending ();
663 } 707 }
664 708
667 711
668 /* calculate blocking time */ 712 /* calculate blocking time */
669 713
670 /* we only need this for !monotonic clockor timers, but as we basically 714 /* we only need this for !monotonic clockor timers, but as we basically
671 always have timers, we just calculate it always */ 715 always have timers, we just calculate it always */
716#if EV_USE_MONOTONIC
717 if (expect_true (have_monotonic))
718 time_update_monotonic ();
719 else
720#endif
721 {
672 ev_now = ev_time (); 722 ev_now = ev_time ();
723 now = ev_now;
724 }
673 725
674 if (flags & EVLOOP_NONBLOCK || idlecnt) 726 if (flags & EVLOOP_NONBLOCK || idlecnt)
675 block = 0.; 727 block = 0.;
676 else 728 else
677 { 729 {
678 block = MAX_BLOCKTIME; 730 block = MAX_BLOCKTIME;
679 731
680 if (timercnt) 732 if (timercnt)
681 { 733 {
682 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 734 ev_tstamp to = timers [0]->at - now + method_fudge;
683 if (block > to) block = to; 735 if (block > to) block = to;
684 } 736 }
685 737
686 if (periodiccnt) 738 if (periodiccnt)
687 { 739 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines