24 #include <core/threading/wait_condition.h> 25 #include <core/threading/mutex.h> 26 #include <core/threading/mutex_data.h> 27 #include <core/exception.h> 31 #if defined(__MACH__) && defined(__APPLE__) 32 # include <sys/time.h> 38 class WaitConditionData
45 cleanup_mutex(
void *arg)
47 Mutex *mutex = (Mutex *) arg;
113 __cond_data =
new WaitConditionData();
114 pthread_cond_init( &(__cond_data->cond), NULL);
119 __mutex =
new Mutex();
128 pthread_cond_destroy( &(__cond_data->cond) );
148 pthread_cleanup_push(cleanup_mutex, __mutex);
149 err = pthread_cond_wait( &(__cond_data->cond), &(__mutex->mutex_data->mutex) );
151 pthread_cleanup_pop(0);
153 err = pthread_cond_wait( &(__cond_data->cond), &(__mutex->mutex_data->mutex) );
156 throw Exception(err,
"Waiting for wait condition failed");
177 struct timespec ts = { sec, nanosec };
181 pthread_cleanup_push(cleanup_mutex, __mutex);
182 err = pthread_cond_timedwait( &(__cond_data->cond), &(__mutex->mutex_data->mutex), &ts );
184 pthread_cleanup_pop(0);
186 err = pthread_cond_timedwait( &(__cond_data->cond), &(__mutex->mutex_data->mutex), &ts );
189 if ( err == ETIMEDOUT ) {
191 }
else if ( err != 0 ) {
193 throw Exception(err,
"Waiting for wait condition failed");
214 if ( ! (sec || nanosec) ) {
219 #if defined(__MACH__) && defined(__APPLE__) 221 if ( gettimeofday(&nowt, NULL) != 0 ) {
222 throw Exception(errno,
"WaitCondition::reltimed_wait: Failed to get current time");
224 now.tv_sec = nowt.tv_sec;
225 now.tv_nsec = nowt.tv_usec * 1000;
227 if ( clock_gettime(CLOCK_REALTIME, &now) != 0 ) {
228 throw Exception(errno,
"WaitCondition::reltimed_wait: Failed to get current time");
232 long int s = now.tv_sec + sec;
233 long int ns = now.tv_nsec + nanosec;
234 if (ns >= 1000000000) {
239 struct timespec ts = { s, ns };
244 pthread_cleanup_push(cleanup_mutex, __mutex);
245 err = pthread_cond_timedwait( &(__cond_data->cond), &(__mutex->mutex_data->mutex), &ts );
247 pthread_cleanup_pop(0);
249 err = pthread_cond_timedwait( &(__cond_data->cond), &(__mutex->mutex_data->mutex), &ts );
252 if ( err == ETIMEDOUT ) {
254 }
else if ( err != 0 ) {
256 throw Exception(err,
"Waiting for wait condition failed");
278 pthread_cond_signal( &(__cond_data->cond) );
281 pthread_cond_signal( &(__cond_data->cond) );
299 pthread_cond_broadcast( &(__cond_data->cond) );
302 pthread_cond_broadcast( &(__cond_data->cond) );
bool reltimed_wait(unsigned int sec, unsigned int nanosec)
Wait with relative timeout.
WaitCondition(Mutex *mutex=0)
Constructor.
~WaitCondition()
Destructor.
Fawkes library namespace.
void wake_all()
Wake up all waiting threads.
void wake_one()
Wake another thread waiting for this condition.
Base class for exceptions in Fawkes.
void wait()
Wait for the condition forever.
Mutex mutual exclusion lock.
bool abstimed_wait(long int sec, long int nanosec)
Wait with absolute timeout.