Added mutex locking to resetDevices

This commit is contained in:
Erik Reider 2022-03-19 11:18:51 +01:00
parent 105f1cefe1
commit 7345918f84
2 changed files with 8 additions and 6 deletions

View File

@ -30,7 +30,7 @@ class UPower : public AModule {
const gchar *signal_name, GVariant *parameters, const gchar *signal_name, GVariant *parameters,
gpointer user_data); gpointer user_data);
void removeDevice(const gchar *objectPath); void removeDevice(const gchar *objectPath);
void addDevice(UpDevice *device); void addDevice(UpDevice *device, bool lockMutex = true);
void setDisplayDevice(); void setDisplayDevice();
void resetDevices(); void resetDevices();

View File

@ -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); g_signal_connect(client, "device-removed", G_CALLBACK(deviceRemoved_cb), this);
resetDevices(); resetDevices();
setDisplayDevice();
} }
UPower::~UPower() { UPower::~UPower() {
@ -92,6 +93,7 @@ void UPower::prepareForSleep_cb(GDBusConnection* system_bus, const gchar* sender
if (!sleeping) { if (!sleeping) {
UPower* up = static_cast<UPower*>(data); UPower* up = static_cast<UPower*>(data);
up->resetDevices(); 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)) { if (G_IS_OBJECT(device)) {
const gchar* objectPath = up_device_get_object_path(device); const gchar* objectPath = up_device_get_object_path(device);
@ -121,7 +123,8 @@ void UPower::addDevice(UpDevice* device) {
return; return;
} }
std::lock_guard<std::mutex> guard(m_Mutex); if (lockMutex) std::lock_guard<std::mutex> guard(m_Mutex);
if (devices.find(objectPath) != devices.end()) { if (devices.find(objectPath) != devices.end()) {
UpDevice* device = devices[objectPath]; UpDevice* device = devices[objectPath];
if (G_IS_OBJECT(device)) { if (G_IS_OBJECT(device)) {
@ -143,6 +146,7 @@ void UPower::setDisplayDevice() {
/** Removes all devices and adds the current devices */ /** Removes all devices and adds the current devices */
void UPower::resetDevices() { void UPower::resetDevices() {
std::lock_guard<std::mutex> guard(m_Mutex);
// Removes all devices // Removes all devices
if (!devices.empty()) { if (!devices.empty()) {
auto it = devices.cbegin(); auto it = devices.cbegin();
@ -158,11 +162,9 @@ void UPower::resetDevices() {
GPtrArray* newDevices = up_client_get_devices2(client); GPtrArray* newDevices = up_client_get_devices2(client);
for (guint i = 0; i < newDevices->len; i++) { for (guint i = 0; i < newDevices->len; i++) {
UpDevice* device = (UpDevice*)g_ptr_array_index(newDevices, i); UpDevice* device = (UpDevice*)g_ptr_array_index(newDevices, i);
if (device) addDevice(device); if (device) addDevice(device, false);
} }
setDisplayDevice();
// Update the widget // Update the widget
dp.emit(); dp.emit();
} }