From 199c8aaa25aacba936bfaada322ac74ee0616c0e Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 29 Jan 2018 21:45:36 +0100 Subject: [PATCH] event/Loop: move code to HandleTimers() --- src/event/Loop.cxx | 46 ++++++++++++++++++++++++++-------------------- src/event/Loop.hxx | 7 +++++++ 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/event/Loop.cxx b/src/event/Loop.cxx index e9bc9fee0..a52921084 100644 --- a/src/event/Loop.cxx +++ b/src/event/Loop.cxx @@ -98,6 +98,29 @@ EventLoop::CancelTimer(TimerEvent &t) noexcept timers.erase(timers.iterator_to(t)); } +inline std::chrono::steady_clock::duration +EventLoop::HandleTimers() noexcept +{ + std::chrono::steady_clock::duration timeout; + + while (!quit) { + auto i = timers.begin(); + if (i == timers.end()) + break; + + TimerEvent &t = *i; + timeout = t.due - now; + if (timeout > timeout.zero()) + return timeout; + + timers.erase(i); + + t.Run(); + } + + return std::chrono::steady_clock::duration(-1); +} + /** * Convert the given timeout specification to a milliseconds integer, * to be used by functions like poll() and epoll_wait(). Any negative @@ -130,26 +153,9 @@ EventLoop::Run() noexcept /* invoke timers */ - std::chrono::steady_clock::duration timeout; - while (true) { - auto i = timers.begin(); - if (i == timers.end()) { - timeout = std::chrono::steady_clock::duration(-1); - break; - } - - TimerEvent &t = *i; - timeout = t.due - now; - if (timeout > timeout.zero()) - break; - - timers.erase(i); - - t.Run(); - - if (quit) - return; - } + const auto timeout = HandleTimers(); + if (quit) + break; /* invoke idle */ diff --git a/src/event/Loop.hxx b/src/event/Loop.hxx index 7dd9959b1..788933f03 100644 --- a/src/event/Loop.hxx +++ b/src/event/Loop.hxx @@ -189,6 +189,13 @@ private: */ void HandleDeferred() noexcept; + /** + * Invoke all expired #TimerEvent instances and return the + * duration until the next timer expires. Returns a negative + * duration if there is no timeout. + */ + std::chrono::steady_clock::duration HandleTimers() noexcept; + bool OnSocketReady(unsigned flags) noexcept override; public: