CMS 3D CMS Logo

List of all members | Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes
edm::shared_memory::WorkerMonitorThread Class Reference

#include <WorkerMonitorThread.h>

Public Member Functions

const WorkerMonitorThreadoperator= (const WorkerMonitorThread &)=delete
 
const WorkerMonitorThreadoperator= (WorkerMonitorThread &&)=delete
 
void setAction (std::function< void()> iFunc)
 
void setupSignalHandling ()
 Sets the unix signal handler which communicates with the thread. More...
 
void startThread ()
 
void stop ()
 
 WorkerMonitorThread ()
 
 WorkerMonitorThread (const WorkerMonitorThread &)=delete
 
 WorkerMonitorThread (WorkerMonitorThread &&)=delete
 
 ~WorkerMonitorThread ()
 

Private Member Functions

void run ()
 

Static Private Member Functions

static void sig_handler (int sig, siginfo_t *, void *)
 

Private Attributes

std::function< void()> action_
 
std::atomic< bool > actionSet_ = false
 
std::atomic< bool > helperReady_ = false
 
std::thread helperThread_
 
std::atomic< bool > stopRequested_ = false
 

Static Private Attributes

static std::atomic< bool > s_helperThreadDone = false
 
static std::atomic< int > s_pipeReadEnd = 0
 
static std::atomic< int > s_pipeWriteEnd = 0
 

Detailed Description

Definition at line 32 of file WorkerMonitorThread.h.

Constructor & Destructor Documentation

edm::shared_memory::WorkerMonitorThread::WorkerMonitorThread ( )
inline

Definition at line 34 of file WorkerMonitorThread.h.

Referenced by ~WorkerMonitorThread().

34 {}
edm::shared_memory::WorkerMonitorThread::~WorkerMonitorThread ( )
inline

Definition at line 35 of file WorkerMonitorThread.h.

References operator=(), stop(), stopRequested_, and WorkerMonitorThread().

35  {
36  if (not stopRequested_.load()) {
37  stop();
38  }
39  }
edm::shared_memory::WorkerMonitorThread::WorkerMonitorThread ( const WorkerMonitorThread )
delete
edm::shared_memory::WorkerMonitorThread::WorkerMonitorThread ( WorkerMonitorThread &&  )
delete

Member Function Documentation

const WorkerMonitorThread& edm::shared_memory::WorkerMonitorThread::operator= ( const WorkerMonitorThread )
delete

Referenced by ~WorkerMonitorThread().

const WorkerMonitorThread& edm::shared_memory::WorkerMonitorThread::operator= ( WorkerMonitorThread &&  )
delete
void WorkerMonitorThread::run ( )
private

Definition at line 41 of file WorkerMonitorThread.cc.

References action_, actionSet_, MessageLogger_cfi::cerr, helperReady_, s_helperThreadDone, and s_pipeReadEnd.

Referenced by setAction(), and startThread().

41  {
42  //std::cerr << "Started cleanup thread\n";
43  sigset_t ensemble;
44 
45  sigemptyset(&ensemble);
46  sigaddset(&ensemble, SIGABRT);
47  sigaddset(&ensemble, SIGILL);
48  sigaddset(&ensemble, SIGBUS);
49  sigaddset(&ensemble, SIGSEGV);
50  sigaddset(&ensemble, SIGTERM);
51  pthread_sigmask(SIG_BLOCK, &ensemble, nullptr);
52 
53  //std::cerr << "Start loop\n";
54  helperReady_ = true;
55  while (true) {
56  int signal = -1;
57  auto res = read(s_pipeReadEnd.load(), &signal, sizeof(signal) / sizeof(char));
58  if (res == -1) {
59  if (errno == EINTR) {
60  continue;
61  }
62  abort();
63  }
64  if (signal != 0) {
65  if (actionSet_) {
66  action_();
67  }
68  std::cerr << "Worker: SIGNAL CAUGHT " << signal << "\n";
69  s_helperThreadDone = true;
70  break;
71  } /* else {
72  std::cerr << "SIGNAL woke\n";
73  } */
74  }
75  //std::cerr << "Ending cleanup thread\n";
76 }
static std::atomic< bool > s_helperThreadDone
Definition: Electron.h:6
void edm::shared_memory::WorkerMonitorThread::setAction ( std::function< void()>  iFunc)
inline

Definition at line 48 of file WorkerMonitorThread.h.

References action_, actionSet_, eostools::move(), run(), setupSignalHandling(), sig_handler(), startThread(), and stop().

Referenced by main().

48  {
49  action_ = std::move(iFunc);
50  actionSet_.store(true);
51  }
def move(src, dest)
Definition: eostools.py:511
void WorkerMonitorThread::setupSignalHandling ( )

Sets the unix signal handler which communicates with the thread.

Definition at line 102 of file WorkerMonitorThread.cc.

References sig_handler().

Referenced by main(), setAction(), and startThread().

102  {
103  struct sigaction act;
104  act.sa_sigaction = sig_handler;
105  act.sa_flags = SA_SIGINFO;
106  sigemptyset(&act.sa_mask);
107  sigaction(SIGABRT, &act, nullptr);
108  sigaction(SIGILL, &act, nullptr);
109  sigaction(SIGBUS, &act, nullptr);
110  sigaction(SIGSEGV, &act, nullptr);
111  sigaction(SIGTERM, &act, nullptr);
112 }
static void sig_handler(int sig, siginfo_t *, void *)
void WorkerMonitorThread::sig_handler ( int  sig,
siginfo_t *  ,
void *   
)
staticprivate

Definition at line 127 of file WorkerMonitorThread.cc.

References createfilelist::int, s_helperThreadDone, s_pipeWriteEnd, and TriggerAnalyzer::write().

Referenced by setAction(), and setupSignalHandling().

127  {
128  write(s_pipeWriteEnd.load(), &sig, sizeof(int) / sizeof(char));
129  while (not s_helperThreadDone) {
130  };
131  signal(sig, SIG_DFL);
132  raise(sig);
133 }
static std::atomic< bool > s_helperThreadDone
def write(self, setup)
void WorkerMonitorThread::startThread ( )

Definition at line 78 of file WorkerMonitorThread.cc.

References helperReady_, helperThread_, eostools::move(), pipe::pipe(), run(), s_pipeReadEnd, s_pipeWriteEnd, setupSignalHandling(), and protons_cff::t.

Referenced by main(), and setAction().

78  {
79  {
80  //Setup watchdog thread for crashing signals
81 
82  int pipeEnds[2] = {0, 0};
83  auto ret = pipe(pipeEnds);
84  if (ret != 0) {
85  abort();
86  }
87  s_pipeReadEnd.store(pipeEnds[0]);
88  s_pipeWriteEnd.store(pipeEnds[1]);
89  //Need to use signal handler since signals generated
90  // from within a program are thread specific which can
91  // only be handed by a signal handler
93 
94  std::thread t(&WorkerMonitorThread::run, this);
95  t.detach();
97  }
98  while (helperReady_.load() == false) {
99  }
100 }
void setupSignalHandling()
Sets the unix signal handler which communicates with the thread.
def pipe(cmdline, input=None)
Definition: pipe.py:5
def move(src, dest)
Definition: eostools.py:511
void WorkerMonitorThread::stop ( )

Definition at line 114 of file WorkerMonitorThread.cc.

References createfilelist::int, s_pipeWriteEnd, stopRequested_, and TriggerAnalyzer::write().

Referenced by setAction(), and ~WorkerMonitorThread().

114  {
115  stopRequested_ = true;
116  int sig = 0;
117  write(s_pipeWriteEnd.load(), &sig, sizeof(int) / sizeof(char));
118 }
def write(self, setup)

Member Data Documentation

std::function<void()> edm::shared_memory::WorkerMonitorThread::action_
private

Definition at line 67 of file WorkerMonitorThread.h.

Referenced by run(), and setAction().

std::atomic<bool> edm::shared_memory::WorkerMonitorThread::actionSet_ = false
private

Definition at line 66 of file WorkerMonitorThread.h.

Referenced by run(), and setAction().

std::atomic<bool> edm::shared_memory::WorkerMonitorThread::helperReady_ = false
private

Definition at line 65 of file WorkerMonitorThread.h.

Referenced by run(), and startThread().

std::thread edm::shared_memory::WorkerMonitorThread::helperThread_
private

Definition at line 73 of file WorkerMonitorThread.h.

Referenced by startThread().

std::atomic< bool > WorkerMonitorThread::s_helperThreadDone = false
staticprivate

Definition at line 71 of file WorkerMonitorThread.h.

Referenced by run(), and sig_handler().

std::atomic< int > WorkerMonitorThread::s_pipeReadEnd = 0
staticprivate

Definition at line 69 of file WorkerMonitorThread.h.

Referenced by run(), and startThread().

std::atomic< int > WorkerMonitorThread::s_pipeWriteEnd = 0
staticprivate

Definition at line 70 of file WorkerMonitorThread.h.

Referenced by sig_handler(), startThread(), and stop().

std::atomic<bool> edm::shared_memory::WorkerMonitorThread::stopRequested_ = false
private

Definition at line 64 of file WorkerMonitorThread.h.

Referenced by stop(), and ~WorkerMonitorThread().