1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-11 02:46:12 -05:00

Fixed threading model for DSPDeviceSourceEngine. Part of #2159

This commit is contained in:
f4exb 2024-08-14 22:08:49 +02:00 committed by Edouard Griffiths
parent 9fa1974ba3
commit d2066495a9
5 changed files with 32 additions and 45 deletions

View File

@ -29,7 +29,7 @@
#include "samplesinkfifo.h" #include "samplesinkfifo.h"
DSPDeviceSourceEngine::DSPDeviceSourceEngine(uint uid, QObject* parent) : DSPDeviceSourceEngine::DSPDeviceSourceEngine(uint uid, QObject* parent) :
QThread(parent), QObject(parent),
m_uid(uid), m_uid(uid),
m_state(StNotStarted), m_state(StNotStarted),
m_deviceSampleSource(nullptr), m_deviceSampleSource(nullptr),
@ -46,15 +46,12 @@ DSPDeviceSourceEngine::DSPDeviceSourceEngine(uint uid, QObject* parent) :
m_qRange(1 << 16), m_qRange(1 << 16),
m_imbalance(65536) m_imbalance(65536)
{ {
setState(StIdle);
connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()), Qt::QueuedConnection); connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()), Qt::QueuedConnection);
moveToThread(this);
} }
DSPDeviceSourceEngine::~DSPDeviceSourceEngine() DSPDeviceSourceEngine::~DSPDeviceSourceEngine()
{ {
stop();
wait();
} }
void DSPDeviceSourceEngine::setState(State state) 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() bool DSPDeviceSourceEngine::initAcquisition()
{ {
qDebug("DSPDeviceSourceEngine::initAcquisition (dummy)"); qDebug("DSPDeviceSourceEngine::initAcquisition (dummy)");

View File

@ -22,7 +22,7 @@
#ifndef INCLUDE_DSPDEVICEENGINE_H #ifndef INCLUDE_DSPDEVICEENGINE_H
#define INCLUDE_DSPDEVICEENGINE_H #define INCLUDE_DSPDEVICEENGINE_H
#include <QThread> #include <QObject>
#include <QTimer> #include <QTimer>
#include <QMutex> #include <QMutex>
#include <QWaitCondition> #include <QWaitCondition>
@ -34,7 +34,7 @@
class DeviceSampleSource; class DeviceSampleSource;
class BasebandSampleSink; class BasebandSampleSink;
class SDRBASE_API DSPDeviceSourceEngine : public QThread { class SDRBASE_API DSPDeviceSourceEngine : public QObject {
Q_OBJECT Q_OBJECT
public: public:
@ -53,9 +53,6 @@ public:
MessageQueue* getInputMessageQueue() { return &m_inputMessageQueue; } MessageQueue* getInputMessageQueue() { return &m_inputMessageQueue; }
void start(); //!< This thread start
void stop(); //!< This thread stop
bool initAcquisition(); //!< Initialize acquisition sequence bool initAcquisition(); //!< Initialize acquisition sequence
bool startAcquisition(); //!< Start acquisition sequence bool startAcquisition(); //!< Start acquisition sequence
void stopAcquistion(); //!< Stop acquisition sequence void stopAcquistion(); //!< Stop acquisition sequence
@ -125,8 +122,6 @@ private:
qint32 m_qRange; qint32 m_qRange;
qint32 m_imbalance; qint32 m_imbalance;
void run();
void iqCorrections(SampleVector::iterator begin, SampleVector::iterator end, bool imbalanceCorrection); void iqCorrections(SampleVector::iterator begin, SampleVector::iterator end, bool imbalanceCorrection);
void dcOffset(SampleVector::iterator begin, SampleVector::iterator end); void dcOffset(SampleVector::iterator begin, SampleVector::iterator end);
void imbalance(SampleVector::iterator begin, SampleVector::iterator end); void imbalance(SampleVector::iterator begin, SampleVector::iterator end);

View File

@ -42,7 +42,7 @@ DSPEngine::DSPEngine() :
DSPEngine::~DSPEngine() DSPEngine::~DSPEngine()
{ {
QList<DSPDeviceSourceEngine*>::iterator it = m_deviceSourceEngines.begin(); auto it = m_deviceSourceEngines.begin();
while (it != m_deviceSourceEngines.end()) while (it != m_deviceSourceEngines.end())
{ {
@ -64,25 +64,44 @@ DSPEngine *DSPEngine::instance()
DSPDeviceSourceEngine *DSPEngine::addDeviceSourceEngine() DSPDeviceSourceEngine *DSPEngine::addDeviceSourceEngine()
{ {
auto *deviceSourceEngine = new DSPDeviceSourceEngine(m_deviceSourceEnginesUIDSequence); auto *deviceSourceEngine = new DSPDeviceSourceEngine(m_deviceSourceEnginesUIDSequence);
// auto *deviceThread = new QThread(); TBD auto *deviceThread = new QThread();
m_deviceSourceEnginesUIDSequence++; m_deviceSourceEnginesUIDSequence++;
m_deviceSourceEngines.push_back(deviceSourceEngine); 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; return deviceSourceEngine;
} }
void DSPEngine::removeLastDeviceSourceEngine() void DSPEngine::removeLastDeviceSourceEngine()
{ {
if (m_deviceSourceEngines.size() > 0) if (!m_deviceSourceEngines.empty())
{ {
DSPDeviceSourceEngine *lastDeviceEngine = m_deviceSourceEngines.back(); const DSPDeviceSourceEngine *lastDeviceEngine = m_deviceSourceEngines.back();
delete lastDeviceEngine;
m_deviceSourceEngines.pop_back(); m_deviceSourceEngines.pop_back();
for (int i = 0; i < m_deviceEngineReferences.size(); i++) for (int i = 0; i < m_deviceEngineReferences.size(); i++)
{ {
if (m_deviceEngineReferences[i].m_deviceSourceEngine == lastDeviceEngine) if (m_deviceEngineReferences[i].m_deviceSourceEngine == lastDeviceEngine)
{ {
QThread* deviceThread = m_deviceEngineReferences[i].m_thread;
deviceThread->exit();
deviceThread->wait();
m_deviceEngineReferences.removeAt(i); m_deviceEngineReferences.removeAt(i);
break; break;
} }
@ -153,7 +172,9 @@ void DSPEngine::removeDeviceEngineAt(int deviceIndex)
if (m_deviceEngineReferences[deviceIndex].m_deviceEngineType == 0) // source if (m_deviceEngineReferences[deviceIndex].m_deviceEngineType == 0) // source
{ {
DSPDeviceSourceEngine *deviceEngine = m_deviceEngineReferences[deviceIndex].m_deviceSourceEngine; DSPDeviceSourceEngine *deviceEngine = m_deviceEngineReferences[deviceIndex].m_deviceSourceEngine;
delete deviceEngine; QThread *deviceThread = m_deviceEngineReferences[deviceIndex].m_thread;
deviceThread->exit();
deviceThread->wait();
m_deviceSourceEngines.removeAll(deviceEngine); m_deviceSourceEngines.removeAll(deviceEngine);
} }
else if (m_deviceEngineReferences[deviceIndex].m_deviceEngineType == 1) // sink else if (m_deviceEngineReferences[deviceIndex].m_deviceEngineType == 1) // sink

View File

@ -343,7 +343,6 @@ MainWindow::~MainWindow()
void MainWindow::sampleSourceAdd(Workspace *deviceWorkspace, Workspace *spectrumWorkspace, int deviceIndex) void MainWindow::sampleSourceAdd(Workspace *deviceWorkspace, Workspace *spectrumWorkspace, int deviceIndex)
{ {
DSPDeviceSourceEngine *dspDeviceSourceEngine = m_dspEngine->addDeviceSourceEngine(); DSPDeviceSourceEngine *dspDeviceSourceEngine = m_dspEngine->addDeviceSourceEngine();
dspDeviceSourceEngine->start();
uint dspDeviceSourceEngineUID = dspDeviceSourceEngine->getUID(); uint dspDeviceSourceEngineUID = dspDeviceSourceEngine->getUID();
char uidCStr[16]; char uidCStr[16];
@ -1010,7 +1009,6 @@ void MainWindow::removeDeviceSet(int deviceSetIndex)
DeviceAPI *sourceAPI = deviceUISet->m_deviceAPI; DeviceAPI *sourceAPI = deviceUISet->m_deviceAPI;
delete deviceUISet; delete deviceUISet;
deviceEngine->stop();
m_dspEngine->removeDeviceEngineAt(deviceSetIndex); m_dspEngine->removeDeviceEngineAt(deviceSetIndex);
DeviceEnumerator::instance()->removeRxSelection(deviceSetIndex); DeviceEnumerator::instance()->removeRxSelection(deviceSetIndex);
@ -1116,7 +1114,6 @@ void MainWindow::removeLastDeviceSet()
DeviceAPI *sourceAPI = m_deviceUIs.back()->m_deviceAPI; DeviceAPI *sourceAPI = m_deviceUIs.back()->m_deviceAPI;
delete m_deviceUIs.back(); delete m_deviceUIs.back();
lastDeviceEngine->stop();
m_dspEngine->removeLastDeviceSourceEngine(); m_dspEngine->removeLastDeviceSourceEngine();
delete sourceAPI; delete sourceAPI;

View File

@ -315,7 +315,6 @@ void MainServer::addSinkDevice()
void MainServer::addSourceDevice() void MainServer::addSourceDevice()
{ {
DSPDeviceSourceEngine *dspDeviceSourceEngine = m_dspEngine->addDeviceSourceEngine(); DSPDeviceSourceEngine *dspDeviceSourceEngine = m_dspEngine->addDeviceSourceEngine();
dspDeviceSourceEngine->start();
uint dspDeviceSourceEngineUID = dspDeviceSourceEngine->getUID(); uint dspDeviceSourceEngineUID = dspDeviceSourceEngine->getUID();
char uidCStr[16]; char uidCStr[16];
@ -423,7 +422,6 @@ void MainServer::removeLastDevice()
DeviceAPI *sourceAPI = m_mainCore->m_deviceSets.back()->m_deviceAPI; DeviceAPI *sourceAPI = m_mainCore->m_deviceSets.back()->m_deviceAPI;
delete m_mainCore->m_deviceSets.back(); delete m_mainCore->m_deviceSets.back();
lastDeviceEngine->stop();
m_dspEngine->removeLastDeviceSourceEngine(); m_dspEngine->removeLastDeviceSourceEngine();
delete sourceAPI; delete sourceAPI;