17 #ifndef DISTRHO_MUTEX_HPP_INCLUDED
18 #define DISTRHO_MUTEX_HPP_INCLUDED
20 #include "../DistrhoUtils.hpp"
22 #ifdef DISTRHO_OS_WINDOWS
26 # include <winsock2.h>
45 Mutex(
const bool inheritPriority =
true) noexcept
48 pthread_mutexattr_t attr;
49 pthread_mutexattr_init(&attr);
50 pthread_mutexattr_setprotocol(&attr, inheritPriority ? PTHREAD_PRIO_INHERIT : PTHREAD_PRIO_NONE);
51 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
52 pthread_mutex_init(&fMutex, &attr);
53 pthread_mutexattr_destroy(&attr);
61 pthread_mutex_destroy(&fMutex);
67 bool lock()
const noexcept
69 return (pthread_mutex_lock(&fMutex) == 0);
76 bool tryLock()
const noexcept
78 return (pthread_mutex_trylock(&fMutex) == 0);
84 void unlock()
const noexcept
86 pthread_mutex_unlock(&fMutex);
90 mutable pthread_mutex_t fMutex;
92 DISTRHO_DECLARE_NON_COPYABLE(
Mutex)
105 #ifdef DISTRHO_OS_WINDOWS
111 #ifdef DISTRHO_OS_WINDOWS
112 InitializeCriticalSection(&fSection);
114 pthread_mutexattr_t attr;
115 pthread_mutexattr_init(&attr);
116 pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
117 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
118 pthread_mutex_init(&fMutex, &attr);
119 pthread_mutexattr_destroy(&attr);
128 #ifdef DISTRHO_OS_WINDOWS
129 DeleteCriticalSection(&fSection);
131 pthread_mutex_destroy(&fMutex);
138 bool lock()
const noexcept
140 #ifdef DISTRHO_OS_WINDOWS
141 EnterCriticalSection(&fSection);
144 return (pthread_mutex_lock(&fMutex) == 0);
152 bool tryLock()
const noexcept
154 #ifdef DISTRHO_OS_WINDOWS
155 return (TryEnterCriticalSection(&fSection) != FALSE);
157 return (pthread_mutex_trylock(&fMutex) == 0);
164 void unlock()
const noexcept
166 #ifdef DISTRHO_OS_WINDOWS
167 LeaveCriticalSection(&fSection);
169 pthread_mutex_unlock(&fMutex);
174 #ifdef DISTRHO_OS_WINDOWS
175 mutable CRITICAL_SECTION fSection;
177 mutable pthread_mutex_t fMutex;
197 pthread_condattr_t cattr;
198 pthread_condattr_init(&cattr);
199 pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_PRIVATE);
200 pthread_cond_init(&fCondition, &cattr);
201 pthread_condattr_destroy(&cattr);
203 pthread_mutexattr_t mattr;
204 pthread_mutexattr_init(&mattr);
205 pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
206 pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
207 pthread_mutex_init(&fMutex, &mattr);
208 pthread_mutexattr_destroy(&mattr);
216 pthread_cond_destroy(&fCondition);
217 pthread_mutex_destroy(&fMutex);
225 pthread_mutex_lock(&fMutex);
230 pthread_cond_wait(&fCondition, &fMutex);
231 } DISTRHO_SAFE_EXCEPTION(
"pthread_cond_wait");
236 pthread_mutex_unlock(&fMutex);
242 void signal() noexcept
244 pthread_mutex_lock(&fMutex);
249 pthread_cond_broadcast(&fCondition);
252 pthread_mutex_unlock(&fMutex);
256 pthread_cond_t fCondition;
257 pthread_mutex_t fMutex;
258 volatile bool fTriggered;
260 DISTRHO_PREVENT_HEAP_ALLOCATION
261 DISTRHO_DECLARE_NON_COPYABLE(
Signal)
267 template <
class Mutex>
285 DISTRHO_PREVENT_HEAP_ALLOCATION
292 template <
class Mutex>
298 fLocked(mutex.tryLock()) {}
302 fLocked(forceLock ? mutex.lock() : mutex.tryLock()) {}
310 bool wasLocked()
const noexcept
315 bool wasNotLocked()
const noexcept
324 DISTRHO_PREVENT_HEAP_ALLOCATION
331 template <
class Mutex>
349 DISTRHO_PREVENT_HEAP_ALLOCATION
Definition: Mutex.hpp:269
Definition: Mutex.hpp:294
Definition: Mutex.hpp:333
Definition: Mutex.hpp:187
#define END_NAMESPACE_DISTRHO
Definition: DistrhoInfo.hpp:834
#define START_NAMESPACE_DISTRHO
Definition: DistrhoInfo.hpp:828