summaryrefslogtreecommitdiff
path: root/src/helpers/Watchdog.hpp
blob: b16cb518f9385380804110c05ed270fbd8146cde (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
#pragma once

#include <memory>
#include <chrono>
#include <thread>
#include <condition_variable>

class CWatchdog {
  public:
    // must be called from the main thread
    CWatchdog();
    ~CWatchdog();

    void              startWatching();
    void              endWatching();

    std::atomic<bool> m_bWatchdogInitialized{false};

  private:
    std::chrono::high_resolution_clock::time_point m_tTriggered;

    pthread_t                                      m_iMainThreadPID = 0;

    std::atomic<bool>                              m_bWatching  = false;
    std::atomic<bool>                              m_bWillWatch = false;

    std::unique_ptr<std::thread>                   m_pWatchdog;
    std::mutex                                     m_mWatchdogMutex;
    std::atomic<bool>                              m_bNotified   = false;
    std::atomic<bool>                              m_bExitThread = false;
    std::condition_variable                        m_cvWatchdogCondition;
};

inline std::unique_ptr<CWatchdog> g_pWatchdog;