From 7d502fb448be74919aedb2ef895cc0f656898871 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 5 Mar 2019 22:06:09 +0100 Subject: [PATCH] event/Loop: round epoll_wait() timeout up This implements proper rounding, amending commit dcbb9fe07ce --- src/event/Loop.cxx | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/event/Loop.cxx b/src/event/Loop.cxx index a5475c3fb..6631c41c7 100644 --- a/src/event/Loop.cxx +++ b/src/event/Loop.cxx @@ -162,6 +162,18 @@ EventLoop::HandleTimers() noexcept return Event::Duration(-1); } +template +static constexpr ToDuration +duration_cast_round_up(std::chrono::duration d) noexcept +{ + using FromDuration = decltype(d); + constexpr auto one = std::chrono::duration_cast(ToDuration(1)); + constexpr auto round_add = one > one.zero() + ? one - FromDuration(1) + : one.zero(); + return std::chrono::duration_cast(d + round_add); +} + /** * Convert the given timeout specification to a milliseconds integer, * to be used by functions like poll() and epoll_wait(). Any negative @@ -171,8 +183,7 @@ static constexpr int ExportTimeoutMS(Event::Duration timeout) { return timeout >= timeout.zero() - /* round up (+1) to avoid unnecessary wakeups */ - ? int(std::chrono::duration_cast(timeout).count()) + 1 + ? int(duration_cast_round_up(timeout).count()) : -1; }