From b9daeef524f71f880fd232812fc559e263b20e98 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 10 Nov 2023 12:27:51 +0100 Subject: [PATCH] event/FineTimerEvent: add SetDue(), ScheduleCurrent() --- src/event/FineTimerEvent.cxx | 24 ++++++++++++++++++++---- src/event/FineTimerEvent.hxx | 27 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/event/FineTimerEvent.cxx b/src/event/FineTimerEvent.cxx index 0a092019d..a32be763d 100644 --- a/src/event/FineTimerEvent.cxx +++ b/src/event/FineTimerEvent.cxx @@ -5,13 +5,29 @@ #include "FineTimerEvent.hxx" #include "Loop.hxx" +void +FineTimerEvent::SetDue(Event::Duration d) noexcept +{ + assert(!IsPending()); + + SetDue(loop.SteadyNow() + d); +} + +void +FineTimerEvent::ScheduleCurrent() noexcept +{ + assert(!IsPending()); + + loop.Insert(*this); +} + void FineTimerEvent::Schedule(Event::Duration d) noexcept { Cancel(); - due = loop.SteadyNow() + d; - loop.Insert(*this); + SetDue(d); + ScheduleCurrent(); } void @@ -26,6 +42,6 @@ FineTimerEvent::ScheduleEarlier(Event::Duration d) noexcept Cancel(); } - due = new_due; - loop.Insert(*this); + SetDue(due); + ScheduleCurrent(); } diff --git a/src/event/FineTimerEvent.hxx b/src/event/FineTimerEvent.hxx index bd30c6cb5..e0cd15d94 100644 --- a/src/event/FineTimerEvent.hxx +++ b/src/event/FineTimerEvent.hxx @@ -14,6 +14,8 @@ #include #endif +#include + class EventLoop; /** @@ -62,6 +64,24 @@ public: return due; } + /** + * Set the due time as an absolute time point. This can be + * done to prepare an eventual ScheduleCurrent() call. Must + * not be called while the timer is already scheduled. + */ + void SetDue(Event::TimePoint _due) noexcept { + assert(!IsPending()); + + due = _due; + } + + /** + * Set the due time as a duration relative to now. This can + * done to prepare an eventual ScheduleCurrent() call. Must + * not be called while the timer is already scheduled. + */ + void SetDue(Event::Duration d) noexcept; + /** * Was this timer scheduled? */ @@ -69,6 +89,13 @@ public: return is_linked(); } + /** + * Schedule the timer at the due time that was already set; + * either by SetDue() or by a Schedule() call that was already + * canceled. + */ + void ScheduleCurrent() noexcept; + void Schedule(Event::Duration d) noexcept; /**