From c3c53f7cacbc100b4d2c66d90a8a416d46dac7dd Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 16 Jul 2024 11:04:28 -0700 Subject: [PATCH] Simulation: check if the timer is cancelled (#102) --- trellis/core/node.cpp | 8 ++++---- trellis/core/timer.hpp | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/trellis/core/node.cpp b/trellis/core/node.cpp index f74b81e..1ac8844 100644 --- a/trellis/core/node.cpp +++ b/trellis/core/node.cpp @@ -122,21 +122,21 @@ void Node::UpdateSimulatedClock(const time::TimePoint& new_time) { auto existing_time = time::Now(); bool reset_timers{false}; if (new_time >= existing_time) { - if (timers_.size() > 0) { + if (!timers_.empty()) { if (time::TimePointToMilliseconds(existing_time) != 0) { // Use a priority queue to store the timers we need to fire in order of nearest expiration auto timer_comp = [](const Timer& a, const Timer& b) { return a->GetExpiry() > b->GetExpiry(); }; std::priority_queue, decltype(timer_comp)> expired_timers(timer_comp); - // First find all the timers that are expiring before our new_time + // First find all the non-cancelled timers that are expiring before our new_time for (auto& timer : timers_) { - if (new_time >= timer->GetExpiry()) { + if (!timer->IsCancelled() && new_time >= timer->GetExpiry()) { expired_timers.push(timer); } } // Step forward in time while firing the timers that are expiring until there are no more timers to fire - while (expired_timers.size()) { + while (!expired_timers.empty()) { auto top = expired_timers.top(); expired_timers.pop(); // Move our simulated time up to the expiration time of this timer diff --git a/trellis/core/timer.hpp b/trellis/core/timer.hpp index 21ec79c..1d2134a 100644 --- a/trellis/core/timer.hpp +++ b/trellis/core/timer.hpp @@ -81,6 +81,8 @@ class TimerImpl { Type GetType() const { return type_; } + bool IsCancelled() { return cancelled_; } + private: void KickOff(); void Reload();