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