1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-26 02:14:22 +00:00

VST: Use RAII initialisation for JUCE GUI and SharedResourcePointer for MessageThread

This commit is contained in:
ed 2021-04-02 14:37:17 +01:00
parent ab90229583
commit 33f37847bc
2 changed files with 73 additions and 53 deletions

View file

@ -185,13 +185,12 @@ namespace
return w;
}
static int numActivePlugins = 0;
static bool messageThreadIsDefinitelyCorrect = false;
}
#endif
static Array<void*> activePlugins;
//==============================================================================
// Ableton Live host specific commands
struct AbletonLiveHostSpecific
@ -316,44 +315,35 @@ public:
vstEffect.flags |= Vst2::effFlagsNoSoundInStop;
#endif
activePlugins.add (this);
#if JUCE_WINDOWS
++numActivePlugins;
#endif
}
~JuceVSTWrapper() override
{
JUCE_AUTORELEASEPOOL
{
{
#if JUCE_LINUX || JUCE_BSD
MessageManagerLock mmLock;
#endif
stopTimer();
deleteEditor (false);
#if JUCE_LINUX || JUCE_BSD
MessageManagerLock mmLock;
#endif
hasShutdown = true;
stopTimer();
deleteEditor (false);
delete processor;
processor = nullptr;
hasShutdown = true;
jassert (editorComp == nullptr);
delete processor;
processor = nullptr;
deleteTempChannels();
jassert (editorComp == nullptr);
jassert (activePlugins.contains (this));
activePlugins.removeFirstMatchingValue (this);
}
deleteTempChannels();
if (activePlugins.size() == 0)
{
#if JUCE_LINUX || JUCE_BSD
SharedMessageThread::deleteInstance();
#endif
shutdownJuce_GUI();
#if JUCE_WINDOWS
#if JUCE_WINDOWS
if (--numActivePlugins == 0)
messageThreadIsDefinitelyCorrect = false;
#endif
}
#endif
}
}
@ -394,8 +384,6 @@ public:
const int numMidiEventsComingIn = midiEvents.getNumEvents();
#endif
jassert (activePlugins.contains (this));
{
const int numIn = processor->getTotalNumInputChannels();
const int numOut = processor->getTotalNumOutputChannels();
@ -2044,6 +2032,12 @@ private:
}
//==============================================================================
ScopedJuceInitialiser_GUI libraryInitialiser;
#if JUCE_LINUX || JUCE_BSD
SharedResourcePointer<MessageThread> messageThread;
#endif
Vst2::audioMasterCallback hostCallback;
AudioProcessor* processor = {};
double sampleRate = 44100.0;
@ -2096,7 +2090,11 @@ namespace
{
JUCE_AUTORELEASEPOOL
{
initialiseJuce_GUI();
ScopedJuceInitialiser_GUI libraryInitialiser;
#if JUCE_LINUX || JUCE_BSD
SharedResourcePointer<MessageThread> messageThread;
#endif
try
{
@ -2164,7 +2162,6 @@ namespace
{
PluginHostType::jucePlugInClientCurrentWrapperType = AudioProcessor::wrapperType_VST;
SharedMessageThread::getInstance();
return pluginEntryPoint (audioMaster);
}

View file

@ -25,47 +25,70 @@
#if JUCE_LINUX || JUCE_BSD
#include <thread>
namespace juce
{
struct SharedMessageThread : public Thread
class MessageThread
{
SharedMessageThread() : Thread ("VstMessageThread")
public:
MessageThread()
{
startThread (7);
while (! initialised)
sleep (1);
startThread();
}
~SharedMessageThread() override
~MessageThread()
{
signalThreadShouldExit();
JUCEApplicationBase::quit();
waitForThreadToExit (5000);
clearSingletonInstance();
MessageManager::getInstance()->stopDispatchLoop();
stopThread();
}
void run() override
void startThread()
{
initialiseJuce_GUI();
initialised = true;
shouldExit = false;
MessageManager::getInstance()->setCurrentThreadAsMessageThread();
thread = std::thread { [this]
{
Thread::setCurrentThreadPriority (7);
Thread::setCurrentThreadName ("JUCE Plugin Message Thread");
XWindowSystem::getInstance();
MessageManager::getInstance()->setCurrentThreadAsMessageThread();
XWindowSystem::getInstance();
while ((! threadShouldExit()) && MessageManager::getInstance()->runDispatchLoopUntil (250))
{}
threadInitialised.signal();
for (;;)
{
if (shouldExit
|| ! MessageManager::getInstance()->runDispatchLoopUntil (250))
{
break;
}
}
} };
threadInitialised.wait();
}
JUCE_DECLARE_SINGLETON (SharedMessageThread, false)
void stopThread()
{
shouldExit = true;
thread.join();
}
bool initialised = false;
bool isThreadRunning() const noexcept { return thread.joinable(); }
private:
WaitableEvent threadInitialised;
std::thread thread;
std::atomic<bool> shouldExit { false };
JUCE_DECLARE_NON_MOVEABLE (MessageThread)
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MessageThread)
};
JUCE_IMPLEMENT_SINGLETON (SharedMessageThread)
} // namespace juce
#endif