| The Gaudi Framework v32r1 (f65d50dc)

WatchdogThread.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_WATCHDOGTHREAD_H_
2 #define GAUDIKERNEL_WATCHDOGTHREAD_H_
3
4 #include <memory>
5
6 #include <boost/date_time/posix_time/posix_time_types.hpp>
7 #include <boost/thread/mutex.hpp>
8 #include <boost/thread/thread_time.hpp>
9
10 // for GAUDI_API
11 #include "GaudiKernel/Kernel.h"
12
13 // forward declaration
14 namespace boost {
15 class thread;
16 }
17
27 class GAUDI_API WatchdogThread {
28 public:
30 // @param timeout the time span that can occur between two pings.
31 // @param autostart if set to true, the second thread is started automatically
32 // on construction, otherwise the user have to call start().
33 WatchdogThread( boost::posix_time::time_duration timeout, bool autostart = false );
34
36 // Stop the thread of not done earlier.
37 virtual ~WatchdogThread();
38
40 void start();
41
43 void stop();
44
46 inline void ping() {
47 boost::mutex::scoped_lock lock( m_lastPingMutex );
48 m_lastPing = boost::get_system_time();
49 onPing();
50 }
51
53 inline void setTimeout( boost::posix_time::time_duration timeout ) { m_timeout = timeout; }
54
56 inline boost::posix_time::time_duration getTimeout() const { return m_timeout; }
57
59 inline boost::system_time getLastPing() const {
60 boost::mutex::scoped_lock lock( m_lastPingMutex );
61 return m_lastPing;
62 }
63
64 protected:
66 virtual void action();
67
69 virtual void onPing();
70
72 virtual void onStart();
73
75 virtual void onStop();
76
77 private:
79 boost::posix_time::time_duration m_timeout;
80
82 boost::system_time m_lastPing;
83
85 std::unique_ptr<boost::thread> m_thread;
86
88 bool m_running;
89
91 // Waits for the time-out and if there was not a ping in the mean time, calls
92 // i_action().
93 void i_run();
94
96 mutable boost::mutex m_lastPingMutex;
97 };
98
99 #endif /* WATCHDOGTHREAD_H_ */
Kernel.h
boost
Definition: iter_pos.hpp:13
WatchdogThread::m_lastPing
boost::system_time m_lastPing
When the last ping was received.
Definition: WatchdogThread.h:82
WatchdogThread::setTimeout
void setTimeout(boost::posix_time::time_duration timeout)
Change the duration of the time-out.
Definition: WatchdogThread.h:53
WatchdogThread::m_timeout
boost::posix_time::time_duration m_timeout
Number of seconds allowed between pings.
Definition: WatchdogThread.h:79
WatchdogThread::ping
void ping()
Function to call to notify the watchdog thread that we are still alive.
Definition: WatchdogThread.h:46
IOTest.start
def start
Definition: IOTest.py:97
gaudirun.action
action
Definition: gaudirun.py:136
WatchdogThread::m_running
bool m_running
Flag to mark the thread as running/stopped (avoid possible race conditions).
Definition: WatchdogThread.h:88
std::unique_ptr< boost::thread >
WatchdogThread
Simple class for asynchronous check of time-out.
Definition: WatchdogThread.h:27
WatchdogThread::getLastPing
boost::system_time getLastPing() const
Get the time of latest ping.
Definition: WatchdogThread.h:59
WatchdogThread::m_lastPingMutex
boost::mutex m_lastPingMutex
Mutex for the access to the m_lastPing data member.
Definition: WatchdogThread.h:96
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:71
WatchdogThread::m_thread
std::unique_ptr< boost::thread > m_thread
Pointer to the running thread;.
Definition: WatchdogThread.h:85
WatchdogThread::getTimeout
boost::posix_time::time_duration getTimeout() const
Get the current time-out value.
Definition: WatchdogThread.h:56