Commit 199c8aaa authored by Max Kellermann's avatar Max Kellermann

event/Loop: move code to HandleTimers()

parent 9ce6828d
......@@ -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 */
......
......@@ -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:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment