00001
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 {
00030 template <class M>
00031 class Condition
00032 {
00033 public:
00034 Condition(M& mutex)
00035 : m_mutex(mutex)
00036 {
00037 ::pthread_cond_init(&m_cond, 0);
00038 }
00039 ~Condition()
00040 {
00041 ::pthread_cond_destroy(&m_cond);
00042 }
00043
00044 inline void signal()
00045 {
00046 ::pthread_cond_signal(&m_cond);
00047 }
00048
00049 inline void broadcast()
00050 {
00051 ::pthread_cond_broadcast(&m_cond);
00052 }
00053
00054 bool wait()
00055 {
00056 return 0 == ::pthread_cond_wait(&m_cond, &m_mutex.mutex_);
00057 }
00058
00059 bool wait(long second, long nano_second = 0)
00060 {
00061 timespec abstime;
00062 abstime.tv_sec = std::time(0) + second;
00063 abstime.tv_nsec = nano_second;
00064 return 0 == ::pthread_cond_timedwait(&m_cond, &m_mutex.mutex_, &abstime);
00065 }
00066
00067 private:
00068 Condition(const M&);
00069 Condition& operator=(const M &);
00070 pthread_cond_t m_cond;
00071 M& m_mutex;
00072 };
00073 };
00074 #endif // COIL_CONDITION_H