event/TimerEvent: add ScheduleEarlier()

This commit is contained in:
Max Kellermann 2018-11-12 22:01:47 +01:00 committed by Max Kellermann
parent 3783350d25
commit 33ba190bec
2 changed files with 14 additions and 2 deletions

View File

@ -27,3 +27,10 @@ TimerEvent::Schedule(Event::Duration d) noexcept
loop.AddTimer(*this, d);
}
void
TimerEvent::ScheduleEarlier(Event::Duration d) noexcept
{
if (!IsPending() || loop.SteadyNow() + d < due)
Schedule(d);
}

View File

@ -53,8 +53,7 @@ class TimerEvent final
public:
TimerEvent(EventLoop &_loop, Callback _callback) noexcept
:loop(_loop), callback(_callback) {
}
:loop(_loop), callback(_callback) {}
auto &GetEventLoop() const noexcept {
return loop;
@ -66,6 +65,12 @@ public:
void Schedule(Event::Duration d) noexcept;
/**
* Like Schedule(), but is a no-op if there is a due time
* earlier than the given one.
*/
void ScheduleEarlier(Event::Duration d) noexcept;
void Cancel() noexcept {
unlink();
}