|
|
|
@@ -22,6 +22,8 @@ |
|
|
|
|
|
|
|
#include <pthread.h> |
|
|
|
|
|
|
|
class CarlaSignal; |
|
|
|
|
|
|
|
// ----------------------------------------------------------------------- |
|
|
|
// CarlaMutex class |
|
|
|
|
|
|
|
@@ -35,12 +37,14 @@ public: |
|
|
|
: fMutex(), |
|
|
|
fTryLockWasCalled(false) |
|
|
|
{ |
|
|
|
pthread_mutexattr_t atts; |
|
|
|
pthread_mutexattr_init(&atts); |
|
|
|
pthread_mutexattr_setprotocol(&atts, inheritPriority ? PTHREAD_PRIO_INHERIT : PTHREAD_PRIO_NONE); |
|
|
|
pthread_mutexattr_settype(&atts, PTHREAD_MUTEX_NORMAL); |
|
|
|
pthread_mutex_init(&fMutex, &atts); |
|
|
|
pthread_mutexattr_destroy(&atts); |
|
|
|
pthread_mutexattr_t attr; |
|
|
|
pthread_mutexattr_init(&attr); |
|
|
|
pthread_mutexattr_setprotocol(&attr, inheritPriority ? PTHREAD_PRIO_INHERIT : PTHREAD_PRIO_NONE); |
|
|
|
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); |
|
|
|
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); |
|
|
|
pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST); |
|
|
|
pthread_mutex_init(&fMutex, &attr); |
|
|
|
pthread_mutexattr_destroy(&attr); |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
@@ -95,6 +99,7 @@ private: |
|
|
|
mutable pthread_mutex_t fMutex; |
|
|
|
mutable volatile bool fTryLockWasCalled; // true if "tryLock()" was called at least once |
|
|
|
|
|
|
|
friend class CarlaSignal; |
|
|
|
CARLA_PREVENT_HEAP_ALLOCATION |
|
|
|
CARLA_DECLARE_NON_COPY_CLASS(CarlaMutex) |
|
|
|
}; |
|
|
|
@@ -118,12 +123,12 @@ public: |
|
|
|
#ifdef CARLA_OS_WIN |
|
|
|
InitializeCriticalSection(&fSection); |
|
|
|
#else |
|
|
|
pthread_mutexattr_t atts; |
|
|
|
pthread_mutexattr_init(&atts); |
|
|
|
pthread_mutexattr_setprotocol(&atts, PTHREAD_PRIO_INHERIT); |
|
|
|
pthread_mutexattr_settype(&atts, PTHREAD_MUTEX_RECURSIVE); |
|
|
|
pthread_mutex_init(&fMutex, &atts); |
|
|
|
pthread_mutexattr_destroy(&atts); |
|
|
|
pthread_mutexattr_t attr; |
|
|
|
pthread_mutexattr_init(&attr); |
|
|
|
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); |
|
|
|
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
|
|
|
pthread_mutex_init(&fMutex, &attr); |
|
|
|
pthread_mutexattr_destroy(&attr); |
|
|
|
#endif |
|
|
|
} |
|
|
|
|
|
|
|
@@ -187,6 +192,68 @@ private: |
|
|
|
CARLA_DECLARE_NON_COPY_CLASS(CarlaRecursiveMutex) |
|
|
|
}; |
|
|
|
|
|
|
|
// ----------------------------------------------------------------------- |
|
|
|
// CarlaSignal class |
|
|
|
|
|
|
|
class CarlaSignal |
|
|
|
{ |
|
|
|
public: |
|
|
|
/* |
|
|
|
* Constructor. |
|
|
|
*/ |
|
|
|
CarlaSignal(CarlaMutex& mutex) noexcept |
|
|
|
: fCondition(), |
|
|
|
fMutex(mutex.fMutex) |
|
|
|
{ |
|
|
|
pthread_condattr_t attr; |
|
|
|
pthread_condattr_init(&attr); |
|
|
|
pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE); |
|
|
|
pthread_cond_init(&fCondition, &attr); |
|
|
|
pthread_condattr_destroy(&attr); |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* Destructor. |
|
|
|
*/ |
|
|
|
~CarlaSignal() noexcept |
|
|
|
{ |
|
|
|
pthread_cond_destroy(&fCondition); |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* Wait for a broadcast. |
|
|
|
*/ |
|
|
|
bool wait() noexcept |
|
|
|
{ |
|
|
|
try { |
|
|
|
return (pthread_cond_wait(&fCondition, &fMutex) == 0); |
|
|
|
} CARLA_SAFE_EXCEPTION_RETURN("pthread_cond_wait", false); |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* Wake up one waiting thread. |
|
|
|
*/ |
|
|
|
void signal() noexcept |
|
|
|
{ |
|
|
|
pthread_cond_signal(&fCondition); |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* Wake up all waiting threads. |
|
|
|
*/ |
|
|
|
void broadcast() noexcept |
|
|
|
{ |
|
|
|
pthread_cond_broadcast(&fCondition); |
|
|
|
} |
|
|
|
|
|
|
|
private: |
|
|
|
pthread_cond_t fCondition; |
|
|
|
pthread_mutex_t& fMutex; |
|
|
|
|
|
|
|
CARLA_PREVENT_HEAP_ALLOCATION |
|
|
|
CARLA_DECLARE_NON_COPY_CLASS(CarlaSignal) |
|
|
|
}; |
|
|
|
|
|
|
|
// ----------------------------------------------------------------------- |
|
|
|
// Helper class to lock&unlock a mutex during a function scope. |
|
|
|
|
|
|
|
|