From 05eda56e668f3a04c1f2600cd1a887ecd6ee1961 Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 23 Dec 2023 14:20:10 -0500 Subject: [PATCH] core_timing: handle concurrent unscheduling of looping events --- src/core/core_timing.cpp | 14 +++++++++++--- src/core/core_timing.h | 5 ++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp index d08c007bbe..c85590d4c5 100644 --- a/src/core/core_timing.cpp +++ b/src/core/core_timing.cpp @@ -159,6 +159,8 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr& event_type, for (auto h : to_remove) { event_queue.erase(h); } + + event_type->sequence_number++; } // Force any in-progress events to finish @@ -202,9 +204,10 @@ std::optional CoreTiming::Advance() { const Event& evt = event_queue.top(); if (const auto event_type{evt.type.lock()}) { - if (evt.reschedule_time == 0) { - const auto evt_time = evt.time; + const auto evt_time = evt.time; + const auto evt_sequence_num = event_type->sequence_number; + if (evt.reschedule_time == 0) { event_queue.pop(); basic_lock.unlock(); @@ -217,10 +220,15 @@ std::optional CoreTiming::Advance() { basic_lock.unlock(); const auto new_schedule_time{event_type->callback( - evt.time, std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt.time})}; + evt_time, std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt_time})}; basic_lock.lock(); + if (evt_sequence_num != event_type->sequence_number) { + // Heap handle is invalidated after external modification. + continue; + } + const auto next_schedule_time{new_schedule_time.has_value() ? new_schedule_time.value().count() : evt.reschedule_time}; diff --git a/src/core/core_timing.h b/src/core/core_timing.h index d8cd599ee7..7e4dff7f3d 100644 --- a/src/core/core_timing.h +++ b/src/core/core_timing.h @@ -27,12 +27,15 @@ using TimedCallback = std::function( /// Contains the characteristics of a particular event. struct EventType { explicit EventType(TimedCallback&& callback_, std::string&& name_) - : callback{std::move(callback_)}, name{std::move(name_)} {} + : callback{std::move(callback_)}, name{std::move(name_)}, sequence_number{0} {} /// The event's callback function. TimedCallback callback; /// A pointer to the name of the event. const std::string name; + /// A monotonic sequence number, incremented when this event is + /// changed externally. + size_t sequence_number; }; enum class UnscheduleEventType {