From f3e117afccfac0ff4196cd6f6b55e1bf14b92a31 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Fri, 19 Jan 2024 14:48:23 +0100 Subject: [PATCH] watchdog: upgrade to rearm rather than die on initial timeout This makes watchdog reusable. Signed-off-by: Simon Rozman --- include/stdex/watchdog.hpp | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/include/stdex/watchdog.hpp b/include/stdex/watchdog.hpp index 67558d9dc..e5b87032e 100644 --- a/include/stdex/watchdog.hpp +++ b/include/stdex/watchdog.hpp @@ -66,17 +66,27 @@ namespace stdex protected: void run() { + size_t phase; for (;;) { - std::unique_lock lk(m_mutex); - auto phase = m_phase; - if (m_cv.wait_for(lk, m_timeout, [&] {return m_quit || phase != m_phase; })) { + { + std::unique_lock lk(m_mutex); + phase = m_phase; + if (m_cv.wait_for(lk, m_timeout, [&] {return m_quit || phase != m_phase; })) { + if (m_quit) + break; + // reset() called in time. + continue; + } + } + // Timeout + m_callback(); + { + // Sleep until next reset(). + std::unique_lock lk(m_mutex); + m_cv.wait(lk, [&] {return m_quit || phase != m_phase; }); if (m_quit) break; } - else { - m_callback(); - break; - } } }