From 0e92ec4790a76278458834d5a83c17274924aca4 Mon Sep 17 00:00:00 2001 From: Etaash Mathamsetty Date: Mon, 29 Apr 2024 15:38:46 -0400 Subject: [PATCH] fix device_data race condition with mutex --- src/device.cpp | 3 +++ src/device.h | 1 + src/hud_elements.cpp | 1 + 3 files changed, 5 insertions(+) diff --git a/src/device.cpp b/src/device.cpp index 2558a983..8ae75609 100644 --- a/src/device.cpp +++ b/src/device.cpp @@ -6,6 +6,7 @@ namespace fs = ghc::filesystem; using namespace std; +std::mutex device_lock; std::vector device_data; std::vector list; bool device_found = false; @@ -28,6 +29,7 @@ static bool operator<(const device_batt& a, const device_batt& b) void device_update(const struct overlay_params& params){ + std::unique_lock l(device_lock); fs::path path("/sys/class/power_supply"); list.clear(); xbox_count = 0; @@ -87,6 +89,7 @@ void device_update(const struct overlay_params& params){ void device_info () { device_count = 0; + std::unique_lock l(device_lock); device_data.clear(); //gamepad counters int xbox_counter = 0; diff --git a/src/device.h b/src/device.h index f7d87036..8c863f91 100644 --- a/src/device.h +++ b/src/device.h @@ -14,6 +14,7 @@ struct device_batt { }; extern std::vector device_data; +extern std::mutex device_lock; extern bool device_found; extern int device_count; diff --git a/src/hud_elements.cpp b/src/hud_elements.cpp index ae06ed8b..7e68c500 100644 --- a/src/hud_elements.cpp +++ b/src/hud_elements.cpp @@ -1144,6 +1144,7 @@ void HudElements::gamescope_frame_timing(){ void HudElements::device_battery() { #ifdef __linux__ + std::unique_lock l(device_lock); if (!HUDElements.params->device_battery.empty()) { if (device_found) { for (int i = 0; i < device_count; i++) {