event/TimerEvent: assign due in Schedule()

This commit is contained in:
Max Kellermann 2021-02-04 21:48:27 +01:00 committed by Max Kellermann
parent 2a30acd99c
commit 271b287356
3 changed files with 15 additions and 6 deletions

View File

@ -152,11 +152,10 @@ EventLoop::AbandonFD(SocketEvent &event) noexcept
} }
void void
EventLoop::AddTimer(TimerEvent &t, Event::Duration d) noexcept EventLoop::Insert(TimerEvent &t) noexcept
{ {
assert(IsInside()); assert(IsInside());
t.due = SteadyNow() + d;
timers.insert(t); timers.insert(t);
again = true; again = true;
} }

View File

@ -214,7 +214,7 @@ public:
*/ */
bool AbandonFD(SocketEvent &event) noexcept; bool AbandonFD(SocketEvent &event) noexcept;
void AddTimer(TimerEvent &t, Event::Duration d) noexcept; void Insert(TimerEvent &t) noexcept;
/** /**
* Schedule a call to DeferEvent::RunDeferred(). * Schedule a call to DeferEvent::RunDeferred().

View File

@ -25,12 +25,22 @@ TimerEvent::Schedule(Event::Duration d) noexcept
{ {
Cancel(); Cancel();
loop.AddTimer(*this, d); due = loop.SteadyNow() + d;
loop.Insert(*this);
} }
void void
TimerEvent::ScheduleEarlier(Event::Duration d) noexcept TimerEvent::ScheduleEarlier(Event::Duration d) noexcept
{ {
if (!IsPending() || loop.SteadyNow() + d < due) const auto new_due = loop.SteadyNow() + d;
Schedule(d);
if (IsPending()) {
if (new_due >= due)
return;
Cancel();
}
due = new_due;
loop.Insert(*this);
} }