/**/
+/*
+ * 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))
}
}
-void inline_size
+void inline_speed
queue_events (EV_P_ W *events, int eventcnt, int type)
{
int i;
ANFD *anfd = anfds + fd;
ev_io *w;
- int events = 0;
+ unsigned char events = 0;
for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
- events |= w->events;
+ events |= (unsigned char)w->events;
#if EV_SELECT_IS_WINSOCKET
if (events)
}
#endif
- anfd->reify = 0;
+ {
+ unsigned char o_events = anfd->events;
+ unsigned char o_reify = anfd->reify;
+
+ anfd->reify = 0;
+ anfd->events = events;
- backend_modify (EV_A_ fd, anfd->events, events);
- anfd->events = events;
+ if (o_events != events || o_reify & EV_IOFDSET)
+ backend_modify (EV_A_ fd, o_events, events);
+ }
}
fdchangecnt = 0;
}
void inline_size
-fd_change (EV_P_ int fd)
+fd_change (EV_P_ int fd, int flags)
{
- if (expect_false (anfds [fd].reify))
- return;
-
- anfds [fd].reify = 1;
+ unsigned char reify = anfds [fd].reify;
+ anfds [fd].reify |= flags;
- ++fdchangecnt;
- array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
- fdchanges [fdchangecnt - 1] = fd;
+ if (expect_true (!reify))
+ {
+ ++fdchangecnt;
+ array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
+ fdchanges [fdchangecnt - 1] = fd;
+ }
}
void inline_speed
if (anfds [fd].events)
{
anfds [fd].events = 0;
- fd_change (EV_A_ fd);
+ fd_change (EV_A_ fd, EV_IOFDSET | 1);
}
}
{
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
{
WT w = heap [k];
- while (k < (N >> 1))
+ for (;;)
{
- int j = k << 1;
+ int c = (k << 1) + 1;
- if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
- ++j;
+ if (c >= N)
+ break;
+
+ 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;
/*****************************************************************************/
-static ev_child *childs [EV_PID_HASHSIZE];
+static WL childs [EV_PID_HASHSIZE];
#ifndef _WIN32
{
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)));*/
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 */
{
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.0001220703125 /* 1/8192 */);
+ ((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 = w->offset + (floor ((ev_rt_now - w->offset) / 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 */
/* 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);
/* now rebuild the heap */
for (i = periodiccnt >> 1; i--; )
- downheap ((WT *)periodics, periodiccnt, i);
+ downheap (periodics, periodiccnt, i);
}
#endif
}
#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;
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;
++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 */
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);
+ fd_change (EV_A_ fd, w->events & EV_IOFDSET | 1);
+ w->events &= ~EV_IOFDSET;
}
void noinline
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);
+ fd_change (EV_A_ w->fd, 1);
}
void noinline
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));*/
}
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;
if (expect_true (--active < --timercnt))
{
timers [active] = timers [timercnt];
- adjustheap ((WT *)timers, timercnt, active);
+ adjustheap (timers, timercnt, active);
}
}
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);
((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));*/
}
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;
if (expect_true (--active < --periodiccnt))
{
periodics [active] = periodics [periodiccnt];
- adjustheap ((WT *)periodics, periodiccnt, active);
+ adjustheap (periodics, periodiccnt, active);
}
}
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)
{
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)
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
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);
}