| # Gaudi Framework, version v22r0 |

Home |  | Generated: 9 Feb 2011

# WatchdogThread Class Reference

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

`#include <WatchdogThread.h>`

Collaboration diagram for WatchdogThread:
[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.

    00013                                                                                   :
    00014   m_timeout(timeout), m_running(false)
    00015 {
    00016   // Start the thread immediately if requested.
    00017   if (autostart) start();
    }

WatchdogThread::~WatchdogThread | ( |  | ) | `[virtual]`

Destructor.

Definition at line 19 of file WatchdogThread.cpp.

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

## 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.

    00079                             {
    00080 }

boost::system_time WatchdogThread::getLastPing | ( |  | ) | const`[inline]`

Get the time of latest ping.

Definition at line 61 of file WatchdogThread.h.

    00061                                               {
    00062     boost::mutex::scoped_lock lock(m_lastPingMutex);
    00063     return m_lastPing;
    00064   }

boost::posix_time::time_duration WatchdogThread::getTimeout | ( |  | ) | const`[inline]`

Get the current time-out value.

Definition at line 56 of file WatchdogThread.h.

    00056                                                          {
    00057     return m_timeout;
    00058   }

void WatchdogThread::i_run | ( |  | ) | `[private]`

Core function of the secondary thread.

Definition at line 48 of file WatchdogThread.cpp.

    00048                            {
    00049   // Copy of the last ping
    00050   boost::system_time lastPing = getLastPing();
    00051
    00052   // set initial check time
    00053   boost::system_time nextCheck = lastPing + getTimeout();
    00054
    00055   try {
    00056     // enter infinite loop
    00057     while (m_running) {
    00058       // Wait until the next check point time is reached.
    00059       // An early exit must be triggered by a call to this->interrupt(), which
    00060       // will produce an exception during the sleep.
    00061       boost::thread::sleep(nextCheck);
    00062       // Check if there was a ping while we were sleeping
    00063       if ( lastPing == getLastPing() ) { // no further accesses
    00064         action();
    00065         // schedule the next check for now + dt (seems a good estimate)
    00066         nextCheck = boost::get_system_time() + getTimeout();
    00067       } else { // there was a ping
    00068         // schedule the next check for last_access + dt
    00069         nextCheck = lastPing = getLastPing();
    00070         nextCheck += getTimeout();
    00071       }
    00072     }
    00073   }
    00074   // Ignore the exception since it is used only to exit from the loop.
    00075   catch (boost::thread_interrupted&) {}
    00076 }

void WatchdogThread::onPing | ( |  | ) | `[protected, virtual]`

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

Definition at line 83 of file WatchdogThread.cpp.

    00083                             {
    00084 }

void WatchdogThread::onStart | ( |  | ) | `[protected, virtual]`

User implemented function that will be called when starting.

Definition at line 87 of file WatchdogThread.cpp.

    00087                              {
    00088 }

void WatchdogThread::onStop | ( |  | ) | `[protected, virtual]`

User implemented function that will be called when stopping.

Definition at line 91 of file WatchdogThread.cpp.

    00091                             {
    00092 }

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.

    00044                      {
    00045     boost::mutex::scoped_lock lock(m_lastPingMutex);
    00046     m_lastPing = boost::get_system_time();
    00047     onPing();
    00048   }

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.

    00051                                                                {
    00052     m_timeout = timeout;
    00053   }

void WatchdogThread::start | ( |  | ) |

Start the watchdog thread.

Definition at line 24 of file WatchdogThread.cpp.

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

void WatchdogThread::stop | ( |  | ) |

Signal the watchdog thread to stop and wait for it.

Definition at line 36 of file WatchdogThread.cpp.

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

## 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:

- /afs/cern.ch/sw/Gaudi/releases/GAUDI/GAUDI_v22r0/GaudiKernel/GaudiKernel/WatchdogThread.h
- /afs/cern.ch/sw/Gaudi/releases/GAUDI/GAUDI_v22r0/GaudiKernel/src/Lib/WatchdogThread.cpp

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines