1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-12-23 01:55:48 -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
testsinkplugin.cpp
testsinksettings.cpp
testsinkthread.cpp
testsinkworker.cpp
)
set(testsink_HEADERS
testsinkoutput.h
testsinkplugin.h
testsinksettings.h
testsinkthread.h
testsinkworker.h
)
include_directories(

View File

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

View File

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

View File

@ -23,10 +23,10 @@
#include "dsp/samplesourcefifo.h"
#include "dsp/basebandsamplesink.h"
#include "testsinkthread.h"
#include "testsinkworker.h"
TestSinkThread::TestSinkThread(SampleSourceFifo* sampleFifo, QObject* parent) :
QThread(parent),
TestSinkWorker::TestSinkWorker(SampleSourceFifo* sampleFifo, QObject* parent) :
QObject(parent),
m_running(false),
m_bufsize(0),
m_samplesChunkSize(0),
@ -41,7 +41,7 @@ TestSinkThread::TestSinkThread(SampleSourceFifo* sampleFifo, QObject* parent) :
{
}
TestSinkThread::~TestSinkThread()
TestSinkWorker::~TestSinkWorker()
{
if (m_running) {
stopWork();
@ -50,31 +50,25 @@ TestSinkThread::~TestSinkThread()
if (m_buf) delete[] m_buf;
}
void TestSinkThread::startWork()
void TestSinkWorker::startWork()
{
qDebug() << "TestSinkThread::startWork: ";
qDebug() << "TestSinkWorker::startWork: ";
m_maxThrottlems = 0;
m_startWaitMutex.lock();
m_elapsedTimer.start();
start();
while(!m_running) {
m_startWaiter.wait(&m_startWaitMutex, 100);
}
m_startWaitMutex.unlock();
m_running = true;
}
void TestSinkThread::stopWork()
void TestSinkWorker::stopWork()
{
qDebug() << "TestSinkThread::stopWork";
qDebug() << "TestSinkWorker::stopWork";
m_running = false;
wait();
}
void TestSinkThread::setSamplerate(int samplerate)
void TestSinkWorker::setSamplerate(int samplerate)
{
if (samplerate != m_samplerate)
{
qDebug() << "TestSinkThread::setSamplerate:"
qDebug() << "TestSinkWorker::setSamplerate:"
<< " new:" << 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)) {
return;
@ -112,7 +106,7 @@ void TestSinkThread::setLog2Interpolation(int log2Interpolation)
if (log2Interpolation != m_log2Interpolation)
{
qDebug() << "TestSinkThread::setLog2Interpolation:"
qDebug() << "TestSinkWorker::setLog2Interpolation:"
<< " new:" << 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;
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";
qDebug() << "TestSinkWorker::connectTimer";
connect(&timer, SIGNAL(timeout()), this, SLOT(tick()));
}
void TestSinkThread::tick()
void TestSinkWorker::tick()
{
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;
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) {
return;

View File

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