From 2ac0b760caae78d48c386be072e9cb8d3693e416 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sun, 17 Sep 2023 13:12:26 -0400 Subject: Fixed race condition in reachability --- src/tracker_state.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/tracker_state.cpp b/src/tracker_state.cpp index cc19b19..0976461 100644 --- a/src/tracker_state.cpp +++ b/src/tracker_state.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -13,6 +14,7 @@ namespace { struct TrackerState { std::map reachability; + std::mutex reachability_mutex; }; enum Decision { kYes, kNo, kMaybe }; @@ -139,8 +141,6 @@ Decision IsPanelReachable_Helper(int panel_id, } // namespace void RecalculateReachability() { - GetState().reachability.clear(); - std::set reachable_rooms; std::set solveable_panels; @@ -221,6 +221,7 @@ void RecalculateReachability() { panel_boundary = new_panel_boundary; } + std::map new_reachability; for (const MapArea& map_area : GD_GetMapAreas()) { for (size_t section_id = 0; section_id < map_area.locations.size(); section_id++) { @@ -232,12 +233,19 @@ void RecalculateReachability() { } } - GetState().reachability[location_section.ap_location_id] = reachable; + new_reachability[location_section.ap_location_id] = reachable; } } + + { + std::lock_guard reachability_guard(GetState().reachability_mutex); + std::swap(GetState().reachability, new_reachability); + } } bool IsLocationReachable(int location_id) { + std::lock_guard reachability_guard(GetState().reachability_mutex); + if (GetState().reachability.count(location_id)) { return GetState().reachability.at(location_id); } else { -- cgit 1.4.1