16 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
18 #elif defined(_WIN32) || defined(_WIN64)
28 class EventAutoReset::Impl
33 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
34 int result = pthread_mutex_init(&_mutex,
nullptr);
36 throwex SystemException(
"Failed to initialize a mutex for the auto-reset event!", result);
37 result = pthread_cond_init(&_cond,
nullptr);
39 throwex SystemException(
"Failed to initialize a conditional variable for the auto-reset event!", result);
40 _signaled = signaled ? 1 : 0;
41 #elif defined(_WIN32) || defined(_WIN64)
42 _event = CreateEvent(
nullptr, FALSE, signaled ? TRUE : FALSE,
nullptr);
43 if (_event ==
nullptr)
44 throwex SystemException(
"Failed to create an auto-reset event!");
50 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
51 int result = pthread_mutex_destroy(&_mutex);
53 fatality(SystemException(
"Failed to destroy a mutex for the auto-reset event!", result));
54 result = pthread_cond_destroy(&_cond);
56 fatality(SystemException(
"Failed to destroy a conditional variable for the auto-reset event!", result));
57 #elif defined(_WIN32) || defined(_WIN64)
58 if (!CloseHandle(_event))
59 fatality(SystemException(
"Failed to close an auto-reset event!"));
65 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
66 int result = pthread_mutex_lock(&_mutex);
68 throwex SystemException(
"Failed to lock a mutex for the auto-reset event!", result);
70 result = pthread_mutex_unlock(&_mutex);
72 throwex SystemException(
"Failed to unlock a mutex for the auto-reset event!", result);
73 result = pthread_cond_signal(&_cond);
75 throwex SystemException(
"Failed to signal an auto-reset event!", result);
76 #elif defined(_WIN32) || defined(_WIN64)
77 if (!SetEvent(_event))
78 throwex SystemException(
"Failed to signal an auto-reset event!");
84 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
85 int result = pthread_mutex_lock(&_mutex);
87 throwex SystemException(
"Failed to lock a mutex for the auto-reset event!", result);
88 bool signaled = (_signaled-- > 0);
89 _signaled = (_signaled < 0) ? 0 : _signaled;
90 result = pthread_mutex_unlock(&_mutex);
92 throwex SystemException(
"Failed to unlock a mutex for the auto-reset event!", result);
94 #elif defined(_WIN32) || defined(_WIN64)
95 DWORD result = WaitForSingleObject(_event, 0);
96 if ((result != WAIT_OBJECT_0) && (result != WAIT_TIMEOUT))
97 throwex SystemException(
"Failed to try lock an auto-reset event!");
98 return (result == WAIT_OBJECT_0);
106 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
107 struct timespec timeout;
108 timeout.tv_sec = timespan.seconds();
109 timeout.tv_nsec = timespan.nanoseconds() % 1000000000;
110 int result = pthread_mutex_lock(&_mutex);
112 throwex SystemException(
"Failed to lock a mutex for the auto-reset event!", result);
113 bool signaled =
true;
116 result = pthread_cond_timedwait(&_cond, &_mutex, &timeout);
117 if ((result != 0) && (result != ETIMEDOUT))
118 throwex SystemException(
"Failed to timeout waiting a conditional variable for the auto-reset event!", result);
119 if (result == ETIMEDOUT)
120 signaled = (_signaled > 0);
122 _signaled = (_signaled > 0) ? (_signaled - 1) : 0;
123 result = pthread_mutex_unlock(&_mutex);
125 throwex SystemException(
"Failed to unlock a mutex for the auto-reset event!", result);
127 #elif defined(_WIN32) || defined(_WIN64)
128 DWORD result = WaitForSingleObject(_event, std::max((DWORD)1, (DWORD)timespan.milliseconds()));
129 if ((result != WAIT_OBJECT_0) && (result != WAIT_TIMEOUT))
130 throwex SystemException(
"Failed to try lock an auto-reset event for the given timeout!");
131 return (result == WAIT_OBJECT_0);
137 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
138 int result = pthread_mutex_lock(&_mutex);
140 throwex SystemException(
"Failed to lock a mutex for the auto-reset event!", result);
143 result = pthread_cond_wait(&_cond, &_mutex);
145 throwex SystemException(
"Failed to waiting a conditional variable for the auto-reset event!", result);
147 _signaled = (_signaled > 0) ? (_signaled - 1) : 0;
148 result = pthread_mutex_unlock(&_mutex);
150 throwex SystemException(
"Failed to unlock a mutex for the auto-reset event!", result);
151 #elif defined(_WIN32) || defined(_WIN64)
152 DWORD result = WaitForSingleObject(_event, INFINITE);
153 if (result != WAIT_OBJECT_0)
154 throwex SystemException(
"Failed to lock an auto-reset event!");
159 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
160 pthread_mutex_t _mutex;
161 pthread_cond_t _cond;
163 #elif defined(_WIN32) || defined(_WIN64)
174 static_assert((StorageSize >=
sizeof(Impl)),
"EventAutoReset::StorageSize must be increased!");
175 static_assert(((StorageAlign %
alignof(Impl)) == 0),
"EventAutoReset::StorageAlign must be adjusted!");
178 new(&_storage)Impl(signaled);
184 reinterpret_cast<Impl*
>(&_storage)->~Impl();
bool TryWait()
Try to wait the event without block.
void Wait()
Try to wait the event with block.
void Signal()
Signal one of waiting thread about event occurred.
EventAutoReset(bool signaled=false)
Default class constructor.
bool TryWaitFor(const Timespan ×pan)
Try to wait the event for the given timespan.
Aligned storage validator.
#define throwex
Throw extended exception macro.
Auto-reset event synchronization primitive definition.
Fatal abort execution definition.
#define fatality(...)
Fatal abort execution extended macro.
C++ Common project definitions.
Aligned storage validator definition.