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(
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 void lock()
const noexcept
66 pthread_mutex_lock(&fMutex);
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_PREVENT_HEAP_ALLOCATION
90 DISTRHO_DECLARE_NON_COPY_CLASS(
Mutex)
103 #ifdef DISTRHO_OS_WINDOWS
109 #ifdef DISTRHO_OS_WINDOWS
110 InitializeCriticalSection(&fSection);
112 pthread_mutexattr_t attr;
113 pthread_mutexattr_init(&attr);
114 pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
115 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
116 pthread_mutex_init(&fMutex, &attr);
117 pthread_mutexattr_destroy(&attr);
126 #ifdef DISTRHO_OS_WINDOWS
127 DeleteCriticalSection(&fSection);
129 pthread_mutex_destroy(&fMutex);
136 void lock()
const noexcept
138 #ifdef DISTRHO_OS_WINDOWS
139 EnterCriticalSection(&fSection);
141 pthread_mutex_lock(&fMutex);
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;
177 DISTRHO_PREVENT_HEAP_ALLOCATION
195 pthread_condattr_t cattr;
196 pthread_condattr_init(&cattr);
197 pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_PRIVATE);
198 pthread_cond_init(&fCondition, &cattr);
199 pthread_condattr_destroy(&cattr);
201 pthread_mutexattr_t mattr;
202 pthread_mutexattr_init(&mattr);
203 pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
204 pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
205 pthread_mutex_init(&fMutex, &mattr);
206 pthread_mutexattr_destroy(&mattr);
214 pthread_cond_destroy(&fCondition);
215 pthread_mutex_destroy(&fMutex);
223 pthread_mutex_lock(&fMutex);
228 pthread_cond_wait(&fCondition, &fMutex);
229 } DISTRHO_SAFE_EXCEPTION(
"pthread_cond_wait");
234 pthread_mutex_unlock(&fMutex);
240 void signal() noexcept
242 pthread_mutex_lock(&fMutex);
247 pthread_cond_broadcast(&fCondition);
250 pthread_mutex_unlock(&fMutex);
254 pthread_cond_t fCondition;
255 pthread_mutex_t fMutex;
256 volatile bool fTriggered;
258 DISTRHO_PREVENT_HEAP_ALLOCATION
259 DISTRHO_DECLARE_NON_COPY_CLASS(
Signal)
265 template <
class Mutex>
283 DISTRHO_PREVENT_HEAP_ALLOCATION
290 template <
class Mutex>
296 fLocked(mutex.tryLock()) {}
304 bool wasLocked()
const noexcept
309 bool wasNotLocked()
const noexcept
318 DISTRHO_PREVENT_HEAP_ALLOCATION
325 template <
class Mutex>
343 DISTRHO_PREVENT_HEAP_ALLOCATION
361 END_NAMESPACE_DISTRHO
363 #endif // DISTRHO_MUTEX_HPP_INCLUDED