diff --git a/sdrbase/device/deviceapi.cpp b/sdrbase/device/deviceapi.cpp index 9f6cd2921..10b355baf 100644 --- a/sdrbase/device/deviceapi.cpp +++ b/sdrbase/device/deviceapi.cpp @@ -88,7 +88,7 @@ void DeviceAPI::removeChannelSink(BasebandSampleSink* sink, bool deleting, int s if (m_deviceSourceEngine) { m_deviceSourceEngine->removeSink(sink, deleting); } else if (m_deviceMIMOEngine) { - m_deviceMIMOEngine->removeChannelSink(sink, streamIndex); + m_deviceMIMOEngine->removeChannelSink(sink, deleting, streamIndex); } } diff --git a/sdrbase/dsp/dspdevicemimoengine.cpp b/sdrbase/dsp/dspdevicemimoengine.cpp index fdeb2390b..176541b4b 100644 --- a/sdrbase/dsp/dspdevicemimoengine.cpp +++ b/sdrbase/dsp/dspdevicemimoengine.cpp @@ -17,6 +17,7 @@ /////////////////////////////////////////////////////////////////////////////////// #include +#include #include "dspcommands.h" #include "basebandsamplesink.h" @@ -155,12 +156,33 @@ void DSPDeviceMIMOEngine::addChannelSource(BasebandSampleSource* source, int ind getInputMessageQueue()->push(cmd); } +// Whether a removal can be run on the engine thread and waited for: not from that thread +// itself, which would deadlock, and not once it has stopped, which would wait forever +static bool canWaitOn(const QObject *engine) +{ + QThread *engineThread = engine->thread(); + return engineThread && engineThread->isRunning() && (QThread::currentThread() != engineThread); +} + void DSPDeviceMIMOEngine::removeChannelSource(BasebandSampleSource* source, bool deleting, int index) { qDebug() << "DSPDeviceMIMOEngine::removeChannelSource: " << source->getSourceName().toStdString().c_str() << " at: " << index; + + // A source about to be deleted has to be removed before this returns + if (deleting && canWaitOn(this)) + { + // Same code as for RemoveBasebandSampleSource in handleMessage + QMetaObject::invokeMethod(this, [this, source, index]() { + if ((unsigned int) index < m_basebandSampleSources.size()) { + m_basebandSampleSources[index].remove(source); + } + }, Qt::BlockingQueuedConnection); + return; + } + auto *cmd = new RemoveBasebandSampleSource(source, index, deleting); getInputMessageQueue()->push(cmd); } @@ -175,12 +197,25 @@ void DSPDeviceMIMOEngine::addChannelSink(BasebandSampleSink* sink, int index) getInputMessageQueue()->push(cmd); } -void DSPDeviceMIMOEngine::removeChannelSink(BasebandSampleSink* sink, int index) +void DSPDeviceMIMOEngine::removeChannelSink(BasebandSampleSink* sink, bool deleting, int index) { qDebug() << "DSPDeviceMIMOEngine::removeChannelSink: " << sink->getSinkName().toStdString().c_str() << " at: " << index; + + // A sink about to be deleted has to be removed before this returns + if (deleting && canWaitOn(this)) + { + // Same code as for RemoveBasebandSampleSink in handleMessage + QMetaObject::invokeMethod(this, [this, sink, index]() { + if ((unsigned int) index < m_basebandSampleSinks.size()) { + m_basebandSampleSinks[index].remove(sink); + } + }, Qt::BlockingQueuedConnection); + return; + } + auto *cmd = new RemoveBasebandSampleSink(sink, index); getInputMessageQueue()->push(cmd); } @@ -197,6 +232,18 @@ void DSPDeviceMIMOEngine::removeMIMOChannel(MIMOChannel *channel) { qDebug() << "DSPDeviceMIMOEngine::removeMIMOChannel: " << channel->getMIMOName().toStdString().c_str(); + // Called by destructors and when a channel moves to another device, and neither can go + // on until the engine has let go of it. + if (canWaitOn(this)) + { + QMetaObject::invokeMethod(this, [this, channel]() { + channel->stopSinks(); + channel->stopSources(); + m_mimoChannels.remove(channel); + }, Qt::BlockingQueuedConnection); + return; + } + auto *cmd = new RemoveMIMOChannel(channel); getInputMessageQueue()->push(cmd); } diff --git a/sdrbase/dsp/dspdevicemimoengine.h b/sdrbase/dsp/dspdevicemimoengine.h index 11b2e779c..0e3b04740 100644 --- a/sdrbase/dsp/dspdevicemimoengine.h +++ b/sdrbase/dsp/dspdevicemimoengine.h @@ -232,7 +232,7 @@ public: void addChannelSource(BasebandSampleSource* source, int index = 0); //!< Add a channel source void removeChannelSource(BasebandSampleSource* source, bool deleting, int index = 0); //!< Remove a channel source void addChannelSink(BasebandSampleSink* sink, int index = 0); //!< Add a channel sink - void removeChannelSink(BasebandSampleSink* sink, int index = 0); //!< Remove a channel sink + void removeChannelSink(BasebandSampleSink* sink, bool deleting, int index = 0); //!< Remove a channel sink void addMIMOChannel(MIMOChannel *channel); //!< Add a MIMO channel void removeMIMOChannel(MIMOChannel *channel); //!< Remove a MIMO channel diff --git a/sdrbase/dsp/dspdevicesinkengine.cpp b/sdrbase/dsp/dspdevicesinkengine.cpp index 9282e7257..0bdfd6615 100644 --- a/sdrbase/dsp/dspdevicesinkengine.cpp +++ b/sdrbase/dsp/dspdevicesinkengine.cpp @@ -101,9 +101,30 @@ void DSPDeviceSinkEngine::addChannelSource(BasebandSampleSource* source) getInputMessageQueue()->push(cmd); } +// Whether a removal can be run on the engine thread and waited for: not from that thread +// itself, which would deadlock, and not once it has stopped, which would wait forever +// Neither should happen in practice: channels are deleted from the main thread, and a device +// set's channels go before its engine does +static bool canWaitOn(const QObject *engine) +{ + QThread *engineThread = engine->thread(); + return engineThread && engineThread->isRunning() && (QThread::currentThread() != engineThread); +} + void DSPDeviceSinkEngine::removeChannelSource(BasebandSampleSource* source, bool deleting) { qDebug() << "DSPDeviceSinkEngine::removeChannelSource: " << source->getSourceName().toStdString().c_str(); + + // When deleting, the source needs to be removed before returning + if (deleting && canWaitOn(this)) + { + // Same code as for DSPRemoveBasebandSampleSource in handleMessage + QMetaObject::invokeMethod(this, [this, source]() { + m_basebandSampleSources.remove(source); + }, Qt::BlockingQueuedConnection); + return; + } + auto *cmd = new DSPRemoveBasebandSampleSource(source, deleting); getInputMessageQueue()->push(cmd); } diff --git a/sdrbase/dsp/dspdevicesourceengine.cpp b/sdrbase/dsp/dspdevicesourceengine.cpp index edbf39ad7..067fc73ae 100644 --- a/sdrbase/dsp/dspdevicesourceengine.cpp +++ b/sdrbase/dsp/dspdevicesourceengine.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include "dsp/dspcommands.h" #include "samplesinkfifo.h" @@ -109,9 +110,31 @@ void DSPDeviceSourceEngine::addSink(BasebandSampleSink* sink) getInputMessageQueue()->push(cmd); } +// Whether a removal can be run on the engine thread and waited for: not from that thread +// itself, which would deadlock, and not once it has stopped, which would wait forever. +// Neither should happen in practice: channels are deleted from the main thread, and a device +// set's channels go before its engine does +static bool canWaitOn(const QObject *engine) +{ + QThread *engineThread = engine->thread(); + return engineThread && engineThread->isRunning() && (QThread::currentThread() != engineThread); +} + void DSPDeviceSourceEngine::removeSink(BasebandSampleSink* sink, bool deleting) { qDebug() << "DSPDeviceSourceEngine::removeSink: " << sink->getSinkName().toStdString().c_str(); + + // When deleting, the sink needs to be removed before returning + if (deleting && canWaitOn(this)) + { + QMetaObject::invokeMethod(this, [this, sink]() { + // Same code as for DSPRemoveBasebandSampleSink in handleMessage + m_basebandSampleSinks.remove(sink); + emit sinkRemoved(); + }, Qt::BlockingQueuedConnection); + return; + } + auto *cmd = new DSPRemoveBasebandSampleSink(sink, deleting); getInputMessageQueue()->push(cmd); } diff --git a/sdrbase/dsp/glscopeinterface.h b/sdrbase/dsp/glscopeinterface.h index d98266581..ad8a52aa6 100644 --- a/sdrbase/dsp/glscopeinterface.h +++ b/sdrbase/dsp/glscopeinterface.h @@ -22,11 +22,15 @@ #include "glscopesettings.h" +class ScopeVis; + class GLScopeInterface { public: GLScopeInterface() {} virtual ~GLScopeInterface() {} + // Called by the ScopeVis this is attached to: set to null before being destroyed + virtual void setScopeVis(ScopeVis *scopeVis) { (void) scopeVis; } virtual void setTraces(std::vector* tracesData, std::vector* traces) = 0; virtual void newTraces(std::vector* traces, int traceIndex, std::vector* projectionTypes) = 0; virtual void setSampleRate(int sampleRate) = 0; diff --git a/sdrbase/dsp/scopevis.cpp b/sdrbase/dsp/scopevis.cpp index fc3c52082..d2e872a02 100644 --- a/sdrbase/dsp/scopevis.cpp +++ b/sdrbase/dsp/scopevis.cpp @@ -81,6 +81,10 @@ ScopeVis::ScopeVis() : ScopeVis::~ScopeVis() { + if (m_glScope) { + m_glScope->setScopeVis(nullptr); + } + disconnect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages())); for (std::vector::iterator it = m_triggerConditions.begin(); it != m_triggerConditions.end(); ++ it) { @@ -90,8 +94,19 @@ ScopeVis::~ScopeVis() void ScopeVis::setGLScope(GLScopeInterface* glScope) { + QMutexLocker configLocker(&m_mutex); + + if (m_glScope && (m_glScope != glScope)) { + m_glScope->setScopeVis(nullptr); + } + m_glScope = glScope; - m_glScope->setTraces(&m_traces.m_tracesData, &m_traces.m_traces[0]); + + if (m_glScope) + { + m_glScope->setScopeVis(this); + m_glScope->setTraces(&m_traces.m_tracesData, &m_traces.m_traces[0]); + } } void ScopeVis::setLiveRate(int sampleRate) diff --git a/sdrgui/gui/glscope.cpp b/sdrgui/gui/glscope.cpp index 8d7316b6e..8137d0aa7 100644 --- a/sdrgui/gui/glscope.cpp +++ b/sdrgui/gui/glscope.cpp @@ -26,6 +26,7 @@ #include #include +#include "dsp/scopevis.h" #include "glscope.h" const GLfloat GLScope::m_q3RadiiConst[] = { @@ -41,6 +42,7 @@ const GLfloat GLScope::m_q3RadiiConst[] = { GLScope::GLScope(QWidget *parent) : QOpenGLWidget(parent), + m_scopeVis(nullptr), m_tracesData(nullptr), m_traces(nullptr), m_projectionTypes(nullptr), @@ -96,6 +98,9 @@ GLScope::GLScope(QWidget *parent) : GLScope::~GLScope() { + if (m_scopeVis) { + m_scopeVis->setGLScope(nullptr); + } } void GLScope::setDisplayGridIntensity(int intensity) diff --git a/sdrgui/gui/glscope.h b/sdrgui/gui/glscope.h index e69eb208c..ba53f03a9 100644 --- a/sdrgui/gui/glscope.h +++ b/sdrgui/gui/glscope.h @@ -55,6 +55,7 @@ public: void connectTimer(const QTimer& timer); void disconnectTimer(); + virtual void setScopeVis(ScopeVis *scopeVis) override { m_scopeVis = scopeVis; } virtual void setTraces(std::vector* tracesData, std::vector* traces); virtual void newTraces(std::vector* traces, int traceIndex, std::vector* projectionTypes); @@ -139,6 +140,7 @@ private: QList m_markers1; QList m_markers2; + ScopeVis *m_scopeVis; //!< The ScopeVis feeding this, to be detached from before this is destroyed std::vector *m_tracesData; std::vector *m_traces; std::vector *m_projectionTypes; diff --git a/sdrgui/gui/glspectrumview.cpp b/sdrgui/gui/glspectrumview.cpp index 395e04257..4ce7bf2d8 100644 --- a/sdrgui/gui/glspectrumview.cpp +++ b/sdrgui/gui/glspectrumview.cpp @@ -1572,8 +1572,6 @@ void GLSpectrumView::paintGL() m_3DSpectrogramBufferPos = 0; - float prop_y = m_3DSpectrogramTexturePos / (m_3DSpectrogramTextureHeight - 1.0); - // Temporarily reduce viewport to waterfall area so anything outside is clipped if (window()->windowHandle()) { devicePixelRatio = window()->windowHandle()->devicePixelRatio();