Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
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 
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_ */
boost::posix_time::time_duration getTimeout() const
Get the current time-out value.
boost::system_time m_lastPing
When the last ping was received.
void setTimeout(boost::posix_time::time_duration timeout)
Change the duration of the time-out.
boost::posix_time::time_duration m_timeout
Number of seconds allowed between pings.
void ping()
Function to call to notify the watchdog thread that we are still alive.
start
Definition: IOTest.py:97
bool m_running
Flag to mark the thread as running/stopped (avoid possible race conditions).
Simple class for asynchronous check of time-out.
boost::mutex m_lastPingMutex
Mutex for the access to the m_lastPing data member.
#define GAUDI_API
Definition: Kernel.h:71
std::unique_ptr< boost::thread > m_thread
Pointer to the running thread;.
boost::system_time getLastPing() const
Get the time of latest ping.