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
43 pthread_mutex_init(&fMutex,
nullptr);
51 pthread_mutex_destroy(&fMutex);
57 void lock()
const noexcept
59 pthread_mutex_lock(&fMutex);
66 bool tryLock()
const noexcept
68 return (pthread_mutex_trylock(&fMutex) == 0);
74 void unlock()
const noexcept
76 pthread_mutex_unlock(&fMutex);
80 mutable pthread_mutex_t fMutex;
82 DISTRHO_PREVENT_HEAP_ALLOCATION
83 DISTRHO_DECLARE_NON_COPY_CLASS(
Mutex)
96 #ifdef DISTRHO_OS_WINDOWS
102 #ifdef DISTRHO_OS_WINDOWS
103 InitializeCriticalSection(&fSection);
105 pthread_mutexattr_t atts;
106 pthread_mutexattr_init(&atts);
107 pthread_mutexattr_settype(&atts, PTHREAD_MUTEX_RECURSIVE);
108 pthread_mutex_init(&fMutex, &atts);
109 pthread_mutexattr_destroy(&atts);
118 #ifdef DISTRHO_OS_WINDOWS
119 DeleteCriticalSection(&fSection);
121 pthread_mutex_destroy(&fMutex);
128 void lock()
const noexcept
130 #ifdef DISTRHO_OS_WINDOWS
131 EnterCriticalSection(&fSection);
133 pthread_mutex_lock(&fMutex);
141 bool tryLock()
const noexcept
143 #ifdef DISTRHO_OS_WINDOWS
144 return (TryEnterCriticalSection(&fSection) != FALSE);
146 return (pthread_mutex_trylock(&fMutex) == 0);
153 void unlock()
const noexcept
155 #ifdef DISTRHO_OS_WINDOWS
156 LeaveCriticalSection(&fSection);
158 pthread_mutex_unlock(&fMutex);
163 #ifdef DISTRHO_OS_WINDOWS
164 mutable CRITICAL_SECTION fSection;
166 mutable pthread_mutex_t fMutex;
169 DISTRHO_PREVENT_HEAP_ALLOCATION
176 template <
class Mutex>
194 DISTRHO_PREVENT_HEAP_ALLOCATION
201 template <
class Mutex>
207 fLocked(mutex.tryLock()) {}
215 bool wasLocked()
const noexcept
220 bool wasNotLocked()
const noexcept
229 DISTRHO_PREVENT_HEAP_ALLOCATION
236 template <
class Mutex>
254 DISTRHO_PREVENT_HEAP_ALLOCATION
272 END_NAMESPACE_DISTRHO
274 #endif // DISTRHO_MUTEX_HPP_INCLUDED
Definition: Mutex.hpp:177
Definition: Mutex.hpp:202
Definition: Mutex.hpp:237