Belle II Software development
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
13using namespace Belle2;
14
15Cond::Cond()
16{
17 init();
18}
19
20Cond::Cond(const Cond& cond) : m_cond_t(cond.m_cond_t)
21{
22}
23
24bool Cond::init()
25{
26 pthread_condattr_t mattr;
27 pthread_condattr_init(&mattr);
28 pthread_condattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
29 if (pthread_cond_init(&m_cond_t, &mattr) != 0) {
30 return false;
31 }
32 pthread_condattr_destroy(&mattr);
33
34 return true;
35}
36
37bool Cond::signal()
38{
39 if (pthread_cond_signal(&m_cond_t) == 0) return true;
40 else return false;
41}
42
43bool Cond::broadcast()
44{
45 if (pthread_cond_broadcast(&m_cond_t) == 0) return true;
46 else return false;
47}
48
49bool Cond::wait(Mutex& mutex)
50{
51 if (pthread_cond_wait(&m_cond_t, &mutex.m_mu) != 0) {
52 return false;
53 }
54 return true;
55}
56
57bool Cond::wait(Mutex& mutex, const unsigned int sec, const unsigned int msec)
58{
59 struct timeval now;
60 struct timespec timeout;
61
62 gettimeofday(&now, NULL);
63 timeout.tv_sec = now.tv_sec + sec;
64 timeout.tv_nsec = now.tv_usec * 1000 + msec;
65 if (pthread_cond_timedwait(&m_cond_t, &mutex.m_mu, &timeout) != 0) { // check on stat code
66 return false;
67 }
68 return true;
69}
70
71bool Cond::destroy()
72{
73 if (pthread_cond_destroy(&m_cond_t) == 0) return true;
74 else return false;
75}
Abstract base class for different kinds of events.