| The Gaudi Framework v29r1 (5cd95fa2)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

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 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 }
WatchdogThread::start
void start()
Start the watchdog thread.
Definition: WatchdogThread.cpp:25
WatchdogThread::m_timeout
boost::posix_time::time_duration m_timeout
Number of seconds allowed between pings.
Definition: WatchdogThread.h:83
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:92

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 }
WatchdogThread::stop
void stop()
Signal the watchdog thread to stop and wait for it.
Definition: WatchdogThread.cpp:38

## Member Function Documentation

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

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 }
WatchdogThread::m_lastPing
boost::system_time m_lastPing
When the last ping was received.
Definition: WatchdogThread.h:86
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:100

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; }
WatchdogThread::m_timeout
boost::posix_time::time_duration m_timeout
Number of seconds allowed between pings.
Definition: WatchdogThread.h:83

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 }
WatchdogThread::getTimeout
boost::posix_time::time_duration getTimeout() const
Get the current time-out value.
Definition: WatchdogThread.h:59
WatchdogThread::m_running
bool m_running
Flag to mark the thread as running/stopped (avoid possible race conditions).
Definition: WatchdogThread.h:92
WatchdogThread::action
virtual void action()
User implemented function that will be called if the time-out is reached.
Definition: WatchdogThread.cpp:84
WatchdogThread::getLastPing
boost::system_time getLastPing() const
Get the time of latest ping.
Definition: WatchdogThread.h:62

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

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

Definition at line 87 of file WatchdogThread.cpp.

87 {}

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

User implemented function that will be called when starting.

Definition at line 90 of file WatchdogThread.cpp.

90 {}

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

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 }
WatchdogThread::m_lastPing
boost::system_time m_lastPing
When the last ping was received.
Definition: WatchdogThread.h:86
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:87
WatchdogThread::m_lastPingMutex
boost::mutex m_lastPingMutex
Mutex for the access to the m_lastPing data member.
Definition: WatchdogThread.h:100

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; }
WatchdogThread::m_timeout
boost::posix_time::time_duration m_timeout
Number of seconds allowed between pings.
Definition: WatchdogThread.h:83

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 }
WatchdogThread::ping
void ping()
Function to call to notify the watchdog thread that we are still alive.
Definition: WatchdogThread.h:48
WatchdogThread::m_running
bool m_running
Flag to mark the thread as running/stopped (avoid possible race conditions).
Definition: WatchdogThread.h:92
std::mem_fn
T mem_fn(T...args)
WatchdogThread::i_run
void i_run()
Core function of the secondary thread.
Definition: WatchdogThread.cpp:51
WatchdogThread::m_thread
std::unique_ptr< boost::thread > m_thread
Pointer to the running thread;.
Definition: WatchdogThread.h:89
WatchdogThread::onStart
virtual void onStart()
User implemented function that will be called when starting.
Definition: WatchdogThread.cpp:90

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 }
WatchdogThread::onStop
virtual void onStop()
User implemented function that will be called when stopping.
Definition: WatchdogThread.cpp:93
Gaudi::NanoSleep
GAUDI_API void NanoSleep(long long nsec)
Small variation on the sleep function for nanoseconds sleep.
Definition: Sleep.cpp:10
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:92
WatchdogThread::m_thread
std::unique_ptr< boost::thread > m_thread
Pointer to the running thread;.
Definition: WatchdogThread.h:89

## 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
| mutable private

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:

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