The Gaudi Framework  v36r1 (3e2fb5a8)
WatchdogThread Class Reference

#include </builds/gaudi/Gaudi/GaudiKernel/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...
 
std::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 37 of file WatchdogThread.h.

Constructor & Destructor Documentation

◆ WatchdogThread()

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

Constructor.

Definition at line 22 of file WatchdogThread.cpp.

23  : m_timeout( std::move( timeout ) ), m_running( false ) {
24  // Start the thread immediately if requested.
25  if ( autostart ) start();
26 }

◆ ~WatchdogThread()

WatchdogThread::~WatchdogThread ( )
virtual

Destructor.

Definition at line 28 of file WatchdogThread.cpp.

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

Member Function Documentation

◆ action()

void WatchdogThread::action ( )
protectedvirtual

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

Definition at line 88 of file WatchdogThread.cpp.

88 {}

◆ getLastPing()

boost::system_time WatchdogThread::getLastPing ( ) const
inline

Get the time of latest ping.

Definition at line 69 of file WatchdogThread.h.

69  {
70  auto lock = std::scoped_lock{m_lastPingMutex};
71  return m_lastPing;
72  }

◆ getTimeout()

boost::posix_time::time_duration WatchdogThread::getTimeout ( ) const
inline

Get the current time-out value.

Definition at line 66 of file WatchdogThread.h.

66 { return m_timeout; }

◆ i_run()

void WatchdogThread::i_run ( )
private

Core function of the secondary thread.

Definition at line 57 of file WatchdogThread.cpp.

57  {
58  // Copy of the last ping
59  boost::system_time lastPing = getLastPing();
60 
61  // set initial check time
62  boost::system_time nextCheck = lastPing + getTimeout();
63 
64  try {
65  // enter infinite loop
66  while ( m_running ) {
67  // Wait until the next check point time is reached.
68  // An early exit must be triggered by a call to this->interrupt(), which
69  // will produce an exception during the sleep.
70  boost::thread::sleep( nextCheck );
71  // Check if there was a ping while we were sleeping
72  if ( lastPing == getLastPing() ) { // no further accesses
73  action();
74  // schedule the next check for now + dt (seems a good estimate)
75  nextCheck = boost::get_system_time() + getTimeout();
76  } else { // there was a ping
77  // schedule the next check for last_access + dt
78  nextCheck = lastPing = getLastPing();
79  nextCheck += getTimeout();
80  }
81  }
82  }
83  // Ignore the exception since it is used only to exit from the loop.
84  catch ( boost::thread_interrupted& ) {}
85 }

◆ onPing()

void WatchdogThread::onPing ( )
protectedvirtual

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

Definition at line 91 of file WatchdogThread.cpp.

91 {}

◆ onStart()

void WatchdogThread::onStart ( )
protectedvirtual

User implemented function that will be called when starting.

Definition at line 94 of file WatchdogThread.cpp.

94 {}

◆ onStop()

void WatchdogThread::onStop ( )
protectedvirtual

User implemented function that will be called when stopping.

Definition at line 97 of file WatchdogThread.cpp.

97 {}

◆ ping()

void WatchdogThread::ping ( )
inline

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

Definition at line 56 of file WatchdogThread.h.

56  {
57  auto lock = std::scoped_lock{m_lastPingMutex};
58  m_lastPing = boost::get_system_time();
59  onPing();
60  }

◆ setTimeout()

void WatchdogThread::setTimeout ( boost::posix_time::time_duration  timeout)
inline

Change the duration of the time-out.

Definition at line 63 of file WatchdogThread.h.

63 { m_timeout = timeout; }

◆ start()

void WatchdogThread::start ( )

Start the watchdog thread.

Definition at line 33 of file WatchdogThread.cpp.

33  {
34  if ( !m_thread ) { // can be started only if the thread is not yet started
35  m_running = true;
36  // call user-defined function
37  onStart();
38  // Initialize the first "last ping"
39  ping();
40  // Start a new thread telling it to call the member function i_run()
41  m_thread = std::make_unique<boost::thread>( std::mem_fn( &WatchdogThread::i_run ), this );
42  }
43 }

◆ stop()

void WatchdogThread::stop ( )

Signal the watchdog thread to stop and wait for it.

Definition at line 45 of file WatchdogThread.cpp.

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

Member Data Documentation

◆ m_lastPing

boost::system_time WatchdogThread::m_lastPing
private

When the last ping was received.

Definition at line 92 of file WatchdogThread.h.

◆ m_lastPingMutex

std::mutex WatchdogThread::m_lastPingMutex
mutableprivate

Mutex for the access to the m_lastPing data member.

Definition at line 106 of file WatchdogThread.h.

◆ m_running

bool WatchdogThread::m_running
private

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

Definition at line 98 of file WatchdogThread.h.

◆ m_thread

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

Pointer to the running thread;.

Definition at line 95 of file WatchdogThread.h.

◆ m_timeout

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

Number of seconds allowed between pings.

Definition at line 89 of file WatchdogThread.h.


The documentation for this class was generated from the following files:
WatchdogThread::stop
void stop()
Signal the watchdog thread to stop and wait for it.
Definition: WatchdogThread.cpp:45
WatchdogThread::ping
void ping()
Function to call to notify the watchdog thread that we are still alive.
Definition: WatchdogThread.h:56
std::lock
T lock(T... args)
std::move
T move(T... args)
std::mem_fn
T mem_fn(T... args)
WatchdogThread::onPing
virtual void onPing()
User implemented function that will be called when ping is called.
Definition: WatchdogThread.cpp:91
WatchdogThread::getTimeout
boost::posix_time::time_duration getTimeout() const
Get the current time-out value.
Definition: WatchdogThread.h:66
std::unique_ptr::reset
T reset(T... args)
WatchdogThread::onStart
virtual void onStart()
User implemented function that will be called when starting.
Definition: WatchdogThread.cpp:94
Gaudi::NanoSleep
GAUDI_API void NanoSleep(long long nsec)
Small variation on the sleep function for nanoseconds sleep.
Definition: Sleep.cpp:19
WatchdogThread::start
void start()
Start the watchdog thread.
Definition: WatchdogThread.cpp:33
WatchdogThread::m_running
bool m_running
Flag to mark the thread as running/stopped (avoid possible race conditions).
Definition: WatchdogThread.h:98
WatchdogThread::m_lastPingMutex
std::mutex m_lastPingMutex
Mutex for the access to the m_lastPing data member.
Definition: WatchdogThread.h:106
WatchdogThread::getLastPing
boost::system_time getLastPing() const
Get the time of latest ping.
Definition: WatchdogThread.h:69
WatchdogThread::action
virtual void action()
User implemented function that will be called if the time-out is reached.
Definition: WatchdogThread.cpp:88
WatchdogThread::m_timeout
boost::posix_time::time_duration m_timeout
Number of seconds allowed between pings.
Definition: WatchdogThread.h:89
WatchdogThread::m_thread
std::unique_ptr< boost::thread > m_thread
Pointer to the running thread;.
Definition: WatchdogThread.h:95
WatchdogThread::m_lastPing
boost::system_time m_lastPing
When the last ping was received.
Definition: WatchdogThread.h:92
WatchdogThread::i_run
void i_run()
Core function of the secondary thread.
Definition: WatchdogThread.cpp:57
WatchdogThread::onStop
virtual void onStop()
User implemented function that will be called when stopping.
Definition: WatchdogThread.cpp:97