Added mutex to devices to avoid cuncurrency issues

master
rockerbacon 5 years ago
parent d5c5c9040c
commit 13bce5cc76
  1. 4
      .gitignore
  2. 1
      CMakeLists.txt
  3. 47
      src/logid/DeviceFinder.cpp
  4. 7
      src/logid/DeviceFinder.h

4
.gitignore vendored

@ -0,0 +1,4 @@
/build
*.swp
*~
/compile_commands.json

@ -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")

@ -20,7 +20,6 @@ void DeviceFinder::addDevice(const char *path)
// Asynchronously scan device
std::thread{[=]()
{
std::vector<Device*> _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++;
}
}
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();
}

@ -1,5 +1,4 @@
#ifndef DEVICEFINDER_H
#define DEVICEFINDER_H
#pragma once
#include <hid/DeviceMonitor.h>
#include <hidpp/SimpleDispatcher.h>
@ -9,6 +8,7 @@
#include <map>
#include <thread>
#include "Device.h"
#include <mutex>
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
Loading…
Cancel
Save