The Gaudi Framework  v38r3 (c3fc9673)
WatchdogThread.cpp
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 /*
12  * Implementation of the class WatchdogThread.
13  */
14 
16 #include <thread>
17 
18 WatchdogThread::WatchdogThread( std::chrono::seconds timeout, bool autostart ) : m_timeout( std::move( timeout ) ) {
19  // Start the thread immediately if requested.
20  if ( autostart ) start();
21 }
22 
24  // Make sure the thread is stopped before exiting.
25  stop();
26 }
27 
29  if ( !m_thread.joinable() ) { // can be started only if the thread is not yet started
30  // call user-defined function
31  onStart();
32  // Initialize the first "last ping"
33  ping();
34  // Start a new thread telling it to call the member function i_run()
35  m_thread = std::thread{ [this, is_stopped = m_stop_thread.get_future()]() {
36  // Copy of the last ping
37  auto lastPing = getLastPing();
38 
39  // set initial check time
40  auto nextCheck = lastPing + getTimeout();
41 
42  // enter infinite loop
43  // Wait until the next check point time is reached.
44  while ( is_stopped.wait_until( nextCheck ) == std::future_status::timeout ) {
45  // Check if there was a ping while we were sleeping
46  if ( lastPing == getLastPing() ) { // no further accesses
47  action();
48  // schedule the next check for now + dt (seems a good estimate)
49  nextCheck = clock::now() + getTimeout();
50  } else { // there was a ping
51  // schedule the next check for last_access + dt
52  lastPing = getLastPing();
53  nextCheck = lastPing + getTimeout();
54  }
55  }
56  } };
57  }
58 }
59 
61  if ( m_thread.joinable() ) {
62  m_stop_thread.set_value(); // tell the thread to stop
63  m_thread.join(); // wait for it
64  // call user-defined function
65  onStop();
66  }
67 }
68 
69 // Default implementation: empty
71 
72 // Default implementation: empty
74 
75 // Default implementation: empty
77 
78 // Default implementation: empty
WatchdogThread::stop
void stop()
Signal the watchdog thread to stop and wait for it.
Definition: WatchdogThread.cpp:60
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
WatchdogThread::WatchdogThread
WatchdogThread(std::chrono::seconds timeout, bool autostart=false)
Constructor.
Definition: WatchdogThread.cpp:18
std::chrono::seconds
std::promise::set_value
T set_value(T... args)
WatchdogThread::~WatchdogThread
virtual ~WatchdogThread()
Destructor.
Definition: WatchdogThread.cpp:23
WatchdogThread::onPing
virtual void onPing()
User implemented function that will be called when ping is called.
Definition: WatchdogThread.cpp:73
std::promise::get_future
T get_future(T... args)
WatchdogThread::onStart
virtual void onStart()
User implemented function that will be called when starting.
Definition: WatchdogThread.cpp:76
WatchdogThread::start
void start()
Start the watchdog thread.
Definition: WatchdogThread.cpp:28
WatchdogThread.h
std::thread::joinable
T joinable(T... args)
std::thread
STL class.
WatchdogThread::action
virtual void action()
User implemented function that will be called if the time-out is reached.
Definition: WatchdogThread.cpp:70
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
STL namespace.
WatchdogThread::getTimeout
std::chrono::seconds getTimeout() const
Get the current time-out value.
Definition: WatchdogThread.h:58
WatchdogThread::onStop
virtual void onStop()
User implemented function that will be called when stopping.
Definition: WatchdogThread.cpp:79
WatchdogThread::m_thread
std::thread m_thread
Running thread;.
Definition: WatchdogThread.h:84
std::thread::join
T join(T... args)
std::chrono::system_clock::now
T now(T... args)