The Gaudi Framework  v38r3 (c3fc9673)
WatchdogThread.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 #pragma once
12 
13 #include <GaudiKernel/Kernel.h> // for GAUDI_API
14 #include <atomic>
15 #include <chrono>
16 #include <future>
17 
28 public:
31 
33  // @param timeout the time span that can occur between two pings.
34  // @param autostart if set to true, the second thread is started automatically
35  // on construction, otherwise the user have to call start().
36  WatchdogThread( std::chrono::seconds timeout, bool autostart = false );
37 
39  // Stop the thread if not done earlier.
40  virtual ~WatchdogThread();
41 
43  void start();
44 
46  void stop();
47 
49  inline void ping() {
50  m_lastPing.store( clock::now() );
51  onPing();
52  }
53 
55  inline void setTimeout( std::chrono::seconds timeout ) { m_timeout = timeout; }
56 
58  inline std::chrono::seconds getTimeout() const { return m_timeout; }
59 
61  inline time_point getLastPing() const { return m_lastPing.load(); }
62 
63 protected:
65  virtual void action();
66 
68  virtual void onPing();
69 
71  virtual void onStart();
72 
74  virtual void onStop();
75 
76 private:
79 
82 
85 
88 };
std::chrono::system_clock
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::promise< void >
std::chrono::seconds
WatchdogThread::setTimeout
void setTimeout(std::chrono::seconds timeout)
Change the duration of the time-out.
Definition: WatchdogThread.h:55
IOTest.start
start
Definition: IOTest.py:110
WatchdogThread
Definition: WatchdogThread.h:27
std::thread
STL class.
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
std::chrono::time_point
std::atomic
STL class.
Kernel.h
WatchdogThread::getTimeout
std::chrono::seconds getTimeout() const
Get the current time-out value.
Definition: WatchdogThread.h:58
gaudirun.action
action
Definition: gaudirun.py:153
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
WatchdogThread::m_thread
std::thread m_thread
Running thread;.
Definition: WatchdogThread.h:84