| The Gaudi Framework v29r0 (ff2e7097)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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