event/Loop: move TimerSet to separate class

This commit is contained in:
Max Kellermann
2021-02-04 21:25:57 +01:00
committed by Max Kellermann
parent 271b287356
commit e802f1f61a
6 changed files with 160 additions and 42 deletions

View File

@@ -34,13 +34,6 @@
#include <stdio.h>
#endif
constexpr bool
EventLoop::TimerCompare::operator()(const TimerEvent &a,
const TimerEvent &b) const noexcept
{
return a.due < b.due;
}
EventLoop::EventLoop(
#ifdef HAVE_THREADED_EVENT_LOOP
ThreadId _thread
@@ -60,7 +53,6 @@ EventLoop::EventLoop(
EventLoop::~EventLoop() noexcept
{
assert(timers.empty());
assert(defer.empty());
assert(idle.empty());
#ifdef HAVE_THREADED_EVENT_LOOP
@@ -156,33 +148,14 @@ EventLoop::Insert(TimerEvent &t) noexcept
{
assert(IsInside());
timers.insert(t);
timers.Insert(t);
again = true;
}
inline Event::Duration
EventLoop::HandleTimers() noexcept
{
const auto now = SteadyNow();
Event::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 Event::Duration(-1);
return timers.Run(SteadyNow());
}
void