aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/helpers/Watchdog.cpp
blob: b919bbc707cb9eb088ffa80fda936f577ad63e21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "Watchdog.hpp"
#include <csignal>
#include "config/ConfigManager.hpp"
#include "../config/ConfigValue.hpp"

CWatchdog::~CWatchdog() {
    m_bExitThread = true;
    m_bNotified   = true;
    m_cvWatchdogCondition.notify_all();

    if (m_pWatchdog && m_pWatchdog->joinable())
        m_pWatchdog->join();
}

CWatchdog::CWatchdog() : m_iMainThreadPID(pthread_self()) {

    m_pWatchdog = std::make_unique<std::thread>([this] {
        static auto PTIMEOUT = CConfigValue<Hyprlang::INT>("debug:watchdog_timeout");

        m_bWatchdogInitialized = true;
        while (!m_bExitThread) {
            std::unique_lock<std::mutex> lk(m_mWatchdogMutex);

            if (!m_bWillWatch)
                m_cvWatchdogCondition.wait(lk, [this] { return m_bNotified || m_bExitThread; });
            else if (!m_cvWatchdogCondition.wait_for(lk, std::chrono::milliseconds((int)(*PTIMEOUT * 1000.0)), [this] { return m_bNotified || m_bExitThread; }))
                pthread_kill(m_iMainThreadPID, SIGUSR1);

            if (m_bExitThread)
                break;

            m_bWatching = false;
            m_bNotified = false;
        }
    });
}

void CWatchdog::startWatching() {
    static auto PTIMEOUT = CConfigValue<Hyprlang::INT>("debug:watchdog_timeout");

    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();
}