Belle II Software  release-08-01-10
HLTMainLoop.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/rfarm/event/hltsocket/HLTMainLoop.h>
9 
10 #include <framework/logging/Logger.h>
11 
12 #include <chrono>
13 #include <csignal>
14 #include <thread>
15 
16 using namespace std::chrono_literals;
17 using namespace Belle2;
18 
19 bool HLTMainLoop::s_interrupted = false;
20 
21 void HLTMainLoop::signalHandler(int /*signalValue*/)
22 {
23  B2RESULT("Termination request...");
24  HLTMainLoop::s_interrupted = true;
25 }
26 
27 HLTMainLoop::HLTMainLoop()
28 {
29  initSignalHandler();
30 }
31 
32 bool HLTMainLoop::isRunning() const
33 {
34  return not s_interrupted;
35 }
36 
37 void HLTMainLoop::initSignalHandler() const
38 {
39  struct sigaction action;
40  action.sa_handler = signalHandler;
41  action.sa_flags = 0;
42  sigemptyset(&action.sa_mask);
43  sigaction(SIGINT, &action, NULL);
44  sigaction(SIGTERM, &action, NULL);
45 }
46 
47 int HLTMainLoop::writeToRingBufferWaiting(RingBuffer* ringBuffer, int* buffer, unsigned int size) const
48 {
49  int irb = 0;
50  while (isRunning()) {
51  irb = ringBuffer->insq(buffer, size);
52  if (irb >= 0) {
53  break;
54  }
55  std::this_thread::sleep_for(20us);
56  }
57 
58  return irb;
59 }
60 
61 int HLTMainLoop::readFromRingBufferWaiting(RingBuffer* ringBuffer, int* buffer) const
62 {
63  int bsize = 0;
64  while ((bsize = ringBuffer->remq(buffer)) == 0 and isRunning()) {
65  std::this_thread::sleep_for(20us);
66  }
67 
68  return bsize;
69 }
Class to manage a Ring Buffer placed in an IPC shared memory.
Definition: RingBuffer.h:39
int insq(const int *buf, int size, bool checkTx=false)
Append a buffer to the RingBuffer.
Definition: RingBuffer.cc:189
int remq(int *buf)
Pick up a buffer from the RingBuffer.
Definition: RingBuffer.cc:308
Abstract base class for different kinds of events.