event/IdleEvent: make a special case of DeferEvent

This commit is contained in:
Max Kellermann
2020-12-01 17:04:14 +01:00
parent 990f2dc1cf
commit c58aaf545f
7 changed files with 66 additions and 70 deletions

View File

@@ -153,15 +153,6 @@ EventLoop::RemoveFD(int fd, SocketEvent &event) noexcept
return poll_backend.Remove(fd);
}
void
EventLoop::AddIdle(IdleEvent &i) noexcept
{
assert(IsInside());
idle.push_back(i);
again = true;
}
void
EventLoop::AddTimer(TimerEvent &t, Event::Duration d) noexcept
{
@@ -202,6 +193,13 @@ EventLoop::AddDeferred(DeferEvent &d) noexcept
again = true;
}
void
EventLoop::AddIdle(DeferEvent &e) noexcept
{
idle.push_front(e);
again = true;
}
void
EventLoop::RunDeferred() noexcept
{
@@ -212,6 +210,19 @@ EventLoop::RunDeferred() noexcept
}
}
bool
EventLoop::RunOneIdle() noexcept
{
if (idle.empty())
return false;
idle.pop_front_and_dispose([](DeferEvent *e){
e->Run();
});
return true;
}
template<class ToDuration, class Rep, class Period>
static constexpr ToDuration
duration_cast_round_up(std::chrono::duration<Rep, Period> d) noexcept
@@ -301,16 +312,12 @@ EventLoop::Run() noexcept
RunDeferred();
/* invoke idle */
while (!idle.empty()) {
IdleEvent &m = idle.front();
idle.pop_front();
m.Run();
if (quit)
return;
}
if (RunOneIdle())
/* check for other new events after each
"idle" invocation to ensure that the other
"idle" events are really invoked at the
very end */
continue;
#ifdef HAVE_THREADED_EVENT_LOOP
/* try to handle DeferEvents without WakeFD