50 Mutex(
const bool inheritPriority =
true) noexcept
56 (void)inheritPriority;
57 InitializeSRWLock(&fMutex);
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);
76 pthread_mutex_destroy(&fMutex);
83 bool lock()
const noexcept
86 AcquireSRWLockExclusive(&fMutex);
89 return (pthread_mutex_lock(&fMutex) == 0);
97 bool tryLock()
const noexcept
100 return (TryAcquireSRWLockExclusive(&fMutex) != FALSE);
102 return (pthread_mutex_trylock(&fMutex) == 0);
109 void unlock()
const noexcept
112 ReleaseSRWLockExclusive(&fMutex);
114 pthread_mutex_unlock(&fMutex);
120 mutable SRWLOCK fMutex;
122 mutable pthread_mutex_t fMutex;
125 DAF_DECLARE_NON_COPYABLE(Mutex)
137 RecursiveMutex()
noexcept
138 #ifdef DAF_OS_WINDOWS
144 #ifdef DAF_OS_WINDOWS
145 InitializeCriticalSection(&fSection);
147 pthread_mutexattr_t attr;
148 pthread_mutexattr_init(&attr);
149 pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
150 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
151 pthread_mutex_init(&fMutex, &attr);
152 pthread_mutexattr_destroy(&attr);
159 ~RecursiveMutex()
noexcept
161 #ifdef DAF_OS_WINDOWS
162 DeleteCriticalSection(&fSection);
164 pthread_mutex_destroy(&fMutex);
171 bool lock()
const noexcept
173 #ifdef DAF_OS_WINDOWS
174 EnterCriticalSection(&fSection);
177 return (pthread_mutex_lock(&fMutex) == 0);
185 bool tryLock()
const noexcept
187 #ifdef DAF_OS_WINDOWS
188 return (TryEnterCriticalSection(&fSection) != FALSE);
190 return (pthread_mutex_trylock(&fMutex) == 0);
197 void unlock()
const noexcept
199 #ifdef DAF_OS_WINDOWS
200 LeaveCriticalSection(&fSection);
202 pthread_mutex_unlock(&fMutex);
207 #ifdef DAF_OS_WINDOWS
208 mutable CRITICAL_SECTION fSection;
210 mutable pthread_mutex_t fMutex;
213 DAF_DECLARE_NON_COPYABLE(RecursiveMutex)
231 InitializeConditionVariable(&fCondition);
232 InitializeCriticalSection(&fMutex);
234 pthread_condattr_t cattr;
235 pthread_condattr_init(&cattr);
236 pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_PRIVATE);
237 pthread_cond_init(&fCondition, &cattr);
238 pthread_condattr_destroy(&cattr);
240 pthread_mutexattr_t mattr;
241 pthread_mutexattr_init(&mattr);
242 pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
243 pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
244 pthread_mutex_init(&fMutex, &mattr);
245 pthread_mutexattr_destroy(&mattr);
256 DeleteCriticalSection(&fMutex);
258 pthread_cond_destroy(&fCondition);
259 pthread_mutex_destroy(&fMutex);
269 EnterCriticalSection(&fMutex);
274 SleepConditionVariableCS(&fCondition, &fMutex, INFINITE);
279 LeaveCriticalSection(&fMutex);
281 pthread_mutex_lock(&fMutex);
286 pthread_cond_wait(&fCondition, &fMutex);
287 } DAF_SAFE_EXCEPTION(
"pthread_cond_wait");
292 pthread_mutex_unlock(&fMutex);
299 void signal()
noexcept
302 EnterCriticalSection(&fMutex);
307 WakeAllConditionVariable(&fCondition);
310 LeaveCriticalSection(&fMutex);
312 pthread_mutex_lock(&fMutex);
317 pthread_cond_broadcast(&fCondition);
320 pthread_mutex_unlock(&fMutex);
326 CONDITION_VARIABLE fCondition;
327 CRITICAL_SECTION fMutex;
329 pthread_cond_t fCondition;
330 pthread_mutex_t fMutex;
332 volatile bool fTriggered;
334 DAF_PREVENT_HEAP_ALLOCATION
335 DAF_DECLARE_NON_COPYABLE(Signal)