Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r16 (ea80daf8)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
WatchdogThread.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 /*
12  * Implementation of the class WatchdogThread.
13  */
14 
16 #include "GaudiKernel/Sleep.h"
17 
18 #include <functional>
19 
20 #include <boost/thread/thread.hpp>
21 
22 WatchdogThread::WatchdogThread( boost::posix_time::time_duration timeout, bool autostart )
23  : m_timeout( std::move( timeout ) ), m_running( false ) {
24  // Start the thread immediately if requested.
25  if ( autostart ) start();
26 }
27 
29  // Make sure the thread is stopped before exiting.
30  stop();
31 }
32 
34  if ( !m_thread ) { // can be started only if the thread is not yet started
35  m_running = true;
36  // call user-defined function
37  onStart();
38  // Initialize the first "last ping"
39  ping();
40  // Start a new thread telling it to call the member function i_run()
41  m_thread = std::make_unique<boost::thread>( std::mem_fn( &WatchdogThread::i_run ), this );
42  }
43 }
44 
46  if ( m_thread ) {
47  m_running = false; // mark the thread as stopped (interrupt doesn't work if the thread is not sleeping)
48  Gaudi::NanoSleep( 1000000 ); // Wait a bit (1ms) to be sure that the interrupt happens during the sleep
49  m_thread->interrupt(); // tell the thread to stop (if it is waiting)
50  m_thread->join(); // wait for it
51  m_thread.reset(); // delete it
52  // call user-defined function
53  onStop();
54  }
55 }
56 
58  // Copy of the last ping
59  boost::system_time lastPing = getLastPing();
60 
61  // set initial check time
62  boost::system_time nextCheck = lastPing + getTimeout();
63 
64  try {
65  // enter infinite loop
66  while ( m_running ) {
67  // Wait until the next check point time is reached.
68  // An early exit must be triggered by a call to this->interrupt(), which
69  // will produce an exception during the sleep.
70  boost::thread::sleep( nextCheck );
71  // Check if there was a ping while we were sleeping
72  if ( lastPing == getLastPing() ) { // no further accesses
73  action();
74  // schedule the next check for now + dt (seems a good estimate)
75  nextCheck = boost::get_system_time() + getTimeout();
76  } else { // there was a ping
77  // schedule the next check for last_access + dt
78  nextCheck = lastPing = getLastPing();
79  nextCheck += getTimeout();
80  }
81  }
82  }
83  // Ignore the exception since it is used only to exit from the loop.
84  catch ( boost::thread_interrupted& ) {}
85 }
86 
87 // Default implementation: empty
89 
90 // Default implementation: empty
92 
93 // Default implementation: empty
95 
96 // Default implementation: empty
WatchdogThread::stop
void stop()
Signal the watchdog thread to stop and wait for it.
Definition: WatchdogThread.cpp:45
WatchdogThread::ping
void ping()
Function to call to notify the watchdog thread that we are still alive.
Definition: WatchdogThread.h:56
std::mem_fn
T mem_fn(T... args)
WatchdogThread::~WatchdogThread
virtual ~WatchdogThread()
Destructor.
Definition: WatchdogThread.cpp:28
WatchdogThread::onPing
virtual void onPing()
User implemented function that will be called when ping is called.
Definition: WatchdogThread.cpp:91
WatchdogThread::getTimeout
boost::posix_time::time_duration getTimeout() const
Get the current time-out value.
Definition: WatchdogThread.h:66
std::unique_ptr::reset
T reset(T... args)
WatchdogThread::onStart
virtual void onStart()
User implemented function that will be called when starting.
Definition: WatchdogThread.cpp:94
Gaudi::NanoSleep
GAUDI_API void NanoSleep(long long nsec)
Small variation on the sleep function for nanoseconds sleep.
Definition: Sleep.cpp:19
WatchdogThread::start
void start()
Start the watchdog thread.
Definition: WatchdogThread.cpp:33
WatchdogThread::m_running
bool m_running
Flag to mark the thread as running/stopped (avoid possible race conditions).
Definition: WatchdogThread.h:98
WatchdogThread.h
WatchdogThread::getLastPing
boost::system_time getLastPing() const
Get the time of latest ping.
Definition: WatchdogThread.h:69
WatchdogThread::action
virtual void action()
User implemented function that will be called if the time-out is reached.
Definition: WatchdogThread.cpp:88
WatchdogThread::m_thread
std::unique_ptr< boost::thread > m_thread
Pointer to the running thread;.
Definition: WatchdogThread.h:95
WatchdogThread::i_run
void i_run()
Core function of the secondary thread.
Definition: WatchdogThread.cpp:57
WatchdogThread::WatchdogThread
WatchdogThread(boost::posix_time::time_duration timeout, bool autostart=false)
Constructor.
Definition: WatchdogThread.cpp:22
Sleep.h
std
STL namespace.
WatchdogThread::onStop
virtual void onStop()
User implemented function that will be called when stopping.
Definition: WatchdogThread.cpp:97