| The Gaudi Framework v28r2-patches (bc4ad17a)

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