Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "Condition.h"
00015
00016 #include <sys/time.h>
00017 #include <iostream>
00018
00019 using namespace std;
00020
00021
00022 Condition::Condition()
00023 {
00024 m_Condition = new TCondition;
00025 pthread_cond_init ( m_Condition, NULL );
00026 m_SignalArrived = false;
00027 }
00028
00029
00030 Condition::~Condition()
00031 {
00032 pthread_cond_destroy ( m_Condition );
00033 delete m_Condition;
00034 }
00035
00036
00037 void Condition::wait ( Mutex& mutex )
00038 {
00039 m_SignalArrivedMutex.lock();
00040 if ( m_SignalArrived )
00041 {
00042 m_SignalArrived = false;
00043 m_SignalArrivedMutex.unlock();
00044 }
00045 else
00046 {
00047 mutex.unlock();
00048 pthread_cond_wait ( m_Condition, m_SignalArrivedMutex.getMutex() );
00049 mutex.lock();
00050 m_SignalArrived = false;
00051 m_SignalArrivedMutex.unlock();
00052 }
00053 }
00054
00055
00056 void Condition::wait( Mutex& mutex, unsigned int maxTime )
00057 {
00058 m_SignalArrivedMutex.lock();
00059 if ( m_SignalArrived || (maxTime==0) )
00060 {
00061 m_SignalArrived = false;
00062 m_SignalArrivedMutex.unlock();
00063 }
00064 else
00065 {
00066 struct timeval currentTime;
00067 gettimeofday ( ¤tTime, NULL );
00068
00069 long int sumUsec = currentTime.tv_usec + (maxTime*1000);
00070
00071 struct timespec endTime;
00072 endTime.tv_sec = currentTime.tv_sec + ( sumUsec / 1000000 );
00073 endTime.tv_nsec = ( sumUsec % 1000000 ) * 1000;
00074
00075 mutex.unlock();
00076 pthread_cond_timedwait ( m_Condition, m_SignalArrivedMutex.getMutex(), &endTime );
00077 mutex.lock();
00078 m_SignalArrived = false;
00079 m_SignalArrivedMutex.unlock();
00080 }
00081 }
00082
00083
00084 void Condition::signal()
00085 {
00086 m_SignalArrivedMutex.lock();
00087 m_SignalArrived = true;
00088 pthread_cond_signal ( m_Condition );
00089 m_SignalArrivedMutex.unlock();
00090 }
00091
00092
00093 void Condition::broadcast()
00094 {
00095 m_SignalArrivedMutex.lock();
00096 m_SignalArrived = true;
00097 pthread_cond_broadcast ( m_Condition );
00098 m_SignalArrivedMutex.unlock();
00099 }
00100