From 92d930a17efcf74c83419b13e3ee367877f8efaf Mon Sep 17 00:00:00 2001 From: dimitri Date: Mon, 18 Mar 2019 16:01:46 +0000 Subject: [PATCH] BLOCKS: Make MIDI port listener thread safe --- .../internal/juce_BlockImplementation.cpp | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/modules/juce_blocks_basics/topology/internal/juce_BlockImplementation.cpp b/modules/juce_blocks_basics/topology/internal/juce_BlockImplementation.cpp index a49612fa3b..7eda9cfb2a 100644 --- a/modules/juce_blocks_basics/topology/internal/juce_BlockImplementation.cpp +++ b/modules/juce_blocks_basics/topology/internal/juce_BlockImplementation.cpp @@ -422,12 +422,41 @@ public: lastMessageReceiveTime = Time::getCurrentTime(); } + MIDIDeviceConnection* getDeviceConnection() + { + return dynamic_cast (detector->getDeviceConnectionFor (*this)); + } + void addDataInputPortListener (DataInputPortListener* listener) override { - Block::addDataInputPortListener (listener); + if (auto deviceConnection = getDeviceConnection()) + { + { + ScopedLock scopedLock (deviceConnection->criticalSecton); + Block::addDataInputPortListener (listener); + } - if (auto midiInput = getMidiInput()) - midiInput->start(); + deviceConnection->midiInput->start(); + } + else + { + Block::addDataInputPortListener (listener); + } + } + + void removeDataInputPortListener (DataInputPortListener* listener) override + { + if (auto deviceConnection = getDeviceConnection()) + { + { + ScopedLock scopedLock (deviceConnection->criticalSecton); + Block::removeDataInputPortListener (listener); + } + } + else + { + Block::removeDataInputPortListener (listener); + } } void sendMessage (const void* message, size_t messageSize) override