Condition.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "Condition.hxx"
00023 #include "Thread.hxx"
00024 #include "ErrSystem.hxx"
00025 #include "Assert.hxx"
00026 #include "xtime.hxx"
00027 #include <errno.h>
00028 #include <cstdio>
00029 #include <sstream>
00030
00031 namespace CLAM
00032 {
00033
00034 Condition::Condition()
00035 {
00036 int res = 0;
00037
00038 res = pthread_cond_init( &mCondition, 0 );
00039
00040 CLAM_ASSERT( res == 0, "pthread_cond_init call failed" );
00041 }
00042
00043 Condition::~Condition()
00044 {
00045 int res = 0;
00046
00047 res = pthread_cond_destroy( &mCondition );
00048
00049 CLAM_ASSERT( res==0, "pthread_cond_destroy call failed" );
00050 }
00051
00052 void Condition::NotifyAll()
00053 {
00054 int res = 0;
00055
00056 res = pthread_cond_broadcast( &mCondition );
00057
00058 CLAM_ASSERT( res==0, "pthread_cond_broadcast call failed!" );
00059 }
00060
00061 void Condition::NotifyOne()
00062 {
00063 int res = 0;
00064
00065 res = pthread_cond_signal( &mCondition );
00066
00067 CLAM_ASSERT( res == 0, "pthread_cond_signal call failed" );
00068 }
00069
00070 void Condition::DoWait( pthread_mutex_t* pmutex )
00071 {
00072 int res = 0;
00073
00074 res = pthread_cond_wait( &mCondition, pmutex );
00075
00076 CLAM_ASSERT( res == 0, "pthread_cond_wait call failed" );
00077 }
00078
00079 bool Condition::DoTimedWait( const xtime& xt, pthread_mutex_t* pmutex )
00080 {
00081 timespec ts;
00082
00083 to_timespec( xt, ts );
00084
00085 int res = 0;
00086
00087 res = pthread_cond_timedwait( &mCondition, pmutex, &ts );
00088
00089 std::ostringstream os;
00090 os << "Something strange has happened. PThread returned from timed wait with result code " << res << std::flush;
00091 CLAM_ASSERT( res == 0 || res == ETIMEDOUT , os.str().c_str() );
00092
00093 return res != ETIMEDOUT;
00094 }
00095
00096 }
00097