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>
32 #define DISTRHO_OS_WINDOWS__TODO
33 #pragma NOTE(DPF Mutex implementation is TODO on MSVC)
51 Mutex(
const bool inheritPriority =
true) noexcept
52 #ifdef DISTRHO_OS_WINDOWS__TODO
57 #ifdef DISTRHO_OS_WINDOWS__TODO
59 pthread_mutexattr_t attr;
60 pthread_mutexattr_init(&attr);
61 pthread_mutexattr_setprotocol(&attr, inheritPriority ? PTHREAD_PRIO_INHERIT : PTHREAD_PRIO_NONE);
62 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
63 pthread_mutex_init(&fMutex, &attr);
64 pthread_mutexattr_destroy(&attr);
73 #ifdef DISTRHO_OS_WINDOWS__TODO
75 pthread_mutex_destroy(&fMutex);
82 bool lock()
const noexcept
84 #ifdef DISTRHO_OS_WINDOWS__TODO
86 return (pthread_mutex_lock(&fMutex) == 0);
94 bool tryLock()
const noexcept
96 #ifdef DISTRHO_OS_WINDOWS__TODO
98 return (pthread_mutex_trylock(&fMutex) == 0);
105 void unlock()
const noexcept
107 #ifdef DISTRHO_OS_WINDOWS__TODO
109 pthread_mutex_unlock(&fMutex);
114 #ifdef DISTRHO_OS_WINDOWS__TODO
116 mutable pthread_mutex_t fMutex;
119 DISTRHO_DECLARE_NON_COPYABLE(
Mutex)
132 #ifdef DISTRHO_OS_WINDOWS
138 #ifdef DISTRHO_OS_WINDOWS
139 InitializeCriticalSection(&fSection);
141 pthread_mutexattr_t attr;
142 pthread_mutexattr_init(&attr);
143 pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
144 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
145 pthread_mutex_init(&fMutex, &attr);
146 pthread_mutexattr_destroy(&attr);
155 #ifdef DISTRHO_OS_WINDOWS
156 DeleteCriticalSection(&fSection);
158 pthread_mutex_destroy(&fMutex);
165 bool lock()
const noexcept
167 #ifdef DISTRHO_OS_WINDOWS
168 EnterCriticalSection(&fSection);
171 return (pthread_mutex_lock(&fMutex) == 0);
179 bool tryLock()
const noexcept
181 #ifdef DISTRHO_OS_WINDOWS
182 return (TryEnterCriticalSection(&fSection) != FALSE);
184 return (pthread_mutex_trylock(&fMutex) == 0);
191 void unlock()
const noexcept
193 #ifdef DISTRHO_OS_WINDOWS
194 LeaveCriticalSection(&fSection);
196 pthread_mutex_unlock(&fMutex);
201 #ifdef DISTRHO_OS_WINDOWS
202 mutable CRITICAL_SECTION fSection;
204 mutable pthread_mutex_t fMutex;
225 pthread_condattr_t cattr;
226 pthread_condattr_init(&cattr);
227 pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_PRIVATE);
228 pthread_cond_init(&fCondition, &cattr);
229 pthread_condattr_destroy(&cattr);
231 pthread_mutexattr_t mattr;
232 pthread_mutexattr_init(&mattr);
233 pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
234 pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
235 pthread_mutex_init(&fMutex, &mattr);
236 pthread_mutexattr_destroy(&mattr);
244 pthread_cond_destroy(&fCondition);
245 pthread_mutex_destroy(&fMutex);
253 pthread_mutex_lock(&fMutex);
258 pthread_cond_wait(&fCondition, &fMutex);
259 } DISTRHO_SAFE_EXCEPTION(
"pthread_cond_wait");
264 pthread_mutex_unlock(&fMutex);
270 void signal() noexcept
272 pthread_mutex_lock(&fMutex);
277 pthread_cond_broadcast(&fCondition);
280 pthread_mutex_unlock(&fMutex);
284 pthread_cond_t fCondition;
285 pthread_mutex_t fMutex;
286 volatile bool fTriggered;
288 DISTRHO_PREVENT_HEAP_ALLOCATION
289 DISTRHO_DECLARE_NON_COPYABLE(
Signal)
296 template <
class Mutex>
314 DISTRHO_PREVENT_HEAP_ALLOCATION
321 template <
class Mutex>
327 fLocked(mutex.tryLock()) {}
331 fLocked(forceLock ? mutex.lock() : mutex.tryLock()) {}
339 bool wasLocked()
const noexcept
344 bool wasNotLocked()
const noexcept
353 DISTRHO_PREVENT_HEAP_ALLOCATION
360 template <
class Mutex>
378 DISTRHO_PREVENT_HEAP_ALLOCATION
Definition: Mutex.hpp:126
Definition: Mutex.hpp:298
Definition: Mutex.hpp:323
Definition: Mutex.hpp:362
Definition: Mutex.hpp:215
#define END_NAMESPACE_DISTRHO
Definition: DistrhoInfo.hpp:949
#define START_NAMESPACE_DISTRHO
Definition: DistrhoInfo.hpp:943