From d2066495a925841b374311de87276640287c5bfe Mon Sep 17 00:00:00 2001 From: f4exb Date: Wed, 14 Aug 2024 22:08:49 +0200 Subject: [PATCH] Fixed threading model for DSPDeviceSourceEngine. Part of #2159 --- sdrbase/dsp/dspdevicesourceengine.cpp | 28 ++------------------- sdrbase/dsp/dspdevicesourceengine.h | 9 ++----- sdrbase/dsp/dspengine.cpp | 35 +++++++++++++++++++++------ sdrgui/mainwindow.cpp | 3 --- sdrsrv/mainserver.cpp | 2 -- 5 files changed, 32 insertions(+), 45 deletions(-) diff --git a/sdrbase/dsp/dspdevicesourceengine.cpp b/sdrbase/dsp/dspdevicesourceengine.cpp index 5403baf77..27869762a 100644 --- a/sdrbase/dsp/dspdevicesourceengine.cpp +++ b/sdrbase/dsp/dspdevicesourceengine.cpp @@ -29,7 +29,7 @@ #include "samplesinkfifo.h" DSPDeviceSourceEngine::DSPDeviceSourceEngine(uint uid, QObject* parent) : - QThread(parent), + QObject(parent), m_uid(uid), m_state(StNotStarted), m_deviceSampleSource(nullptr), @@ -46,15 +46,12 @@ DSPDeviceSourceEngine::DSPDeviceSourceEngine(uint uid, QObject* parent) : m_qRange(1 << 16), m_imbalance(65536) { + setState(StIdle); connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()), Qt::QueuedConnection); - - moveToThread(this); } DSPDeviceSourceEngine::~DSPDeviceSourceEngine() { - stop(); - wait(); } void DSPDeviceSourceEngine::setState(State state) @@ -66,27 +63,6 @@ void DSPDeviceSourceEngine::setState(State state) } } -void DSPDeviceSourceEngine::run() -{ - qDebug("DSPDeviceSourceEngine::run"); - setState(StIdle); - exec(); -} - -void DSPDeviceSourceEngine::start() -{ - qDebug("DSPDeviceSourceEngine::start"); - QThread::start(); -} - -void DSPDeviceSourceEngine::stop() -{ - qDebug("DSPDeviceSourceEngine::stop"); - gotoIdle(); - setState(StNotStarted); - QThread::exit(); -} - bool DSPDeviceSourceEngine::initAcquisition() { qDebug("DSPDeviceSourceEngine::initAcquisition (dummy)"); diff --git a/sdrbase/dsp/dspdevicesourceengine.h b/sdrbase/dsp/dspdevicesourceengine.h index 0b10490d7..ba5b839ac 100644 --- a/sdrbase/dsp/dspdevicesourceengine.h +++ b/sdrbase/dsp/dspdevicesourceengine.h @@ -22,7 +22,7 @@ #ifndef INCLUDE_DSPDEVICEENGINE_H #define INCLUDE_DSPDEVICEENGINE_H -#include +#include #include #include #include @@ -34,7 +34,7 @@ class DeviceSampleSource; class BasebandSampleSink; -class SDRBASE_API DSPDeviceSourceEngine : public QThread { +class SDRBASE_API DSPDeviceSourceEngine : public QObject { Q_OBJECT public: @@ -53,9 +53,6 @@ public: MessageQueue* getInputMessageQueue() { return &m_inputMessageQueue; } - void start(); //!< This thread start - void stop(); //!< This thread stop - bool initAcquisition(); //!< Initialize acquisition sequence bool startAcquisition(); //!< Start acquisition sequence void stopAcquistion(); //!< Stop acquisition sequence @@ -125,8 +122,6 @@ private: qint32 m_qRange; qint32 m_imbalance; - void run(); - void iqCorrections(SampleVector::iterator begin, SampleVector::iterator end, bool imbalanceCorrection); void dcOffset(SampleVector::iterator begin, SampleVector::iterator end); void imbalance(SampleVector::iterator begin, SampleVector::iterator end); diff --git a/sdrbase/dsp/dspengine.cpp b/sdrbase/dsp/dspengine.cpp index 96dc47fcb..b6d688ba7 100644 --- a/sdrbase/dsp/dspengine.cpp +++ b/sdrbase/dsp/dspengine.cpp @@ -42,7 +42,7 @@ DSPEngine::DSPEngine() : DSPEngine::~DSPEngine() { - QList::iterator it = m_deviceSourceEngines.begin(); + auto it = m_deviceSourceEngines.begin(); while (it != m_deviceSourceEngines.end()) { @@ -64,25 +64,44 @@ DSPEngine *DSPEngine::instance() DSPDeviceSourceEngine *DSPEngine::addDeviceSourceEngine() { auto *deviceSourceEngine = new DSPDeviceSourceEngine(m_deviceSourceEnginesUIDSequence); - // auto *deviceThread = new QThread(); TBD + auto *deviceThread = new QThread(); m_deviceSourceEnginesUIDSequence++; m_deviceSourceEngines.push_back(deviceSourceEngine); - m_deviceEngineReferences.push_back(DeviceEngineReference{0, m_deviceSourceEngines.back(), nullptr, nullptr, nullptr}); + m_deviceEngineReferences.push_back(DeviceEngineReference{0, m_deviceSourceEngines.back(), nullptr, nullptr, deviceThread}); + deviceSourceEngine->moveToThread(deviceThread); + + QObject::connect( + deviceThread, + &QThread::finished, + deviceSourceEngine, + &QObject::deleteLater + ); + QObject::connect( + deviceThread, + &QThread::finished, + deviceThread, + &QThread::deleteLater + ); + + deviceThread->start(); + return deviceSourceEngine; } void DSPEngine::removeLastDeviceSourceEngine() { - if (m_deviceSourceEngines.size() > 0) + if (!m_deviceSourceEngines.empty()) { - DSPDeviceSourceEngine *lastDeviceEngine = m_deviceSourceEngines.back(); - delete lastDeviceEngine; + const DSPDeviceSourceEngine *lastDeviceEngine = m_deviceSourceEngines.back(); m_deviceSourceEngines.pop_back(); for (int i = 0; i < m_deviceEngineReferences.size(); i++) { if (m_deviceEngineReferences[i].m_deviceSourceEngine == lastDeviceEngine) { + QThread* deviceThread = m_deviceEngineReferences[i].m_thread; + deviceThread->exit(); + deviceThread->wait(); m_deviceEngineReferences.removeAt(i); break; } @@ -153,7 +172,9 @@ void DSPEngine::removeDeviceEngineAt(int deviceIndex) if (m_deviceEngineReferences[deviceIndex].m_deviceEngineType == 0) // source { DSPDeviceSourceEngine *deviceEngine = m_deviceEngineReferences[deviceIndex].m_deviceSourceEngine; - delete deviceEngine; + QThread *deviceThread = m_deviceEngineReferences[deviceIndex].m_thread; + deviceThread->exit(); + deviceThread->wait(); m_deviceSourceEngines.removeAll(deviceEngine); } else if (m_deviceEngineReferences[deviceIndex].m_deviceEngineType == 1) // sink diff --git a/sdrgui/mainwindow.cpp b/sdrgui/mainwindow.cpp index 3b905a7dd..3c3cef816 100644 --- a/sdrgui/mainwindow.cpp +++ b/sdrgui/mainwindow.cpp @@ -343,7 +343,6 @@ MainWindow::~MainWindow() void MainWindow::sampleSourceAdd(Workspace *deviceWorkspace, Workspace *spectrumWorkspace, int deviceIndex) { DSPDeviceSourceEngine *dspDeviceSourceEngine = m_dspEngine->addDeviceSourceEngine(); - dspDeviceSourceEngine->start(); uint dspDeviceSourceEngineUID = dspDeviceSourceEngine->getUID(); char uidCStr[16]; @@ -1010,7 +1009,6 @@ void MainWindow::removeDeviceSet(int deviceSetIndex) DeviceAPI *sourceAPI = deviceUISet->m_deviceAPI; delete deviceUISet; - deviceEngine->stop(); m_dspEngine->removeDeviceEngineAt(deviceSetIndex); DeviceEnumerator::instance()->removeRxSelection(deviceSetIndex); @@ -1116,7 +1114,6 @@ void MainWindow::removeLastDeviceSet() DeviceAPI *sourceAPI = m_deviceUIs.back()->m_deviceAPI; delete m_deviceUIs.back(); - lastDeviceEngine->stop(); m_dspEngine->removeLastDeviceSourceEngine(); delete sourceAPI; diff --git a/sdrsrv/mainserver.cpp b/sdrsrv/mainserver.cpp index 27ae511b0..7f66b948e 100644 --- a/sdrsrv/mainserver.cpp +++ b/sdrsrv/mainserver.cpp @@ -315,7 +315,6 @@ void MainServer::addSinkDevice() void MainServer::addSourceDevice() { DSPDeviceSourceEngine *dspDeviceSourceEngine = m_dspEngine->addDeviceSourceEngine(); - dspDeviceSourceEngine->start(); uint dspDeviceSourceEngineUID = dspDeviceSourceEngine->getUID(); char uidCStr[16]; @@ -423,7 +422,6 @@ void MainServer::removeLastDevice() DeviceAPI *sourceAPI = m_mainCore->m_deviceSets.back()->m_deviceAPI; delete m_mainCore->m_deviceSets.back(); - lastDeviceEngine->stop(); m_dspEngine->removeLastDeviceSourceEngine(); delete sourceAPI;