| The Gaudi Framework v28r2p1 (f1a77ff4)

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

12 :
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:24
WatchdogThread::m_timeout
boost::posix_time::time_duration m_timeout
Number of seconds allowed between pings.
Definition: WatchdogThread.h:81
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:90

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

Destructor.

Definition at line 19 of file WatchdogThread.cpp.

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

## Member Function Documentation

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

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

Definition at line 79 of file WatchdogThread.cpp.

79 {
80 }

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

Get the time of latest ping.

Definition at line 61 of file WatchdogThread.h.

61 {
62 boost::mutex::scoped_lock lock(m_lastPingMutex);
63 return m_lastPing;
64 }
WatchdogThread::m_lastPing
boost::system_time m_lastPing
When the last ping was received.
Definition: WatchdogThread.h:84
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:98

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

Get the current time-out value.

Definition at line 56 of file WatchdogThread.h.

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

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

Core function of the secondary thread.

Definition at line 48 of file WatchdogThread.cpp.

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

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

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

Definition at line 83 of file WatchdogThread.cpp.

83 {
84 }

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

User implemented function that will be called when starting.

Definition at line 87 of file WatchdogThread.cpp.

87 {
88 }

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

User implemented function that will be called when stopping.

Definition at line 91 of file WatchdogThread.cpp.

91 {
92 }

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

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

Definition at line 44 of file WatchdogThread.h.

44 {
45 boost::mutex::scoped_lock lock(m_lastPingMutex);
46 m_lastPing = boost::get_system_time();
47 onPing();
48 }
WatchdogThread::m_lastPing
boost::system_time m_lastPing
When the last ping was received.
Definition: WatchdogThread.h:84
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:83
WatchdogThread::m_lastPingMutex
boost::mutex m_lastPingMutex
Mutex for the access to the m_lastPing data member.
Definition: WatchdogThread.h:98

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

Change the duration of the time-out.

Definition at line 51 of file WatchdogThread.h.

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

void WatchdogThread::start | ( |  | ) |

Start the watchdog thread.

Definition at line 24 of file WatchdogThread.cpp.

24 {
25 if (!m_thread) { // can be started only if the thread is not yet started
26 m_running = true;
27 // call user-defined function
28 onStart();
29 // Initialize the first "last ping"
30 ping();
31 // Start a new thread telling it to call the member function i_run()
32 m_thread.reset(new boost::thread(std::mem_fun(&WatchdogThread::i_run), this));
33 }
34 }
WatchdogThread::ping
void ping()
Function to call to notify the watchdog thread that we are still alive.
Definition: WatchdogThread.h:44
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:90
WatchdogThread::i_run
void i_run()
Core function of the secondary thread.
Definition: WatchdogThread.cpp:48
WatchdogThread::m_thread
std::unique_ptr< boost::thread > m_thread
Pointer to the running thread;.
Definition: WatchdogThread.h:87
WatchdogThread::onStart
virtual void onStart()
User implemented function that will be called when starting.
Definition: WatchdogThread.cpp:87

void WatchdogThread::stop | ( |  | ) |

Signal the watchdog thread to stop and wait for it.

Definition at line 36 of file WatchdogThread.cpp.

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

## Member Data Documentation

boost::system_time WatchdogThread::m_lastPing
| private

When the last ping was received.

Definition at line 84 of file WatchdogThread.h.

boost::mutex WatchdogThread::m_lastPingMutex
| mutable private

Mutex for the access to the m_lastPing data member.

Definition at line 98 of file WatchdogThread.h.

bool WatchdogThread::m_running
| private

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

Definition at line 90 of file WatchdogThread.h.

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

Pointer to the running thread;.

Definition at line 87 of file WatchdogThread.h.

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

Number of seconds allowed between pings.

Definition at line 81 of file WatchdogThread.h.

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

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