From 13bce5cc766d7f7662ef8b6ef71b2b044675c957 Mon Sep 17 00:00:00 2001 From: rockerbacon Date: Fri, 13 Sep 2019 11:21:26 -0300 Subject: [PATCH] Added mutex to devices to avoid cuncurrency issues --- .gitignore | 4 ++++ CMakeLists.txt | 1 + src/logid/DeviceFinder.cpp | 47 ++++++++++++++++++++------------------ src/logid/DeviceFinder.h | 7 +++--- 4 files changed, 34 insertions(+), 25 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eeff230 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/build +*.swp +*~ +/compile_commands.json diff --git a/CMakeLists.txt b/CMakeLists.txt index 69fbf1e..29fa7c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required(VERSION 3.10) project(logiops) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -Wall -Wextra") diff --git a/src/logid/DeviceFinder.cpp b/src/logid/DeviceFinder.cpp index 268c8f4..1e9e261 100644 --- a/src/logid/DeviceFinder.cpp +++ b/src/logid/DeviceFinder.cpp @@ -20,7 +20,6 @@ void DeviceFinder::addDevice(const char *path) // Asynchronously scan device std::thread{[=]() { - std::vector _devs; const int max_tries = 10; const int try_delay = 250000; @@ -50,7 +49,16 @@ void DeviceFinder::addDevice(const char *path) if(major > 1) // HID++ 2.0 devices only { auto dev = new Device(string_path, index); - _devs.push_back(dev); + + this->devicesMutex.lock(); + this->devices.insert({ + dev, + std::thread{[dev]() { + dev->start(); + }} + }); + this->devicesMutex.unlock(); + log_printf(INFO, "%s detected: device %d on %s", d.name().c_str(), index, string_path.c_str()); } break; @@ -93,28 +101,23 @@ void DeviceFinder::addDevice(const char *path) catch(HIDPP::Dispatcher::NoHIDPPReportException &e) { } catch(std::system_error &e) { log_printf(WARN, "Failed to open %s: %s", string_path.c_str(), e.what()); } - for(auto dev : _devs) - devices.insert({dev, std::thread{[dev]() { dev->start(); }}}); - }}.detach(); } void DeviceFinder::removeDevice(const char* path) { - // Iterate through Devices, stop all in path - auto it = devices.begin(); - while (it != devices.end()) - { - if(it->first->path == path) - { - log_printf(INFO, "%s on %s disconnected.", it->first->name.c_str(), path); - it->first->stop(); - it->second.join(); - delete(it->first); - devices.erase(it); - it++; - } - else - it++; - } -} \ No newline at end of file + devicesMutex.lock(); + // Iterate through Devices, stop all in path + for (auto it = devices.begin(); it != devices.end(); it++) + { + if(it->first->path == path) + { + log_printf(INFO, "%s on %s disconnected.", it->first->name.c_str(), path); + it->first->stop(); + it->second.join(); + delete(it->first); + devices.erase(it); + } + } + devicesMutex.unlock(); +} diff --git a/src/logid/DeviceFinder.h b/src/logid/DeviceFinder.h index e64d6c6..2e1e5e2 100644 --- a/src/logid/DeviceFinder.h +++ b/src/logid/DeviceFinder.h @@ -1,5 +1,4 @@ -#ifndef DEVICEFINDER_H -#define DEVICEFINDER_H +#pragma once #include #include @@ -9,6 +8,7 @@ #include #include #include "Device.h" +#include class Device; @@ -19,8 +19,9 @@ public: protected: void addDevice(const char* path); void removeDevice(const char* path); +private: + std::mutex devicesMutex; }; extern DeviceFinder* finder; -#endif //DEVICEFINDER_H \ No newline at end of file