The Gaudi Framework  v29r0 (ff2e7097)
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 {
16  class thread;
17 }
18 
29 {
30 public:
32  // @param timeout the time span that can occur between two pings.
33  // @param autostart if set to true, the second thread is started automatically
34  // on construction, otherwise the user have to call start().
35  WatchdogThread( boost::posix_time::time_duration timeout, bool autostart = false );
36 
38  // Stop the thread of not done earlier.
39  virtual ~WatchdogThread();
40 
42  void start();
43 
45  void stop();
46 
48  inline void ping()
49  {
50  boost::mutex::scoped_lock lock( m_lastPingMutex );
51  m_lastPing = boost::get_system_time();
52  onPing();
53  }
54 
56  inline void setTimeout( boost::posix_time::time_duration timeout ) { m_timeout = timeout; }
57 
59  inline boost::posix_time::time_duration getTimeout() const { return m_timeout; }
60 
62  inline boost::system_time getLastPing() const
63  {
64  boost::mutex::scoped_lock lock( m_lastPingMutex );
65  return m_lastPing;
66  }
67 
68 protected:
70  virtual void action();
71 
73  virtual void onPing();
74 
76  virtual void onStart();
77 
79  virtual void onStop();
80 
81 private:
83  boost::posix_time::time_duration m_timeout;
84 
86  boost::system_time m_lastPing;
87 
90 
92  bool m_running;
93 
95  // Waits for the time-out and if there was not a ping in the mean time, calls
96  // i_action().
97  void i_run();
98 
100  mutable boost::mutex m_lastPingMutex;
101 };
102 
103 #endif /* WATCHDOGTHREAD_H_ */
boost::posix_time::time_duration getTimeout() const
Get the current time-out value.
The namespace threadpool contains a thread pool and related utility classes.
Definition: iter_pos.hpp:13
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:99
std::function< StatusCode()> action
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:110
std::unique_ptr< boost::thread > m_thread
Pointer to the running thread;.
boost::system_time getLastPing() const
Get the time of latest ping.