From b611b1824a8b121b43b530a8f63e9c3f9636c468 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 15 Oct 2020 20:19:01 +0200 Subject: [PATCH] event/Loop: move code to Wait() --- src/event/Loop.cxx | 26 +++++++++++++++++--------- src/event/Loop.hxx | 8 ++++++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/event/Loop.cxx b/src/event/Loop.cxx index 80507aa5d..e540f796a 100644 --- a/src/event/Loop.cxx +++ b/src/event/Loop.cxx @@ -184,6 +184,22 @@ ExportTimeoutMS(Event::Duration timeout) : -1; } +inline bool +EventLoop::Wait(Event::Duration timeout) noexcept +{ + const auto poll_result = + poll_group.ReadEvents(ExportTimeoutMS(timeout)); + + ready_sockets.clear(); + for (size_t i = 0; i < poll_result.GetSize(); ++i) { + auto &s = *(SocketEvent *)poll_result.GetObject(i); + s.SetReadyFlags(poll_result.GetEvents(i)); + ready_sockets.push_back(s); + } + + return poll_result.GetSize() > 0; +} + void EventLoop::Run() noexcept { @@ -257,15 +273,7 @@ EventLoop::Run() noexcept /* wait for new event */ - const auto poll_result = - poll_group.ReadEvents(ExportTimeoutMS(timeout)); - - ready_sockets.clear(); - for (size_t i = 0; i < poll_result.GetSize(); ++i) { - auto &s = *(SocketEvent *)poll_result.GetObject(i); - s.SetReadyFlags(poll_result.GetEvents(i)); - ready_sockets.push_back(s); - } + Wait(timeout); now = std::chrono::steady_clock::now(); diff --git a/src/event/Loop.hxx b/src/event/Loop.hxx index 3ffb310e9..a44cda78c 100644 --- a/src/event/Loop.hxx +++ b/src/event/Loop.hxx @@ -258,6 +258,14 @@ private: */ Event::Duration HandleTimers() noexcept; + /** + * Call epoll_wait() and pass all returned events to + * SocketEvent::SetReadyFlags(). + * + * @return true if one or more sockets have become ready + */ + bool Wait(Event::Duration timeout) noexcept; + #ifdef HAVE_THREADED_EVENT_LOOP void OnSocketReady(unsigned flags) noexcept; #endif