17 #ifndef DISTRHO_MUTEX_HPP_INCLUDED 
   18 #define DISTRHO_MUTEX_HPP_INCLUDED 
   20 #include "../DistrhoUtils.hpp" 
   22 #ifdef DISTRHO_OS_WINDOWS 
   23 # include <winsock2.h> 
   29 START_NAMESPACE_DISTRHO
 
   42     Mutex(
const bool inheritPriority = 
true) noexcept
 
   45         pthread_mutexattr_t attr;
 
   46         pthread_mutexattr_init(&attr);
 
   47         pthread_mutexattr_setprotocol(&attr, inheritPriority ? PTHREAD_PRIO_INHERIT : PTHREAD_PRIO_NONE);
 
   48         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
 
   49         pthread_mutex_init(&fMutex, &attr);
 
   50         pthread_mutexattr_destroy(&attr);
 
   58         pthread_mutex_destroy(&fMutex);
 
   64     bool lock() 
const noexcept
 
   66         return (pthread_mutex_lock(&fMutex) == 0);
 
   73     bool tryLock() 
const noexcept
 
   75         return (pthread_mutex_trylock(&fMutex) == 0);
 
   81     void unlock() 
const noexcept
 
   83         pthread_mutex_unlock(&fMutex);
 
   87     mutable pthread_mutex_t fMutex;
 
   89     DISTRHO_DECLARE_NON_COPYABLE(
Mutex)
 
  102 #ifdef DISTRHO_OS_WINDOWS 
  108 #ifdef DISTRHO_OS_WINDOWS 
  109         InitializeCriticalSection(&fSection);
 
  111         pthread_mutexattr_t attr;
 
  112         pthread_mutexattr_init(&attr);
 
  113         pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
 
  114         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
 
  115         pthread_mutex_init(&fMutex, &attr);
 
  116         pthread_mutexattr_destroy(&attr);
 
  125 #ifdef DISTRHO_OS_WINDOWS 
  126         DeleteCriticalSection(&fSection);
 
  128         pthread_mutex_destroy(&fMutex);
 
  135     bool lock() 
const noexcept
 
  137 #ifdef DISTRHO_OS_WINDOWS 
  138         EnterCriticalSection(&fSection);
 
  141         return (pthread_mutex_lock(&fMutex) == 0);
 
  149     bool tryLock() 
const noexcept
 
  151 #ifdef DISTRHO_OS_WINDOWS 
  152         return (TryEnterCriticalSection(&fSection) != FALSE);
 
  154         return (pthread_mutex_trylock(&fMutex) == 0);
 
  161     void unlock() 
const noexcept
 
  163 #ifdef DISTRHO_OS_WINDOWS 
  164         LeaveCriticalSection(&fSection);
 
  166         pthread_mutex_unlock(&fMutex);
 
  171 #ifdef DISTRHO_OS_WINDOWS 
  172     mutable CRITICAL_SECTION fSection;
 
  174     mutable pthread_mutex_t fMutex;
 
  194         pthread_condattr_t cattr;
 
  195         pthread_condattr_init(&cattr);
 
  196         pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_PRIVATE);
 
  197         pthread_cond_init(&fCondition, &cattr);
 
  198         pthread_condattr_destroy(&cattr);
 
  200         pthread_mutexattr_t mattr;
 
  201         pthread_mutexattr_init(&mattr);
 
  202         pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
 
  203         pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
 
  204         pthread_mutex_init(&fMutex, &mattr);
 
  205         pthread_mutexattr_destroy(&mattr);
 
  213         pthread_cond_destroy(&fCondition);
 
  214         pthread_mutex_destroy(&fMutex);
 
  222         pthread_mutex_lock(&fMutex);
 
  227                 pthread_cond_wait(&fCondition, &fMutex);
 
  228             } DISTRHO_SAFE_EXCEPTION(
"pthread_cond_wait");
 
  233         pthread_mutex_unlock(&fMutex);
 
  239     void signal() noexcept
 
  241         pthread_mutex_lock(&fMutex);
 
  246             pthread_cond_broadcast(&fCondition);
 
  249         pthread_mutex_unlock(&fMutex);
 
  253     pthread_cond_t  fCondition;
 
  254     pthread_mutex_t fMutex;
 
  255     volatile bool   fTriggered;
 
  257     DISTRHO_PREVENT_HEAP_ALLOCATION
 
  258     DISTRHO_DECLARE_NON_COPYABLE(
Signal)
 
  264 template <
class Mutex>
 
  282     DISTRHO_PREVENT_HEAP_ALLOCATION
 
  289 template <
class Mutex>
 
  295           fLocked(mutex.tryLock()) {}
 
  299           fLocked(forceLock ? mutex.lock() : mutex.tryLock()) {}
 
  307     bool wasLocked() 
const noexcept
 
  312     bool wasNotLocked() 
const noexcept
 
  321     DISTRHO_PREVENT_HEAP_ALLOCATION
 
  328 template <
class Mutex>
 
  346     DISTRHO_PREVENT_HEAP_ALLOCATION
 
  364 END_NAMESPACE_DISTRHO
 
  366 #endif // DISTRHO_MUTEX_HPP_INCLUDED