event/Loop: move code to HandleTimers()
This commit is contained in:
parent
9ce6828d72
commit
199c8aaa25
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user