15 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
18 #elif defined(_WIN32) || defined(_WIN64)
28 class ConditionVariable::Impl
33 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
34 int result = pthread_cond_init(&_cond,
nullptr);
36 throwex SystemException(
"Failed to initialize a condition variable!", result);
37 #elif defined(_WIN32) || defined(_WIN64)
38 InitializeConditionVariable(&_cond);
44 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
45 int result = pthread_cond_destroy(&_cond);
47 fatality(SystemException(
"Failed to destroy a condition variable!", result));
48 #elif defined(_WIN32) || defined(_WIN64)
55 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
56 int result = pthread_cond_signal(&_cond);
58 throwex SystemException(
"Failed to signal a condition variable!", result);
59 #elif defined(_WIN32) || defined(_WIN64)
60 WakeConditionVariable(&_cond);
66 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
67 int result = pthread_cond_broadcast(&_cond);
69 throwex SystemException(
"Failed to broadcast a condition variable!", result);
70 #elif defined(_WIN32) || defined(_WIN64)
71 WakeAllConditionVariable(&_cond);
75 void Wait(CriticalSection& cs)
77 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
78 int result = pthread_cond_wait(&_cond, (pthread_mutex_t*)cs.native());
80 throwex SystemException(
"Failed to waiting a condition variable!", result);
81 #elif defined(_WIN32) || defined(_WIN64)
82 if (!SleepConditionVariableCS(&_cond, (CRITICAL_SECTION*)cs.native(), INFINITE))
83 throwex SystemException(
"Failed to waiting a condition variable!");
87 bool TryWaitFor(CriticalSection& cs,
const Timespan& timespan)
91 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
92 struct timespec timeout;
93 timeout.tv_sec = timespan.seconds();
94 timeout.tv_nsec = timespan.nanoseconds() % 1000000000;
95 int result = pthread_cond_timedwait(&_cond, (pthread_mutex_t*)cs.native(), &timeout);
96 if ((result != 0) && (result != ETIMEDOUT))
97 throwex SystemException(
"Failed to waiting a condition variable for the given timeout!", result);
99 #elif defined(_WIN32) || defined(_WIN64)
100 if (!SleepConditionVariableCS(&_cond, (CRITICAL_SECTION*)cs.native(), std::max((DWORD)0, (DWORD)timespan.milliseconds())))
102 if (GetLastError() != ERROR_TIMEOUT)
103 throwex SystemException(
"Failed to waiting a condition variable for the given timeout!");
111 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
112 pthread_cond_t _cond;
113 #elif defined(_WIN32) || defined(_WIN64)
114 CONDITION_VARIABLE _cond;
124 static_assert((StorageSize >=
sizeof(Impl)),
"ConditionVariable::StorageSize must be increased!");
125 static_assert(((StorageAlign %
alignof(Impl)) == 0),
"ConditionVariable::StorageAlign must be adjusted!");
128 new(&_storage)Impl();
134 reinterpret_cast<Impl*
>(&_storage)->~Impl();
bool TryWaitFor(CriticalSection &cs, const Timespan ×pan)
Try to wait for the given timespan until condition variable is notified.
void Wait(CriticalSection &cs)
Wait until condition variable is notified.
void NotifyOne()
Notify one of waiting thread about event occurred.
void NotifyAll()
Notify all waiting threads about event occurred.
Critical section synchronization primitive.
Aligned storage validator.
Condition variable 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.