The Gaudi Framework  v30r3 (a5ef0a68)
WatchdogThread Class Reference

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

#include <GaudiKernel/WatchdogThread.h>

Collaboration diagram for WatchdogThread:

Public Member Functions

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

Protected Member Functions

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

Private Member Functions

void i_run ()
 Core function of the secondary thread. More...
 

Private Attributes

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

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

13  : m_timeout( std::move( timeout ) ), m_running( false )
14 {
15  // Start the thread immediately if requested.
16  if ( autostart ) start();
17 }
void start()
Start the watchdog thread.
boost::posix_time::time_duration m_timeout
Number of seconds allowed between pings.
T move(T...args)
bool m_running
Flag to mark the thread as running/stopped (avoid possible race conditions).
WatchdogThread::~WatchdogThread ( )
virtual

Destructor.

Definition at line 19 of file WatchdogThread.cpp.

20 {
21  // Make sure the thread is stopped before exiting.
22  stop();
23 }
void stop()
Signal the watchdog thread to stop and wait for it.

Member Function Documentation

void WatchdogThread::action ( )
protectedvirtual

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

Definition at line 84 of file WatchdogThread.cpp.

84 {}
boost::system_time WatchdogThread::getLastPing ( ) const
inline

Get the time of latest ping.

Definition at line 62 of file WatchdogThread.h.

63  {
64  boost::mutex::scoped_lock lock( m_lastPingMutex );
65  return m_lastPing;
66  }
boost::system_time m_lastPing
When the last ping was received.
T lock(T...args)
boost::mutex m_lastPingMutex
Mutex for the access to the m_lastPing data member.
boost::posix_time::time_duration WatchdogThread::getTimeout ( ) const
inline

Get the current time-out value.

Definition at line 59 of file WatchdogThread.h.

59 { return m_timeout; }
boost::posix_time::time_duration m_timeout
Number of seconds allowed between pings.
void WatchdogThread::i_run ( )
private

Core function of the secondary thread.

Definition at line 51 of file WatchdogThread.cpp.

52 {
53  // Copy of the last ping
54  boost::system_time lastPing = getLastPing();
55 
56  // set initial check time
57  boost::system_time nextCheck = lastPing + getTimeout();
58 
59  try {
60  // enter infinite loop
61  while ( m_running ) {
62  // Wait until the next check point time is reached.
63  // An early exit must be triggered by a call to this->interrupt(), which
64  // will produce an exception during the sleep.
65  boost::thread::sleep( nextCheck );
66  // Check if there was a ping while we were sleeping
67  if ( lastPing == getLastPing() ) { // no further accesses
68  action();
69  // schedule the next check for now + dt (seems a good estimate)
70  nextCheck = boost::get_system_time() + getTimeout();
71  } else { // there was a ping
72  // schedule the next check for last_access + dt
73  nextCheck = lastPing = getLastPing();
74  nextCheck += getTimeout();
75  }
76  }
77  }
78  // Ignore the exception since it is used only to exit from the loop.
79  catch ( boost::thread_interrupted& ) {
80  }
81 }
boost::posix_time::time_duration getTimeout() const
Get the current time-out value.
bool m_running
Flag to mark the thread as running/stopped (avoid possible race conditions).
virtual void action()
User implemented function that will be called if the time-out is reached.
boost::system_time getLastPing() const
Get the time of latest ping.
void WatchdogThread::onPing ( )
protectedvirtual

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

Definition at line 87 of file WatchdogThread.cpp.

87 {}
void WatchdogThread::onStart ( )
protectedvirtual

User implemented function that will be called when starting.

Definition at line 90 of file WatchdogThread.cpp.

90 {}
void WatchdogThread::onStop ( )
protectedvirtual

User implemented function that will be called when stopping.

Definition at line 93 of file WatchdogThread.cpp.

93 {}
void WatchdogThread::ping ( )
inline

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

Definition at line 48 of file WatchdogThread.h.

49  {
50  boost::mutex::scoped_lock lock( m_lastPingMutex );
51  m_lastPing = boost::get_system_time();
52  onPing();
53  }
boost::system_time m_lastPing
When the last ping was received.
T lock(T...args)
virtual void onPing()
User implemented function that will be called when ping is called.
boost::mutex m_lastPingMutex
Mutex for the access to the m_lastPing data member.
void WatchdogThread::setTimeout ( boost::posix_time::time_duration  timeout)
inline

Change the duration of the time-out.

Definition at line 56 of file WatchdogThread.h.

56 { m_timeout = timeout; }
boost::posix_time::time_duration m_timeout
Number of seconds allowed between pings.
void WatchdogThread::start ( )

Start the watchdog thread.

Definition at line 25 of file WatchdogThread.cpp.

26 {
27  if ( !m_thread ) { // can be started only if the thread is not yet started
28  m_running = true;
29  // call user-defined function
30  onStart();
31  // Initialize the first "last ping"
32  ping();
33  // Start a new thread telling it to call the member function i_run()
34  m_thread = std::make_unique<boost::thread>( std::mem_fn( &WatchdogThread::i_run ), this );
35  }
36 }
void ping()
Function to call to notify the watchdog thread that we are still alive.
bool m_running
Flag to mark the thread as running/stopped (avoid possible race conditions).
T mem_fn(T...args)
void i_run()
Core function of the secondary thread.
std::unique_ptr< boost::thread > m_thread
Pointer to the running thread;.
virtual void onStart()
User implemented function that will be called when starting.
void WatchdogThread::stop ( )

Signal the watchdog thread to stop and wait for it.

Definition at line 38 of file WatchdogThread.cpp.

39 {
40  if ( m_thread ) {
41  m_running = false; // mark the thread as stopped (interrupt doesn't work if the thread is not sleeping)
42  Gaudi::NanoSleep( 1000000 ); // Wait a bit (1ms) to be sure that the interrupt happens during the sleep
43  m_thread->interrupt(); // tell the thread to stop (if it is waiting)
44  m_thread->join(); // wait for it
45  m_thread.reset(); // delete it
46  // call user-defined function
47  onStop();
48  }
49 }
virtual void onStop()
User implemented function that will be called when stopping.
GAUDI_API void NanoSleep(long long nsec)
Small variation on the sleep function for nanoseconds sleep.
Definition: Sleep.cpp:10
T reset(T...args)
bool m_running
Flag to mark the thread as running/stopped (avoid possible race conditions).
std::unique_ptr< boost::thread > m_thread
Pointer to the running thread;.

Member Data Documentation

boost::system_time WatchdogThread::m_lastPing
private

When the last ping was received.

Definition at line 86 of file WatchdogThread.h.

boost::mutex WatchdogThread::m_lastPingMutex
mutableprivate

Mutex for the access to the m_lastPing data member.

Definition at line 100 of file WatchdogThread.h.

bool WatchdogThread::m_running
private

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

Definition at line 92 of file WatchdogThread.h.

std::unique_ptr<boost::thread> WatchdogThread::m_thread
private

Pointer to the running thread;.

Definition at line 89 of file WatchdogThread.h.

boost::posix_time::time_duration WatchdogThread::m_timeout
private

Number of seconds allowed between pings.

Definition at line 83 of file WatchdogThread.h.


The documentation for this class was generated from the following files: