Use safe thread class instead of std::thread

This commit is contained in:
pixl
2020-06-24 17:31:16 -04:00
parent d84363019b
commit 4ba9248038
7 changed files with 74 additions and 23 deletions

View File

@@ -20,6 +20,7 @@
#include "Report.h"
#include "Receiver.h"
#include "Error.h"
#include "../../util/thread.h"
using namespace logid::backend::dj;
using namespace logid::backend;
@@ -214,13 +215,14 @@ std::string Receiver::getDeviceName(hidpp::DeviceIndex index)
return name;
}
hidpp::DeviceIndex Receiver::deviceDisconnectionEvent(hidpp::Report& report)
hidpp::DeviceIndex Receiver::deviceDisconnectionEvent(const hidpp::Report&
report)
{
assert(report.subId() == DeviceDisconnection);
return report.deviceIndex();
}
hidpp::DeviceConnectionEvent Receiver::deviceConnectionEvent(
hidpp::DeviceConnectionEvent Receiver::deviceConnectionEvent(const
hidpp::Report &report)
{
assert(report.subId() == DeviceConnection);
@@ -322,7 +324,10 @@ Receiver::ConnectionStatusEvent Receiver::connectionStatusEvent(Report& report)
void Receiver::listen()
{
if(!_raw_device->isListening())
std::thread{[this]() { this->_raw_device->listen(); }}.detach();
///TODO: Kill RawDevice?
thread::spawn({[raw=this->_raw_device]() {
raw->listen();
}});
if(_raw_device->eventHandlers().find("RECV_HIDPP") ==
_raw_device->eventHandlers().end()) {

View File

@@ -140,9 +140,9 @@ namespace dj
std::string getDeviceName(hidpp::DeviceIndex index);
static hidpp::DeviceIndex deviceDisconnectionEvent(
hidpp::Report& report);
const hidpp::Report& report);
static hidpp::DeviceConnectionEvent deviceConnectionEvent(
hidpp::Report& report);
const hidpp::Report& report);
void listen();
void stopListening();

View File

@@ -17,6 +17,8 @@
*/
#include "ReceiverMonitor.h"
#include "../../util/thread.h"
#include "../../util.h"
#include <utility>
#include <cassert>
@@ -55,14 +57,24 @@ void ReceiverMonitor::run()
/* Running in a new thread prevents deadlocks since the
* receiver may be enumerating.
*/
std::thread{[this](hidpp::Report report) {
thread::spawn({[this, report]() {
if (report.subId() == Receiver::DeviceConnection)
this->addDevice(this->_receiver->deviceConnectionEvent(
report));
this->addDevice(this->_receiver->deviceConnectionEvent
(report));
else if (report.subId() == Receiver::DeviceDisconnection)
this->removeDevice(this->_receiver->
deviceDisconnectionEvent(report));
}, report}.detach();
}}, {[report, path=this->_receiver->rawDevice()->hidrawPath()]
(std::exception& e) {
if(report.subId() == Receiver::DeviceConnection)
log_printf(ERROR, "Failed to add device %d to receiver "
"on %s: %s", report.deviceIndex(),
path.c_str(), e.what());
else if(report.subId() == Receiver::DeviceDisconnection)
log_printf(ERROR, "Failed to remove device %d from "
"receiver on %s: %s", report.deviceIndex()
,path.c_str(), e.what());
}});
};
_receiver->addHidppEventHandler("RECVMON", event_handler);