Gaudi Framework, version v23r0

Home   Generated: Mon Jan 30 2012
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes

WatchdogThread Class Reference

Simple class for asynchronous check of time-out. More...

#include <WatchdogThread.h>

Collaboration diagram for WatchdogThread:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 WatchdogThread (boost::posix_time::time_duration timeout, bool autostart=false)
 Constructor.
virtual ~WatchdogThread ()
 Destructor.
void start ()
 Start the watchdog thread.
void stop ()
 Signal the watchdog thread to stop and wait for it.
void ping ()
 Function to call to notify the watchdog thread that we are still alive.
void setTimeout (boost::posix_time::time_duration timeout)
 Change the duration of the time-out.
boost::posix_time::time_duration getTimeout () const
 Get the current time-out value.
boost::system_time getLastPing () const
 Get the time of latest ping.

Protected Member Functions

virtual void action ()
 User implemented function that will be called if the time-out is reached.
virtual void onPing ()
 User implemented function that will be called when ping is called.
virtual void onStart ()
 User implemented function that will be called when starting.
virtual void onStop ()
 User implemented function that will be called when stopping.

Private Member Functions

void i_run ()
 Core function of the secondary thread.

Private Attributes

boost::posix_time::time_duration m_timeout
 Number of seconds allowed between pings.
boost::system_time m_lastPing
 When the last ping was received.
std::auto_ptr< boost::thread > m_thread
 Pointer to the running thread;.
bool m_running
 Flag to mark the thread as running/stopped (avoid possible race conditions).
boost::mutex m_lastPingMutex
 Mutex for the access to the m_lastPing data member.

Detailed Description

Simple class for asynchronous check of time-out.

The user must provide a callable with the action to be performed when the time-out occurs.

Author:
Marco Clemencic
Date:
2010-02-23

Definition at line 25 of file WatchdogThread.h.


Constructor & Destructor Documentation

WatchdogThread::WatchdogThread ( boost::posix_time::time_duration  timeout,
bool  autostart = false 
)

Constructor.

Definition at line 12 of file WatchdogThread.cpp.

                                                                                  :
  m_timeout(timeout), m_running(false)
{
  // Start the thread immediately if requested.
  if (autostart) start();
}
WatchdogThread::~WatchdogThread (  ) [virtual]

Destructor.

Definition at line 19 of file WatchdogThread.cpp.

                                {
  // Make sure the thread is stopped before exiting.
  stop();
}

Member Function Documentation

void WatchdogThread::action (  ) [protected, virtual]

User implemented function that will be called if the time-out is reached.

Definition at line 79 of file WatchdogThread.cpp.

                            {
}
boost::system_time WatchdogThread::getLastPing (  ) const [inline]

Get the time of latest ping.

Definition at line 61 of file WatchdogThread.h.

                                              {
    boost::mutex::scoped_lock lock(m_lastPingMutex);
    return m_lastPing;
  }
boost::posix_time::time_duration WatchdogThread::getTimeout (  ) const [inline]

Get the current time-out value.

Definition at line 56 of file WatchdogThread.h.

                                                         {
    return m_timeout;
  }
void WatchdogThread::i_run (  ) [private]

Core function of the secondary thread.

Definition at line 48 of file WatchdogThread.cpp.

                           {
  // Copy of the last ping
  boost::system_time lastPing = getLastPing();

  // set initial check time
  boost::system_time nextCheck = lastPing + getTimeout();

  try {
    // enter infinite loop
    while (m_running) {
      // Wait until the next check point time is reached.
      // An early exit must be triggered by a call to this->interrupt(), which
      // will produce an exception during the sleep.
      boost::thread::sleep(nextCheck);
      // Check if there was a ping while we were sleeping
      if ( lastPing == getLastPing() ) { // no further accesses
        action();
        // schedule the next check for now + dt (seems a good estimate)
        nextCheck = boost::get_system_time() + getTimeout();
      } else { // there was a ping
        // schedule the next check for last_access + dt
        nextCheck = lastPing = getLastPing();
        nextCheck += getTimeout();
      }
    }
  }
  // Ignore the exception since it is used only to exit from the loop.
  catch (boost::thread_interrupted&) {}
}
void WatchdogThread::onPing (  ) [protected, virtual]

User implemented function that will be called when ping is called.

Definition at line 83 of file WatchdogThread.cpp.

                            {
}
void WatchdogThread::onStart (  ) [protected, virtual]

User implemented function that will be called when starting.

Definition at line 87 of file WatchdogThread.cpp.

                             {
}
void WatchdogThread::onStop (  ) [protected, virtual]

User implemented function that will be called when stopping.

Definition at line 91 of file WatchdogThread.cpp.

                            {
}
void WatchdogThread::ping (  ) [inline]

Function to call to notify the watchdog thread that we are still alive.

Definition at line 44 of file WatchdogThread.h.

                     {
    boost::mutex::scoped_lock lock(m_lastPingMutex);
    m_lastPing = boost::get_system_time();
    onPing();
  }
void WatchdogThread::setTimeout ( boost::posix_time::time_duration  timeout ) [inline]

Change the duration of the time-out.

Definition at line 51 of file WatchdogThread.h.

                                                               {
    m_timeout = timeout;
  }
void WatchdogThread::start (  )

Start the watchdog thread.

Definition at line 24 of file WatchdogThread.cpp.

                           {
  if (!m_thread.get()) { // can be started only if the thread is not yet started
    m_running = true;
    // call user-defined function
    onStart();
    // Initialize the first "last ping"
    ping();
    // Start a new thread telling it to call the member function i_run()
    m_thread = std::auto_ptr<boost::thread>(new boost::thread(std::mem_fun(&WatchdogThread::i_run), this));
  }
}
void WatchdogThread::stop (  )

Signal the watchdog thread to stop and wait for it.

Definition at line 36 of file WatchdogThread.cpp.

                          {
  if (m_thread.get()) {
    m_running = false; // mark the thread as stopped (interrupt doesn't work if the thread is not sleeping)
    Gaudi::NanoSleep(1000000); // Wait a bit (1ms) to be sure that the interrupt happens during the sleep
    m_thread->interrupt(); // tell the thread to stop (if it is waiting)
    m_thread->join(); // wait for it
    m_thread.reset(); // delete it
    // call user-defined function
    onStop();
  }
}

Member Data Documentation

boost::system_time WatchdogThread::m_lastPing [private]

When the last ping was received.

Definition at line 84 of file WatchdogThread.h.

boost::mutex WatchdogThread::m_lastPingMutex [mutable, private]

Mutex for the access to the m_lastPing data member.

Definition at line 98 of file WatchdogThread.h.

bool WatchdogThread::m_running [private]

Flag to mark the thread as running/stopped (avoid possible race conditions).

Definition at line 90 of file WatchdogThread.h.

std::auto_ptr<boost::thread> WatchdogThread::m_thread [private]

Pointer to the running thread;.

Definition at line 87 of file WatchdogThread.h.

boost::posix_time::time_duration WatchdogThread::m_timeout [private]

Number of seconds allowed between pings.

Definition at line 81 of file WatchdogThread.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated at Mon Jan 30 2012 13:53:25 for Gaudi Framework, version v23r0 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004