14 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
17 #elif defined(_WIN32) || defined(_WIN64)
26 class CriticalSection::Impl
31 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
32 pthread_mutexattr_t mutex_attribute;
33 int result = pthread_mutexattr_init(&mutex_attribute);
35 throwex SystemException(
"Failed to initialize a mutex attribute!", result);
36 result = pthread_mutexattr_settype(&mutex_attribute, PTHREAD_MUTEX_RECURSIVE);
38 throwex SystemException(
"Failed to set a mutex recursive attribute!", result);
39 result = pthread_mutex_init(&_lock, &mutex_attribute);
41 throwex SystemException(
"Failed to initialize a mutex!", result);
42 result = pthread_mutexattr_destroy(&mutex_attribute);
44 throwex SystemException(
"Failed to destroy a mutex attribute!", result);
45 #elif defined(_WIN32) || defined(_WIN64)
46 InitializeCriticalSection(&_lock);
52 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
53 int result = pthread_mutex_destroy(&_lock);
55 fatality(SystemException(
"Failed to destroy a mutex!", result));
56 #elif defined(_WIN32) || defined(_WIN64)
57 DeleteCriticalSection(&_lock);
61 void* native() noexcept
68 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
69 int result = pthread_mutex_trylock(&_lock);
70 if ((result != 0) && (result != EAGAIN) && (result != EBUSY) && (result != EDEADLK))
71 throwex SystemException(
"Failed to try lock a mutex!", result);
73 #elif defined(_WIN32) || defined(_WIN64)
74 return (TryEnterCriticalSection(&_lock) != 0);
80 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
81 int result = pthread_mutex_lock(&_lock);
83 throwex SystemException(
"Failed to lock a mutex!", result);
84 #elif defined(_WIN32) || defined(_WIN64)
85 EnterCriticalSection(&_lock);
91 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
92 int result = pthread_mutex_unlock(&_lock);
94 throwex SystemException(
"Failed to unlock a mutex!", result);
95 #elif defined(_WIN32) || defined(_WIN64)
96 LeaveCriticalSection(&_lock);
101 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
102 pthread_mutex_t _lock;
103 #elif defined(_WIN32) || defined(_WIN64)
104 CRITICAL_SECTION _lock;
114 static_assert((StorageSize >=
sizeof(Impl)),
"CriticalSection::StorageSize must be increased!");
115 static_assert(((StorageAlign %
alignof(Impl)) == 0),
"CriticalSection::StorageAlign must be adjusted!");
118 new(&_storage)Impl();
124 reinterpret_cast<Impl*
>(&_storage)->~Impl();
127 void* CriticalSection::native() noexcept {
return impl().native(); }
bool TryLock()
Try to acquire critical section without block.
void Lock()
Acquire critical section with block.
bool TryLockFor(const Timespan ×pan)
Try to acquire critical section for the given timespan.
void Unlock()
Release critical section.
High resolution timestamp.
static void Yield() noexcept
Yield to other threads.
Aligned storage validator.
Critical section synchronization primitive definition.
#define throwex
Throw extended exception macro.
Fatal abort execution definition.
#define fatality(...)
Fatal abort execution extended macro.
C++ Common project definitions.
Aligned storage validator definition.