Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v38r0 (2143aa4c)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
WatchdogThread.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIKERNEL_WATCHDOGTHREAD_H_
12 #define GAUDIKERNEL_WATCHDOGTHREAD_H_
13 
14 #include <memory>
15 #include <mutex>
16 
17 #include "boost/date_time/posix_time/posix_time_types.hpp"
18 #include "boost/thread/thread_time.hpp"
19 
20 // for GAUDI_API
21 #include "GaudiKernel/Kernel.h"
22 
23 // forward declaration
24 namespace boost {
25  class thread;
26 }
27 
38 public:
40  // @param timeout the time span that can occur between two pings.
41  // @param autostart if set to true, the second thread is started automatically
42  // on construction, otherwise the user have to call start().
43  WatchdogThread( boost::posix_time::time_duration timeout, bool autostart = false );
44 
46  // Stop the thread of not done earlier.
47  virtual ~WatchdogThread();
48 
50  void start();
51 
53  void stop();
54 
56  inline void ping() {
57  auto lock = std::scoped_lock{ m_lastPingMutex };
58  m_lastPing = boost::get_system_time();
59  onPing();
60  }
61 
63  inline void setTimeout( boost::posix_time::time_duration timeout ) { m_timeout = timeout; }
64 
66  inline boost::posix_time::time_duration getTimeout() const { return m_timeout; }
67 
69  inline boost::system_time getLastPing() const {
70  auto lock = std::scoped_lock{ m_lastPingMutex };
71  return m_lastPing;
72  }
73 
74 protected:
76  virtual void action();
77 
79  virtual void onPing();
80 
82  virtual void onStart();
83 
85  virtual void onStop();
86 
87 private:
89  boost::posix_time::time_duration m_timeout;
90 
92  boost::system_time m_lastPing;
93 
96 
98  bool m_running;
99 
101  // Waits for the time-out and if there was not a ping in the mean time, calls
102  // i_action().
103  void i_run();
104 
107 };
108 
109 #endif /* WATCHDOGTHREAD_H_ */
WatchdogThread::ping
void ping()
Function to call to notify the watchdog thread that we are still alive.
Definition: WatchdogThread.h:56
boost
Definition: WatchdogThread.h:24
WatchdogThread::getTimeout
boost::posix_time::time_duration getTimeout() const
Get the current time-out value.
Definition: WatchdogThread.h:66
IOTest.start
start
Definition: IOTest.py:108
WatchdogThread::m_running
bool m_running
Flag to mark the thread as running/stopped (avoid possible race conditions).
Definition: WatchdogThread.h:98
WatchdogThread
Definition: WatchdogThread.h:37
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::setTimeout
void setTimeout(boost::posix_time::time_duration timeout)
Change the duration of the time-out.
Definition: WatchdogThread.h:63
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
Kernel.h
std::mutex
STL class.
gaudirun.action
action
Definition: gaudirun.py:153
std::unique_ptr< boost::thread >
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81