From 271b2873566dd14d052e963ac75b3ccd64c62959 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 4 Feb 2021 21:48:27 +0100 Subject: [PATCH] event/TimerEvent: assign `due` in Schedule() --- src/event/Loop.cxx | 3 +-- src/event/Loop.hxx | 2 +- src/event/TimerEvent.cxx | 16 +++++++++++++--- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/event/Loop.cxx b/src/event/Loop.cxx index ea19c2bd9..2260edead 100644 --- a/src/event/Loop.cxx +++ b/src/event/Loop.cxx @@ -152,11 +152,10 @@ EventLoop::AbandonFD(SocketEvent &event) noexcept } void -EventLoop::AddTimer(TimerEvent &t, Event::Duration d) noexcept +EventLoop::Insert(TimerEvent &t) noexcept { assert(IsInside()); - t.due = SteadyNow() + d; timers.insert(t); again = true; } diff --git a/src/event/Loop.hxx b/src/event/Loop.hxx index 664ec1c77..d74550c5a 100644 --- a/src/event/Loop.hxx +++ b/src/event/Loop.hxx @@ -214,7 +214,7 @@ public: */ bool AbandonFD(SocketEvent &event) noexcept; - void AddTimer(TimerEvent &t, Event::Duration d) noexcept; + void Insert(TimerEvent &t) noexcept; /** * Schedule a call to DeferEvent::RunDeferred(). diff --git a/src/event/TimerEvent.cxx b/src/event/TimerEvent.cxx index 765e79174..dcb24d7b6 100644 --- a/src/event/TimerEvent.cxx +++ b/src/event/TimerEvent.cxx @@ -25,12 +25,22 @@ TimerEvent::Schedule(Event::Duration d) noexcept { Cancel(); - loop.AddTimer(*this, d); + due = loop.SteadyNow() + d; + loop.Insert(*this); } void TimerEvent::ScheduleEarlier(Event::Duration d) noexcept { - if (!IsPending() || loop.SteadyNow() + d < due) - Schedule(d); + const auto new_due = loop.SteadyNow() + d; + + if (IsPending()) { + if (new_due >= due) + return; + + Cancel(); + } + + due = new_due; + loop.Insert(*this); }