17 #include <util/watchdog.h>
22 using namespace std::chrono;
24 watchdog::watchdog(system_clock::duration timeout, std::function<
void(
void)> alarm_cb, system_clock::duration sleep_duration)
26 , sleep_duration(sleep_duration)
27 , alarm_cb(std::move(alarm_cb)) {
30 guard_thread = std::thread([
this] { guard(); });
33 watchdog::watchdog(system_clock::duration timeout, std::function<
void(
void)> alarm)
34 :
watchdog(timeout, alarm, timeout / 10){};
36 watchdog::~watchdog() {
42 void watchdog::guard() {
46 std::unique_lock<std::mutex> live_lock(guard_mutex);
47 wakeup.wait(live_lock, [
this]() {
return !this->idle.load() || !this->live.load(); });
52 auto now = system_clock::now();
53 if((now - touched.load()) > timeout) {
60 std::unique_lock<std::mutex> live_lock(guard_mutex);
61 wakeup.wait_for(live_lock, sleep_duration, [
this]() {
return !this->live.load(); });
a watch dog based on https://github.com/didenko/TimeoutGuard
watchdog(std::chrono::system_clock::duration timeout, std::function< void(void)> alarm_cb, std::chrono::system_clock::duration sleep_duration)