The Gaudi Framework  master (594c33fa)
WatchdogThread Class Reference

#include </builds/gaudi/Gaudi/GaudiKernel/include/GaudiKernel/WatchdogThread.h>

Collaboration diagram for WatchdogThread:

Public Types

using clock = std::chrono::system_clock
 
using time_point = std::chrono::time_point< clock >
 

Public Member Functions

 WatchdogThread (std::chrono::seconds 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 (std::chrono::seconds timeout)
 Change the duration of the time-out. More...
 
std::chrono::seconds getTimeout () const
 Get the current time-out value. More...
 
time_point 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 Attributes

std::chrono::seconds m_timeout
 Number of seconds allowed between pings. More...
 
std::atomic< time_pointm_lastPing
 When the last ping was received. More...
 
std::thread m_thread
 Running thread;. More...
 
std::promise< void > m_stop_thread
 Flag to mark the thread as running/stopped (avoid possible race conditions). 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 27 of file WatchdogThread.h.

Member Typedef Documentation

◆ clock

◆ time_point

Constructor & Destructor Documentation

◆ WatchdogThread()

WatchdogThread::WatchdogThread ( std::chrono::seconds  timeout,
bool  autostart = false 
)

Constructor.

Definition at line 18 of file WatchdogThread.cpp.

18  : m_timeout( std::move( timeout ) ) {
19  // Start the thread immediately if requested.
20  if ( autostart ) start();
21 }

◆ ~WatchdogThread()

WatchdogThread::~WatchdogThread ( )
virtual

Destructor.

Definition at line 23 of file WatchdogThread.cpp.

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

Member Function Documentation

◆ action()

void WatchdogThread::action ( )
protectedvirtual

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

Definition at line 70 of file WatchdogThread.cpp.

70 {}

◆ getLastPing()

time_point WatchdogThread::getLastPing ( ) const
inline

Get the time of latest ping.

Definition at line 61 of file WatchdogThread.h.

61 { return m_lastPing.load(); }

◆ getTimeout()

std::chrono::seconds WatchdogThread::getTimeout ( ) const
inline

Get the current time-out value.

Definition at line 58 of file WatchdogThread.h.

58 { return m_timeout; }

◆ onPing()

void WatchdogThread::onPing ( )
protectedvirtual

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

Definition at line 73 of file WatchdogThread.cpp.

73 {}

◆ onStart()

void WatchdogThread::onStart ( )
protectedvirtual

User implemented function that will be called when starting.

Definition at line 76 of file WatchdogThread.cpp.

76 {}

◆ onStop()

void WatchdogThread::onStop ( )
protectedvirtual

User implemented function that will be called when stopping.

Definition at line 79 of file WatchdogThread.cpp.

79 {}

◆ ping()

void WatchdogThread::ping ( )
inline

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

Definition at line 49 of file WatchdogThread.h.

49  {
50  m_lastPing.store( clock::now() );
51  onPing();
52  }

◆ setTimeout()

void WatchdogThread::setTimeout ( std::chrono::seconds  timeout)
inline

Change the duration of the time-out.

Definition at line 55 of file WatchdogThread.h.

55 { m_timeout = timeout; }

◆ start()

void WatchdogThread::start ( )

Start the watchdog thread.

Definition at line 28 of file WatchdogThread.cpp.

28  {
29  if ( !m_thread.joinable() ) { // can be started only if the thread is not yet started
30  // call user-defined function
31  onStart();
32  // Initialize the first "last ping"
33  ping();
34  // Start a new thread telling it to call the member function i_run()
35  m_thread = std::thread{ [this, is_stopped = m_stop_thread.get_future()]() {
36  // Copy of the last ping
37  auto lastPing = getLastPing();
38 
39  // set initial check time
40  auto nextCheck = lastPing + getTimeout();
41 
42  // enter infinite loop
43  // Wait until the next check point time is reached.
44  while ( is_stopped.wait_until( nextCheck ) == std::future_status::timeout ) {
45  // Check if there was a ping while we were sleeping
46  if ( lastPing == getLastPing() ) { // no further accesses
47  action();
48  // schedule the next check for now + dt (seems a good estimate)
49  nextCheck = clock::now() + getTimeout();
50  } else { // there was a ping
51  // schedule the next check for last_access + dt
52  lastPing = getLastPing();
53  nextCheck = lastPing + getTimeout();
54  }
55  }
56  } };
57  }
58 }

◆ stop()

void WatchdogThread::stop ( )

Signal the watchdog thread to stop and wait for it.

Definition at line 60 of file WatchdogThread.cpp.

60  {
61  if ( m_thread.joinable() ) {
62  m_stop_thread.set_value(); // tell the thread to stop
63  m_thread.join(); // wait for it
64  // call user-defined function
65  onStop();
66  }
67 }

Member Data Documentation

◆ m_lastPing

std::atomic<time_point> WatchdogThread::m_lastPing
private

When the last ping was received.

Definition at line 81 of file WatchdogThread.h.

◆ m_stop_thread

std::promise<void> WatchdogThread::m_stop_thread
private

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

Definition at line 87 of file WatchdogThread.h.

◆ m_thread

std::thread WatchdogThread::m_thread
private

Running thread;.

Definition at line 84 of file WatchdogThread.h.

◆ m_timeout

std::chrono::seconds WatchdogThread::m_timeout
private

Number of seconds allowed between pings.

Definition at line 78 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:60
WatchdogThread::ping
void ping()
Function to call to notify the watchdog thread that we are still alive.
Definition: WatchdogThread.h:49
WatchdogThread::getLastPing
time_point getLastPing() const
Get the time of latest ping.
Definition: WatchdogThread.h:61
std::move
T move(T... args)
std::promise::set_value
T set_value(T... args)
WatchdogThread::onPing
virtual void onPing()
User implemented function that will be called when ping is called.
Definition: WatchdogThread.cpp:73
std::promise::get_future
T get_future(T... args)
WatchdogThread::onStart
virtual void onStart()
User implemented function that will be called when starting.
Definition: WatchdogThread.cpp:76
WatchdogThread::start
void start()
Start the watchdog thread.
Definition: WatchdogThread.cpp:28
std::thread::joinable
T joinable(T... args)
std::thread
STL class.
WatchdogThread::action
virtual void action()
User implemented function that will be called if the time-out is reached.
Definition: WatchdogThread.cpp:70
WatchdogThread::m_lastPing
std::atomic< time_point > m_lastPing
When the last ping was received.
Definition: WatchdogThread.h:81
WatchdogThread::m_timeout
std::chrono::seconds m_timeout
Number of seconds allowed between pings.
Definition: WatchdogThread.h:78
WatchdogThread::m_stop_thread
std::promise< void > m_stop_thread
Flag to mark the thread as running/stopped (avoid possible race conditions).
Definition: WatchdogThread.h:87
WatchdogThread::getTimeout
std::chrono::seconds getTimeout() const
Get the current time-out value.
Definition: WatchdogThread.h:58
WatchdogThread::onStop
virtual void onStop()
User implemented function that will be called when stopping.
Definition: WatchdogThread.cpp:79
WatchdogThread::m_thread
std::thread m_thread
Running thread;.
Definition: WatchdogThread.h:84
std::thread::join
T join(T... args)
std::chrono::system_clock::now
T now(T... args)