16 #if defined(__APPLE__) || defined(__CYGWIN__)
19 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
21 #elif defined(_WIN32) || defined(_WIN64)
36 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
37 int result = pthread_mutex_init(&_mutex,
nullptr);
39 throwex SystemException(
"Failed to initialize a mutex!", result);
40 #elif defined(_WIN32) || defined(_WIN64)
41 _mutex = CreateMutex(
nullptr, FALSE,
nullptr);
42 if (_mutex ==
nullptr)
43 throwex SystemException(
"Failed to create a mutex!");
49 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
50 int result = pthread_mutex_destroy(&_mutex);
52 fatality(SystemException(
"Failed to destroy a mutex!", result));
53 #elif defined(_WIN32) || defined(_WIN64)
54 if (!CloseHandle(_mutex))
55 fatality(SystemException(
"Failed to close a mutex!"));
61 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
62 int result = pthread_mutex_trylock(&_mutex);
63 if ((result != 0) && (result != EAGAIN) && (result != EBUSY) && (result != EDEADLK))
64 throwex SystemException(
"Failed to try lock a mutex!", result);
66 #elif defined(_WIN32) || defined(_WIN64)
67 DWORD result = WaitForSingleObject(_mutex, 0);
68 if ((result != WAIT_OBJECT_0) && (result != WAIT_TIMEOUT))
69 throwex SystemException(
"Failed to try lock a mutex!");
70 return (result == WAIT_OBJECT_0);
78 #if defined(__APPLE__) || defined(__CYGWIN__)
80 Timestamp finish = NanoTimestamp() + timespan;
88 while (NanoTimestamp() < finish)
99 #elif defined(unix) || defined(__unix) || defined(__unix__)
100 struct timespec timeout;
101 timeout.tv_sec = timespan.seconds();
102 timeout.tv_nsec = timespan.nanoseconds() % 1000000000;
103 int result = pthread_mutex_timedlock(&_mutex, &timeout);
104 if ((result != 0) && (result != ETIMEDOUT))
105 throwex SystemException(
"Failed to try lock a mutex for the given timeout!", result);
106 return (result == 0);
107 #elif defined(_WIN32) || defined(_WIN64)
108 DWORD result = WaitForSingleObject(_mutex, std::max((DWORD)1, (DWORD)timespan.milliseconds()));
109 if ((result != WAIT_OBJECT_0) && (result != WAIT_TIMEOUT))
110 throwex SystemException(
"Failed to try lock a mutex for the given timeout!");
111 return (result == WAIT_OBJECT_0);
117 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
118 int result = pthread_mutex_lock(&_mutex);
120 throwex SystemException(
"Failed to lock a mutex!", result);
121 #elif defined(_WIN32) || defined(_WIN64)
122 DWORD result = WaitForSingleObject(_mutex, INFINITE);
123 if (result != WAIT_OBJECT_0)
124 throwex SystemException(
"Failed to lock a mutex!");
130 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
131 int result = pthread_mutex_unlock(&_mutex);
133 throwex SystemException(
"Failed to unlock a mutex!", result);
134 #elif defined(_WIN32) || defined(_WIN64)
135 if (!ReleaseMutex(_mutex))
136 throwex SystemException(
"Failed to unlock a mutex!");
141 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
142 pthread_mutex_t _mutex;
143 #elif defined(_WIN32) || defined(_WIN64)
154 static_assert((StorageSize >=
sizeof(Impl)),
"Mutex::StorageSize must be increased!");
155 static_assert(((StorageAlign %
alignof(Impl)) == 0),
"Mutex::StorageAlign must be adjusted!");
158 new(&_storage)Impl();
164 reinterpret_cast<Impl*
>(&_storage)->~Impl();
bool TryLock()
Try to acquire mutex without block.
void Lock()
Acquire mutex with block.
void Unlock()
Release mutex.
bool TryLockFor(const Timespan ×pan)
Try to acquire mutex for the given timespan.
static void Yield() noexcept
Yield to other threads.
Aligned storage validator.
#define throwex
Throw extended exception macro.
Fatal abort execution definition.
#define fatality(...)
Fatal abort execution extended macro.
Mutex synchronization primitive definition.
C++ Common project definitions.
Aligned storage validator definition.