| # Gaudi Framework, version v21r10p1 |

Home |  | Generated: 29 Jul 2010

# WatchdogThread.h

Go to the documentation of this file.

    00001 #ifndef GAUDIKERNEL_WATCHDOGTHREAD_H_
    00002 #define GAUDIKERNEL_WATCHDOGTHREAD_H_
    00003
    00004 #include <memory>
    00005
    00006 #include <boost/date_time/posix_time/posix_time_types.hpp>
    00007 #include <boost/thread/thread_time.hpp>
    00008 #include <boost/thread/mutex.hpp>
    00009
    00010 // for GAUDI_API
    00011 #include "GaudiKernel/Kernel.h"
    00012
    00013 // forward declaration
    00014 namespace boost { class thread; }
    00015
    00025 class GAUDI_API WatchdogThread {
    00026 public:
    00028   //  @param timeout the time span that can occur between two pings.
    00029   //  @param autostart if set to true, the second thread is started automatically
    00030   //                   on construction, otherwise the user have to call start().
    00031   WatchdogThread(boost::posix_time::time_duration timeout, bool autostart = false);
    00032
    00034   //  Stop the thread of not done earlier.
    00035   virtual ~WatchdogThread();
    00036
    00038   void start();
    00039
    00041   void stop();
    00042
    00044   inline void ping() {
    00045     boost::mutex::scoped_lock lock(m_lastPingMutex);
    00046     m_lastPing = boost::get_system_time();
    00047     onPing();
    00048   }
    00049
    00051   inline void setTimeout(boost::posix_time::time_duration timeout) {
    00052     m_timeout = timeout;
    00053   }
    00054
    00056   inline boost::posix_time::time_duration getTimeout() const {
    00057     return m_timeout;
    00058   }
    00059
    00061   inline boost::system_time getLastPing() const {
    00062     boost::mutex::scoped_lock lock(m_lastPingMutex);
    00063     return m_lastPing;
    00064   }
    00065
    00066 protected:
    00068   virtual void action();
    00069
    00071   virtual void onPing();
    00072
    00074   virtual void onStart();
    00075
    00077   virtual void onStop();
    00078
    00079 private:
    00081   boost::posix_time::time_duration m_timeout;
    00082
    00084   boost::system_time m_lastPing;
    00085
    00087   std::auto_ptr<boost::thread> m_thread;
    00088
    00090   bool m_running;
    00091
    00093   //  Waits for the time-out and if there was not a ping in the mean time, calls
    00094   //  i_action().
    00095   void i_run();
    00096
    00098   mutable boost::mutex m_lastPingMutex;
    00099 };
    00100
    00101 #endif /* WATCHDOGTHREAD_H_ */