Skip to content

Commit

Permalink
Fixed threading model for DSPDeviceSourceEngine. Part of #2159
Browse files Browse the repository at this point in the history
  • Loading branch information
f4exb committed Aug 27, 2024
1 parent 9fa1974 commit d206649
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 45 deletions.
28 changes: 2 additions & 26 deletions sdrbase/dsp/dspdevicesourceengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)
Expand All @@ -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)");
Expand Down
9 changes: 2 additions & 7 deletions sdrbase/dsp/dspdevicesourceengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#ifndef INCLUDE_DSPDEVICEENGINE_H
#define INCLUDE_DSPDEVICEENGINE_H

#include <QThread>
#include <QObject>
#include <QTimer>
#include <QMutex>
#include <QWaitCondition>
Expand All @@ -34,7 +34,7 @@
class DeviceSampleSource;
class BasebandSampleSink;

class SDRBASE_API DSPDeviceSourceEngine : public QThread {
class SDRBASE_API DSPDeviceSourceEngine : public QObject {
Q_OBJECT

public:
Expand All @@ -53,9 +53,6 @@ class SDRBASE_API DSPDeviceSourceEngine : public QThread {

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
Expand Down Expand Up @@ -125,8 +122,6 @@ class SDRBASE_API DSPDeviceSourceEngine : public QThread {
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);
Expand Down
35 changes: 28 additions & 7 deletions sdrbase/dsp/dspengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ DSPEngine::DSPEngine() :

DSPEngine::~DSPEngine()
{
QList<DSPDeviceSourceEngine*>::iterator it = m_deviceSourceEngines.begin();
auto it = m_deviceSourceEngines.begin();

while (it != m_deviceSourceEngines.end())
{
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions sdrgui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions sdrsrv/mainserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ void MainServer::addSinkDevice()
void MainServer::addSourceDevice()
{
DSPDeviceSourceEngine *dspDeviceSourceEngine = m_dspEngine->addDeviceSourceEngine();
dspDeviceSourceEngine->start();

uint dspDeviceSourceEngineUID = dspDeviceSourceEngine->getUID();
char uidCStr[16];
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d206649

Please sign in to comment.