From e3910c0aa15b3cdb26e8e953258775454e4fd3b2 Mon Sep 17 00:00:00 2001 From: f4exb Date: Mon, 26 Apr 2021 19:33:02 +0200 Subject: [PATCH] Revision of mutex locks in FIFOs --- sdrbase/dsp/datafifo.cpp | 12 ++++++++---- sdrbase/dsp/datafifo.h | 3 +-- sdrbase/dsp/samplemififo.cpp | 9 +++++++-- sdrbase/dsp/samplemofifo.cpp | 6 ++++-- sdrbase/dsp/samplesinkfifo.cpp | 12 ++++++++---- sdrbase/dsp/samplesinkfifo.h | 5 ++--- 6 files changed, 30 insertions(+), 17 deletions(-) diff --git a/sdrbase/dsp/datafifo.cpp b/sdrbase/dsp/datafifo.cpp index a572c0161..2773e1dce 100644 --- a/sdrbase/dsp/datafifo.cpp +++ b/sdrbase/dsp/datafifo.cpp @@ -31,6 +31,7 @@ void DataFifo::create(unsigned int s) void DataFifo::reset() { + QMutexLocker mutexLocker(&m_mutex); m_suppressed = -1; m_fill = 0; m_head = 0; @@ -39,7 +40,8 @@ void DataFifo::reset() DataFifo::DataFifo(QObject* parent) : QObject(parent), - m_data() + m_data(), + m_mutex(QMutex::Recursive) { m_suppressed = -1; m_size = 0; @@ -50,7 +52,8 @@ DataFifo::DataFifo(QObject* parent) : DataFifo::DataFifo(int size, QObject* parent) : QObject(parent), - m_data() + m_data(), + m_mutex(QMutex::Recursive) { m_suppressed = -1; create(size); @@ -58,7 +61,8 @@ DataFifo::DataFifo(int size, QObject* parent) : DataFifo::DataFifo(const DataFifo& other) : QObject(other.parent()), - m_data(other.m_data) + m_data(other.m_data), + m_mutex(QMutex::Recursive) { m_suppressed = -1; m_size = m_data.size(); @@ -75,8 +79,8 @@ DataFifo::~DataFifo() bool DataFifo::setSize(int size) { + QMutexLocker mutexLocker(&m_mutex); create(size); - return m_data.size() == size; } diff --git a/sdrbase/dsp/datafifo.h b/sdrbase/dsp/datafifo.h index d8ba18139..f0eaec3bc 100644 --- a/sdrbase/dsp/datafifo.h +++ b/sdrbase/dsp/datafifo.h @@ -31,11 +31,10 @@ class SDRBASE_API DataFifo : public QObject { Q_OBJECT private: - QMutex m_mutex; QElapsedTimer m_msgRateTimer; int m_suppressed; - QByteArray m_data; + QMutex m_mutex; unsigned int m_size; unsigned int m_fill; diff --git a/sdrbase/dsp/samplemififo.cpp b/sdrbase/dsp/samplemififo.cpp index b95cc9ac9..d7c8372d5 100644 --- a/sdrbase/dsp/samplemififo.cpp +++ b/sdrbase/dsp/samplemififo.cpp @@ -19,6 +19,7 @@ void SampleMIFifo::init(unsigned int nbStreams, unsigned int size) { + QMutexLocker mutexLocker(&m_mutex); m_nbStreams = nbStreams; m_size = size; m_fill = 0; @@ -52,12 +53,14 @@ SampleMIFifo::SampleMIFifo(QObject *parent) : m_nbStreams(0), m_size(0), m_fill(0), - m_head(0) + m_head(0), + m_mutex(QMutex::Recursive) { } SampleMIFifo::SampleMIFifo(unsigned int nbStreams, unsigned int size, QObject *parent) : - QObject(parent) + QObject(parent), + m_mutex(QMutex::Recursive) { init(nbStreams, size); } @@ -235,6 +238,8 @@ void SampleMIFifo::readSync( return; } + QMutexLocker mutexLocker(&m_mutex); + if (m_head < m_fill) { ipart1Begin = m_head; diff --git a/sdrbase/dsp/samplemofifo.cpp b/sdrbase/dsp/samplemofifo.cpp index dd140de37..414d9a1eb 100644 --- a/sdrbase/dsp/samplemofifo.cpp +++ b/sdrbase/dsp/samplemofifo.cpp @@ -27,13 +27,15 @@ SampleMOFifo::SampleMOFifo(QObject *parent) : {} SampleMOFifo::SampleMOFifo(unsigned int nbStreams, unsigned int size, QObject *parent) : - QObject(parent) + QObject(parent), + m_mutex(QMutex::Recursive) { init(nbStreams, size); } void SampleMOFifo::init(unsigned int nbStreams, unsigned int size) { + QMutexLocker mutexLocker(&m_mutex); m_data.resize(nbStreams); m_vReadCount.resize(nbStreams); m_vReadHead.resize(nbStreams); @@ -231,4 +233,4 @@ void SampleMOFifo::writeAsync( //!< in place write with given amount unsigned int SampleMOFifo::getSizePolicy(unsigned int sampleRate) { return (sampleRate/100)*64; // .64s -} \ No newline at end of file +} diff --git a/sdrbase/dsp/samplesinkfifo.cpp b/sdrbase/dsp/samplesinkfifo.cpp index e3908d4b6..fc5e98fac 100644 --- a/sdrbase/dsp/samplesinkfifo.cpp +++ b/sdrbase/dsp/samplesinkfifo.cpp @@ -32,6 +32,7 @@ void SampleSinkFifo::create(unsigned int s) void SampleSinkFifo::reset() { + QMutexLocker mutexLocker(&m_mutex); m_suppressed = -1; m_fill = 0; m_head = 0; @@ -40,7 +41,8 @@ void SampleSinkFifo::reset() SampleSinkFifo::SampleSinkFifo(QObject* parent) : QObject(parent), - m_data() + m_data(), + m_mutex(QMutex::Recursive) { m_suppressed = -1; m_size = 0; @@ -51,7 +53,8 @@ SampleSinkFifo::SampleSinkFifo(QObject* parent) : SampleSinkFifo::SampleSinkFifo(int size, QObject* parent) : QObject(parent), - m_data() + m_data(), + m_mutex(QMutex::Recursive) { m_suppressed = -1; create(size); @@ -59,7 +62,8 @@ SampleSinkFifo::SampleSinkFifo(int size, QObject* parent) : SampleSinkFifo::SampleSinkFifo(const SampleSinkFifo& other) : QObject(other.parent()), - m_data(other.m_data) + m_data(other.m_data), + m_mutex(QMutex::Recursive) { m_suppressed = -1; m_size = m_data.size(); @@ -76,8 +80,8 @@ SampleSinkFifo::~SampleSinkFifo() bool SampleSinkFifo::setSize(int size) { + QMutexLocker mutexLocker(&m_mutex); create(size); - return m_data.size() == (unsigned int)size; } diff --git a/sdrbase/dsp/samplesinkfifo.h b/sdrbase/dsp/samplesinkfifo.h index 316acd6eb..64c93912e 100644 --- a/sdrbase/dsp/samplesinkfifo.h +++ b/sdrbase/dsp/samplesinkfifo.h @@ -29,11 +29,10 @@ class SDRBASE_API SampleSinkFifo : public QObject { Q_OBJECT private: - QMutex m_mutex; QElapsedTimer m_msgRateTimer; int m_suppressed; - SampleVector m_data; + QMutex m_mutex; unsigned int m_size; unsigned int m_fill; @@ -50,7 +49,7 @@ public: bool setSize(int size); void reset(); - inline unsigned int size() const { return m_size; } + inline unsigned int size() { QMutexLocker mutexLocker(&m_mutex); unsigned int size = m_size; return size; } inline unsigned int fill() { QMutexLocker mutexLocker(&m_mutex); unsigned int fill = m_fill; return fill; } unsigned int write(const quint8* data, unsigned int count);