fix device_data race condition with mutex

pull/1316/head
Etaash Mathamsetty 1 month ago committed by flightlessmango
parent 44eb25e10e
commit 0e92ec4790

@ -6,6 +6,7 @@
namespace fs = ghc::filesystem; namespace fs = ghc::filesystem;
using namespace std; using namespace std;
std::mutex device_lock;
std::vector<device_batt> device_data; std::vector<device_batt> device_data;
std::vector<std::string> list; std::vector<std::string> list;
bool device_found = false; 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){ void device_update(const struct overlay_params& params){
std::unique_lock<std::mutex> l(device_lock);
fs::path path("/sys/class/power_supply"); fs::path path("/sys/class/power_supply");
list.clear(); list.clear();
xbox_count = 0; xbox_count = 0;
@ -87,6 +89,7 @@ void device_update(const struct overlay_params& params){
void device_info () { void device_info () {
device_count = 0; device_count = 0;
std::unique_lock<std::mutex> l(device_lock);
device_data.clear(); device_data.clear();
//gamepad counters //gamepad counters
int xbox_counter = 0; int xbox_counter = 0;

@ -14,6 +14,7 @@ struct device_batt {
}; };
extern std::vector<device_batt> device_data; extern std::vector<device_batt> device_data;
extern std::mutex device_lock;
extern bool device_found; extern bool device_found;
extern int device_count; extern int device_count;

@ -1144,6 +1144,7 @@ void HudElements::gamescope_frame_timing(){
void HudElements::device_battery() void HudElements::device_battery()
{ {
#ifdef __linux__ #ifdef __linux__
std::unique_lock<std::mutex> l(device_lock);
if (!HUDElements.params->device_battery.empty()) { if (!HUDElements.params->device_battery.empty()) {
if (device_found) { if (device_found) {
for (int i = 0; i < device_count; i++) { for (int i = 0; i < device_count; i++) {

Loading…
Cancel
Save