Belle II Software development
RunInfoBuffer.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/readout/RunInfoBuffer.h"
9
10#include <daq/slc/system/LogFile.h>
11#include <daq/slc/system/LockGuard.h>
12
13#include <cstring>
14#include <cstdlib>
15
16using namespace Belle2;
17
18size_t RunInfoBuffer::size()
19{
20 return m_mutex.size() + m_cond.size() +
21 sizeof(ronode_info);
22}
23
24bool RunInfoBuffer::open(const std::string& nodename,
25 int nodeid, bool recreate)
26{
27 m_nodename = nodename;
28 std::string username = getenv("USER");
29 m_path = "/run_info_" + username + "_" + nodename;
30 if (!m_memory.open(m_path, size())) {
31 perror("shm_open");
32 LogFile::fatal("Failed to open %s", m_path.c_str());
33 return false;
34 }
35 char* buf = (char*)m_memory.map(0, size());
36 if (buf == NULL) {
37 return false;
38 }
39 m_mutex = MMutex(buf);
40 buf += m_mutex.size();
41 m_cond = MCond(buf);
42 buf += m_cond.size();
43 m_info = (ronode_info*)buf;
44 if (recreate) init();
45 if (nodeid > 0) setNodeId(nodeid);
46 return true;
47}
48
49bool RunInfoBuffer::init()
50{
51 if (m_info == NULL) return false;
52 m_mutex.init();
53 m_cond.init();
54 memset(m_info, 0, sizeof(ronode_info));
55 LogFile::debug("Initialized %s", m_path.c_str());
56 return true;
57}
58
59void RunInfoBuffer::clear()
60{
61 if (m_info == NULL) return;
62 MLockGuard lockGuard(m_mutex);
63 memset(m_info, 0, sizeof(ronode_info));
64}
65
66bool RunInfoBuffer::close()
67{
68 m_memory.close();
69 return true;
70}
71
72bool RunInfoBuffer::unlink()
73{
74 m_memory.unlink();
75 m_memory.close();
76 return true;
77}
78
79bool RunInfoBuffer::lock()
80{
81 if (m_info == NULL) return false;
82 return m_mutex.lock();
83}
84
85bool RunInfoBuffer::unlock()
86{
87 if (m_info == NULL) return false;
88 return m_mutex.unlock();
89}
90
91bool RunInfoBuffer::wait()
92{
93 if (m_info == NULL) return false;
94 return m_cond.wait(m_mutex);
95}
96
97bool RunInfoBuffer::wait(int time)
98{
99 if (m_info == NULL) return false;
100 return m_cond.wait(m_mutex, time, 0);
101}
102
103bool RunInfoBuffer::notify()
104{
105 if (m_info == NULL) return false;
106 return m_cond.broadcast();
107}
108
109bool RunInfoBuffer::waitRunning(int timeout)
110{
111 if (m_info == NULL) return false;
112 MLockGuard lockGuard(m_mutex);
113 if (getState() != RunInfoBuffer::RUNNING) {
114 if (!wait(timeout)) {
115 return false;
116 }
117 }
118 return true;
119}
120
121bool RunInfoBuffer::waitReady(int timeout)
122{
123 if (m_info == NULL) return false;
124 MLockGuard lockGuard(m_mutex);
125 if (getState() != RunInfoBuffer::READY &&
126 getState() != RunInfoBuffer::RUNNING) {
127 if (!wait(timeout)) {
128 if (getState() != RunInfoBuffer::READY &&
129 getState() != RunInfoBuffer::RUNNING) {
130 return false;
131 }
132 }
133 }
134 return true;
135}
136
137bool RunInfoBuffer::reportRunning()
138{
139 if (m_info == NULL) return false;
140 MLockGuard lockGuard(m_mutex);
141 setState(RunInfoBuffer::RUNNING);
142 notify();
143 return true;
144}
145
146bool RunInfoBuffer::reportError(EFlag eflag)
147{
148 if (m_info == NULL) return false;
149 MLockGuard lockGuard(m_mutex);
150 setErrorFlag(eflag);
151 notify();
152 return true;
153}
154
155bool RunInfoBuffer::reportReady()
156{
157 if (m_info == NULL) return false;
158 MLockGuard lockGuard(m_mutex);
159 setState(RunInfoBuffer::READY);
160 notify();
161 return true;
162}
163
164bool RunInfoBuffer::reportNotReady()
165{
166 if (m_info == NULL) return false;
167 MLockGuard lockGuard(m_mutex);
168 setState(RunInfoBuffer::NOTREADY);
169 notify();
170 return true;
171}
172
173void RunInfoBuffer::copyEventHeader(int* buf)
174{
175 if (m_info == NULL) return;
176 memcpy(&(m_info->header), buf, sizeof(event_header));
177}
Lock Guard for a Mutex instance.
Definition: LockGuard.h:47
Abstract base class for different kinds of events.