| The Gaudi Framework v32r1 (f65d50dc)

Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
WatchdogThread Class Reference

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

`#include <GaudiKernel/WatchdogThread.h>`

Collaboration diagram for WatchdogThread:
[legend]

## 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 27 of file WatchdogThread.h.

## Constructor & Destructor Documentation

## ◆ WatchdogThread()

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 // Start the thread immediately if requested.
15 if ( autostart ) start();
16 }
WatchdogThread::start
void start()
Start the watchdog thread.
Definition: WatchdogThread.cpp:23
WatchdogThread::m_timeout
boost::posix_time::time_duration m_timeout
Number of seconds allowed between pings.
Definition: WatchdogThread.h:79
std::move
T move(T... args)
WatchdogThread::m_running
bool m_running
Flag to mark the thread as running/stopped (avoid possible race conditions).
Definition: WatchdogThread.h:88

## ◆ ~WatchdogThread()

WatchdogThread::~WatchdogThread | ( |  | ) |
| virtual

Destructor.

Definition at line 18 of file WatchdogThread.cpp.

18 {
19 // Make sure the thread is stopped before exiting.
20 stop();
21 }
WatchdogThread::stop
void stop()
Signal the watchdog thread to stop and wait for it.
Definition: WatchdogThread.cpp:35

## Member Function Documentation

## ◆ action()

void WatchdogThread::action | ( |  | ) |
| protected virtual

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

Definition at line 78 of file WatchdogThread.cpp.

78 {}

## ◆ getLastPing()

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

Get the time of latest ping.

Definition at line 59 of file WatchdogThread.h.

59 {
60 boost::mutex::scoped_lock lock( m_lastPingMutex );
61 return m_lastPing;
62 }
WatchdogThread::m_lastPing
boost::system_time m_lastPing
When the last ping was received.
Definition: WatchdogThread.h:82
std::lock
T lock(T... args)
WatchdogThread::m_lastPingMutex
boost::mutex m_lastPingMutex
Mutex for the access to the m_lastPing data member.
Definition: WatchdogThread.h:96

## ◆ getTimeout()

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

Get the current time-out value.

Definition at line 56 of file WatchdogThread.h.

56 { return m_timeout; }
WatchdogThread::m_timeout
boost::posix_time::time_duration m_timeout
Number of seconds allowed between pings.
Definition: WatchdogThread.h:79

## ◆ i_run()

void WatchdogThread::i_run | ( |  | ) |
| private

Core function of the secondary thread.

Definition at line 47 of file WatchdogThread.cpp.

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

## ◆ onPing()

void WatchdogThread::onPing | ( |  | ) |
| protected virtual

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

Definition at line 81 of file WatchdogThread.cpp.

81 {}

## ◆ onStart()

void WatchdogThread::onStart | ( |  | ) |
| protected virtual

User implemented function that will be called when starting.

Definition at line 84 of file WatchdogThread.cpp.

84 {}

## ◆ onStop()

void WatchdogThread::onStop | ( |  | ) |
| protected virtual

User implemented function that will be called when stopping.

Definition at line 87 of file WatchdogThread.cpp.

87 {}

## ◆ ping()

void WatchdogThread::ping | ( |  | ) |
| inline

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

Definition at line 46 of file WatchdogThread.h.

46 {
47 boost::mutex::scoped_lock lock( m_lastPingMutex );
48 m_lastPing = boost::get_system_time();
49 onPing();
50 }
WatchdogThread::m_lastPing
boost::system_time m_lastPing
When the last ping was received.
Definition: WatchdogThread.h:82
std::lock
T lock(T... args)
WatchdogThread::onPing
virtual void onPing()
User implemented function that will be called when ping is called.
Definition: WatchdogThread.cpp:81
WatchdogThread::m_lastPingMutex
boost::mutex m_lastPingMutex
Mutex for the access to the m_lastPing data member.
Definition: WatchdogThread.h:96

## ◆ setTimeout()

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

Change the duration of the time-out.

Definition at line 53 of file WatchdogThread.h.

53 { m_timeout = timeout; }
WatchdogThread::m_timeout
boost::posix_time::time_duration m_timeout
Number of seconds allowed between pings.
Definition: WatchdogThread.h:79

## ◆ start()

void WatchdogThread::start | ( |  | ) |

Start the watchdog thread.

Definition at line 23 of file WatchdogThread.cpp.

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

## ◆ stop()

void WatchdogThread::stop | ( |  | ) |

Signal the watchdog thread to stop and wait for it.

Definition at line 35 of file WatchdogThread.cpp.

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

## Member Data Documentation

## ◆ m_lastPing

boost::system_time WatchdogThread::m_lastPing
| private

When the last ping was received.

Definition at line 82 of file WatchdogThread.h.

## ◆ m_lastPingMutex

boost::mutex WatchdogThread::m_lastPingMutex
| mutable private

Mutex for the access to the m_lastPing data member.

Definition at line 96 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 88 of file WatchdogThread.h.

## ◆ m_thread

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

Pointer to the running thread;.

Definition at line 85 of file WatchdogThread.h.

## ◆ m_timeout

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

Number of seconds allowed between pings.

Definition at line 79 of file WatchdogThread.h.

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

- GaudiKernel/GaudiKernel/WatchdogThread.h
- GaudiKernel/src/Lib/WatchdogThread.cpp