16 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
18 #elif defined(_WIN32) || defined(_WIN64)
28 class EventManualReset::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 manual-reset event!", result);
37 result = pthread_cond_init(&_cond,
nullptr);
39 throwex SystemException(
"Failed to initialize a conditional variable for the manual-reset event!", result);
41 #elif defined(_WIN32) || defined(_WIN64)
42 _event = CreateEvent(
nullptr, TRUE, signaled ? TRUE : FALSE,
nullptr);
43 if (_event ==
nullptr)
44 throwex SystemException(
"Failed to create a manual-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 manual-reset event!", result));
54 result = pthread_cond_destroy(&_cond);
56 fatality(SystemException(
"Failed to destroy a conditional variable for the manual-reset event!", result));
57 #elif defined(_WIN32) || defined(_WIN64)
58 if (!CloseHandle(_event))
59 fatality(SystemException(
"Failed to close a manual-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 manual-reset event!", result);
70 result = pthread_mutex_unlock(&_mutex);
72 throwex SystemException(
"Failed to unlock a mutex for the manual-reset event!", result);
73 #elif defined(_WIN32) || defined(_WIN64)
74 if (!ResetEvent(_event))
75 throwex SystemException(
"Failed to reset a manual-reset event!");
81 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
82 int result = pthread_mutex_lock(&_mutex);
84 throwex SystemException(
"Failed to lock a mutex for the manual-reset event!", result);
86 result = pthread_mutex_unlock(&_mutex);
88 throwex SystemException(
"Failed to unlock a mutex for the manual-reset event!", result);
89 result = pthread_cond_broadcast(&_cond);
91 throwex SystemException(
"Failed to signal an manual-reset event!", result);
92 #elif defined(_WIN32) || defined(_WIN64)
93 if (!SetEvent(_event))
94 throwex SystemException(
"Failed to signal a manual-reset event!");
100 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
101 int result = pthread_mutex_lock(&_mutex);
103 throwex SystemException(
"Failed to lock a mutex for the manual-reset event!", result);
104 bool signaled = _signaled;
105 result = pthread_mutex_unlock(&_mutex);
107 throwex SystemException(
"Failed to unlock a mutex for the manual-reset event!", result);
109 #elif defined(_WIN32) || defined(_WIN64)
110 DWORD result = WaitForSingleObject(_event, 0);
111 if ((result != WAIT_OBJECT_0) && (result != WAIT_TIMEOUT))
112 throwex SystemException(
"Failed to try lock a manual-reset event!");
113 return (result == WAIT_OBJECT_0);
121 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
122 struct timespec timeout;
123 timeout.tv_sec = timespan.seconds();
124 timeout.tv_nsec = timespan.nanoseconds() % 1000000000;
125 int result = pthread_mutex_lock(&_mutex);
127 throwex SystemException(
"Failed to lock a mutex for the manual-reset event!", result);
128 bool signaled =
true;
131 result = pthread_cond_timedwait(&_cond, &_mutex, &timeout);
132 if ((result != 0) && (result != ETIMEDOUT))
133 throwex SystemException(
"Failed to timeout waiting a conditional variable for the manual-reset event!", result);
134 if (result == ETIMEDOUT)
135 signaled = _signaled;
137 result = pthread_mutex_unlock(&_mutex);
139 throwex SystemException(
"Failed to unlock a mutex for the manual-reset event!", result);
141 #elif defined(_WIN32) || defined(_WIN64)
142 DWORD result = WaitForSingleObject(_event, std::max((DWORD)1, (DWORD)timespan.milliseconds()));
143 if ((result != WAIT_OBJECT_0) && (result != WAIT_TIMEOUT))
144 throwex SystemException(
"Failed to try lock a manual-reset event for the given timeout!");
145 return (result == WAIT_OBJECT_0);
151 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
152 int result = pthread_mutex_lock(&_mutex);
154 throwex SystemException(
"Failed to lock a mutex for the manual-reset event!", result);
157 result = pthread_cond_wait(&_cond, &_mutex);
159 throwex SystemException(
"Failed to waiting a conditional variable for the manual-reset event!", result);
161 result = pthread_mutex_unlock(&_mutex);
163 throwex SystemException(
"Failed to unlock a mutex for the manual-reset event!", result);
164 #elif defined(_WIN32) || defined(_WIN64)
165 DWORD result = WaitForSingleObject(_event, INFINITE);
166 if (result != WAIT_OBJECT_0)
167 throwex SystemException(
"Failed to lock a manual-reset event!");
172 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
173 pthread_mutex_t _mutex;
174 pthread_cond_t _cond;
176 #elif defined(_WIN32) || defined(_WIN64)
187 static_assert((StorageSize >=
sizeof(Impl)),
"EventManualReset::StorageSize must be increased!");
188 static_assert(((StorageAlign %
alignof(Impl)) == 0),
"EventManualReset::StorageAlign must be adjusted!");
191 new(&_storage)Impl(signaled);
197 reinterpret_cast<Impl*
>(&_storage)->~Impl();
bool TryWait()
Try to wait the event without block.
void Reset()
Reset the event.
bool TryWaitFor(const Timespan ×pan)
Try to wait the event for the given timespan.
EventManualReset(bool signaled=false)
Default class constructor.
void Wait()
Try to wait the event with block.
void Signal()
Signal one of waiting thread about event occurred.
Aligned storage validator.
#define throwex
Throw extended exception macro.
Manual-reset event synchronization primitive definition.
Fatal abort execution definition.
#define fatality(...)
Fatal abort execution extended macro.
C++ Common project definitions.
Aligned storage validator definition.