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)
21{
22 m_cond_t = cond.m_cond_t;
23}
24
25bool 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
38bool Cond::signal()
39{
40 if (pthread_cond_signal(&m_cond_t) == 0) return true;
41 else return false;
42}
43
44bool Cond::broadcast()
45{
46 if (pthread_cond_broadcast(&m_cond_t) == 0) return true;
47 else return false;
48}
49
50bool 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
58bool 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 if (pthread_cond_timedwait(&m_cond_t, &mutex.m_mu, &timeout) != 0) { // check on stat code
67 return false;
68 }
69 return true;
70}
71
72bool Cond::destroy()
73{
74 if (pthread_cond_destroy(&m_cond_t) == 0) return true;
75 else return false;
76}
Abstract base class for different kinds of events.