#include "GCondition.h"
#include <errno.h>
#include <sys/time.h>
#include <unistd.h>
ClassImp(GCondition);
GCondition::GCondition(GMutex::Init_e e) : GMutex(e)
{
pthread_cond_init(&mCond, 0);
}
GCondition::~GCondition()
{
pthread_cond_destroy(&mCond);
}
Int_t GCondition::Wait()
{
int ret = pthread_cond_wait(&mCond, &mMut);
return ret;
}
Int_t GCondition::TimedWaitMS(UInt_t wait_ms)
{
struct timeval now;
struct timespec timeout;
gettimeofday(&now, 0);
int mus_add = now.tv_usec + 1000*wait_ms;
timeout.tv_sec = now.tv_sec + mus_add/1000000;
timeout.tv_nsec = (mus_add%1000000) * 1000;
int retcode = pthread_cond_timedwait(&mCond, &mMut, &timeout);
if(retcode == ETIMEDOUT) {
return 1;
} else {
return 0;
}
}
Int_t GCondition::TimedWaitMuS(UInt_t wait_mus)
{
struct timeval now;
struct timespec timeout;
gettimeofday(&now, 0);
int mus_add = now.tv_usec + wait_mus;
timeout.tv_sec = now.tv_sec + mus_add/1000000;
timeout.tv_nsec = (mus_add%1000000) * 1000;
int retcode = pthread_cond_timedwait(&mCond, &mMut, &timeout);
if(retcode == ETIMEDOUT) {
return 1;
} else {
return 0;
}
}
Int_t GCondition::Signal()
{
int ret = pthread_cond_signal(&mCond);
return ret;
}
Int_t GCondition::Broadcast()
{
int ret = pthread_cond_broadcast(&mCond);
return ret;
}
Int_t GCondition::LockSignal()
{
Lock();
int ret = pthread_cond_signal(&mCond);
Unlock();
return ret;
}
Int_t GCondition::LockBroadcast()
{
Lock();
int ret = pthread_cond_broadcast(&mCond);
Unlock();
return ret;
}