Add mutex to ensure thread safety

smart-pointers
Phil Taylor 2023-02-10 15:23:57 +00:00
rodzic fa47bfb4bc
commit c8a29bb400
6 zmienionych plików z 42 dodań i 16 usunięć

Wyświetl plik

@ -70,6 +70,9 @@ void controllerSetup::mousePressed(QPoint p)
}
bool found = false;
QMutexLocker locker(mutex);
for (BUTTON& b : *buttons)
{
if (b.dev == currentDevice && b.pos.contains(p))
@ -119,20 +122,25 @@ void controllerSetup::mousePressed(QPoint p)
offEvent->hide();
knobEvent->hide();
}
}
void controllerSetup::onEventIndexChanged(int index) {
Q_UNUSED(index);
if (currentButton != Q_NULLPTR && onEvent->currentData().toInt() < commands->size()) {
QMutexLocker locker(mutex);
currentButton->onCommand = &commands->at(onEvent->currentData().toInt());
currentButton->onText->setPlainText(currentButton->onCommand->text);
}
}
void controllerSetup::offEventIndexChanged(int index) {
Q_UNUSED(index);
if (currentButton != Q_NULLPTR && offEvent->currentData().toInt() < commands->size()) {
QMutexLocker locker(mutex);
currentButton->offCommand = &commands->at(offEvent->currentData().toInt());
currentButton->offText->setPlainText(currentButton->offCommand->text);
}
@ -141,17 +149,21 @@ void controllerSetup::offEventIndexChanged(int index) {
void controllerSetup::knobEventIndexChanged(int index) {
Q_UNUSED(index);
if (currentKnob != Q_NULLPTR && knobEvent->currentData().toInt() < commands->size()) {
QMutexLocker locker(mutex);
currentKnob->command = &commands->at(knobEvent->currentData().toInt());
currentKnob->text->setPlainText(currentKnob->command->text);
}
}
void controllerSetup::newDevice(unsigned char devType, QVector<BUTTON>* but, QVector<KNOB>* kb, QVector<COMMAND>* cmd)
void controllerSetup::newDevice(unsigned char devType, QVector<BUTTON>* but, QVector<KNOB>* kb, QVector<COMMAND>* cmd, QMutex* mut)
{
buttons = but;
knobs = kb;
commands = cmd;
mutex = mut;
QMutexLocker locker(mutex);
// Remove any existing button text:
for (QGraphicsItem* item : scene->items())

Wyświetl plik

@ -38,7 +38,7 @@ signals:
void sendSensitivity(int val);
public slots:
void newDevice(unsigned char devType, QVector<BUTTON>* but, QVector<KNOB>* kb, QVector<COMMAND>* cmd);
void newDevice(unsigned char devType, QVector<BUTTON>* but, QVector<KNOB>* kb, QVector<COMMAND>* cmd, QMutex* mut);
void mousePressed(QPoint p);
void onEventIndexChanged(int index);
void offEventIndexChanged(int index);
@ -67,7 +67,7 @@ private:
QGraphicsProxyWidget* offEventProxy = Q_NULLPTR;
QGraphicsProxyWidget* knobEventProxy = Q_NULLPTR;
QString deviceName;
QMutex* mutex;
};

Wyświetl plik

@ -38,9 +38,10 @@ usbController::~usbController()
#endif
}
void usbController::init(int sens)
void usbController::init(int sens, QMutex* mut)
{
sensitivity = sens;
this->mutex = mut;
this->sensitivity = sens;
emit sendSensitivity(sensitivity);
if (HID_API_VERSION == HID_API_MAKE_VERSION(hid_version()->major, hid_version()->minor, hid_version()->patch)) {
@ -207,7 +208,7 @@ void usbController::run()
qInfo(logUsbControl()) << "Button Guide" << pressed;
});
emit newDevice(usbDevice, buttonList, knobList, commands); // Let the UI know we have a new controller
emit newDevice(usbDevice, buttonList, knobList, commands, &usbMutex); // Let the UI know we have a new controller
return;
}
}
@ -267,7 +268,7 @@ void usbController::run()
// Let the UI know we have a new controller
emit newDevice(usbDevice, buttonList, knobList, commands);
emit newDevice(usbDevice, buttonList, knobList, commands, mutex);
// Run the periodic timer to get data
QTimer::singleShot(25, this, SLOT(runTimer()));
}
@ -297,13 +298,16 @@ void usbController::runTimer()
res = 0;
}
QMutexLocker locker(mutex);
while (res > 0) {
QByteArray data(HIDDATALENGTH, 0x0);
res = hid_read(this->handle, (unsigned char*)data.data(), HIDDATALENGTH);
if (res < 0)
{
qInfo(logUsbControl()) << "USB Device disconnected" << this->product;
emit newDevice(0, buttonList, knobList, commands);
emit newDevice(0, buttonList, knobList, commands, mutex);
this->product = "";
this->manufacturer = "";
this->serial = "<none>";
@ -602,6 +606,7 @@ void usbController::runTimer()
}
}
// Run every 25ms
QTimer::singleShot(25, this, SLOT(runTimer()));
}

Wyświetl plik

@ -11,6 +11,7 @@
#include <QColor>
#include <QVector>
#include <QList>
#include <QMutex>
#if defined(USB_CONTROLLER) && QT_VERSION < QT_VERSION_CHECK(6,0,0)
#include <QGamepad>
@ -120,7 +121,7 @@ public:
~usbController();
public slots:
void init(int sens);
void init(int sens, QMutex *mut);
void run();
void runTimer();
void ledControl(bool on, unsigned char num);
@ -137,7 +138,7 @@ signals:
void doShuttle(bool plus, quint8 level);
void setBand(int band);
void button(const COMMAND* cmd);
void newDevice(unsigned char devType, QVector<BUTTON>* but, QVector<KNOB>* kb, QVector<COMMAND>* cmd);
void newDevice(unsigned char devType, QVector<BUTTON>* but, QVector<KNOB>* kb, QVector<COMMAND>* cmd, QMutex* mut);
void sendSensitivity(int val);
private:
@ -162,6 +163,7 @@ private:
QString path = "";
int sensitivity = 1;
QList<int> knobValues;
QMutex* mutex=Q_NULLPTR;
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
QGamepad* gamepad=Q_NULLPTR;
#endif

Wyświetl plik

@ -1671,7 +1671,7 @@ void wfmain::setupUsbControllerDevice()
connect(usbControllerDev, SIGNAL(doShuttle(bool, unsigned char)), this, SLOT(doShuttle(bool, unsigned char)));
connect(usbControllerDev, SIGNAL(button(const COMMAND*)), this, SLOT(buttonControl(const COMMAND*)));
connect(usbControllerDev, SIGNAL(setBand(int)), this, SLOT(setBand(int)));
connect(usbControllerDev, SIGNAL(newDevice(unsigned char, QVector<BUTTON>*, QVector<KNOB>*, QVector<COMMAND>*)), shut, SLOT(newDevice(unsigned char, QVector<BUTTON>*, QVector<KNOB>*, QVector<COMMAND>*)));
connect(usbControllerDev, SIGNAL(newDevice(unsigned char, QVector<BUTTON>*, QVector<KNOB>*, QVector<COMMAND>*, QMutex*)), shut, SLOT(newDevice(unsigned char, QVector<BUTTON>*, QVector<KNOB>*, QVector<COMMAND>*,QMutex*)));
usbControllerThread->start(QThread::LowestPriority);
connect(this, SIGNAL(sendUsbControllerCommands(QVector<COMMAND>*)), usbControllerDev, SLOT(receiveCommands(QVector<COMMAND>*)));
@ -1680,7 +1680,7 @@ void wfmain::setupUsbControllerDevice()
connect(shut, SIGNAL(sendSensitivity(int)), usbControllerDev, SLOT(receiveSensitivity(int)));
connect(shut, SIGNAL(sendSensitivity(int)), this, SLOT(receiveUsbSensitivity(int)));
connect(usbControllerDev, SIGNAL(sendSensitivity(int)), shut, SLOT(receiveSensitivity(int)));
connect(this, SIGNAL(initUsbController(int)), usbControllerDev, SLOT(init(int)));
connect(this, SIGNAL(initUsbController(int,QMutex*)), usbControllerDev, SLOT(init(int,QMutex*)));
#endif
}
@ -2440,11 +2440,14 @@ void wfmain::loadSettings()
ui->usbButtonsResetBtn->setEnabled(prefs.enableUSBControllers);
ui->usbCommandsResetBtn->setEnabled(prefs.enableUSBControllers);
ui->usbResetLbl->setVisible(prefs.enableUSBControllers);
/*Ensure that no operations on the usb commands/buttons/knobs take place*/
QMutexLocker locker(&usbMutex);
if (prefs.enableUSBControllers) {
// Setup USB Controller
setupUsbControllerDevice();
emit initUsbController(prefs.usbSensitivity);
emit initUsbController(prefs.usbSensitivity, &usbMutex);
emit sendUsbControllerCommands(&usbCommands);
emit sendUsbControllerButtons(&usbButtons);
emit sendUsbControllerKnobs(&usbKnobs);
@ -2515,7 +2518,6 @@ void wfmain::loadSettings()
settings->endArray();
}
int numKnobs = settings->beginReadArray("Knobs");
// This is the last time the knobs were changed, (v1.58)
if (numKnobs == 0 || priorVersionFloat < 1.58) {
@ -2954,6 +2956,7 @@ void wfmain::saveSettings()
settings->setValue("EnableUSBControllers", prefs.enableUSBControllers);
settings->setValue("USBSensitivity", prefs.usbSensitivity);
QMutexLocker locker(&usbMutex);
settings->beginWriteArray("Buttons");
for (int nb = 0; nb < usbButtons.count(); nb++)
{
@ -9117,7 +9120,7 @@ void wfmain::on_enableUsbChk_clicked(bool checked)
if (checked) {
// Setup USB Controller
setupUsbControllerDevice();
emit initUsbController(prefs.usbSensitivity);
emit initUsbController(prefs.usbSensitivity, &usbMutex);
emit sendUsbControllerCommands(&usbCommands);
emit sendUsbControllerButtons(&usbButtons);
emit sendUsbControllerKnobs(&usbKnobs);
@ -9263,6 +9266,7 @@ void wfmain::on_cwButton_clicked()
void wfmain::resetUsbButtons()
{
#ifdef USB_CONTROLLER
qInfo(logUsbControl) << "Resetting USB Buttons to defaults";
usbButtons.clear();
// ShuttleXpress
@ -9342,6 +9346,7 @@ void wfmain::resetUsbButtons()
void wfmain::resetUsbKnobs()
{
#ifdef USB_CONTROLLER
qInfo(logUsbControl) << "Resetting USB Knobs to defaults";
usbKnobs.clear();
// eCoder
usbKnobs.append(KNOB(eCoderPlus, 1, QRect(120, 153, 72, 27), Qt::green, &usbCommands[0]));
@ -9355,6 +9360,7 @@ void wfmain::resetUsbKnobs()
void wfmain::resetUsbCommands()
{
#ifdef USB_CONTROLLER
qInfo(logUsbControl) << "Resetting USB Commands to defaults";
usbCommands.clear();
int num = 0;
usbCommands.append(COMMAND(num++, "None", commandButton, cmdNone, 0x0));

Wyświetl plik

@ -223,7 +223,7 @@ signals:
void openShuttle();
void requestRigState();
void stateUpdated();
void initUsbController(int sens);
void initUsbController(int sens, QMutex* mutex);
void sendUsbControllerCommands(QVector<COMMAND>* cmds);
void sendUsbControllerButtons(QVector<BUTTON>* buts);
void sendUsbControllerKnobs(QVector<KNOB>* kbs);
@ -1150,6 +1150,7 @@ private:
QVector<COMMAND> usbCommands;
QVector<BUTTON> usbButtons;
QVector<KNOB> usbKnobs;
QMutex usbMutex;
#endif
dxClusterClient* cluster = Q_NULLPTR;