00001 // -*- C++ -*- 00020 #ifndef COIL_CONDITION_H 00021 #define COIL_CONDITION_H 00022 00023 #include <coil/config_coil.h> 00024 #if defined (WIN32) 00025 #pragma warning( disable : 4244 ) 00026 #pragma warning( disable : 4312 ) 00027 #endif 00028 #include <ace/OS_NS_Thread.h> 00029 #if defined (WIN32) 00030 #pragma warning( default : 4244 ) 00031 #pragma warning( default : 4312 ) 00032 #endif 00033 #include <coil/TimeValue.h> 00034 #include <coil/Time.h> 00035 00036 namespace coil 00037 { 00038 template <class Mutex> 00039 class Condition 00040 { 00041 public: 00042 Condition(Mutex& mutex) 00043 : m_mutex(mutex) 00044 { 00045 ACE_OS::cond_init(&m_cond, 0); 00046 } 00047 00048 ~Condition() 00049 { 00050 ACE_OS::cond_destroy(&this->m_cond); 00051 } 00052 00053 inline void signal() 00054 { 00055 m_mutex.trylock(); 00056 ACE_OS::cond_signal(&m_cond); 00057 } 00058 00059 inline void broadcast() 00060 { 00061 m_mutex.trylock(); 00062 ACE_OS::cond_broadcast(&m_cond); 00063 } 00064 00065 bool wait() 00066 { 00067 m_mutex.trylock(); 00068 return 0 == ACE_OS::cond_wait(&m_cond, &m_mutex.mutex_); 00069 } 00070 00071 bool wait(long second, long nano_second = 0) 00072 { 00073 m_mutex.trylock(); 00074 coil::TimeValue now(coil::gettimeofday()); 00075 coil::TimeValue abst(second, nano_second * 1000); 00076 abst += now; 00077 ACE_Time_Value abstime(abst.sec(), abst.usec()); 00078 return 0 == ACE_OS::cond_timedwait(m_cond, &m_mutex.mutex_, &abstime); 00079 } 00080 00081 private: 00082 Condition(const Mutex&); 00083 Condition& operator=(const Mutex &); 00084 ACE_cond_t m_cond; 00085 Mutex& m_mutex; 00086 }; 00087 }; 00088 #endif // COIL_CONDITION_H