Belle II Software  release-05-01-25
Cond.cc
1 #include "daq/slc/system/Cond.h"
2 
3 #include <pthread.h>
4 #include <sys/time.h>
5 
6 using namespace Belle2;
7 
8 Cond::Cond()
9 {
10  init();
11 }
12 
13 Cond::Cond(const Cond& cond)
14 {
15  m_cond_t = cond.m_cond_t;
16 }
17 
18 bool Cond::init()
19 {
20  pthread_condattr_t mattr;
21  pthread_condattr_init(&mattr);
22  pthread_condattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
23  if (pthread_cond_init(&m_cond_t, &mattr) != 0) {
24  return false;
25  }
26  pthread_condattr_destroy(&mattr);
27 
28  return true;
29 }
30 
31 bool Cond::signal()
32 {
33  if (pthread_cond_signal(&m_cond_t) == 0) return true;
34  else return false;
35 }
36 
37 bool Cond::broadcast()
38 {
39  if (pthread_cond_broadcast(&m_cond_t) == 0) return true;
40  else return false;
41 }
42 
43 bool Cond::wait(Mutex& mutex)
44 {
45  if (pthread_cond_wait(&m_cond_t, &mutex.m_mu) != 0) {
46  return false;
47  }
48  return true;
49 }
50 
51 bool Cond::wait(Mutex& mutex, const unsigned int sec, const unsigned int msec)
52 {
53  struct timeval now;
54  struct timespec timeout;
55 
56  gettimeofday(&now, NULL);
57  timeout.tv_sec = now.tv_sec + sec;
58  timeout.tv_nsec = now.tv_usec * 1000 + msec;
59  int stat = 0;
60  if ((stat = pthread_cond_timedwait(&m_cond_t, &mutex.m_mu, &timeout)) != 0) {
61  return false;
62  }
63  return true;
64 }
65 
66 bool Cond::destroy()
67 {
68  if (pthread_cond_destroy(&m_cond_t) == 0) return true;
69  else return false;
70 }
Belle2::Mutex
Definition: Mutex.h:12
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Cond
Definition: Cond.h:12