Add a little mutex just in case

Signed-off-by: falkTX <falktx@falktx.com>
This commit is contained in:
falkTX 2021-10-18 02:30:14 +01:00
parent e0e50db2bc
commit d11db41c43
No known key found for this signature in database
GPG key ID: CDBAA37ABC74FBA0

View file

@ -32,6 +32,7 @@
#include <osdialog.h> #include <osdialog.h>
#include "PluginContext.hpp" #include "PluginContext.hpp"
#include "extra/Mutex.hpp"
namespace rack { namespace rack {
namespace plugin { namespace plugin {
@ -123,6 +124,7 @@ class CardinalPlugin : public CardinalBasePlugin
// for base/context handling // for base/context handling
bool fIsActive; bool fIsActive;
rack::audio::Device* fCurrentDevice; rack::audio::Device* fCurrentDevice;
Mutex fDeviceMutex;
struct ScopedContext { struct ScopedContext {
ScopedContext(CardinalPlugin* const plugin) ScopedContext(CardinalPlugin* const plugin)
@ -201,6 +203,7 @@ protected:
bool canAssignDevice() const noexcept override bool canAssignDevice() const noexcept override
{ {
const MutexLocker cml(fDeviceMutex);
return fCurrentDevice == nullptr; return fCurrentDevice == nullptr;
} }
@ -208,11 +211,14 @@ protected:
{ {
DISTRHO_SAFE_ASSERT_RETURN(fCurrentDevice == nullptr,); DISTRHO_SAFE_ASSERT_RETURN(fCurrentDevice == nullptr,);
const MutexLocker cml(fDeviceMutex);
fCurrentDevice = dev; fCurrentDevice = dev;
} }
bool clearDevice(rack::audio::Device* const dev) noexcept override bool clearDevice(rack::audio::Device* const dev) noexcept override
{ {
const MutexLocker cml(fDeviceMutex);
if (fCurrentDevice != dev) if (fCurrentDevice != dev)
return false; return false;
@ -298,14 +304,22 @@ protected:
fAudioBufferOut = new float[bufferSize]; fAudioBufferOut = new float[bufferSize];
std::memset(fAudioBufferIn, 0, sizeof(float)*bufferSize); std::memset(fAudioBufferIn, 0, sizeof(float)*bufferSize);
{
const MutexLocker cml(fDeviceMutex);
if (fCurrentDevice != nullptr) if (fCurrentDevice != nullptr)
fCurrentDevice->onStartStream(); fCurrentDevice->onStartStream();
} }
}
void deactivate() override void deactivate() override
{ {
{
const MutexLocker cml(fDeviceMutex);
if (fCurrentDevice != nullptr) if (fCurrentDevice != nullptr)
fCurrentDevice->onStopStream(); fCurrentDevice->onStopStream();
}
delete[] fAudioBufferIn; delete[] fAudioBufferIn;
delete[] fAudioBufferOut; delete[] fAudioBufferOut;
@ -322,6 +336,8 @@ protected:
fContext->engine->stepBlock(frames); fContext->engine->stepBlock(frames);
*/ */
const MutexLocker cml(fDeviceMutex);
if (fCurrentDevice == nullptr) if (fCurrentDevice == nullptr)
{ {
if (outputs[0] != inputs[0]) if (outputs[0] != inputs[0])