OpenRTM-aist  1.2.1
Condition.h
Go to the documentation of this file.
1 // -*- C++ -*-
20 #ifndef COIL_CONDITION_H
21 #define COIL_CONDITION_H
22 
23 #include <pthread.h>
24 #include <algorithm>
25 #include <ctime>
26 #include <sys/time.h>
27 
28 namespace coil
29 {
43  template <class M>
44  class Condition
45  {
46  public:
47 
63  Condition(M& mutex)
64  : m_mutex(mutex)
65  {
66  ::pthread_cond_init(&m_cond, 0);
67  }
68 
85  {
86  ::pthread_cond_destroy(&m_cond);
87  }
88 
104  inline void signal()
105  {
106  ::pthread_cond_signal(&m_cond);
107  }
108 
124  inline void broadcast()
125  {
126  ::pthread_cond_broadcast(&m_cond);
127  }
128 
148  bool wait()
149  {
150  return 0 == ::pthread_cond_wait(&m_cond, &m_mutex.mutex_);
151  }
152 
178  bool wait(long second, long nano_second = 0)
179  {
180  struct timeval tv;
181  timespec abstime;
182 
183  ::gettimeofday(&tv, NULL);
184  abstime.tv_sec = tv.tv_sec + second;
185  abstime.tv_nsec = tv.tv_usec * 1000 + nano_second;
186  if (abstime.tv_nsec >= 1000000000) {
187  abstime.tv_nsec -= 1000000000;
188  abstime.tv_sec ++;
189  }
190  return 0 == ::pthread_cond_timedwait(&m_cond, &m_mutex.mutex_, &abstime);
191  }
192 
193  private:
194  Condition(const M&);
195  Condition& operator=(const M &);
196  pthread_cond_t m_cond;
197  M& m_mutex;
198  };
199 };
200 #endif // COIL_CONDITION_H
bool wait()
Wait of the thread practice.
Definition: Condition.h:148
void signal()
Resume of the thread practice.
Definition: Condition.h:104
int gettimeofday(struct timeval *tv, struct timezone *tz)
Get the time and timezone.
Definition: Time.h:137
bool wait(long second, long nano_second=0)
Thread practice wait of set time.
Definition: Condition.h:178
~Condition()
Destructor.
Definition: Condition.h:84
Condition template class.
Definition: Condition.h:44
Condition(M &mutex)
Constructor.
Definition: Condition.h:63
void broadcast()
Resume of all the thread practice.
Definition: Condition.h:124
Common Object Interface Layer.
Definition: Affinity.h:28