Browse Source

Alternative, cleaner approach to JUCE run loop on carla-plugin

Fixes #1238

Signed-off-by: falkTX <falktx@falktx.com>
tags/v2.3.0-RC1
falkTX 4 years ago
parent
commit
7dc9e8fce4
Signed by: falkTX <falktx@falktx.com> GPG Key ID: CDBAA37ABC74FBA0
3 changed files with 37 additions and 94 deletions
  1. +34
    -47
      source/backend/engine/CarlaEngineNative.cpp
  2. +3
    -24
      source/plugin/carla-lv2.cpp
  3. +0
    -23
      source/plugin/carla-vst.cpp

+ 34
- 47
source/backend/engine/CarlaEngineNative.cpp View File

@@ -47,7 +47,7 @@
# endif # endif
# include "AppConfig.h" # include "AppConfig.h"
# include "juce_events/juce_events.h" # include "juce_events/juce_events.h"
# define USE_JUCE_MESSAGE_THREAD
# define USE_REFCOUNTER_JUCE_MESSAGE_MANAGER
# if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) # if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
# pragma GCC diagnostic pop # pragma GCC diagnostic pop
# endif # endif
@@ -74,33 +74,27 @@ static const uint16_t kUiHeight = 712;


// ----------------------------------------------------------------------- // -----------------------------------------------------------------------


#ifdef USE_JUCE_MESSAGE_THREAD
#ifdef USE_REFCOUNTER_JUCE_MESSAGE_MANAGER
static int numScopedInitInstances = 0; static int numScopedInitInstances = 0;


class SharedJuceMessageThread : public juce::Thread
struct ReferenceCountedJuceMessageMessager
{ {
public:
SharedJuceMessageThread()
: juce::Thread ("SharedJuceMessageThread"),
initialised (false) {}

~SharedJuceMessageThread()
ReferenceCountedJuceMessageMessager()
{ {
CARLA_SAFE_ASSERT(numScopedInitInstances == 0); CARLA_SAFE_ASSERT(numScopedInitInstances == 0);
}


// in case something fails
juce::MessageManager::getInstance()->stopDispatchLoop();
waitForThreadToExit (5000);
~ReferenceCountedJuceMessageMessager()
{
CARLA_SAFE_ASSERT(numScopedInitInstances == 0);
} }


void incRef() void incRef()
{ {
if (numScopedInitInstances++ == 0) if (numScopedInitInstances++ == 0)
{ {
startThread (7);

while (! initialised)
juce::Thread::sleep (1);
juce::initialiseJuce_GUI();
juce::MessageManager::getInstance()->setCurrentThreadAsMessageThread();
} }
} }


@@ -108,26 +102,9 @@ public:
{ {
if (--numScopedInitInstances == 0) if (--numScopedInitInstances == 0)
{ {
juce::MessageManager::getInstance()->stopDispatchLoop();
waitForThreadToExit (5000);
juce::shutdownJuce_GUI();
} }
} }

protected:
void run() override
{
const juce::ScopedJuceInitialiser_GUI juceInitialiser;

juce::MessageManager::getInstance()->setCurrentThreadAsMessageThread();
initialised = true;

juce::MessageManager::getInstance()->runDispatchLoop();
}

private:
volatile bool initialised;

CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SharedJuceMessageThread);
}; };
#endif #endif


@@ -172,11 +149,11 @@ public:
const uint32_t cvIns = 0, const uint32_t cvOuts = 0) const uint32_t cvIns = 0, const uint32_t cvOuts = 0)
: CarlaEngine(), : CarlaEngine(),
pHost(host), pHost(host),
#ifdef USE_JUCE_MESSAGE_THREAD
#ifdef USE_REFCOUNTER_JUCE_MESSAGE_MANAGER
// if not running inside Carla, we will have to run event loop ourselves // if not running inside Carla, we will have to run event loop ourselves
kNeedsJuceMsgThread(host->dispatcher(pHost->handle,
NATIVE_HOST_OPCODE_INTERNAL_PLUGIN, 0, 0, nullptr, 0.0f) == 0),
fJuceMsgThread(),
kNeedsJuceEvents(host->dispatcher(pHost->handle,
NATIVE_HOST_OPCODE_INTERNAL_PLUGIN, 0, 0, nullptr, 0.0f) == 0),
fJuceMsgMgr(),
#endif #endif
kIsPatchbay(isPatchbay), kIsPatchbay(isPatchbay),
kHasMidiOut(withMidiOut), kHasMidiOut(withMidiOut),
@@ -192,9 +169,9 @@ public:


carla_zeroFloats(fParameters, kNumInParams+kNumOutParams); carla_zeroFloats(fParameters, kNumInParams+kNumOutParams);


#ifdef USE_JUCE_MESSAGE_THREAD
if (kNeedsJuceMsgThread)
fJuceMsgThread->incRef();
#ifdef USE_REFCOUNTER_JUCE_MESSAGE_MANAGER
if (kNeedsJuceEvents)
fJuceMsgMgr->incRef();
#endif #endif


pData->bufferSize = pHost->get_buffer_size(pHost->handle); pData->bufferSize = pHost->get_buffer_size(pHost->handle);
@@ -258,9 +235,9 @@ public:


pData->graph.destroy(); pData->graph.destroy();


#ifdef USE_JUCE_MESSAGE_THREAD
if (kNeedsJuceMsgThread)
fJuceMsgThread->decRef();
#ifdef USE_REFCOUNTER_JUCE_MESSAGE_MANAGER
if (kNeedsJuceEvents)
fJuceMsgMgr->decRef();
#endif #endif


carla_debug("CarlaEngineNative::~CarlaEngineNative() - END"); carla_debug("CarlaEngineNative::~CarlaEngineNative() - END");
@@ -1342,6 +1319,16 @@ protected:


void uiIdle() void uiIdle()
{ {
#ifdef USE_REFCOUNTER_JUCE_MESSAGE_MANAGER
if (kNeedsJuceEvents)
{
const juce::MessageManagerLock mml;

if (const juce::MessageManager* const msgMgr = juce::MessageManager::getInstanceWithoutCreating())
for (; msgMgr->dispatchNextMessageOnSystemQueue(true);) {}
}
#endif

for (uint i=0; i < pData->curPluginCount; ++i) for (uint i=0; i < pData->curPluginCount; ++i)
{ {
if (const CarlaPluginPtr plugin = pData->plugins[i].plugin) if (const CarlaPluginPtr plugin = pData->plugins[i].plugin)
@@ -1707,9 +1694,9 @@ public:
private: private:
const NativeHostDescriptor* const pHost; const NativeHostDescriptor* const pHost;


#ifdef USE_JUCE_MESSAGE_THREAD
const bool kNeedsJuceMsgThread;
const juce::SharedResourcePointer<SharedJuceMessageThread> fJuceMsgThread;
#ifdef USE_REFCOUNTER_JUCE_MESSAGE_MANAGER
const bool kNeedsJuceEvents;
const juce::SharedResourcePointer<ReferenceCountedJuceMessageMessager> fJuceMsgMgr;
#endif #endif


const bool kIsPatchbay; // rack if false const bool kIsPatchbay; // rack if false


+ 3
- 24
source/plugin/carla-lv2.cpp View File

@@ -23,22 +23,6 @@
#include "CarlaPipeUtils.hpp" #include "CarlaPipeUtils.hpp"
#include "CarlaString.hpp" #include "CarlaString.hpp"


#if defined(USING_JUCE) && (defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN))
# if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wconversion"
# pragma GCC diagnostic ignored "-Weffc++"
# pragma GCC diagnostic ignored "-Wsign-conversion"
# pragma GCC diagnostic ignored "-Wundef"
# pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
# endif
# include "AppConfig.h"
# include "juce_events/juce_events.h"
# if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
# pragma GCC diagnostic pop
# endif
#endif

#include "water/files/File.h" #include "water/files/File.h"


static const char* const kPathForCarlaFiles = "carlafiles"; static const char* const kPathForCarlaFiles = "carlafiles";
@@ -71,9 +55,6 @@ public:
#endif #endif
kIgnoreParameters(std::strncmp(desc->label, "carla", 5) == 0), kIgnoreParameters(std::strncmp(desc->label, "carla", 5) == 0),
fMidiEventCount(0), fMidiEventCount(0),
#if defined(USING_JUCE) && (defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN))
fJuceInitialiser(),
#endif
fLastProjectPath(), fLastProjectPath(),
fLoadedFile(), fLoadedFile(),
fWorkerUISignal(0) fWorkerUISignal(0)
@@ -852,12 +833,14 @@ protected:
case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS: case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
case NATIVE_HOST_OPCODE_RELOAD_ALL: case NATIVE_HOST_OPCODE_RELOAD_ALL:
case NATIVE_HOST_OPCODE_HOST_IDLE: case NATIVE_HOST_OPCODE_HOST_IDLE:
case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
case NATIVE_HOST_OPCODE_QUEUE_INLINE_DISPLAY: case NATIVE_HOST_OPCODE_QUEUE_INLINE_DISPLAY:
case NATIVE_HOST_OPCODE_REQUEST_IDLE: case NATIVE_HOST_OPCODE_REQUEST_IDLE:
// nothing // nothing
break; break;


case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
return 1;

case NATIVE_HOST_OPCODE_GET_FILE_PATH: case NATIVE_HOST_OPCODE_GET_FILE_PATH:
CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0); CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
if (std::strcmp((char*)ptr, "carla") == 0 && fLastProjectPath != nullptr) if (std::strcmp((char*)ptr, "carla") == 0 && fLastProjectPath != nullptr)
@@ -920,10 +903,6 @@ private:
uint32_t fMidiEventCount; uint32_t fMidiEventCount;
NativeMidiEvent fMidiEvents[kMaxMidiEvents]; NativeMidiEvent fMidiEvents[kMaxMidiEvents];


#if defined(USING_JUCE) && (defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN))
juce::SharedResourcePointer<juce::ScopedJuceInitialiser_GUI> fJuceInitialiser;
#endif

CarlaString fLastProjectPath; CarlaString fLastProjectPath;
CarlaString fLoadedFile; CarlaString fLoadedFile;




+ 0
- 23
source/plugin/carla-vst.cpp View File

@@ -42,22 +42,6 @@
#include "CarlaMathUtils.hpp" #include "CarlaMathUtils.hpp"
#include "CarlaVstUtils.hpp" #include "CarlaVstUtils.hpp"


#if defined(USING_JUCE) && (defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN))
# if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wconversion"
# pragma GCC diagnostic ignored "-Weffc++"
# pragma GCC diagnostic ignored "-Wsign-conversion"
# pragma GCC diagnostic ignored "-Wundef"
# pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
# endif
# include "AppConfig.h"
# include "juce_events/juce_events.h"
# if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
# pragma GCC diagnostic pop
# endif
#endif

static uint32_t d_lastBufferSize = 0; static uint32_t d_lastBufferSize = 0;
static double d_lastSampleRate = 0.0; static double d_lastSampleRate = 0.0;


@@ -98,9 +82,6 @@ public:
fUiLauncher(nullptr), fUiLauncher(nullptr),
fHostType(kHostTypeNull), fHostType(kHostTypeNull),
fMidiOutEvents(), fMidiOutEvents(),
#if defined(USING_JUCE) && (defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN))
fJuceInitialiser(),
#endif
fStateChunk(nullptr) fStateChunk(nullptr)
{ {
fHost.handle = this; fHost.handle = this;
@@ -880,10 +861,6 @@ private:
CARLA_DECLARE_NON_COPY_STRUCT(FixedVstEvents); CARLA_DECLARE_NON_COPY_STRUCT(FixedVstEvents);
} fMidiOutEvents; } fMidiOutEvents;


#if defined(USING_JUCE) && (defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN))
juce::SharedResourcePointer<juce::ScopedJuceInitialiser_GUI> fJuceInitialiser;
#endif

char* fStateChunk; char* fStateChunk;


// ------------------------------------------------------------------- // -------------------------------------------------------------------


Loading…
Cancel
Save