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