From 33ba190bec97f30a8509d731d163e51e710f8518 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 12 Nov 2018 22:01:47 +0100 Subject: [PATCH] event/TimerEvent: add ScheduleEarlier() --- src/event/TimerEvent.cxx | 7 +++++++ src/event/TimerEvent.hxx | 9 +++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/event/TimerEvent.cxx b/src/event/TimerEvent.cxx index e2fc384e2..52bf2cd5a 100644 --- a/src/event/TimerEvent.cxx +++ b/src/event/TimerEvent.cxx @@ -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); +} diff --git a/src/event/TimerEvent.hxx b/src/event/TimerEvent.hxx index baadcdb4e..0fe775fac 100644 --- a/src/event/TimerEvent.hxx +++ b/src/event/TimerEvent.hxx @@ -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(); }