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));
|
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,
|
* Convert the given timeout specification to a milliseconds integer,
|
||||||
* to be used by functions like poll() and epoll_wait(). Any negative
|
* to be used by functions like poll() and epoll_wait(). Any negative
|
||||||
@ -130,26 +153,9 @@ EventLoop::Run() noexcept
|
|||||||
|
|
||||||
/* invoke timers */
|
/* invoke timers */
|
||||||
|
|
||||||
std::chrono::steady_clock::duration timeout;
|
const auto timeout = HandleTimers();
|
||||||
while (true) {
|
if (quit)
|
||||||
auto i = timers.begin();
|
break;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* invoke idle */
|
/* invoke idle */
|
||||||
|
|
||||||
|
@ -189,6 +189,13 @@ private:
|
|||||||
*/
|
*/
|
||||||
void HandleDeferred() noexcept;
|
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;
|
bool OnSocketReady(unsigned flags) noexcept override;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
Loading…
Reference in New Issue
Block a user