watchdog: upgrade to rearm rather than die on initial timeout

This makes watchdog reusable.

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2024-01-19 14:48:23 +01:00
parent 528ea5fffe
commit f3e117afcc

View File

@ -66,17 +66,27 @@ namespace stdex
protected: protected:
void run() void run()
{ {
size_t phase;
for (;;) { for (;;) {
std::unique_lock<std::mutex> lk(m_mutex); {
auto phase = m_phase; std::unique_lock<std::mutex> lk(m_mutex);
if (m_cv.wait_for(lk, m_timeout, [&] {return m_quit || phase != m_phase; })) { 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<std::mutex> lk(m_mutex);
m_cv.wait(lk, [&] {return m_quit || phase != m_phase; });
if (m_quit) if (m_quit)
break; break;
} }
else {
m_callback();
break;
}
} }
} }