24#include <core/exception.h>
25#include <core/threading/mutex.h>
26#include <core/threading/mutex_data.h>
27#include <core/threading/wait_condition.h>
31#if defined(__MACH__) && defined(__APPLE__)
38class WaitConditionData
45cleanup_mutex(
void *arg)
47 Mutex *mutex = (Mutex *)arg;
111 cond_data_ =
new WaitConditionData();
112 pthread_cond_init(&(cond_data_->cond), NULL);
117 mutex_ =
new Mutex();
125 pthread_cond_destroy(&(cond_data_->cond));
144 pthread_cleanup_push(cleanup_mutex, mutex_);
145 err = pthread_cond_wait(&(cond_data_->cond), &(mutex_->mutex_data->mutex));
147 pthread_cleanup_pop(0);
149 err = pthread_cond_wait(&(cond_data_->cond), &(mutex_->mutex_data->mutex));
152 throw Exception(err,
"Waiting for wait condition failed");
172 struct timespec ts = {sec, nanosec};
176 pthread_cleanup_push(cleanup_mutex, mutex_);
177 err = pthread_cond_timedwait(&(cond_data_->cond), &(mutex_->mutex_data->mutex), &ts);
179 pthread_cleanup_pop(0);
181 err = pthread_cond_timedwait(&(cond_data_->cond), &(mutex_->mutex_data->mutex), &ts);
184 if (err == ETIMEDOUT) {
186 }
else if (err != 0) {
188 throw Exception(err,
"Waiting for wait condition failed");
208 if (!(sec || nanosec)) {
213#if defined(__MACH__) && defined(__APPLE__)
215 if (gettimeofday(&nowt, NULL) != 0) {
216 throw Exception(errno,
"WaitCondition::reltimed_wait: Failed to get current time");
218 now.tv_sec = nowt.tv_sec;
219 now.tv_nsec = nowt.tv_usec * 1000;
221 if (clock_gettime(CLOCK_REALTIME, &now) != 0) {
222 throw Exception(errno,
"WaitCondition::reltimed_wait: Failed to get current time");
226 long int s = now.tv_sec + sec;
227 long int ns = now.tv_nsec + nanosec;
228 if (ns >= 1000000000) {
233 struct timespec ts = {s, ns};
238 pthread_cleanup_push(cleanup_mutex, mutex_);
239 err = pthread_cond_timedwait(&(cond_data_->cond), &(mutex_->mutex_data->mutex), &ts);
241 pthread_cleanup_pop(0);
243 err = pthread_cond_timedwait(&(cond_data_->cond), &(mutex_->mutex_data->mutex), &ts);
246 if (err == ETIMEDOUT) {
248 }
else if (err != 0) {
250 throw Exception(err,
"Waiting for wait condition failed");
271 pthread_cond_signal(&(cond_data_->cond));
274 pthread_cond_signal(&(cond_data_->cond));
291 pthread_cond_broadcast(&(cond_data_->cond));
294 pthread_cond_broadcast(&(cond_data_->cond));
Base class for exceptions in Fawkes.
Mutex mutual exclusion lock.
void lock()
Lock this mutex.
void unlock()
Unlock the mutex.
void wait()
Wait for the condition forever.
~WaitCondition()
Destructor.
void wake_one()
Wake another thread waiting for this condition.
WaitCondition(Mutex *mutex=0)
Constructor.
void wake_all()
Wake up all waiting threads.
bool reltimed_wait(unsigned int sec, unsigned int nanosec)
Wait with relative timeout.
bool abstimed_wait(long int sec, long int nanosec)
Wait with absolute timeout.
Fawkes library namespace.