1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-26 17:58:43 -05:00

TestSink: refactored Thread to Worker object moved to thread. Equivalent to FileInput changes

This commit is contained in:
f4exb 2020-07-12 03:55:25 +02:00
parent 4f462c1b88
commit cb8ac3a156
5 changed files with 63 additions and 69 deletions

View File

@ -4,14 +4,14 @@ set(testsink_SOURCES
testsinkoutput.cpp testsinkoutput.cpp
testsinkplugin.cpp testsinkplugin.cpp
testsinksettings.cpp testsinksettings.cpp
testsinkthread.cpp testsinkworker.cpp
) )
set(testsink_HEADERS set(testsink_HEADERS
testsinkoutput.h testsinkoutput.h
testsinkplugin.h testsinkplugin.h
testsinksettings.h testsinksettings.h
testsinkthread.h testsinkworker.h
) )
include_directories( include_directories(

View File

@ -30,7 +30,7 @@
#include "device/deviceapi.h" #include "device/deviceapi.h"
#include "testsinkoutput.h" #include "testsinkoutput.h"
#include "testsinkthread.h" #include "testsinkworker.h"
MESSAGE_CLASS_DEFINITION(TestSinkOutput::MsgConfigureTestSink, Message) MESSAGE_CLASS_DEFINITION(TestSinkOutput::MsgConfigureTestSink, Message)
MESSAGE_CLASS_DEFINITION(TestSinkOutput::MsgStartStop, Message) MESSAGE_CLASS_DEFINITION(TestSinkOutput::MsgStartStop, Message)
@ -39,7 +39,7 @@ TestSinkOutput::TestSinkOutput(DeviceAPI *deviceAPI) :
m_deviceAPI(deviceAPI), m_deviceAPI(deviceAPI),
m_settings(), m_settings(),
m_spectrumVis(SDR_TX_SCALEF), m_spectrumVis(SDR_TX_SCALEF),
m_testSinkThread(nullptr), m_testSinkWorker(nullptr),
m_deviceDescription("TestSink"), m_deviceDescription("TestSink"),
m_masterTimer(deviceAPI->getMasterTimer()) m_masterTimer(deviceAPI->getMasterTimer())
{ {
@ -66,12 +66,13 @@ bool TestSinkOutput::start()
QMutexLocker mutexLocker(&m_mutex); QMutexLocker mutexLocker(&m_mutex);
qDebug() << "TestSinkOutput::start"; qDebug() << "TestSinkOutput::start";
m_testSinkThread = new TestSinkThread(&m_sampleSourceFifo); m_testSinkWorker = new TestSinkWorker(&m_sampleSourceFifo);
m_testSinkThread->setSpectrumSink(&m_spectrumVis); m_testSinkWorker->moveToThread(&m_testSinkWorkerThread);
m_testSinkThread->setSamplerate(m_settings.m_sampleRate); m_testSinkWorker->setSpectrumSink(&m_spectrumVis);
m_testSinkThread->setLog2Interpolation(m_settings.m_log2Interp); m_testSinkWorker->setSamplerate(m_settings.m_sampleRate);
m_testSinkThread->connectTimer(m_masterTimer); m_testSinkWorker->setLog2Interpolation(m_settings.m_log2Interp);
m_testSinkThread->startWork(); m_testSinkWorker->connectTimer(m_masterTimer);
startWorker();
mutexLocker.unlock(); mutexLocker.unlock();
qDebug("TestSinkOutput::start: started"); qDebug("TestSinkOutput::start: started");
@ -83,14 +84,27 @@ void TestSinkOutput::stop()
qDebug() << "TestSinkOutput::stop"; qDebug() << "TestSinkOutput::stop";
QMutexLocker mutexLocker(&m_mutex); QMutexLocker mutexLocker(&m_mutex);
if(m_testSinkThread != 0) if(m_testSinkWorker != 0)
{ {
m_testSinkThread->stopWork(); stopWorker();
delete m_testSinkThread; delete m_testSinkWorker;
m_testSinkThread = nullptr; m_testSinkWorker = nullptr;
} }
} }
void TestSinkOutput::startWorker()
{
m_testSinkWorker->startWork();
m_testSinkWorkerThread.start();
}
void TestSinkOutput::stopWorker()
{
m_testSinkWorker->stopWork();
m_testSinkWorkerThread.quit();
m_testSinkWorkerThread.wait();
}
QByteArray TestSinkOutput::serialize() const QByteArray TestSinkOutput::serialize() const
{ {
return m_settings.serialize(); return m_settings.serialize();
@ -196,8 +210,8 @@ void TestSinkOutput::applySettings(const TestSinkSettings& settings, bool force)
{ {
m_settings.m_sampleRate = settings.m_sampleRate; m_settings.m_sampleRate = settings.m_sampleRate;
if (m_testSinkThread) { if (m_testSinkWorker) {
m_testSinkThread->setSamplerate(m_settings.m_sampleRate); m_testSinkWorker->setSamplerate(m_settings.m_sampleRate);
} }
forwardChange = true; forwardChange = true;
@ -207,8 +221,8 @@ void TestSinkOutput::applySettings(const TestSinkSettings& settings, bool force)
{ {
m_settings.m_log2Interp = settings.m_log2Interp; m_settings.m_log2Interp = settings.m_log2Interp;
if (m_testSinkThread) { if (m_testSinkWorker) {
m_testSinkThread->setLog2Interpolation(m_settings.m_log2Interp); m_testSinkWorker->setLog2Interpolation(m_settings.m_log2Interp);
} }
forwardChange = true; forwardChange = true;

View File

@ -20,6 +20,7 @@
#include <QString> #include <QString>
#include <QTimer> #include <QTimer>
#include <QThread>
#include <ctime> #include <ctime>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
@ -28,7 +29,7 @@
#include "dsp/spectrumvis.h" #include "dsp/spectrumvis.h"
#include "testsinksettings.h" #include "testsinksettings.h"
class TestSinkThread; class TestSinkWorker;
class DeviceAPI; class DeviceAPI;
class BasebandSampleSink; class BasebandSampleSink;
@ -113,10 +114,13 @@ private:
TestSinkSettings m_settings; TestSinkSettings m_settings;
SpectrumVis m_spectrumVis; SpectrumVis m_spectrumVis;
std::ofstream m_ofstream; std::ofstream m_ofstream;
TestSinkThread* m_testSinkThread; TestSinkWorker* m_testSinkWorker;
QThread m_testSinkWorkerThread;
QString m_deviceDescription; QString m_deviceDescription;
const QTimer& m_masterTimer; const QTimer& m_masterTimer;
void startWorker();
void stopWorker();
void applySettings(const TestSinkSettings& settings, bool force = false); void applySettings(const TestSinkSettings& settings, bool force = false);
}; };

View File

@ -23,10 +23,10 @@
#include "dsp/samplesourcefifo.h" #include "dsp/samplesourcefifo.h"
#include "dsp/basebandsamplesink.h" #include "dsp/basebandsamplesink.h"
#include "testsinkthread.h" #include "testsinkworker.h"
TestSinkThread::TestSinkThread(SampleSourceFifo* sampleFifo, QObject* parent) : TestSinkWorker::TestSinkWorker(SampleSourceFifo* sampleFifo, QObject* parent) :
QThread(parent), QObject(parent),
m_running(false), m_running(false),
m_bufsize(0), m_bufsize(0),
m_samplesChunkSize(0), m_samplesChunkSize(0),
@ -41,7 +41,7 @@ TestSinkThread::TestSinkThread(SampleSourceFifo* sampleFifo, QObject* parent) :
{ {
} }
TestSinkThread::~TestSinkThread() TestSinkWorker::~TestSinkWorker()
{ {
if (m_running) { if (m_running) {
stopWork(); stopWork();
@ -50,31 +50,25 @@ TestSinkThread::~TestSinkThread()
if (m_buf) delete[] m_buf; if (m_buf) delete[] m_buf;
} }
void TestSinkThread::startWork() void TestSinkWorker::startWork()
{ {
qDebug() << "TestSinkThread::startWork: "; qDebug() << "TestSinkWorker::startWork: ";
m_maxThrottlems = 0; m_maxThrottlems = 0;
m_startWaitMutex.lock();
m_elapsedTimer.start(); m_elapsedTimer.start();
start(); m_running = true;
while(!m_running) {
m_startWaiter.wait(&m_startWaitMutex, 100);
}
m_startWaitMutex.unlock();
} }
void TestSinkThread::stopWork() void TestSinkWorker::stopWork()
{ {
qDebug() << "TestSinkThread::stopWork"; qDebug() << "TestSinkWorker::stopWork";
m_running = false; m_running = false;
wait();
} }
void TestSinkThread::setSamplerate(int samplerate) void TestSinkWorker::setSamplerate(int samplerate)
{ {
if (samplerate != m_samplerate) if (samplerate != m_samplerate)
{ {
qDebug() << "TestSinkThread::setSamplerate:" qDebug() << "TestSinkWorker::setSamplerate:"
<< " new:" << samplerate << " new:" << samplerate
<< " old:" << m_samplerate; << " old:" << m_samplerate;
@ -104,7 +98,7 @@ void TestSinkThread::setSamplerate(int samplerate)
} }
} }
void TestSinkThread::setLog2Interpolation(int log2Interpolation) void TestSinkWorker::setLog2Interpolation(int log2Interpolation)
{ {
if ((log2Interpolation < 0) || (log2Interpolation > 6)) { if ((log2Interpolation < 0) || (log2Interpolation > 6)) {
return; return;
@ -112,7 +106,7 @@ void TestSinkThread::setLog2Interpolation(int log2Interpolation)
if (log2Interpolation != m_log2Interpolation) if (log2Interpolation != m_log2Interpolation)
{ {
qDebug() << "TestSinkThread::setLog2Interpolation:" qDebug() << "TestSinkWorker::setLog2Interpolation:"
<< " new:" << log2Interpolation << " new:" << log2Interpolation
<< " old:" << m_log2Interpolation; << " old:" << m_log2Interpolation;
@ -136,26 +130,13 @@ void TestSinkThread::setLog2Interpolation(int log2Interpolation)
} }
} }
void TestSinkThread::run() void TestSinkWorker::connectTimer(const QTimer& timer)
{ {
m_running = true; qDebug() << "TestSinkWorker::connectTimer";
m_startWaiter.wakeAll();
while(m_running) // actual work is in the tick() function
{
sleep(1);
}
m_running = false;
}
void TestSinkThread::connectTimer(const QTimer& timer)
{
qDebug() << "TestSinkThread::connectTimer";
connect(&timer, SIGNAL(timeout()), this, SLOT(tick())); connect(&timer, SIGNAL(timeout()), this, SLOT(tick()));
} }
void TestSinkThread::tick() void TestSinkWorker::tick()
{ {
if (m_running) if (m_running)
{ {
@ -184,7 +165,7 @@ void TestSinkThread::tick()
} }
} }
void TestSinkThread::callbackPart(SampleVector& data, unsigned int iBegin, unsigned int iEnd) void TestSinkWorker::callbackPart(SampleVector& data, unsigned int iBegin, unsigned int iEnd)
{ {
SampleVector::iterator beginRead = data.begin() + iBegin; SampleVector::iterator beginRead = data.begin() + iBegin;
unsigned int chunkSize = iEnd - iBegin; unsigned int chunkSize = iEnd - iBegin;
@ -225,7 +206,7 @@ void TestSinkThread::callbackPart(SampleVector& data, unsigned int iBegin, unsig
} }
} }
void TestSinkThread::feedSpectrum(int16_t *buf, unsigned int bufSize) void TestSinkWorker::feedSpectrum(int16_t *buf, unsigned int bufSize)
{ {
if (!m_spectrumSink) { if (!m_spectrumSink) {
return; return;

View File

@ -15,12 +15,10 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // // along with this program. If not, see <http://www.gnu.org/licenses/>. //
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDE_TESTSINKTHREAD_H #ifndef INCLUDE_TESTSINKWORKER_H
#define INCLUDE_TESTSINKTHREAD_H #define INCLUDE_TESTSINKWORKER_H
#include <QThread> #include <QObject>
#include <QMutex>
#include <QWaitCondition>
#include <QTimer> #include <QTimer>
#include <QElapsedTimer> #include <QElapsedTimer>
#include <iostream> #include <iostream>
@ -37,12 +35,12 @@
class SampleSourceFifo; class SampleSourceFifo;
class BasebandSampleSink; class BasebandSampleSink;
class TestSinkThread : public QThread { class TestSinkWorker : public QObject {
Q_OBJECT Q_OBJECT
public: public:
TestSinkThread(SampleSourceFifo* sampleFifo, QObject* parent = nullptr); TestSinkWorker(SampleSourceFifo* sampleFifo, QObject* parent = nullptr);
~TestSinkThread(); ~TestSinkWorker();
void startWork(); void startWork();
void stopWork(); void stopWork();
@ -64,8 +62,6 @@ private:
int16_t m_imag; int16_t m_imag;
}; };
#pragma pack(pop) #pragma pack(pop)
QMutex m_startWaitMutex;
QWaitCondition m_startWaiter;
volatile bool m_running; volatile bool m_running;
std::size_t m_bufsize; std::size_t m_bufsize;
@ -85,7 +81,6 @@ private:
BasebandSampleSink* m_spectrumSink; BasebandSampleSink* m_spectrumSink;
IncrementalVector<Sample> m_samplesVector; IncrementalVector<Sample> m_samplesVector;
void run();
void callbackPart(SampleVector& data, unsigned int iBegin, unsigned int iEnd); void callbackPart(SampleVector& data, unsigned int iBegin, unsigned int iEnd);
void feedSpectrum(int16_t *buf, unsigned int bufSize); void feedSpectrum(int16_t *buf, unsigned int bufSize);
@ -93,4 +88,4 @@ private slots:
void tick(); void tick();
}; };
#endif // INCLUDE_TESTSINKTHREAD_H #endif // INCLUDE_TESTSINKWORKER_H