From b8681d59a93a00d1101f9e8a247f85b6d720f935 Mon Sep 17 00:00:00 2001 From: f4exb Date: Sun, 12 Jul 2020 02:39:29 +0200 Subject: [PATCH] TestSource: refactored Thread to Worker object moved to thread. Equivalent to FileInput changes --- .../samplesource/testsource/CMakeLists.txt | 4 +- .../testsource/testsourceinput.cpp | 97 ++++++++------ .../samplesource/testsource/testsourceinput.h | 8 +- ...tsourcethread.cpp => testsourceworker.cpp} | 119 ++++++------------ ...{testsourcethread.h => testsourceworker.h} | 43 ++----- 5 files changed, 112 insertions(+), 159 deletions(-) rename plugins/samplesource/testsource/{testsourcethread.cpp => testsourceworker.cpp} (80%) rename plugins/samplesource/testsource/{testsourcethread.h => testsourceworker.h} (93%) diff --git a/plugins/samplesource/testsource/CMakeLists.txt b/plugins/samplesource/testsource/CMakeLists.txt index 24437a148..3a30d246a 100644 --- a/plugins/samplesource/testsource/CMakeLists.txt +++ b/plugins/samplesource/testsource/CMakeLists.txt @@ -3,7 +3,7 @@ project(testsource) set(testsource_SOURCES testsourceinput.cpp testsourceplugin.cpp - testsourcethread.cpp + testsourceworker.cpp testsourcesettings.cpp testsourcewebapiadapter.cpp ) @@ -11,7 +11,7 @@ set(testsource_SOURCES set(testsource_HEADERS testsourceinput.h testsourceplugin.h - testsourcethread.h + testsourceworker.h testsourcesettings.h testsourcewebapiadapter.h ) diff --git a/plugins/samplesource/testsource/testsourceinput.cpp b/plugins/samplesource/testsource/testsourceinput.cpp index c5f2a3dd9..e29778d7c 100644 --- a/plugins/samplesource/testsource/testsourceinput.cpp +++ b/plugins/samplesource/testsource/testsourceinput.cpp @@ -30,7 +30,7 @@ #include "testsourceinput.h" #include "device/deviceapi.h" -#include "testsourcethread.h" +#include "testsourceworker.h" #include "dsp/dspcommands.h" #include "dsp/dspengine.h" #include "dsp/filerecord.h" @@ -43,7 +43,7 @@ MESSAGE_CLASS_DEFINITION(TestSourceInput::MsgStartStop, Message) TestSourceInput::TestSourceInput(DeviceAPI *deviceAPI) : m_deviceAPI(deviceAPI), m_settings(), - m_testSourceThread(0), + m_testSourceWorker(nullptr), m_deviceDescription(), m_running(false), m_masterTimer(deviceAPI->getMasterTimer()) @@ -87,11 +87,14 @@ bool TestSourceInput::start() { QMutexLocker mutexLocker(&m_mutex); - if (m_running) stop(); + if (m_running) { + stop(); + } - m_testSourceThread = new TestSourceThread(&m_sampleFifo); - m_testSourceThread->setSamplerate(m_settings.m_sampleRate); - m_testSourceThread->startStop(true); + m_testSourceWorker = new TestSourceWorker(&m_sampleFifo); + m_testSourceWorker->moveToThread(&m_testSourceWorkerThread); + m_testSourceWorker->setSamplerate(m_settings.m_sampleRate); + startWorker(); mutexLocker.unlock(); @@ -105,16 +108,30 @@ void TestSourceInput::stop() { QMutexLocker mutexLocker(&m_mutex); - if (m_testSourceThread != 0) + if (m_testSourceWorker) { - m_testSourceThread->startStop(false); - m_testSourceThread->deleteLater(); - m_testSourceThread = 0; + stopWorker(); + m_testSourceWorker->deleteLater(); + m_testSourceWorker = nullptr; } m_running = false; } +void TestSourceInput::startWorker() +{ + m_testSourceWorker->startWork(); + m_testSourceWorkerThread.start(); +} + +void TestSourceInput::stopWorker() +{ + m_testSourceWorker->stopWork(); + m_testSourceWorkerThread.quit(); + m_testSourceWorkerThread.wait(); +} + + QByteArray TestSourceInput::serialize() const { return m_settings.serialize(); @@ -266,9 +283,9 @@ bool TestSourceInput::applySettings(const TestSourceSettings& settings, bool for { reverseAPIKeys.append("sampleRate"); - if (m_testSourceThread != 0) + if (m_testSourceWorker != 0) { - m_testSourceThread->setSamplerate(settings.m_sampleRate); + m_testSourceWorker->setSamplerate(settings.m_sampleRate); qDebug("TestSourceInput::applySettings: sample rate set to %d", settings.m_sampleRate); } } @@ -277,9 +294,9 @@ bool TestSourceInput::applySettings(const TestSourceSettings& settings, bool for { reverseAPIKeys.append("log2Decim"); - if (m_testSourceThread != 0) + if (m_testSourceWorker != 0) { - m_testSourceThread->setLog2Decimation(settings.m_log2Decim); + m_testSourceWorker->setLog2Decimation(settings.m_log2Decim); qDebug() << "TestSourceInput::applySettings: set decimation to " << (1<setFcPos((int) settings.m_fcPos); - m_testSourceThread->setFrequencyShift(frequencyShift); + m_testSourceWorker->setFcPos((int) settings.m_fcPos); + m_testSourceWorker->setFrequencyShift(frequencyShift); qDebug() << "TestSourceInput::applySettings:" << " center freq: " << settings.m_centerFrequency << " Hz" << " device center freq: " << deviceCenterFrequency << " Hz" @@ -332,8 +349,8 @@ bool TestSourceInput::applySettings(const TestSourceSettings& settings, bool for { reverseAPIKeys.append("amplitudeBits"); - if (m_testSourceThread != 0) { - m_testSourceThread->setAmplitudeBits(settings.m_amplitudeBits); + if (m_testSourceWorker != 0) { + m_testSourceWorker->setAmplitudeBits(settings.m_amplitudeBits); } } @@ -341,8 +358,8 @@ bool TestSourceInput::applySettings(const TestSourceSettings& settings, bool for { reverseAPIKeys.append("dcFactor"); - if (m_testSourceThread != 0) { - m_testSourceThread->setDCFactor(settings.m_dcFactor); + if (m_testSourceWorker != 0) { + m_testSourceWorker->setDCFactor(settings.m_dcFactor); } } @@ -350,8 +367,8 @@ bool TestSourceInput::applySettings(const TestSourceSettings& settings, bool for { reverseAPIKeys.append("iFactor"); - if (m_testSourceThread != 0) { - m_testSourceThread->setIFactor(settings.m_iFactor); + if (m_testSourceWorker != 0) { + m_testSourceWorker->setIFactor(settings.m_iFactor); } } @@ -359,8 +376,8 @@ bool TestSourceInput::applySettings(const TestSourceSettings& settings, bool for { reverseAPIKeys.append("qFactor"); - if (m_testSourceThread != 0) { - m_testSourceThread->setQFactor(settings.m_qFactor); + if (m_testSourceWorker != 0) { + m_testSourceWorker->setQFactor(settings.m_qFactor); } } @@ -368,8 +385,8 @@ bool TestSourceInput::applySettings(const TestSourceSettings& settings, bool for { reverseAPIKeys.append("phaseImbalance"); - if (m_testSourceThread != 0) { - m_testSourceThread->setPhaseImbalance(settings.m_phaseImbalance); + if (m_testSourceWorker != 0) { + m_testSourceWorker->setPhaseImbalance(settings.m_phaseImbalance); } } @@ -377,8 +394,8 @@ bool TestSourceInput::applySettings(const TestSourceSettings& settings, bool for { reverseAPIKeys.append("sampleSizeIndex"); - if (m_testSourceThread != 0) { - m_testSourceThread->setBitSize(settings.m_sampleSizeIndex); + if (m_testSourceWorker != 0) { + m_testSourceWorker->setBitSize(settings.m_sampleSizeIndex); } } @@ -397,8 +414,8 @@ bool TestSourceInput::applySettings(const TestSourceSettings& settings, bool for { reverseAPIKeys.append("modulationTone"); - if (m_testSourceThread != 0) { - m_testSourceThread->setToneFrequency(settings.m_modulationTone * 10); + if (m_testSourceWorker != 0) { + m_testSourceWorker->setToneFrequency(settings.m_modulationTone * 10); } } @@ -406,16 +423,16 @@ bool TestSourceInput::applySettings(const TestSourceSettings& settings, bool for { reverseAPIKeys.append("modulation"); - if (m_testSourceThread != 0) + if (m_testSourceWorker != 0) { - m_testSourceThread->setModulation(settings.m_modulation); + m_testSourceWorker->setModulation(settings.m_modulation); if (settings.m_modulation == TestSourceSettings::ModulationPattern0) { - m_testSourceThread->setPattern0(); + m_testSourceWorker->setPattern0(); } else if (settings.m_modulation == TestSourceSettings::ModulationPattern1) { - m_testSourceThread->setPattern1(); + m_testSourceWorker->setPattern1(); } else if (settings.m_modulation == TestSourceSettings::ModulationPattern2) { - m_testSourceThread->setPattern2(); + m_testSourceWorker->setPattern2(); } } } @@ -424,8 +441,8 @@ bool TestSourceInput::applySettings(const TestSourceSettings& settings, bool for { reverseAPIKeys.append("amModulation"); - if (m_testSourceThread != 0) { - m_testSourceThread->setAMModulation(settings.m_amModulation / 100.0f); + if (m_testSourceWorker != 0) { + m_testSourceWorker->setAMModulation(settings.m_amModulation / 100.0f); } } @@ -433,8 +450,8 @@ bool TestSourceInput::applySettings(const TestSourceSettings& settings, bool for { reverseAPIKeys.append("fmDeviation"); - if (m_testSourceThread != 0) { - m_testSourceThread->setFMDeviation(settings.m_fmDeviation * 100.0f); + if (m_testSourceWorker != 0) { + m_testSourceWorker->setFMDeviation(settings.m_fmDeviation * 100.0f); } } diff --git a/plugins/samplesource/testsource/testsourceinput.h b/plugins/samplesource/testsource/testsourceinput.h index d075f7ac5..8280770ec 100644 --- a/plugins/samplesource/testsource/testsourceinput.h +++ b/plugins/samplesource/testsource/testsourceinput.h @@ -22,12 +22,13 @@ #include #include #include +#include #include #include "testsourcesettings.h" class DeviceAPI; -class TestSourceThread; +class TestSourceWorker; class FileRecord; class QNetworkAccessManager; class QNetworkReply; @@ -154,13 +155,16 @@ private: FileRecord *m_fileSink; //!< File sink to record device I/Q output QMutex m_mutex; TestSourceSettings m_settings; - TestSourceThread* m_testSourceThread; + TestSourceWorker* m_testSourceWorker; + QThread m_testSourceWorkerThread; QString m_deviceDescription; bool m_running; const QTimer& m_masterTimer; QNetworkAccessManager *m_networkManager; QNetworkRequest m_networkRequest; + void startWorker(); + void stopWorker(); bool applySettings(const TestSourceSettings& settings, bool force); void webapiReverseSendSettings(QList& deviceSettingsKeys, const TestSourceSettings& settings, bool force); void webapiReverseSendStartStop(bool start); diff --git a/plugins/samplesource/testsource/testsourcethread.cpp b/plugins/samplesource/testsource/testsourceworker.cpp similarity index 80% rename from plugins/samplesource/testsource/testsourcethread.cpp rename to plugins/samplesource/testsource/testsourceworker.cpp index 71f4e9e33..2d4e72e20 100644 --- a/plugins/samplesource/testsource/testsourcethread.cpp +++ b/plugins/samplesource/testsource/testsourceworker.cpp @@ -19,16 +19,14 @@ #include #include #include -#include "testsourcethread.h" +#include "testsourceworker.h" #include "dsp/samplesinkfifo.h" #define TESTSOURCE_BLOCKSIZE 16384 -MESSAGE_CLASS_DEFINITION(TestSourceThread::MsgStartStop, Message) - -TestSourceThread::TestSourceThread(SampleSinkFifo* sampleFifo, QObject* parent) : - QThread(parent), +TestSourceWorker::TestSourceWorker(SampleSinkFifo* sampleFifo, QObject* parent) : + QObject(parent), m_running(false), m_buf(0), m_bufsize(0), @@ -69,32 +67,28 @@ TestSourceThread::TestSourceThread(SampleSinkFifo* sampleFifo, QObject* parent) connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()), Qt::QueuedConnection); } -TestSourceThread::~TestSourceThread() +TestSourceWorker::~TestSourceWorker() { } -void TestSourceThread::startWork() +void TestSourceWorker::startWork() { + qDebug("TestSourceWorker::startWork"); m_timer.setTimerType(Qt::PreciseTimer); connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick())); m_timer.start(50); - m_startWaitMutex.lock(); - m_elapsedTimer.start(); - start(); - while(!m_running) - m_startWaiter.wait(&m_startWaitMutex, 100); - m_startWaitMutex.unlock(); + m_running = true; } -void TestSourceThread::stopWork() +void TestSourceWorker::stopWork() { - m_running = false; - wait(); + qDebug("TestSourceWorker::stopWork"); m_timer.stop(); disconnect(&m_timer, SIGNAL(timeout()), this, SLOT(tick())); + m_running = false; } -void TestSourceThread::setSamplerate(int samplerate) +void TestSourceWorker::setSamplerate(int samplerate) { QMutexLocker mutexLocker(&m_mutex); @@ -105,17 +99,17 @@ void TestSourceThread::setSamplerate(int samplerate) m_toneNco.setFreq(m_toneFrequency, m_samplerate); } -void TestSourceThread::setLog2Decimation(unsigned int log2_decim) +void TestSourceWorker::setLog2Decimation(unsigned int log2_decim) { m_log2Decim = log2_decim; } -void TestSourceThread::setFcPos(int fcPos) +void TestSourceWorker::setFcPos(int fcPos) { m_fcPos = fcPos; } -void TestSourceThread::setBitSize(quint32 bitSizeIndex) +void TestSourceWorker::setBitSize(quint32 bitSizeIndex) { switch (bitSizeIndex) { @@ -135,7 +129,7 @@ void TestSourceThread::setBitSize(quint32 bitSizeIndex) } } -void TestSourceThread::setAmplitudeBits(int32_t amplitudeBits) +void TestSourceWorker::setAmplitudeBits(int32_t amplitudeBits) { m_amplitudeBits = amplitudeBits; m_amplitudeBitsDC = m_dcBias * amplitudeBits; @@ -143,76 +137,57 @@ void TestSourceThread::setAmplitudeBits(int32_t amplitudeBits) m_amplitudeBitsQ = (1.0f + m_qBias) * amplitudeBits; } -void TestSourceThread::setDCFactor(float dcFactor) +void TestSourceWorker::setDCFactor(float dcFactor) { m_dcBias = dcFactor; m_amplitudeBitsDC = m_dcBias * m_amplitudeBits; } -void TestSourceThread::setIFactor(float iFactor) +void TestSourceWorker::setIFactor(float iFactor) { m_iBias = iFactor; m_amplitudeBitsI = (1.0f + m_iBias) * m_amplitudeBits; } -void TestSourceThread::setQFactor(float iFactor) +void TestSourceWorker::setQFactor(float iFactor) { m_qBias = iFactor; m_amplitudeBitsQ = (1.0f + m_qBias) * m_amplitudeBits; } -void TestSourceThread::setPhaseImbalance(float phaseImbalance) +void TestSourceWorker::setPhaseImbalance(float phaseImbalance) { m_phaseImbalance = phaseImbalance; } -void TestSourceThread::setFrequencyShift(int shift) +void TestSourceWorker::setFrequencyShift(int shift) { m_nco.setFreq(shift, m_samplerate); } -void TestSourceThread::setToneFrequency(int toneFrequency) +void TestSourceWorker::setToneFrequency(int toneFrequency) { m_toneNco.setFreq(toneFrequency, m_samplerate); } -void TestSourceThread::setModulation(TestSourceSettings::Modulation modulation) +void TestSourceWorker::setModulation(TestSourceSettings::Modulation modulation) { m_modulation = modulation; } -void TestSourceThread::setAMModulation(float amModulation) +void TestSourceWorker::setAMModulation(float amModulation) { m_amModulation = amModulation < 0.0f ? 0.0f : amModulation > 1.0f ? 1.0f : amModulation; } -void TestSourceThread::setFMDeviation(float deviation) +void TestSourceWorker::setFMDeviation(float deviation) { float fmDeviationUnit = deviation / (float) m_samplerate; m_fmDeviationUnit = fmDeviationUnit < 0.0f ? 0.0f : fmDeviationUnit > 0.5f ? 0.5f : fmDeviationUnit; - qDebug("TestSourceThread::setFMDeviation: m_fmDeviationUnit: %f", m_fmDeviationUnit); + qDebug("TestSourceWorker::setFMDeviation: m_fmDeviationUnit: %f", m_fmDeviationUnit); } -void TestSourceThread::startStop(bool start) -{ - MsgStartStop *msg = MsgStartStop::create(start); - m_inputMessageQueue.push(msg); -} - -void TestSourceThread::run() -{ - m_running = true; - m_startWaiter.wakeAll(); - - while (m_running) // actual work is in the tick() function - { - sleep(1); - } - - m_running = false; -} - -void TestSourceThread::setBuffers(quint32 chunksize) +void TestSourceWorker::setBuffers(quint32 chunksize) { if (chunksize > m_bufsize) { @@ -220,14 +195,14 @@ void TestSourceThread::setBuffers(quint32 chunksize) if (m_buf == 0) { - qDebug() << "TestSourceThread::setBuffer: Allocate buffer: " + qDebug() << "TestSourceWorker::setBuffer: Allocate buffer: " << " size: " << m_bufsize << " bytes" << " #samples: " << (m_bufsize/4); m_buf = (qint16*) malloc(m_bufsize); } else { - qDebug() << "TestSourceThread::setBuffer: Re-allocate buffer: " + qDebug() << "TestSourceWorker::setBuffer: Re-allocate buffer: " << " size: " << m_bufsize << " bytes" << " #samples: " << (m_bufsize/4); free(m_buf); @@ -238,7 +213,7 @@ void TestSourceThread::setBuffers(quint32 chunksize) } } -void TestSourceThread::generate(quint32 chunksize) +void TestSourceWorker::generate(quint32 chunksize) { int n = chunksize / 2; setBuffers(chunksize); @@ -361,13 +336,13 @@ void TestSourceThread::generate(quint32 chunksize) callback(m_buf, n); } -void TestSourceThread::pullAF(Real& afSample) +void TestSourceWorker::pullAF(Real& afSample) { afSample = m_toneNco.next(); } // call appropriate conversion (decimation) routine depending on the number of sample bits -void TestSourceThread::callback(const qint16* buf, qint32 len) +void TestSourceWorker::callback(const qint16* buf, qint32 len) { SampleVector::iterator it = m_convertBuffer.begin(); @@ -388,7 +363,7 @@ void TestSourceThread::callback(const qint16* buf, qint32 len) m_sampleFifo->write(m_convertBuffer.begin(), it); } -void TestSourceThread::tick() +void TestSourceWorker::tick() { if (m_running) { @@ -406,9 +381,9 @@ void TestSourceThread::tick() if (m_histoCounter < 49) { m_histoCounter++; } else { - // qDebug("TestSourceThread::tick: -----------"); + // qDebug("TestSourceWorker::tick: -----------"); // for (std::map::iterator it = m_timerHistogram.begin(); it != m_timerHistogram.end(); ++it) { - // qDebug("TestSourceThread::tick: %d: %d", it->first, it->second); + // qDebug("TestSourceWorker::tick: %d: %d", it->first, it->second); // } m_histoCounter = 0; } @@ -425,29 +400,11 @@ void TestSourceThread::tick() } } -void TestSourceThread::handleInputMessages() +void TestSourceWorker::handleInputMessages() { - Message* message; - - while ((message = m_inputMessageQueue.pop()) != 0) - { - if (MsgStartStop::match(*message)) - { - MsgStartStop* notif = (MsgStartStop*) message; - qDebug("TestSourceThread::handleInputMessages: MsgStartStop: %s", notif->getStartStop() ? "start" : "stop"); - - if (notif->getStartStop()) { - startWork(); - } else { - stopWork(); - } - - delete message; - } - } } -void TestSourceThread::setPattern0() +void TestSourceWorker::setPattern0() { m_pulseWidth = 150; m_pulseSampleCount = 0; @@ -456,13 +413,13 @@ void TestSourceThread::setPattern0() m_pulsePatternPlaces = 3; } -void TestSourceThread::setPattern1() +void TestSourceWorker::setPattern1() { m_pulseWidth = 1000; m_pulseSampleCount = 0; } -void TestSourceThread::setPattern2() +void TestSourceWorker::setPattern2() { m_pulseWidth = 1000; m_pulseSampleCount = 0; diff --git a/plugins/samplesource/testsource/testsourcethread.h b/plugins/samplesource/testsource/testsourceworker.h similarity index 93% rename from plugins/samplesource/testsource/testsourcethread.h rename to plugins/samplesource/testsource/testsourceworker.h index de1e2b944..90fcebc30 100644 --- a/plugins/samplesource/testsource/testsourcethread.h +++ b/plugins/samplesource/testsource/testsourceworker.h @@ -15,14 +15,12 @@ // along with this program. If not, see . // /////////////////////////////////////////////////////////////////////////////////// -#ifndef _TESTSOURCE_TESTSOURCETHREAD_H_ -#define _TESTSOURCE_TESTSOURCETHREAD_H_ +#ifndef _TESTSOURCE_TESTSOURCEWORKER_H_ +#define _TESTSOURCE_TESTSOURCEWORKER_H_ #include -#include -#include -#include +#include #include #include #include @@ -37,33 +35,15 @@ #define TESTSOURCE_THROTTLE_MS 50 -class TestSourceThread : public QThread { +class TestSourceWorker : public QObject { Q_OBJECT public: - class MsgStartStop : public Message { - MESSAGE_CLASS_DECLARATION + TestSourceWorker(SampleSinkFifo* sampleFifo, QObject* parent = 0); + ~TestSourceWorker(); - public: - bool getStartStop() const { return m_startStop; } - - static MsgStartStop* create(bool startStop) { - return new MsgStartStop(startStop); - } - - protected: - bool m_startStop; - - MsgStartStop(bool startStop) : - Message(), - m_startStop(startStop) - { } - }; - - TestSourceThread(SampleSinkFifo* sampleFifo, QObject* parent = 0); - ~TestSourceThread(); - - void startStop(bool start); + void startWork(); + void stopWork(); void setSamplerate(int samplerate); void setLog2Decimation(unsigned int log2_decim); void setFcPos(int fcPos); @@ -83,8 +63,6 @@ public: void setPattern2(); private: - QMutex m_startWaitMutex; - QWaitCondition m_startWaiter; volatile bool m_running; qint16 *m_buf; @@ -138,9 +116,6 @@ private: std::map m_timerHistogram; uint32_t m_histoCounter; - void startWork(); - void stopWork(); - void run(); void callback(const qint16* buf, qint32 len); void setBuffers(quint32 chunksize); void generate(quint32 chunksize); @@ -387,4 +362,4 @@ private slots: void handleInputMessages(); }; -#endif // _TESTSOURCE_TESTSOURCETHREAD_H_ +#endif // _TESTSOURCE_TESTSOURCEWORKER_H_