about summary refs log tree commit diff stats
path: root/src/tracker_state.cpp
diff options
context:
space:
mode:
authorStar Rauchenberger <fefferburbia@gmail.com>2023-09-17 13:12:26 -0400
committerStar Rauchenberger <fefferburbia@gmail.com>2023-09-17 13:12:26 -0400
commit2ac0b760caae78d48c386be072e9cb8d3693e416 (patch)
tree9f02370841fde19a929065420dbceb155aea8186 /src/tracker_state.cpp
parent8e89e198830a0908ddd8f4c91cbc2bff08b0b32c (diff)
downloadlingo-ap-tracker-2ac0b760caae78d48c386be072e9cb8d3693e416.tar.gz
lingo-ap-tracker-2ac0b760caae78d48c386be072e9cb8d3693e416.tar.bz2
lingo-ap-tracker-2ac0b760caae78d48c386be072e9cb8d3693e416.zip
Fixed race condition in reachability
Diffstat (limited to 'src/tracker_state.cpp')
-rw-r--r--src/tracker_state.cpp14
1 files 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 @@
2 2
3#include <list> 3#include <list>
4#include <map> 4#include <map>
5#include <mutex>
5#include <set> 6#include <set>
6#include <sstream> 7#include <sstream>
7#include <tuple> 8#include <tuple>
@@ -13,6 +14,7 @@ namespace {
13 14
14struct TrackerState { 15struct TrackerState {
15 std::map<int, bool> reachability; 16 std::map<int, bool> reachability;
17 std::mutex reachability_mutex;
16}; 18};
17 19
18enum Decision { kYes, kNo, kMaybe }; 20enum Decision { kYes, kNo, kMaybe };
@@ -139,8 +141,6 @@ Decision IsPanelReachable_Helper(int panel_id,
139} // namespace 141} // namespace
140 142
141void RecalculateReachability() { 143void RecalculateReachability() {
142 GetState().reachability.clear();
143
144 std::set<int> reachable_rooms; 144 std::set<int> reachable_rooms;
145 std::set<int> solveable_panels; 145 std::set<int> solveable_panels;
146 146
@@ -221,6 +221,7 @@ void RecalculateReachability() {
221 panel_boundary = new_panel_boundary; 221 panel_boundary = new_panel_boundary;
222 } 222 }
223 223
224 std::map<int, bool> new_reachability;
224 for (const MapArea& map_area : GD_GetMapAreas()) { 225 for (const MapArea& map_area : GD_GetMapAreas()) {
225 for (size_t section_id = 0; section_id < map_area.locations.size(); 226 for (size_t section_id = 0; section_id < map_area.locations.size();
226 section_id++) { 227 section_id++) {
@@ -232,12 +233,19 @@ void RecalculateReachability() {
232 } 233 }
233 } 234 }
234 235
235 GetState().reachability[location_section.ap_location_id] = reachable; 236 new_reachability[location_section.ap_location_id] = reachable;
236 } 237 }
237 } 238 }
239
240 {
241 std::lock_guard reachability_guard(GetState().reachability_mutex);
242 std::swap(GetState().reachability, new_reachability);
243 }
238} 244}
239 245
240bool IsLocationReachable(int location_id) { 246bool IsLocationReachable(int location_id) {
247 std::lock_guard reachability_guard(GetState().reachability_mutex);
248
241 if (GetState().reachability.count(location_id)) { 249 if (GetState().reachability.count(location_id)) {
242 return GetState().reachability.at(location_id); 250 return GetState().reachability.at(location_id);
243 } else { 251 } else {