00001 // -*- C++ -*- 00020 #ifndef COIL_CONDITION_H 00021 #define COIL_CONDITION_H 00022 00023 #include <pthread.h> 00024 #include <algorithm> 00025 #include <ctime> 00026 00027 00028 namespace coil 00029 { 00043 template <class M> 00044 class Condition 00045 { 00046 public: 00047 00063 Condition(M& mutex) 00064 : m_mutex(mutex) 00065 { 00066 ::pthread_cond_init(&m_cond, 0); 00067 } 00068 00084 ~Condition() 00085 { 00086 ::pthread_cond_destroy(&m_cond); 00087 } 00088 00104 inline void signal() 00105 { 00106 ::pthread_cond_signal(&m_cond); 00107 } 00108 00124 inline void broadcast() 00125 { 00126 ::pthread_cond_broadcast(&m_cond); 00127 } 00128 00148 bool wait() 00149 { 00150 return 0 == ::pthread_cond_wait(&m_cond, &m_mutex.mutex_); 00151 } 00152 00178 bool wait(long second, long nano_second = 0) 00179 { 00180 timespec abstime; 00181 abstime.tv_sec = std::time(0) + second; 00182 abstime.tv_nsec = nano_second; 00183 return 0 == ::pthread_cond_timedwait(&m_cond, &m_mutex.mutex_, &abstime); 00184 } 00185 00186 private: 00187 Condition(const M&); 00188 Condition& operator=(const M &); 00189 pthread_cond_t m_cond; 00190 M& m_mutex; 00191 }; 00192 }; 00193 #endif // COIL_CONDITION_H