Browse Source

Test scoped action lock with a wait condition

tags/1.9.8
Filipe Coelho 7 years ago
parent
commit
1436d9d70b
2 changed files with 59 additions and 18 deletions
  1. +53
    -16
      source/backend/engine/CarlaEngineInternal.cpp
  2. +6
    -2
      source/backend/engine/CarlaEngineInternal.hpp

+ 53
- 16
source/backend/engine/CarlaEngineInternal.cpp View File

@@ -369,26 +369,45 @@ EngineNextAction::EngineNextAction() noexcept
: opcode(kEnginePostActionNull), : opcode(kEnginePostActionNull),
pluginId(0), pluginId(0),
value(0), value(0),
mutex(false) {}
condition(),
mutex(),
triggered(false)
{
pthread_condattr_t cattr;
pthread_condattr_init(&cattr);
pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_PRIVATE);
pthread_cond_init(&condition, &cattr);
pthread_condattr_destroy(&cattr);

pthread_mutexattr_t mattr;
pthread_mutexattr_init(&mattr);
pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
pthread_mutex_init(&mutex, &mattr);
pthread_mutexattr_destroy(&mattr);
}


EngineNextAction::~EngineNextAction() noexcept EngineNextAction::~EngineNextAction() noexcept
{ {
CARLA_SAFE_ASSERT(opcode == kEnginePostActionNull); CARLA_SAFE_ASSERT(opcode == kEnginePostActionNull);
}


void EngineNextAction::ready() const noexcept
{
mutex.lock();
mutex.unlock();
pthread_cond_destroy(&condition);
pthread_mutex_destroy(&mutex);
} }


// void EngineNextAction::ready() const noexcept
// {
// mutex.lock();
// mutex.unlock();
// }

void EngineNextAction::clearAndReset() noexcept void EngineNextAction::clearAndReset() noexcept
{ {
mutex.lock();
pthread_mutex_lock(&mutex);
opcode = kEnginePostActionNull; opcode = kEnginePostActionNull;
pluginId = 0; pluginId = 0;
value = 0; value = 0;
mutex.unlock();
pthread_mutex_unlock(&mutex);
} }


// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
@@ -514,7 +533,7 @@ bool CarlaEngine::ProtectedData::init(const char* const clientName)
carla_zeroStructs(plugins, maxPluginNumber); carla_zeroStructs(plugins, maxPluginNumber);
#endif #endif


nextAction.ready();
nextAction.clearAndReset();
thread.startThread(); thread.startThread();


return true; return true;
@@ -532,7 +551,7 @@ void CarlaEngine::ProtectedData::close()
aboutToClose = true; aboutToClose = true;


thread.stopThread(500); thread.stopThread(500);
nextAction.ready();
nextAction.clearAndReset();


#ifdef HAVE_LIBLO #ifdef HAVE_LIBLO
osc.close(); osc.close();
@@ -654,8 +673,15 @@ void CarlaEngine::ProtectedData::doNextPluginAction(const bool unlock) noexcept


if (unlock) if (unlock)
{ {
nextAction.mutex.tryLock();
nextAction.mutex.unlock();
pthread_mutex_lock(&nextAction.mutex);

if (! nextAction.triggered)
{
nextAction.triggered = true;
pthread_cond_broadcast(&nextAction.condition);
}

pthread_mutex_unlock(&nextAction.mutex);
} }
} }


@@ -681,7 +707,7 @@ ScopedActionLock::ScopedActionLock(CarlaEngine* const engine, const EnginePostAc
{ {
CARLA_SAFE_ASSERT_RETURN(action != kEnginePostActionNull,); CARLA_SAFE_ASSERT_RETURN(action != kEnginePostActionNull,);


pData->nextAction.mutex.lock();
pthread_mutex_lock(&pData->nextAction.mutex);


CARLA_SAFE_ASSERT_RETURN(pData->nextAction.opcode == kEnginePostActionNull,); CARLA_SAFE_ASSERT_RETURN(pData->nextAction.opcode == kEnginePostActionNull,);


@@ -693,20 +719,31 @@ ScopedActionLock::ScopedActionLock(CarlaEngine* const engine, const EnginePostAc
{ {
// block wait for unlock on processing side // block wait for unlock on processing side
carla_stdout("ScopedPluginAction(%i) - blocking START", pluginId); carla_stdout("ScopedPluginAction(%i) - blocking START", pluginId);
pData->nextAction.mutex.lock();

while (! pData->nextAction.triggered)
{
try {
pthread_cond_wait(&pData->nextAction.condition, &pData->nextAction.mutex);
} CARLA_SAFE_EXCEPTION("pthread_cond_wait");
}

pData->nextAction.triggered = false;

carla_stdout("ScopedPluginAction(%i) - blocking DONE", pluginId); carla_stdout("ScopedPluginAction(%i) - blocking DONE", pluginId);
} }
else else
{ {
pData->doNextPluginAction(false); pData->doNextPluginAction(false);
} }

pthread_mutex_unlock(&pData->nextAction.mutex);
} }


ScopedActionLock::~ScopedActionLock() noexcept ScopedActionLock::~ScopedActionLock() noexcept
{ {
pthread_mutex_lock(&pData->nextAction.mutex);
CARLA_SAFE_ASSERT(pData->nextAction.opcode == kEnginePostActionNull); CARLA_SAFE_ASSERT(pData->nextAction.opcode == kEnginePostActionNull);
pData->nextAction.mutex.tryLock();
pData->nextAction.mutex.unlock();
pthread_mutex_unlock(&pData->nextAction.mutex);
} }


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


+ 6
- 2
source/backend/engine/CarlaEngineInternal.hpp View File

@@ -176,11 +176,15 @@ struct EngineNextAction {
EnginePostAction opcode; EnginePostAction opcode;
uint pluginId; uint pluginId;
uint value; uint value;
CarlaMutex mutex;

// reused from CarlaSignal class
pthread_cond_t condition;
pthread_mutex_t mutex;
volatile bool triggered;


EngineNextAction() noexcept; EngineNextAction() noexcept;
~EngineNextAction() noexcept; ~EngineNextAction() noexcept;
void ready() const noexcept;
// void ready() const noexcept;
void clearAndReset() noexcept; void clearAndReset() noexcept;


CARLA_DECLARE_NON_COPY_STRUCT(EngineNextAction) CARLA_DECLARE_NON_COPY_STRUCT(EngineNextAction)


Loading…
Cancel
Save