Browse Source

Merge pull request #2972 from hxcrd/pluto-fixes

PlutoSDR: fix four crashes with Rx + Tx device sets on one device (#2721)
chirpmodem-v8.0.0
Edouard Griffiths 2 months ago committed by GitHub
parent
commit
e345b9bc3b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 1
      devices/plutosdr/deviceplutosdrshared.cpp
  2. 11
      devices/plutosdr/deviceplutosdrshared.h
  3. 44
      plugins/samplesink/plutosdroutput/plutosdroutput.cpp
  4. 35
      plugins/samplesource/plutosdrinput/plutosdrinput.cpp
  5. 6
      sdrgui/mainwindow.cpp

1
devices/plutosdr/deviceplutosdrshared.cpp

@ -22,3 +22,4 @@ @@ -22,3 +22,4 @@
MESSAGE_CLASS_DEFINITION(DevicePlutoSDRShared::MsgCrossReportToBuddy, Message)
const unsigned int DevicePlutoSDRShared::m_sampleFifoMinRate = 48000;
QRecursiveMutex DevicePlutoSDRShared::m_threadsMutex;

11
devices/plutosdr/deviceplutosdrshared.h

@ -22,6 +22,8 @@ @@ -22,6 +22,8 @@
#include <stdint.h>
#include <QRecursiveMutex>
#include "util/message.h"
#include "export.h"
@ -94,6 +96,15 @@ public: @@ -94,6 +96,15 @@ public:
static const unsigned int m_sampleFifoMinRate;
/**
* Serializes every access to m_thread across all PlutoSDR device sets.
* A buddy suspends and resumes this device's sample thread through m_thread, from another
* thread and without holding this device's own mutex. Without a common lock it can restart
* a thread that stop() is in the middle of tearing down (QThread: Destroyed while thread is
* still running) or call through a pointer that has just been deleted.
*/
static QRecursiveMutex m_threadsMutex;
DevicePlutoSDRShared() :
m_deviceParams(0),
m_thread(0),

44
plugins/samplesink/plutosdroutput/plutosdroutput.cpp

@ -108,6 +108,11 @@ bool PlutoSDROutput::start() @@ -108,6 +108,11 @@ bool PlutoSDROutput::start()
return true;
}
if (!m_deviceShared.m_deviceParams)
{
qCritical("PlutoSDROutput::start: device not open");
return false;
}
if (!m_deviceShared.m_deviceParams->getBox())
{
qCritical("PlutoSDROutput::start: device not open");
@ -121,8 +126,11 @@ bool PlutoSDROutput::start() @@ -121,8 +126,11 @@ bool PlutoSDROutput::start()
qDebug("PlutoSDROutput::start: thread created");
m_plutoSDROutputThread->setLog2Interpolation(m_settings.m_log2Interp);
m_plutoSDROutputThread->startWork();
m_deviceShared.m_thread = m_plutoSDROutputThread;
{
QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex);
m_plutoSDROutputThread->startWork();
m_deviceShared.m_thread = m_plutoSDROutputThread;
}
m_running = true;
mutexLocker.unlock();
@ -141,14 +149,20 @@ void PlutoSDROutput::stop() @@ -141,14 +149,20 @@ void PlutoSDROutput::stop()
m_running = false;
if (m_plutoSDROutputThread != 0)
// Unpublish the thread before tearing it down, and keep the shared lock through
// stop/wait/delete. applySettings() holds the same lock across its suspend ->
// apply -> resume transaction, so teardown must not interleave with that sequence.
QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex);
m_deviceShared.m_thread = nullptr;
m_deviceShared.m_threadWasRunning = false;
if (m_plutoSDROutputThread)
{
m_plutoSDROutputThread->stopWork();
m_plutoSDROutputThread->wait();
delete m_plutoSDROutputThread;
m_plutoSDROutputThread = 0;
m_plutoSDROutputThread = nullptr;
}
m_deviceShared.m_thread = 0;
}
void PlutoSDROutput::handleError(int errorCode)
@ -156,6 +170,9 @@ void PlutoSDROutput::handleError(int errorCode) @@ -156,6 +170,9 @@ void PlutoSDROutput::handleError(int errorCode)
QMutexLocker mutexLocker(&m_mutex);
m_running = false;
QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex);
m_deviceShared.m_thread = nullptr;
m_deviceShared.m_threadWasRunning = false;
if (m_plutoSDROutputThread)
{
if (m_plutoSDROutputThread->isRunning())
@ -166,7 +183,6 @@ void PlutoSDROutput::handleError(int errorCode) @@ -166,7 +183,6 @@ void PlutoSDROutput::handleError(int errorCode)
delete m_plutoSDROutputThread;
m_plutoSDROutputThread = nullptr;
}
m_deviceShared.m_thread = 0;
const QString errorMessage =
tr("Radio needs a restart: %1 (%2)")
@ -395,6 +411,7 @@ void PlutoSDROutput::closeDevice() @@ -395,6 +411,7 @@ void PlutoSDROutput::closeDevice()
void PlutoSDROutput::suspendBuddies()
{
QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex);
// suspend Rx buddy's thread
for (unsigned int i = 0; i < m_deviceAPI->getSourceBuddies().size(); i++)
@ -410,6 +427,7 @@ void PlutoSDROutput::suspendBuddies() @@ -410,6 +427,7 @@ void PlutoSDROutput::suspendBuddies()
void PlutoSDROutput::resumeBuddies()
{
QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex);
// resume Rx buddy's thread
for (unsigned int i = 0; i < m_deviceAPI->getSourceBuddies().size(); i++)
@ -433,6 +451,10 @@ bool PlutoSDROutput::applySettings(const PlutoSDROutputSettings& settings, const @@ -433,6 +451,10 @@ bool PlutoSDROutput::applySettings(const PlutoSDROutputSettings& settings, const
qDebug().noquote() << "PlutoSDROutput::applySettings: force:" << force << settings.getDebugString(settingsKeys, force);
// Held for the whole suspend -> apply -> resume sequence below (own thread and buddies'),
// so that a concurrent stop()/start() of either device set cannot interleave with it.
QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex);
bool forwardChangeOwnDSP = false;
bool forwardChangeOtherDSP = false;
bool ownThreadWasRunning = false;
@ -610,13 +632,17 @@ bool PlutoSDROutput::applySettings(const PlutoSDROutputSettings& settings, const @@ -610,13 +632,17 @@ bool PlutoSDROutput::applySettings(const PlutoSDROutputSettings& settings, const
{
DevicePlutoSDRShared *buddySharedPtr = (DevicePlutoSDRShared *) (*itSource)->getBuddySharedPtr();
if (buddySharedPtr && buddySharedPtr->m_threadWasRunning) {
// m_threadWasRunning is sticky state on the buddy's shared struct, but the
// buddy's thread can be gone by the time we resume (its device was stopped,
// or a preset was loaded into it), which nulls m_thread. The matching
// suspend block above already tests m_thread; test it here too.
if (buddySharedPtr && buddySharedPtr->m_threadWasRunning && buddySharedPtr->m_thread) {
buddySharedPtr->m_thread->startWork();
}
}
}
if (ownThreadWasRunning) {
if (ownThreadWasRunning && m_plutoSDROutputThread) {
m_plutoSDROutputThread->startWork();
}

35
plugins/samplesource/plutosdrinput/plutosdrinput.cpp

@ -127,8 +127,11 @@ bool PlutoSDRInput::start() @@ -127,8 +127,11 @@ bool PlutoSDRInput::start()
m_plutoSDRInputThread->setLog2Decimation(m_settings.m_log2Decim);
m_plutoSDRInputThread->setIQOrder(m_settings.m_iqOrder);
m_plutoSDRInputThread->startWork();
m_deviceShared.m_thread = m_plutoSDRInputThread;
{
QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex);
m_plutoSDRInputThread->startWork();
m_deviceShared.m_thread = m_plutoSDRInputThread;
}
m_running = true;
mutexLocker.unlock();
@ -147,14 +150,20 @@ void PlutoSDRInput::stop() @@ -147,14 +150,20 @@ void PlutoSDRInput::stop()
m_running = false;
// Unpublish the thread before tearing it down, and keep the shared lock through
// stop/wait/delete. applySettings() holds the same lock across its suspend ->
// apply -> resume transaction, so teardown must not interleave with that sequence.
QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex);
m_deviceShared.m_thread = nullptr;
m_deviceShared.m_threadWasRunning = false;
if (m_plutoSDRInputThread)
{
m_plutoSDRInputThread->stopWork();
m_plutoSDRInputThread->wait();
delete m_plutoSDRInputThread;
m_plutoSDRInputThread = nullptr;
}
m_deviceShared.m_thread = nullptr;
}
void PlutoSDRInput::handleError(int errorCode)
@ -162,6 +171,9 @@ void PlutoSDRInput::handleError(int errorCode) @@ -162,6 +171,9 @@ void PlutoSDRInput::handleError(int errorCode)
QMutexLocker mutexLocker(&m_mutex);
m_running = false;
QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex);
m_deviceShared.m_thread = nullptr;
m_deviceShared.m_threadWasRunning = false;
if (m_plutoSDRInputThread)
{
if (m_plutoSDRInputThread->isRunning())
@ -172,7 +184,6 @@ void PlutoSDRInput::handleError(int errorCode) @@ -172,7 +184,6 @@ void PlutoSDRInput::handleError(int errorCode)
delete m_plutoSDRInputThread;
m_plutoSDRInputThread = nullptr;
}
m_deviceShared.m_thread = nullptr;
if (m_deviceAPI->getSinkBuddies().size() == 0)
{
@ -419,6 +430,7 @@ void PlutoSDRInput::closeDevice() @@ -419,6 +430,7 @@ void PlutoSDRInput::closeDevice()
void PlutoSDRInput::suspendBuddies()
{
QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex);
// suspend Tx buddy's thread
for (unsigned int i = 0; i < m_deviceAPI->getSinkBuddies().size(); i++)
@ -434,6 +446,7 @@ void PlutoSDRInput::suspendBuddies() @@ -434,6 +446,7 @@ void PlutoSDRInput::suspendBuddies()
void PlutoSDRInput::resumeBuddies()
{
QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex);
// resume Tx buddy's thread
for (unsigned int i = 0; i < m_deviceAPI->getSinkBuddies().size(); i++)
@ -455,6 +468,10 @@ bool PlutoSDRInput::applySettings(const PlutoSDRInputSettings& settings, const Q @@ -455,6 +468,10 @@ bool PlutoSDRInput::applySettings(const PlutoSDRInputSettings& settings, const Q
return false;
}
// Held for the whole suspend -> apply -> resume sequence below (own thread and buddies'),
// so that a concurrent stop()/start() of either device set cannot interleave with it.
QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex);
bool forwardChangeOwnDSP = false;
bool forwardChangeOtherDSP = false;
bool ownThreadWasRunning = false;
@ -677,13 +694,17 @@ bool PlutoSDRInput::applySettings(const PlutoSDRInputSettings& settings, const Q @@ -677,13 +694,17 @@ bool PlutoSDRInput::applySettings(const PlutoSDRInputSettings& settings, const Q
{
DevicePlutoSDRShared *buddySharedPtr = (DevicePlutoSDRShared *) (*itSink)->getBuddySharedPtr();
if (buddySharedPtr && buddySharedPtr->m_threadWasRunning) {
// m_threadWasRunning is sticky state on the buddy's shared struct, but the
// buddy's thread can be gone by the time we resume (its device was stopped,
// or a preset was loaded into it), which nulls m_thread. The matching
// suspend block above already tests m_thread; test it here too.
if (buddySharedPtr && buddySharedPtr->m_threadWasRunning && buddySharedPtr->m_thread) {
buddySharedPtr->m_thread->startWork();
}
}
}
if (ownThreadWasRunning) {
if (ownThreadWasRunning && m_plutoSDRInputThread) {
m_plutoSDRInputThread->startWork();
}

6
sdrgui/mainwindow.cpp

@ -805,9 +805,7 @@ void RemoveDeviceSetFSM::removeUI() @@ -805,9 +805,7 @@ void RemoveDeviceSetFSM::removeUI()
QObject::disconnect(m_deviceUISet->m_deviceAPI, nullptr, deviceGUI, nullptr);
delete deviceGUI;
m_deviceUISet->m_deviceAPI->resetSamplingDeviceId();
if (!m_deviceMIMOEngine) {
m_deviceUISet->m_deviceAPI->clearBuddiesLists(); // clear old API buddies lists
}
// Buddy lists are cleared in removeDeviceSet(), after the sample source/sink is deleted (#2721).
}
void RemoveDeviceSetFSM::stopEngine()
@ -848,11 +846,13 @@ void RemoveDeviceSetFSM::removeDeviceSet() @@ -848,11 +846,13 @@ void RemoveDeviceSetFSM::removeDeviceSet()
if (m_deviceSourceEngine)
{
delete deviceAPI->getSampleSource();
deviceAPI->clearBuddiesLists(); // after the delete, so closeDevice() still saw its buddies (#2721)
delete m_deviceSourceEngine;
}
else if (m_deviceSinkEngine)
{
delete deviceAPI->getSampleSink();
deviceAPI->clearBuddiesLists(); // after the delete, so closeDevice() still saw its buddies (#2721)
delete m_deviceSinkEngine;
}
else

Loading…
Cancel
Save