From 7345918f84ba520f71c287f799c033cdf2b190d5 Mon Sep 17 00:00:00 2001 From: Erik Reider <35975961+ErikReider@users.noreply.github.com> Date: Sat, 19 Mar 2022 11:18:51 +0100 Subject: [PATCH] Added mutex locking to resetDevices --- include/modules/upower.hpp | 2 +- src/modules/upower.cpp | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/include/modules/upower.hpp b/include/modules/upower.hpp index 9b09046..45467d3 100644 --- a/include/modules/upower.hpp +++ b/include/modules/upower.hpp @@ -30,7 +30,7 @@ class UPower : public AModule { const gchar *signal_name, GVariant *parameters, gpointer user_data); void removeDevice(const gchar *objectPath); - void addDevice(UpDevice *device); + void addDevice(UpDevice *device, bool lockMutex = true); void setDisplayDevice(); void resetDevices(); diff --git a/src/modules/upower.cpp b/src/modules/upower.cpp index 8f1a373..d42caa5 100644 --- a/src/modules/upower.cpp +++ b/src/modules/upower.cpp @@ -53,6 +53,7 @@ UPower::UPower(const std::string& id, const Json::Value& config) g_signal_connect(client, "device-removed", G_CALLBACK(deviceRemoved_cb), this); resetDevices(); + setDisplayDevice(); } UPower::~UPower() { @@ -92,6 +93,7 @@ void UPower::prepareForSleep_cb(GDBusConnection* system_bus, const gchar* sender if (!sleeping) { UPower* up = static_cast(data); up->resetDevices(); + up->setDisplayDevice(); } } } @@ -107,7 +109,7 @@ void UPower::removeDevice(const gchar* objectPath) { } } -void UPower::addDevice(UpDevice* device) { +void UPower::addDevice(UpDevice* device, bool lockMutex) { if (G_IS_OBJECT(device)) { const gchar* objectPath = up_device_get_object_path(device); @@ -121,7 +123,8 @@ void UPower::addDevice(UpDevice* device) { return; } - std::lock_guard guard(m_Mutex); + if (lockMutex) std::lock_guard guard(m_Mutex); + if (devices.find(objectPath) != devices.end()) { UpDevice* device = devices[objectPath]; if (G_IS_OBJECT(device)) { @@ -143,6 +146,7 @@ void UPower::setDisplayDevice() { /** Removes all devices and adds the current devices */ void UPower::resetDevices() { + std::lock_guard guard(m_Mutex); // Removes all devices if (!devices.empty()) { auto it = devices.cbegin(); @@ -158,11 +162,9 @@ void UPower::resetDevices() { GPtrArray* newDevices = up_client_get_devices2(client); for (guint i = 0; i < newDevices->len; i++) { UpDevice* device = (UpDevice*)g_ptr_array_index(newDevices, i); - if (device) addDevice(device); + if (device) addDevice(device, false); } - setDisplayDevice(); - // Update the widget dp.emit(); }