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
106 __cond_data =
new WaitConditionData();
107 pthread_cond_init( &(__cond_data->cond), NULL);
112 __mutex =
new Mutex();
121 pthread_cond_destroy( &(__cond_data->cond) );
141 err = pthread_cond_wait( &(__cond_data->cond), &(__mutex->mutex_data->mutex) );
144 err = pthread_cond_wait( &(__cond_data->cond), &(__mutex->mutex_data->mutex) );
147 throw Exception(err,
"Waiting for wait condition failed");
168 struct timespec ts = { sec, nanosec };
172 err = pthread_cond_timedwait( &(__cond_data->cond), &(__mutex->mutex_data->mutex), &ts );
175 err = pthread_cond_timedwait( &(__cond_data->cond), &(__mutex->mutex_data->mutex), &ts );
178 if ( err == ETIMEDOUT ) {
180 }
else if ( err != 0 ) {
182 throw Exception(err,
"Waiting for wait condition failed");
203 if ( ! (sec || nanosec) ) {
208 #if defined(__MACH__) && defined(__APPLE__)
210 if ( gettimeofday(&nowt, NULL) != 0 ) {
211 throw Exception(errno,
"WaitCondition::reltimed_wait: Failed to get current time");
213 now.tv_sec = nowt.tv_sec;
214 now.tv_nsec = nowt.tv_usec * 1000;
216 if ( clock_gettime(CLOCK_REALTIME, &now) != 0 ) {
217 throw Exception(errno,
"WaitCondition::reltimed_wait: Failed to get current time");
221 long int s = now.tv_sec + sec;
222 long int ns = now.tv_nsec + nanosec;
223 if (ns >= 1000000000) {
228 struct timespec ts = { s, ns };
233 err = pthread_cond_timedwait( &(__cond_data->cond), &(__mutex->mutex_data->mutex), &ts );
236 err = pthread_cond_timedwait( &(__cond_data->cond), &(__mutex->mutex_data->mutex), &ts );
239 if ( err == ETIMEDOUT ) {
241 }
else if ( err != 0 ) {
243 throw Exception(err,
"Waiting for wait condition failed");
265 pthread_cond_signal( &(__cond_data->cond) );
268 pthread_cond_signal( &(__cond_data->cond) );
286 pthread_cond_broadcast( &(__cond_data->cond) );
289 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.
void unlock()
Unlock the mutex.
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.
void lock()
Lock this mutex.
Mutex mutual exclusion lock.
bool abstimed_wait(long int sec, long int nanosec)
Wait with absolute timeout.