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