From 730ef9d3470c2ee038e4f637a11f3d4488e868df Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Fri, 12 Feb 2021 13:18:08 -0500 Subject: [PATCH] serialqueue: Improve timing of sleep durations If any callback is invoked from the pollreactor then obtain a new eventtime before calculating a sleep duration. This improves the timing of events. Signed-off-by: Kevin O'Connor --- klippy/chelper/serialqueue.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/klippy/chelper/serialqueue.c b/klippy/chelper/serialqueue.c index 4c40cc11..cc22d021 100644 --- a/klippy/chelper/serialqueue.c +++ b/klippy/chelper/serialqueue.c @@ -123,24 +123,27 @@ pollreactor_update_timer(struct pollreactor *pr, int pos, double waketime) // Internal code to invoke timer callbacks static int -pollreactor_check_timers(struct pollreactor *pr, double eventtime) +pollreactor_check_timers(struct pollreactor *pr, double eventtime, int busy) { if (eventtime >= pr->next_timer) { + // Find and run pending timers pr->next_timer = PR_NEVER; int i; for (i=0; inum_timers; i++) { struct pollreactor_timer *timer = &pr->timers[i]; double t = timer->waketime; if (eventtime >= t) { + busy = 1; t = timer->callback(pr->callback_data, eventtime); timer->waketime = t; } if (t < pr->next_timer) pr->next_timer = t; } - if (eventtime >= pr->next_timer) - return 0; } + if (busy) + return 0; + // Calculate sleep duration double timeout = ceil((pr->next_timer - eventtime) * 1000.); return timeout < 1. ? 1 : (timeout > 1000. ? 1000 : (int)timeout); } @@ -150,11 +153,14 @@ static void pollreactor_run(struct pollreactor *pr) { double eventtime = get_monotonic(); + int busy = 1; while (! pr->must_exit) { - int timeout = pollreactor_check_timers(pr, eventtime); + int timeout = pollreactor_check_timers(pr, eventtime, busy); + busy = 0; int ret = poll(pr->fds, pr->num_fds, timeout); eventtime = get_monotonic(); if (ret > 0) { + busy = 1; int i; for (i=0; inum_fds; i++) if (pr->fds[i].revents)