16 #if (defined(unix) || defined(__unix) || defined(__unix__)) && !defined(__APPLE__) && !defined(__CYGWIN__)
19 #elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
29 class NamedEventManualReset::Impl
32 Impl(
const std::string&
name,
bool signaled) : _name(
name)
33 #if (defined(unix) || defined(__unix) || defined(__unix__)) && !defined(__APPLE__) && !defined(__CYGWIN__)
37 #if defined(__APPLE__)
38 throwex SystemException(
"Named manual-reset event is not supported!");
39 #elif (defined(unix) || defined(__unix) || defined(__unix__)) && !defined(__CYGWIN__)
43 pthread_mutexattr_t mutex_attribute;
44 int result = pthread_mutexattr_init(&mutex_attribute);
46 throwex SystemException(
"Failed to initialize a mutex attribute for the named manual-reset event!", result);
47 result = pthread_mutexattr_setpshared(&mutex_attribute, PTHREAD_PROCESS_SHARED);
49 throwex SystemException(
"Failed to set a mutex process shared attribute for the named manual-reset event!", result);
50 result = pthread_mutex_init(&_shared->mutex, &mutex_attribute);
52 throwex SystemException(
"Failed to initialize a mutex for the named manual-reset event!", result);
53 result = pthread_mutexattr_destroy(&mutex_attribute);
55 throwex SystemException(
"Failed to destroy a mutex attribute for the named manual-reset event!", result);
57 pthread_condattr_t cond_attribute;
58 result = pthread_condattr_init(&cond_attribute);
60 throwex SystemException(
"Failed to initialize a conditional variable attribute for the named manual-reset event!", result);
61 result = pthread_condattr_setpshared(&cond_attribute, PTHREAD_PROCESS_SHARED);
63 throwex SystemException(
"Failed to set a conditional variable process shared attribute for the named manual-reset event!", result);
64 result = pthread_cond_init(&_shared->cond, &cond_attribute);
66 throwex SystemException(
"Failed to initialize a conditional variable for the named manual-reset event!", result);
67 result = pthread_condattr_destroy(&cond_attribute);
69 throwex SystemException(
"Failed to destroy a conditional variable attribute for the named manual-reset event!", result);
71 _shared->signaled = signaled;
73 #elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
74 _event = CreateEventA(
nullptr, TRUE, signaled ? TRUE : FALSE,
name.c_str());
75 if (_event ==
nullptr)
76 throwex SystemException(
"Failed to create or open a named manual-reset event!");
82 #if defined(__APPLE__)
83 fatality(SystemException(
"Named manual-reset event is not supported!"));
84 #elif (defined(unix) || defined(__unix) || defined(__unix__)) && !defined(__CYGWIN__)
88 int result = pthread_mutex_destroy(&_shared->mutex);
90 fatality(SystemException(
"Failed to destroy a mutex for the named manual-reset event!", result));
91 result = pthread_cond_destroy(&_shared->cond);
93 fatality(SystemException(
"Failed to destroy a conditional variable for the named manual-reset event!", result));
95 #elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
96 if (!CloseHandle(_event))
97 fatality(SystemException(
"Failed to close a named manual-reset event!"));
101 const std::string&
name()
const
108 #if defined(__APPLE__)
109 throwex SystemException(
"Named manual-reset event is not supported!");
110 #elif (defined(unix) || defined(__unix) || defined(__unix__)) && !defined(__CYGWIN__)
111 int result = pthread_mutex_lock(&_shared->mutex);
113 throwex SystemException(
"Failed to lock a mutex for the named manual-reset event!", result);
114 _shared->signaled =
false;
115 result = pthread_mutex_unlock(&_shared->mutex);
117 throwex SystemException(
"Failed to unlock a mutex for the named manual-reset event!", result);
118 #elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
119 if (!ResetEvent(_event))
120 throwex SystemException(
"Failed to reset a named manual-reset event!");
126 #if defined(__APPLE__)
127 throwex SystemException(
"Named manual-reset event is not supported!");
128 #elif (defined(unix) || defined(__unix) || defined(__unix__)) && !defined(__CYGWIN__)
129 int result = pthread_mutex_lock(&_shared->mutex);
131 throwex SystemException(
"Failed to lock a mutex for the named manual-reset event!", result);
132 _shared->signaled =
true;
133 result = pthread_mutex_unlock(&_shared->mutex);
135 throwex SystemException(
"Failed to unlock a mutex for the named manual-reset event!", result);
136 result = pthread_cond_broadcast(&_shared->cond);
138 throwex SystemException(
"Failed to signal an named manual-reset event!", result);
139 #elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
140 if (!SetEvent(_event))
141 throwex SystemException(
"Failed to signal a named manual-reset event!");
147 #if defined(__APPLE__)
148 throwex SystemException(
"Named manual-reset event is not supported!");
149 #elif (defined(unix) || defined(__unix) || defined(__unix__)) && !defined(__CYGWIN__)
150 int result = pthread_mutex_lock(&_shared->mutex);
152 throwex SystemException(
"Failed to lock a mutex for the named manual-reset event!", result);
153 bool signaled = _shared->signaled;
154 result = pthread_mutex_unlock(&_shared->mutex);
156 throwex SystemException(
"Failed to unlock a mutex for the named manual-reset event!", result);
158 #elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
159 DWORD result = WaitForSingleObject(_event, 0);
160 if ((result != WAIT_OBJECT_0) && (result != WAIT_TIMEOUT))
161 throwex SystemException(
"Failed to try lock a named manual-reset event!");
162 return (result == WAIT_OBJECT_0);
170 #if defined(__APPLE__)
171 throwex SystemException(
"Named manual-reset event is not supported!");
172 #elif (defined(unix) || defined(__unix) || defined(__unix__)) && !defined(__CYGWIN__)
173 struct timespec timeout;
174 timeout.tv_sec = timespan.seconds();
175 timeout.tv_nsec = timespan.nanoseconds() % 1000000000;
176 int result = pthread_mutex_lock(&_shared->mutex);
178 throwex SystemException(
"Failed to lock a mutex for the named manual-reset event!", result);
179 bool signaled =
true;
180 while (!_shared->signaled)
182 result = pthread_cond_timedwait(&_shared->cond, &_shared->mutex, &timeout);
183 if ((result != 0) && (result != ETIMEDOUT))
184 throwex SystemException(
"Failed to timeout waiting a conditional variable for the named manual-reset event!", result);
185 if (result == ETIMEDOUT)
186 signaled = _shared->signaled;
188 result = pthread_mutex_unlock(&_shared->mutex);
190 throwex SystemException(
"Failed to unlock a mutex for the named manual-reset event!", result);
192 #elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
193 DWORD result = WaitForSingleObject(_event, std::max((DWORD)1, (DWORD)timespan.milliseconds()));
194 if ((result != WAIT_OBJECT_0) && (result != WAIT_TIMEOUT))
195 throwex SystemException(
"Failed to try lock a named manual-reset event for the given timeout!");
196 return (result == WAIT_OBJECT_0);
202 #if defined(__APPLE__)
203 throwex SystemException(
"Named manual-reset event is not supported!");
204 #elif (defined(unix) || defined(__unix) || defined(__unix__)) && !defined(__CYGWIN__)
205 int result = pthread_mutex_lock(&_shared->mutex);
207 throwex SystemException(
"Failed to lock a mutex for the named manual-reset event!", result);
208 while (!_shared->signaled)
210 result = pthread_cond_wait(&_shared->cond, &_shared->mutex);
212 throwex SystemException(
"Failed to waiting a conditional variable for the named manual-reset event!", result);
214 result = pthread_mutex_unlock(&_shared->mutex);
216 throwex SystemException(
"Failed to unlock a mutex for the named manual-reset event!", result);
217 #elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
218 DWORD result = WaitForSingleObject(_event, INFINITE);
219 if (result != WAIT_OBJECT_0)
220 throwex SystemException(
"Failed to lock a named manual-reset event!");
226 #if (defined(unix) || defined(__unix) || defined(__unix__)) && !defined(__APPLE__) && !defined(__CYGWIN__)
230 pthread_mutex_t mutex;
236 SharedType<EventHeader> _shared;
237 #elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
248 static_assert((StorageSize >=
sizeof(Impl)),
"NamedEventManualReset::StorageSize must be increased!");
249 static_assert(((StorageAlign %
alignof(Impl)) == 0),
"NamedEventManualReset::StorageAlign must be adjusted!");
252 new(&_storage)Impl(
name, signaled);
258 reinterpret_cast<Impl*
>(&_storage)->~Impl();
void Signal()
Signal one of waiting thread about event occurred.
const std::string & name() const
Get the event name.
void Wait()
Try to wait the event with block.
NamedEventManualReset(const std::string &name, bool signaled=false)
Default class constructor.
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.
Aligned storage validator.
#define throwex
Throw extended exception macro.
Fatal abort execution definition.
#define fatality(...)
Fatal abort execution extended macro.
Named manual-reset event synchronization primitive definition.
C++ Common project definitions.
Shared memory type definition.
Aligned storage validator definition.