Browse Source

Detach channels from the DSP engines and the scope before they are deleted

Removing a channel's sink or source from a device engine was a queued
message, so a channel could be deleted while the engine thread was
still feeding it. When the caller is deleting, the removal now runs on
the engine thread and is waited for, provided that thread is running
and is not the caller. The MIMO engine gains the same deleting flag and
does the same for its MIMO channels.

ScopeVis and GLScope now attach and detach under the vis mutex and each
clears the other on destruction, as SpectrumVis and GLSpectrum already
do, so a scope window closing while its channel is still running no
longer leaves a dangling pointer on either side.

Also drops an unused variable in GLSpectrumView::paintGL.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
chirpmodem-v8.0.0
Jon Beniston 2 months ago
parent
commit
d7623536cf
  1. 2
      sdrbase/device/deviceapi.cpp
  2. 49
      sdrbase/dsp/dspdevicemimoengine.cpp
  3. 2
      sdrbase/dsp/dspdevicemimoengine.h
  4. 21
      sdrbase/dsp/dspdevicesinkengine.cpp
  5. 23
      sdrbase/dsp/dspdevicesourceengine.cpp
  6. 4
      sdrbase/dsp/glscopeinterface.h
  7. 17
      sdrbase/dsp/scopevis.cpp
  8. 5
      sdrgui/gui/glscope.cpp
  9. 2
      sdrgui/gui/glscope.h
  10. 2
      sdrgui/gui/glspectrumview.cpp

2
sdrbase/device/deviceapi.cpp

@ -88,7 +88,7 @@ void DeviceAPI::removeChannelSink(BasebandSampleSink* sink, bool deleting, int s @@ -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);
}
}

49
sdrbase/dsp/dspdevicemimoengine.cpp

@ -17,6 +17,7 @@ @@ -17,6 +17,7 @@
///////////////////////////////////////////////////////////////////////////////////
#include <QDebug>
#include <QThread>
#include "dspcommands.h"
#include "basebandsamplesink.h"
@ -155,12 +156,33 @@ void DSPDeviceMIMOEngine::addChannelSource(BasebandSampleSource* source, int ind @@ -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) @@ -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) @@ -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);
}

2
sdrbase/dsp/dspdevicemimoengine.h

@ -232,7 +232,7 @@ public: @@ -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

21
sdrbase/dsp/dspdevicesinkengine.cpp

@ -101,9 +101,30 @@ void DSPDeviceSinkEngine::addChannelSource(BasebandSampleSource* source) @@ -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);
}

23
sdrbase/dsp/dspdevicesourceengine.cpp

@ -25,6 +25,7 @@ @@ -25,6 +25,7 @@
#include <dsp/devicesamplesource.h>
#include <stdio.h>
#include <QDebug>
#include <QThread>
#include "dsp/dspcommands.h"
#include "samplesinkfifo.h"
@ -109,9 +110,31 @@ void DSPDeviceSourceEngine::addSink(BasebandSampleSink* sink) @@ -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);
}

4
sdrbase/dsp/glscopeinterface.h

@ -22,11 +22,15 @@ @@ -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<GLScopeSettings::TraceData>* tracesData, std::vector<float *>* traces) = 0;
virtual void newTraces(std::vector<float *>* traces, int traceIndex, std::vector<Projector::ProjectionType>* projectionTypes) = 0;
virtual void setSampleRate(int sampleRate) = 0;

17
sdrbase/dsp/scopevis.cpp

@ -81,6 +81,10 @@ ScopeVis::ScopeVis() : @@ -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<TriggerCondition*>::iterator it = m_triggerConditions.begin(); it != m_triggerConditions.end(); ++ it) {
@ -90,8 +94,19 @@ ScopeVis::~ScopeVis() @@ -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)

5
sdrgui/gui/glscope.cpp

@ -26,6 +26,7 @@ @@ -26,6 +26,7 @@
#include <QSurface>
#include <algorithm>
#include "dsp/scopevis.h"
#include "glscope.h"
const GLfloat GLScope::m_q3RadiiConst[] = {
@ -41,6 +42,7 @@ 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) : @@ -96,6 +98,9 @@ GLScope::GLScope(QWidget *parent) :
GLScope::~GLScope()
{
if (m_scopeVis) {
m_scopeVis->setGLScope(nullptr);
}
}
void GLScope::setDisplayGridIntensity(int intensity)

2
sdrgui/gui/glscope.h

@ -55,6 +55,7 @@ public: @@ -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<GLScopeSettings::TraceData>* tracesData, std::vector<float *>* traces);
virtual void newTraces(std::vector<float *>* traces, int traceIndex, std::vector<Projector::ProjectionType>* projectionTypes);
@ -139,6 +140,7 @@ private: @@ -139,6 +140,7 @@ private:
QList<ScopeMarker> m_markers1;
QList<ScopeMarker> m_markers2;
ScopeVis *m_scopeVis; //!< The ScopeVis feeding this, to be detached from before this is destroyed
std::vector<GLScopeSettings::TraceData> *m_tracesData;
std::vector<float *> *m_traces;
std::vector<Projector::ProjectionType> *m_projectionTypes;

2
sdrgui/gui/glspectrumview.cpp

@ -1572,8 +1572,6 @@ void GLSpectrumView::paintGL() @@ -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();

Loading…
Cancel
Save