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