From d2ed278ba3949919c720c6a91ffe4ea23f085183 Mon Sep 17 00:00:00 2001 From: f4exb Date: Mon, 24 Aug 2015 23:23:45 +0200 Subject: [PATCH] Deep redesign: Rest of demods: Mutex to prevent concurrent critical updates with feed method --- plugins/channel/am/amdemod.cpp | 14 +++++++++----- plugins/channel/am/amdemod.h | 4 ++-- plugins/channel/chanalyzer/chanalyzer.cpp | 11 ++++++++++- plugins/channel/chanalyzer/chanalyzer.h | 2 ++ plugins/channel/lora/lorademod.cpp | 15 ++++++++++++++- plugins/channel/lora/lorademod.h | 2 ++ plugins/channel/nfm/nfmdemod.h | 1 - plugins/channel/ssb/ssbdemod.cpp | 17 +++++++++++++++-- plugins/channel/ssb/ssbdemod.h | 3 +++ plugins/channel/tcpsrc/tcpsrc.cpp | 15 ++++++++++++++- plugins/channel/tcpsrc/tcpsrc.h | 3 +++ 11 files changed, 74 insertions(+), 13 deletions(-) diff --git a/plugins/channel/am/amdemod.cpp b/plugins/channel/am/amdemod.cpp index 2626d6261..2c7a8e13e 100644 --- a/plugins/channel/am/amdemod.cpp +++ b/plugins/channel/am/amdemod.cpp @@ -28,7 +28,8 @@ MESSAGE_CLASS_DEFINITION(AMDemod::MsgConfigureAMDemod, Message) AMDemod::AMDemod() : - m_audioFifo(4, 48000) + m_audioFifo(4, 48000), + m_settingsMutex(QMutex::Recursive) { setObjectName("AMDemod"); @@ -66,10 +67,7 @@ void AMDemod::feed(SampleVector::const_iterator begin, SampleVector::const_itera { Complex ci; - if (m_audioFifo.size() == 0) - { - return; - } + m_settingsMutex.lock(); for (SampleVector::const_iterator it = begin; it != end; ++it) { @@ -152,6 +150,8 @@ void AMDemod::feed(SampleVector::const_iterator begin, SampleVector::const_itera } m_sampleBuffer.clear(); + + m_settingsMutex.unlock(); } void AMDemod::start() @@ -223,15 +223,19 @@ void AMDemod::apply() if((m_config.m_inputSampleRate != m_running.m_inputSampleRate) || (m_config.m_rfBandwidth != m_running.m_rfBandwidth)) { + m_settingsMutex.lock(); m_interpolator.create(16, m_config.m_inputSampleRate, m_config.m_rfBandwidth / 2.2); m_interpolatorDistanceRemain = 0; m_interpolatorDistance = (Real) m_config.m_inputSampleRate / (Real) m_config.m_audioSampleRate; + m_settingsMutex.unlock(); } if((m_config.m_afBandwidth != m_running.m_afBandwidth) || (m_config.m_audioSampleRate != m_running.m_audioSampleRate)) { + m_settingsMutex.lock(); m_lowpass.create(21, m_config.m_audioSampleRate, m_config.m_afBandwidth); + m_settingsMutex.unlock(); } if(m_config.m_squelch != m_running.m_squelch) diff --git a/plugins/channel/am/amdemod.h b/plugins/channel/am/amdemod.h index c34e84b6a..aa12e915a 100644 --- a/plugins/channel/am/amdemod.h +++ b/plugins/channel/am/amdemod.h @@ -17,6 +17,7 @@ #ifndef INCLUDE_AMDEMOD_H #define INCLUDE_AMDEMOD_H +#include #include #include "dsp/samplesink.h" #include "dsp/nco.h" @@ -27,8 +28,6 @@ #include "audio/audiofifo.h" #include "util/message.h" -class AudioFifo; - class AMDemod : public SampleSink { Q_OBJECT public: @@ -123,6 +122,7 @@ private: AudioFifo m_audioFifo; SampleVector m_sampleBuffer; + QMutex m_settingsMutex; void apply(); }; diff --git a/plugins/channel/chanalyzer/chanalyzer.cpp b/plugins/channel/chanalyzer/chanalyzer.cpp index 1a3ec24cc..394737748 100644 --- a/plugins/channel/chanalyzer/chanalyzer.cpp +++ b/plugins/channel/chanalyzer/chanalyzer.cpp @@ -27,7 +27,8 @@ MESSAGE_CLASS_DEFINITION(ChannelAnalyzer::MsgConfigureChannelAnalyzer, Message) ChannelAnalyzer::ChannelAnalyzer(SampleSink* sampleSink) : - m_sampleSink(sampleSink) + m_sampleSink(sampleSink), + m_settingsMutex(QMutex::Recursive) { m_Bandwidth = 5000; m_LowCutoff = 300; @@ -66,6 +67,8 @@ void ChannelAnalyzer::feed(SampleVector::const_iterator begin, SampleVector::con int decim = 1<real() / 32768.0, it->imag() / 32768.0); @@ -111,6 +114,8 @@ void ChannelAnalyzer::feed(SampleVector::const_iterator begin, SampleVector::con } m_sampleBuffer.clear(); + + m_settingsMutex.unlock(); } void ChannelAnalyzer::start() @@ -163,6 +168,8 @@ bool ChannelAnalyzer::handleMessage(const Message& cmd) lowCutoff = 0; } + m_settingsMutex.lock(); + m_Bandwidth = band; m_LowCutoff = lowCutoff; @@ -172,6 +179,8 @@ bool ChannelAnalyzer::handleMessage(const Message& cmd) m_spanLog2 = cfg.getSpanLog2(); m_ssb = cfg.getSSB(); + m_settingsMutex.unlock(); + qDebug() << " - MsgConfigureChannelAnalyzer: m_Bandwidth: " << m_Bandwidth << " m_LowCutoff: " << m_LowCutoff << " m_spanLog2: " << m_spanLog2 diff --git a/plugins/channel/chanalyzer/chanalyzer.h b/plugins/channel/chanalyzer/chanalyzer.h index 8a6da515f..3fc5ad7b1 100644 --- a/plugins/channel/chanalyzer/chanalyzer.h +++ b/plugins/channel/chanalyzer/chanalyzer.h @@ -18,6 +18,7 @@ #ifndef INCLUDE_CHANALYZER_H #define INCLUDE_CHANALYZER_H +#include #include #include "dsp/samplesink.h" #include "dsp/nco.h" @@ -99,6 +100,7 @@ private: SampleSink* m_sampleSink; SampleVector m_sampleBuffer; + QMutex m_settingsMutex; }; #endif // INCLUDE_CHANALYZER_H diff --git a/plugins/channel/lora/lorademod.cpp b/plugins/channel/lora/lorademod.cpp index 206080800..f1a96907c 100644 --- a/plugins/channel/lora/lorademod.cpp +++ b/plugins/channel/lora/lorademod.cpp @@ -27,7 +27,8 @@ MESSAGE_CLASS_DEFINITION(LoRaDemod::MsgConfigureLoRaDemod, Message) LoRaDemod::LoRaDemod(SampleSink* sampleSink) : - m_sampleSink(sampleSink) + m_sampleSink(sampleSink), + m_settingsMutex(QMutex::Recursive) { setObjectName("LoRaDemod"); @@ -242,6 +243,8 @@ void LoRaDemod::feed(SampleVector::const_iterator begin, SampleVector::const_ite m_sampleBuffer.clear(); + m_settingsMutex.lock(); + for(SampleVector::const_iterator it = begin; it < end; ++it) { Complex c(it->real() / 32768.0, it->imag() / 32768.0); @@ -265,6 +268,8 @@ void LoRaDemod::feed(SampleVector::const_iterator begin, SampleVector::const_ite { m_sampleSink->feed(m_sampleBuffer.begin(), m_sampleBuffer.end(), false); } + + m_settingsMutex.unlock(); } void LoRaDemod::start() @@ -283,11 +288,15 @@ bool LoRaDemod::handleMessage(const Message& cmd) { Channelizer::MsgChannelizerNotification& notif = (Channelizer::MsgChannelizerNotification&) cmd; + m_settingsMutex.lock(); + m_sampleRate = notif.getSampleRate(); m_nco.setFreq(-notif.getFrequencyOffset(), m_sampleRate); m_interpolator.create(16, m_sampleRate, m_Bandwidth/1.9); m_sampleDistanceRemain = m_sampleRate / m_Bandwidth; + m_settingsMutex.unlock(); + qDebug() << "LoRaDemod::handleMessage: MsgChannelizerNotification: m_sampleRate: " << m_sampleRate << " frequencyOffset: " << notif.getFrequencyOffset(); @@ -297,9 +306,13 @@ bool LoRaDemod::handleMessage(const Message& cmd) { MsgConfigureLoRaDemod& cfg = (MsgConfigureLoRaDemod&) cmd; + m_settingsMutex.lock(); + m_Bandwidth = cfg.getBandwidth(); m_interpolator.create(16, m_sampleRate, m_Bandwidth/1.9); + m_settingsMutex.unlock(); + qDebug() << " MsgConfigureLoRaDemod: m_Bandwidth: " << m_Bandwidth; return true; diff --git a/plugins/channel/lora/lorademod.h b/plugins/channel/lora/lorademod.h index 9ba1c7507..272b3cc48 100644 --- a/plugins/channel/lora/lorademod.h +++ b/plugins/channel/lora/lorademod.h @@ -18,6 +18,7 @@ #ifndef INCLUDE_LoRaDEMOD_H #define INCLUDE_LoRaDEMOD_H +#include #include #include "dsp/samplesink.h" #include "dsp/nco.h" @@ -97,6 +98,7 @@ private: SampleSink* m_sampleSink; SampleVector m_sampleBuffer; + QMutex m_settingsMutex; }; #endif // INCLUDE_LoRaDEMOD_H diff --git a/plugins/channel/nfm/nfmdemod.h b/plugins/channel/nfm/nfmdemod.h index 27c1c7bb7..00a6f3d8f 100644 --- a/plugins/channel/nfm/nfmdemod.h +++ b/plugins/channel/nfm/nfmdemod.h @@ -32,7 +32,6 @@ #include "audio/audiofifo.h" #include "util/message.h" -class AudioFifo; class NFMDemodGUI; class NFMDemod : public SampleSink { diff --git a/plugins/channel/ssb/ssbdemod.cpp b/plugins/channel/ssb/ssbdemod.cpp index 117924bac..fcdc224cb 100644 --- a/plugins/channel/ssb/ssbdemod.cpp +++ b/plugins/channel/ssb/ssbdemod.cpp @@ -28,7 +28,8 @@ MESSAGE_CLASS_DEFINITION(SSBDemod::MsgConfigureSSBDemod, Message) SSBDemod::SSBDemod(SampleSink* sampleSink) : m_sampleSink(sampleSink), - m_audioFifo(4, 24000) + m_audioFifo(4, 24000), + m_settingsMutex(QMutex::Recursive) { setObjectName("SSBDemod"); @@ -76,6 +77,9 @@ void SSBDemod::feed(SampleVector::const_iterator begin, SampleVector::const_iter fftfilt::cmplx *sideband, sum; Real avg; int n_out; + + m_settingsMutex.lock(); + int decim = 1<<(m_spanLog2 - 1); unsigned char decim_mask = decim - 1; // counter LSB bit mask for decimation by 2^(m_scaleLog2 - 1) @@ -142,6 +146,8 @@ void SSBDemod::feed(SampleVector::const_iterator begin, SampleVector::const_iter } m_sampleBuffer.clear(); + + m_settingsMutex.unlock(); } void SSBDemod::start() @@ -162,11 +168,15 @@ bool SSBDemod::handleMessage(const Message& cmd) { Channelizer::MsgChannelizerNotification& notif = (Channelizer::MsgChannelizerNotification&) cmd; + m_settingsMutex.lock(); + m_sampleRate = notif.getSampleRate(); m_nco.setFreq(-notif.getFrequencyOffset(), m_sampleRate); m_interpolator.create(16, m_sampleRate, m_Bandwidth); m_sampleDistanceRemain = m_sampleRate / m_audioSampleRate; + m_settingsMutex.unlock(); + qDebug() << "SSBDemod::handleMessage: MsgChannelizerNotification: m_sampleRate: " << m_sampleRate << " frequencyOffset" << notif.getFrequencyOffset(); @@ -176,6 +186,8 @@ bool SSBDemod::handleMessage(const Message& cmd) { MsgConfigureSSBDemod& cfg = (MsgConfigureSSBDemod&) cmd; + m_settingsMutex.lock(); + band = cfg.getBandwidth(); lowCutoff = cfg.getLoCutoff(); @@ -191,7 +203,6 @@ bool SSBDemod::handleMessage(const Message& cmd) band = 100.0f; lowCutoff = 0; } - m_Bandwidth = band; m_LowCutoff = lowCutoff; @@ -203,6 +214,8 @@ bool SSBDemod::handleMessage(const Message& cmd) m_spanLog2 = cfg.getSpanLog2(); + m_settingsMutex.unlock(); + qDebug() << " - MsgConfigureSSBDemod: m_Bandwidth: " << m_Bandwidth << " m_LowCutoff: " << m_LowCutoff << " m_volume: " << m_volume diff --git a/plugins/channel/ssb/ssbdemod.h b/plugins/channel/ssb/ssbdemod.h index c6aeb3678..05d5593eb 100644 --- a/plugins/channel/ssb/ssbdemod.h +++ b/plugins/channel/ssb/ssbdemod.h @@ -18,6 +18,7 @@ #ifndef INCLUDE_SSBDEMOD_H #define INCLUDE_SSBDEMOD_H +#include #include #include "dsp/samplesink.h" #include "dsp/nco.h" @@ -97,6 +98,8 @@ private: uint m_audioBufferFill; AudioFifo m_audioFifo; quint32 m_audioSampleRate; + + QMutex m_settingsMutex; }; #endif // INCLUDE_SSBDEMOD_H diff --git a/plugins/channel/tcpsrc/tcpsrc.cpp b/plugins/channel/tcpsrc/tcpsrc.cpp index dbf7ea8e3..7ee908c6b 100644 --- a/plugins/channel/tcpsrc/tcpsrc.cpp +++ b/plugins/channel/tcpsrc/tcpsrc.cpp @@ -25,7 +25,8 @@ MESSAGE_CLASS_DEFINITION(TCPSrc::MsgTCPSrcConfigure, Message) MESSAGE_CLASS_DEFINITION(TCPSrc::MsgTCPSrcConnection, Message) MESSAGE_CLASS_DEFINITION(TCPSrc::MsgTCPSrcSpectrum, Message) -TCPSrc::TCPSrc(MessageQueue* uiMessageQueue, TCPSrcGUI* tcpSrcGUI, SampleSink* spectrum) +TCPSrc::TCPSrc(MessageQueue* uiMessageQueue, TCPSrcGUI* tcpSrcGUI, SampleSink* spectrum) : + m_settingsMutex(QMutex::Recursive) { setObjectName("TCPSrc"); @@ -78,6 +79,8 @@ void TCPSrc::feed(SampleVector::const_iterator begin, SampleVector::const_iterat m_sampleBuffer.clear(); + m_settingsMutex.lock(); + // Rtl-Sdr uses full 16-bit scale; FCDPP does not int rescale = 30000 * (1 << m_boost); @@ -139,6 +142,8 @@ void TCPSrc::feed(SampleVector::const_iterator begin, SampleVector::const_iterat } } } + + m_settingsMutex.unlock(); } void TCPSrc::start() @@ -166,11 +171,15 @@ bool TCPSrc::handleMessage(const Message& cmd) { Channelizer::MsgChannelizerNotification& notif = (Channelizer::MsgChannelizerNotification&) cmd; + m_settingsMutex.lock(); + m_inputSampleRate = notif.getSampleRate(); m_nco.setFreq(-notif.getFrequencyOffset(), m_inputSampleRate); m_interpolator.create(16, m_inputSampleRate, m_rfBandwidth / 2.0); m_sampleDistanceRemain = m_inputSampleRate / m_outputSampleRate; + m_settingsMutex.unlock(); + qDebug() << "TCPSrc::handleMessage: MsgChannelizerNotification: m_inputSampleRate: " << m_inputSampleRate << " frequencyOffset: " << notif.getFrequencyOffset(); @@ -180,6 +189,8 @@ bool TCPSrc::handleMessage(const Message& cmd) { MsgTCPSrcConfigure& cfg = (MsgTCPSrcConfigure&) cmd; + m_settingsMutex.lock(); + m_sampleFormat = cfg.getSampleFormat(); m_outputSampleRate = cfg.getOutputSampleRate(); m_rfBandwidth = cfg.getRFBandwidth(); @@ -209,6 +220,8 @@ bool TCPSrc::handleMessage(const Message& cmd) TCPFilter->create_filter(0.0, m_rfBandwidth / 2.0 / m_outputSampleRate); } + m_settingsMutex.unlock(); + qDebug() << " - MsgTCPSrcConfigure: m_sampleFormat: " << m_sampleFormat << " m_outputSampleRate: " << m_outputSampleRate << " m_rfBandwidth: " << m_rfBandwidth diff --git a/plugins/channel/tcpsrc/tcpsrc.h b/plugins/channel/tcpsrc/tcpsrc.h index bb7448c87..a005f9baf 100644 --- a/plugins/channel/tcpsrc/tcpsrc.h +++ b/plugins/channel/tcpsrc/tcpsrc.h @@ -1,6 +1,7 @@ #ifndef INCLUDE_TCPSRC_H #define INCLUDE_TCPSRC_H +#include #include #include "dsp/samplesink.h" #include "dsp/nco.h" @@ -156,6 +157,8 @@ protected: quint32 m_nextSSBId; quint32 m_nextS16leId; + QMutex m_settingsMutex; + void closeAllSockets(Sockets* sockets); protected slots: