diff options
| author | vaxerski <43317083+vaxerski@users.noreply.github.com> | 2023-09-29 16:38:13 +0100 |
|---|---|---|
| committer | vaxerski <43317083+vaxerski@users.noreply.github.com> | 2023-09-29 16:38:13 +0100 |
| commit | 9cc614d096db4b7c1de6baaa055ca6f741aa4370 (patch) | |
| tree | 23e6c59fed8dea0c27669bd9a4dec6a31e54aa8b /src/helpers/Watchdog.cpp | |
| parent | 3f09b14381e8b28dd2cc1d292763374f2d6c8484 (diff) | |
internal: add a watchdog
a watchdog will abort processing a signal if a timeout specified via debug:watchdog_timeout is reached.
Diffstat (limited to 'src/helpers/Watchdog.cpp')
| -rw-r--r-- | src/helpers/Watchdog.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/helpers/Watchdog.cpp b/src/helpers/Watchdog.cpp new file mode 100644 index 00000000..92f20dfb --- /dev/null +++ b/src/helpers/Watchdog.cpp @@ -0,0 +1,49 @@ +#include "Watchdog.hpp" +#include <signal.h> +#include "config/ConfigManager.hpp" + +CWatchdog::CWatchdog() { + m_iMainThreadPID = pthread_self(); + + m_pWatchdog = std::make_unique<std::thread>([this] { + static auto* const PTIMEOUT = &g_pConfigManager->getConfigValuePtr("debug:watchdog_timeout")->intValue; + + while (1337) { + std::unique_lock lk(m_mWatchdogMutex); + + if (!m_bWillWatch) + m_cvWatchdogCondition.wait(lk, [this] { return m_bNotified; }); + else { + if (m_cvWatchdogCondition.wait_for(lk, std::chrono::milliseconds((int)(*PTIMEOUT * 1000.0)), [this] { return m_bNotified; }) == false) + pthread_kill(m_iMainThreadPID, SIGUSR1); + } + + m_bWatching = false; + m_bNotified = false; + } + }); + + m_pWatchdog->detach(); +} + +void CWatchdog::startWatching() { + static auto* const PTIMEOUT = &g_pConfigManager->getConfigValuePtr("debug:watchdog_timeout")->intValue; + + if (*PTIMEOUT == 0) + return; + + m_tTriggered = std::chrono::high_resolution_clock::now(); + m_bWillWatch = true; + m_bWatching = true; + + m_bNotified = true; + m_cvWatchdogCondition.notify_all(); +} + +void CWatchdog::endWatching() { + m_bWatching = false; + m_bWillWatch = false; + + m_bNotified = true; + m_cvWatchdogCondition.notify_all(); +}
\ No newline at end of file |
