X-Git-Url: https://git.llucax.com/software/libev.git/blobdiff_plain/7c2c0ecea2a4196af25910beaaf0faa4468e8e52..590184764c24027d601abb040776a5b5f7314dd5:/ev.c diff --git a/ev.c b/ev.c index 2001247..c9ff2f1 100644 --- a/ev.c +++ b/ev.c @@ -218,29 +218,40 @@ extern "C" { /**/ +/* + * This is used to avoid floating point rounding problems. + * It is added to ev_rt_now when scheduling periodics + * to ensure progress, time-wise, even when rounding + * errors are against us. + * This value is good at least till the year 4000. + * Better solutions welcome. + */ +#define TIME_EPSILON 0.0001220703125 /* 1/8192 */ + #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ -/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ +/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */ #if __GNUC__ >= 3 # define expect(expr,value) __builtin_expect ((expr),(value)) -# define inline_size static inline /* inline for codesize */ -# if EV_MINIMAL -# define noinline __attribute__ ((noinline)) -# define inline_speed static noinline -# else -# define noinline -# define inline_speed static inline -# endif +# define noinline __attribute__ ((noinline)) #else # define expect(expr,value) (expr) -# define inline_speed static -# define inline_size static # define noinline +# if __STDC_VERSION__ < 199901L +# define inline +# endif #endif #define expect_false(expr) expect ((expr) != 0, 0) #define expect_true(expr) expect ((expr) != 0, 1) +#define inline_size static inline + +#if EV_MINIMAL +# define inline_speed static noinline +#else +# define inline_speed static inline +#endif #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) #define ABSPRI(w) (((W)w)->priority - EV_MINPRI) @@ -419,7 +430,7 @@ array_nextsize (int elem, int cur, int cnt) return ncur; } -inline_speed void * +static noinline void * array_realloc (int elem, void *base, int *cur, int cnt) { *cur = array_nextsize (elem, *cur, cnt); @@ -454,20 +465,20 @@ void noinline ev_feed_event (EV_P_ void *w, int revents) { W w_ = (W)w; + int pri = ABSPRI (w_); if (expect_false (w_->pending)) + pendings [pri][w_->pending - 1].events |= revents; + else { - pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; - return; + w_->pending = ++pendingcnt [pri]; + array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); + pendings [pri][w_->pending - 1].w = w_; + pendings [pri][w_->pending - 1].events = revents; } - - w_->pending = ++pendingcnt [ABSPRI (w_)]; - array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); - pendings [ABSPRI (w_)][w_->pending - 1].w = w_; - pendings [ABSPRI (w_)][w_->pending - 1].events = revents; } -void inline_size +void inline_speed queue_events (EV_P_ W *events, int eventcnt, int type) { int i; @@ -509,7 +520,8 @@ fd_event (EV_P_ int fd, int revents) void ev_feed_fd_event (EV_P_ int fd, int revents) { - fd_event (EV_A_ fd, revents); + if (fd >= 0 && fd < anfdmax) + fd_event (EV_A_ fd, revents); } void inline_size @@ -628,16 +640,20 @@ upheap (WT *heap, int k) { WT w = heap [k]; - while (k && heap [k >> 1]->at > w->at) + while (k) { - heap [k] = heap [k >> 1]; + int p = (k - 1) >> 1; + + if (heap [p]->at <= w->at) + break; + + heap [k] = heap [p]; ((W)heap [k])->active = k + 1; - k >>= 1; + k = p; } heap [k] = w; ((W)heap [k])->active = k + 1; - } void inline_speed @@ -645,19 +661,23 @@ downheap (WT *heap, int N, int k) { WT w = heap [k]; - while (k < (N >> 1)) + for (;;) { - int j = k << 1; + int c = (k << 1) + 1; + + if (c >= N) + break; - if (j + 1 < N && heap [j]->at > heap [j + 1]->at) - ++j; + c += c + 1 < N && heap [c]->at > heap [c + 1]->at + ? 1 : 0; - if (w->at <= heap [j]->at) + if (w->at <= heap [c]->at) break; - heap [k] = heap [j]; + heap [k] = heap [c]; ((W)heap [k])->active = k + 1; - k = j; + + k = c; } heap [k] = w; @@ -749,7 +769,7 @@ sigcb (EV_P_ ev_io *iow, int revents) ev_feed_signal_event (EV_A_ signum + 1); } -void inline_size +void inline_speed fd_intern (int fd) { #ifdef _WIN32 @@ -774,7 +794,7 @@ siginit (EV_P) /*****************************************************************************/ -static ev_child *childs [EV_PID_HASHSIZE]; +static WL childs [EV_PID_HASHSIZE]; #ifndef _WIN32 @@ -1165,6 +1185,12 @@ ev_default_fork (void) /*****************************************************************************/ +void +ev_invoke (EV_P_ void *w, int revents) +{ + EV_CB_INVOKE ((W)w, revents); +} + void inline_speed call_pending (EV_P) { @@ -1190,7 +1216,7 @@ timers_reify (EV_P) { while (timercnt && ((WT)timers [0])->at <= mn_now) { - ev_timer *w = timers [0]; + ev_timer *w = (ev_timer *)timers [0]; /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ @@ -1203,7 +1229,7 @@ timers_reify (EV_P) if (((WT)w)->at < mn_now) ((WT)w)->at = mn_now; - downheap ((WT *)timers, timercnt, 0); + downheap (timers, timercnt, 0); } else ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ @@ -1218,22 +1244,23 @@ periodics_reify (EV_P) { while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) { - ev_periodic *w = periodics [0]; + ev_periodic *w = (ev_periodic *)periodics [0]; /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ /* first reschedule or stop timer */ if (w->reschedule_cb) { - ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); + ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON); assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); - downheap ((WT *)periodics, periodiccnt, 0); + downheap (periodics, periodiccnt, 0); } else if (w->interval) { - ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; + ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; + if (((WT)w)->at - ev_rt_now <= TIME_EPSILON) ((WT)w)->at += w->interval; assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); - downheap ((WT *)periodics, periodiccnt, 0); + downheap (periodics, periodiccnt, 0); } else ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ @@ -1250,17 +1277,17 @@ periodics_reschedule (EV_P) /* adjust periodics after time jump */ for (i = 0; i < periodiccnt; ++i) { - ev_periodic *w = periodics [i]; + ev_periodic *w = (ev_periodic *)periodics [i]; if (w->reschedule_cb) ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); else if (w->interval) - ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; + ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; } /* now rebuild the heap */ for (i = periodiccnt >> 1; i--; ) - downheap ((WT *)periodics, periodiccnt, i); + downheap (periodics, periodiccnt, i); } #endif @@ -1287,74 +1314,65 @@ idle_reify (EV_P) } #endif -int inline_size -time_update_monotonic (EV_P) -{ - mn_now = get_clock (); - - if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) - { - ev_rt_now = rtmn_diff + mn_now; - return 0; - } - else - { - now_floor = mn_now; - ev_rt_now = ev_time (); - return 1; - } -} - -void inline_size -time_update (EV_P) +void inline_speed +time_update (EV_P_ ev_tstamp max_block) { int i; #if EV_USE_MONOTONIC if (expect_true (have_monotonic)) { - if (time_update_monotonic (EV_A)) + ev_tstamp odiff = rtmn_diff; + + mn_now = get_clock (); + + /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ + /* interpolate in the meantime */ + if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) { - ev_tstamp odiff = rtmn_diff; - - /* loop a few times, before making important decisions. - * on the choice of "4": one iteration isn't enough, - * in case we get preempted during the calls to - * ev_time and get_clock. a second call is almost guaranteed - * to succeed in that case, though. and looping a few more times - * doesn't hurt either as we only do this on time-jumps or - * in the unlikely event of having been preempted here. - */ - for (i = 4; --i; ) - { - rtmn_diff = ev_rt_now - mn_now; + ev_rt_now = rtmn_diff + mn_now; + return; + } + + now_floor = mn_now; + ev_rt_now = ev_time (); - if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) - return; /* all is well */ + /* loop a few times, before making important decisions. + * on the choice of "4": one iteration isn't enough, + * in case we get preempted during the calls to + * ev_time and get_clock. a second call is almost guaranteed + * to succeed in that case, though. and looping a few more times + * doesn't hurt either as we only do this on time-jumps or + * in the unlikely event of having been preempted here. + */ + for (i = 4; --i; ) + { + rtmn_diff = ev_rt_now - mn_now; - ev_rt_now = ev_time (); - mn_now = get_clock (); - now_floor = mn_now; - } + if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) + return; /* all is well */ + + ev_rt_now = ev_time (); + mn_now = get_clock (); + now_floor = mn_now; + } # if EV_PERIODIC_ENABLE - periodics_reschedule (EV_A); + periodics_reschedule (EV_A); # endif - /* no timer adjustment, as the monotonic clock doesn't jump */ - /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ - } + /* no timer adjustment, as the monotonic clock doesn't jump */ + /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ } else #endif { ev_rt_now = ev_time (); - if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) + if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP)) { #if EV_PERIODIC_ENABLE periodics_reschedule (EV_A); #endif - /* adjust timers. this is easy, as the offset is the same for all of them */ for (i = 0; i < timercnt; ++i) ((WT)timers [i])->at += ev_rt_now - mn_now; @@ -1408,7 +1426,7 @@ ev_loop (EV_P_ int flags) } #endif - /* queue check watchers (and execute them) */ + /* queue prepare watchers (and execute them) */ if (expect_false (preparecnt)) { queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); @@ -1434,15 +1452,7 @@ ev_loop (EV_P_ int flags) else { /* update time to cancel out callback processing overhead */ -#if EV_USE_MONOTONIC - if (expect_true (have_monotonic)) - time_update_monotonic (EV_A); - else -#endif - { - ev_rt_now = ev_time (); - mn_now = ev_rt_now; - } + time_update (EV_A_ 1e100); block = MAX_BLOCKTIME; @@ -1465,10 +1475,10 @@ ev_loop (EV_P_ int flags) ++loop_count; backend_poll (EV_A_ block); - } - /* update ev_rt_now, do magic */ - time_update (EV_A); + /* update ev_rt_now, do magic */ + time_update (EV_A_ block); + } /* queue pending timers and reschedule them */ timers_reify (EV_A); /* relative timers called last */ @@ -1534,22 +1544,21 @@ clear_pending (EV_P_ W w) } } -void -ev_clear_pending (EV_P_ void *w, int invoke) +int +ev_clear_pending (EV_P_ void *w) { W w_ = (W)w; int pending = w_->pending; - if (pending) + if (expect_true (pending)) { ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; - w_->pending = 0; p->w = 0; - - if (invoke) - EV_CB_INVOKE (w_, p->events); + return p->events; } + else + return 0; } void inline_size @@ -1578,7 +1587,7 @@ ev_stop (EV_P_ W w) /*****************************************************************************/ -void +void noinline ev_io_start (EV_P_ ev_io *w) { int fd = w->fd; @@ -1590,12 +1599,12 @@ ev_io_start (EV_P_ ev_io *w) ev_start (EV_A_ (W)w, 1); array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); - wlist_add ((WL *)&anfds[fd].head, (WL)w); + wlist_add (&anfds[fd].head, (WL)w); fd_change (EV_A_ fd); } -void +void noinline ev_io_stop (EV_P_ ev_io *w) { clear_pending (EV_A_ (W)w); @@ -1604,13 +1613,13 @@ ev_io_stop (EV_P_ ev_io *w) assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); - wlist_del ((WL *)&anfds[w->fd].head, (WL)w); + wlist_del (&anfds[w->fd].head, (WL)w); ev_stop (EV_A_ (W)w); fd_change (EV_A_ w->fd); } -void +void noinline ev_timer_start (EV_P_ ev_timer *w) { if (expect_false (ev_is_active (w))) @@ -1621,21 +1630,21 @@ ev_timer_start (EV_P_ ev_timer *w) assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); ev_start (EV_A_ (W)w, ++timercnt); - array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); - timers [timercnt - 1] = w; - upheap ((WT *)timers, timercnt - 1); + array_needsize (WT, timers, timermax, timercnt, EMPTY2); + timers [timercnt - 1] = (WT)w; + upheap (timers, timercnt - 1); /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ } -void +void noinline ev_timer_stop (EV_P_ ev_timer *w) { clear_pending (EV_A_ (W)w); if (expect_false (!ev_is_active (w))) return; - assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); + assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w)); { int active = ((W)w)->active; @@ -1643,7 +1652,7 @@ ev_timer_stop (EV_P_ ev_timer *w) if (expect_true (--active < --timercnt)) { timers [active] = timers [timercnt]; - adjustheap ((WT *)timers, timercnt, active); + adjustheap (timers, timercnt, active); } } @@ -1652,7 +1661,7 @@ ev_timer_stop (EV_P_ ev_timer *w) ev_stop (EV_A_ (W)w); } -void +void noinline ev_timer_again (EV_P_ ev_timer *w) { if (ev_is_active (w)) @@ -1660,7 +1669,7 @@ ev_timer_again (EV_P_ ev_timer *w) if (w->repeat) { ((WT)w)->at = mn_now + w->repeat; - adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); + adjustheap (timers, timercnt, ((W)w)->active - 1); } else ev_timer_stop (EV_A_ w); @@ -1673,7 +1682,7 @@ ev_timer_again (EV_P_ ev_timer *w) } #if EV_PERIODIC_ENABLE -void +void noinline ev_periodic_start (EV_P_ ev_periodic *w) { if (expect_false (ev_is_active (w))) @@ -1685,25 +1694,27 @@ ev_periodic_start (EV_P_ ev_periodic *w) { assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); /* this formula differs from the one in periodic_reify because we do not always round up */ - ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; + ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; } + else + ((WT)w)->at = w->offset; ev_start (EV_A_ (W)w, ++periodiccnt); - array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); - periodics [periodiccnt - 1] = w; - upheap ((WT *)periodics, periodiccnt - 1); + array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); + periodics [periodiccnt - 1] = (WT)w; + upheap (periodics, periodiccnt - 1); /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ } -void +void noinline ev_periodic_stop (EV_P_ ev_periodic *w) { clear_pending (EV_A_ (W)w); if (expect_false (!ev_is_active (w))) return; - assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); + assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w)); { int active = ((W)w)->active; @@ -1711,14 +1722,14 @@ ev_periodic_stop (EV_P_ ev_periodic *w) if (expect_true (--active < --periodiccnt)) { periodics [active] = periodics [periodiccnt]; - adjustheap ((WT *)periodics, periodiccnt, active); + adjustheap (periodics, periodiccnt, active); } } ev_stop (EV_A_ (W)w); } -void +void noinline ev_periodic_again (EV_P_ ev_periodic *w) { /* TODO: use adjustheap and recalculation */ @@ -1731,7 +1742,7 @@ ev_periodic_again (EV_P_ ev_periodic *w) # define SA_RESTART 0 #endif -void +void noinline ev_signal_start (EV_P_ ev_signal *w) { #if EV_MULTIPLICITY @@ -1742,9 +1753,22 @@ ev_signal_start (EV_P_ ev_signal *w) assert (("ev_signal_start called with illegal signal number", w->signum > 0)); + { +#ifndef _WIN32 + sigset_t full, prev; + sigfillset (&full); + sigprocmask (SIG_SETMASK, &full, &prev); +#endif + + array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); + +#ifndef _WIN32 + sigprocmask (SIG_SETMASK, &prev, 0); +#endif + } + ev_start (EV_A_ (W)w, 1); - array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); - wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); + wlist_add (&signals [w->signum - 1].head, (WL)w); if (!((WL)w)->next) { @@ -1760,14 +1784,14 @@ ev_signal_start (EV_P_ ev_signal *w) } } -void +void noinline ev_signal_stop (EV_P_ ev_signal *w) { clear_pending (EV_A_ (W)w); if (expect_false (!ev_is_active (w))) return; - wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); + wlist_del (&signals [w->signum - 1].head, (WL)w); ev_stop (EV_A_ (W)w); if (!signals [w->signum - 1].head) @@ -1784,7 +1808,7 @@ ev_child_start (EV_P_ ev_child *w) return; ev_start (EV_A_ (W)w, 1); - wlist_add ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); + wlist_add (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); } void @@ -1794,7 +1818,7 @@ ev_child_stop (EV_P_ ev_child *w) if (expect_false (!ev_is_active (w))) return; - wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); + wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); ev_stop (EV_A_ (W)w); }