Browse Source

Proper signal class implementation

pull/6/head
falkTX 8 years ago
parent
commit
97fb4236d0
2 changed files with 45 additions and 27 deletions
  1. +42
    -24
      distrho/extra/Mutex.hpp
  2. +3
    -3
      distrho/extra/Thread.hpp

+ 42
- 24
distrho/extra/Mutex.hpp View File

@@ -86,7 +86,6 @@ public:
private:
mutable pthread_mutex_t fMutex;

friend class Signal;
DISTRHO_PREVENT_HEAP_ALLOCATION
DISTRHO_DECLARE_NON_COPY_CLASS(Mutex)
};
@@ -188,15 +187,23 @@ public:
/*
* Constructor.
*/
Signal(Mutex& mutex) noexcept
Signal() noexcept
: fCondition(),
fMutex(mutex.fMutex)
fMutex(),
fTriggered(false)
{
pthread_condattr_t attr;
pthread_condattr_init(&attr);
pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
pthread_cond_init(&fCondition, &attr);
pthread_condattr_destroy(&attr);
pthread_condattr_t cattr;
pthread_condattr_init(&cattr);
pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_PRIVATE);
pthread_cond_init(&fCondition, &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(&fMutex, &mattr);
pthread_mutexattr_destroy(&mattr);
}

/*
@@ -205,37 +212,48 @@ public:
~Signal() noexcept
{
pthread_cond_destroy(&fCondition);
pthread_mutex_destroy(&fMutex);
}

/*
* Wait for a broadcast.
* Wait for a signal.
*/
bool wait() noexcept
void wait() noexcept
{
try {
return (pthread_cond_wait(&fCondition, &fMutex) == 0);
} DISTRHO_SAFE_EXCEPTION_RETURN("pthread_cond_wait", false);
}
pthread_mutex_lock(&fMutex);

/*
* Wake up one waiting thread.
*/
void signal() noexcept
{
pthread_cond_signal(&fCondition);
while (! fTriggered)
{
try {
pthread_cond_wait(&fCondition, &fMutex);
} DISTRHO_SAFE_EXCEPTION("pthread_cond_wait");
}

fTriggered = false;

pthread_mutex_unlock(&fMutex);
}

/*
* Wake up all waiting threads.
*/
void broadcast() noexcept
void signal() noexcept
{
pthread_cond_broadcast(&fCondition);
pthread_mutex_lock(&fMutex);

if (! fTriggered)
{
fTriggered = true;
pthread_cond_broadcast(&fCondition);
}

pthread_mutex_unlock(&fMutex);
}

private:
pthread_cond_t fCondition;
pthread_mutex_t& fMutex;
pthread_cond_t fCondition;
pthread_mutex_t fMutex;
volatile bool fTriggered;

DISTRHO_PREVENT_HEAP_ALLOCATION
DISTRHO_DECLARE_NON_COPY_CLASS(Signal)


+ 3
- 3
distrho/extra/Thread.hpp View File

@@ -38,7 +38,7 @@ protected:
*/
Thread(const char* const threadName = nullptr) noexcept
: fLock(),
fSignal(fLock),
fSignal(),
fName(threadName),
#ifdef PTW32_DLLPORT
fHandle({nullptr, 0}),
@@ -126,7 +126,7 @@ public:
*/
bool stopThread(const int timeOutMilliseconds) noexcept
{
const MutexLocker cml(fLock);
const MutexLocker ml(fLock);

if (isThreadRunning())
{
@@ -262,7 +262,7 @@ private:
setCurrentThreadName(fName);

// report ready
fSignal.broadcast();
fSignal.signal();

try {
run();


Loading…
Cancel
Save