Added mutex to devices to avoid cuncurrency issues
This commit is contained in:
parent
d5c5c9040c
commit
13bce5cc76
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
/build
|
||||||
|
*.swp
|
||||||
|
*~
|
||||||
|
/compile_commands.json
|
|
@ -1,6 +1,7 @@
|
||||||
cmake_minimum_required(VERSION 3.10)
|
cmake_minimum_required(VERSION 3.10)
|
||||||
project(logiops)
|
project(logiops)
|
||||||
|
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
|
||||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -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
|
// Asynchronously scan device
|
||||||
std::thread{[=]()
|
std::thread{[=]()
|
||||||
{
|
{
|
||||||
std::vector<Device*> _devs;
|
|
||||||
const int max_tries = 10;
|
const int max_tries = 10;
|
||||||
const int try_delay = 250000;
|
const int try_delay = 250000;
|
||||||
|
|
||||||
|
@ -50,7 +49,16 @@ void DeviceFinder::addDevice(const char *path)
|
||||||
if(major > 1) // HID++ 2.0 devices only
|
if(major > 1) // HID++ 2.0 devices only
|
||||||
{
|
{
|
||||||
auto dev = new Device(string_path, index);
|
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());
|
log_printf(INFO, "%s detected: device %d on %s", d.name().c_str(), index, string_path.c_str());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -93,28 +101,23 @@ void DeviceFinder::addDevice(const char *path)
|
||||||
catch(HIDPP::Dispatcher::NoHIDPPReportException &e) { }
|
catch(HIDPP::Dispatcher::NoHIDPPReportException &e) { }
|
||||||
catch(std::system_error &e) { log_printf(WARN, "Failed to open %s: %s", string_path.c_str(), e.what()); }
|
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();
|
}}.detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeviceFinder::removeDevice(const char* path)
|
void DeviceFinder::removeDevice(const char* path)
|
||||||
{
|
{
|
||||||
// Iterate through Devices, stop all in path
|
devicesMutex.lock();
|
||||||
auto it = devices.begin();
|
// Iterate through Devices, stop all in path
|
||||||
while (it != devices.end())
|
for (auto it = devices.begin(); it != devices.end(); it++)
|
||||||
{
|
{
|
||||||
if(it->first->path == path)
|
if(it->first->path == path)
|
||||||
{
|
{
|
||||||
log_printf(INFO, "%s on %s disconnected.", it->first->name.c_str(), path);
|
log_printf(INFO, "%s on %s disconnected.", it->first->name.c_str(), path);
|
||||||
it->first->stop();
|
it->first->stop();
|
||||||
it->second.join();
|
it->second.join();
|
||||||
delete(it->first);
|
delete(it->first);
|
||||||
devices.erase(it);
|
devices.erase(it);
|
||||||
it++;
|
}
|
||||||
}
|
}
|
||||||
else
|
devicesMutex.unlock();
|
||||||
it++;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -1,5 +1,4 @@
|
||||||
#ifndef DEVICEFINDER_H
|
#pragma once
|
||||||
#define DEVICEFINDER_H
|
|
||||||
|
|
||||||
#include <hid/DeviceMonitor.h>
|
#include <hid/DeviceMonitor.h>
|
||||||
#include <hidpp/SimpleDispatcher.h>
|
#include <hidpp/SimpleDispatcher.h>
|
||||||
|
@ -9,6 +8,7 @@
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include "Device.h"
|
#include "Device.h"
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
class Device;
|
class Device;
|
||||||
|
|
||||||
|
@ -19,8 +19,9 @@ public:
|
||||||
protected:
|
protected:
|
||||||
void addDevice(const char* path);
|
void addDevice(const char* path);
|
||||||
void removeDevice(const char* path);
|
void removeDevice(const char* path);
|
||||||
|
private:
|
||||||
|
std::mutex devicesMutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern DeviceFinder* finder;
|
extern DeviceFinder* finder;
|
||||||
|
|
||||||
#endif //DEVICEFINDER_H
|
|
Loading…
Reference in New Issue
Block a user