new class: ConditionVariable, Mutex has getPThreadMutex() method

master
Denes Matetelki 14 years ago
parent 4082b369d7
commit a39371125e

@ -4,5 +4,6 @@
#define TRACE(x) std::cout << x << " " << __PRETTY_FUNCTION__ << \ #define TRACE(x) std::cout << x << " " << __PRETTY_FUNCTION__ << \
" @ " << __FILE__ << " : " << __LINE__ << std::endl " @ " << __FILE__ << " : " << __LINE__ << std::endl
#else #else
/// @todo Get rid of the "warning: statement has no effect" compiler msgs
#define TRACE(x) "" #define TRACE(x) ""
#endif #endif

@ -0,0 +1,29 @@
#ifndef CONDITION_VARIABLE_HPP
#define CONDITION_VARIABLE_HPP
#include "Mutex.hpp"
#include <pthread.h> // pthread_cond
class ConditionVariable
{
public:
ConditionVariable(Mutex* m);
~ConditionVariable();
void wait(const int interval = 0);
void signal();
void broadcast();
private:
ConditionVariable(const ConditionVariable&);
ConditionVariable& operator=(const ConditionVariable&);
Mutex* m_mutex;
pthread_cond_t m_condVar;
};
#endif // CONDITION_VARIABLE_HPP

@ -14,6 +14,7 @@ public:
void lock(); void lock();
void unlock(); void unlock();
bool tryLock(int interval = 0); bool tryLock(int interval = 0);
pthread_mutex_t* getPThreadMutex();
private: private:

@ -0,0 +1,49 @@
#include "ConditionVariable.hpp"
#include "Common.hpp"
#include <time.h>
ConditionVariable::ConditionVariable(Mutex* m)
: m_mutex(m)
, m_condVar(PTHREAD_COND_INITIALIZER)
{
TRACE(this);
pthread_cond_init( &m_condVar, 0 );
}
ConditionVariable::~ConditionVariable()
{
TRACE(this);
pthread_cond_destroy( &m_condVar );
}
void ConditionVariable::wait(const int interval)
{
TRACE(this);
if ( interval == 0 ) {
pthread_cond_wait( &m_condVar, m_mutex->getPThreadMutex() );
} else {
timespec abs_time;
clock_gettime ( CLOCK_REALTIME, &abs_time );
abs_time.tv_nsec += interval * 1000000;
if ( abs_time.tv_nsec >= 1000000000 ) {
abs_time.tv_nsec -= 1000000000;
abs_time.tv_sec += 1;
}
pthread_cond_timedwait( &m_condVar, m_mutex->getPThreadMutex(), &abs_time );
}
}
void ConditionVariable::signal()
{
TRACE(this);
pthread_cond_signal( &m_condVar );
}
void ConditionVariable::broadcast()
{
TRACE(this);
pthread_cond_broadcast( &m_condVar );
}

@ -64,3 +64,8 @@ bool Mutex::tryLock(int interval)
} }
} }
pthread_mutex_t* Mutex::getPThreadMutex()
{
return &m_mutex;
}

Loading…
Cancel
Save