]> granicus.if.org Git - icinga2/blob - lib/base/timer.hpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / timer.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef TIMER_H
4 #define TIMER_H
5
6 #include "base/i2-base.hpp"
7 #include "base/object.hpp"
8 #include <boost/signals2.hpp>
9
10 namespace icinga {
11
12 class TimerHolder;
13
14 /**
15  * A timer that periodically triggers an event.
16  *
17  * @ingroup base
18  */
19 class Timer final : public Object
20 {
21 public:
22         DECLARE_PTR_TYPEDEFS(Timer);
23
24         ~Timer() override;
25
26         static void Initialize();
27         static void Uninitialize();
28         static void InitializeThread();
29         static void UninitializeThread();
30
31         void SetInterval(double interval);
32         double GetInterval() const;
33
34         static void AdjustTimers(double adjustment);
35
36         void Start();
37         void Stop(bool wait = false);
38
39         void Reschedule(double next = -1);
40         double GetNext() const;
41
42         boost::signals2::signal<void(const Timer * const&)> OnTimerExpired;
43
44 private:
45         double m_Interval{0}; /**< The interval of the timer. */
46         double m_Next{0}; /**< When the next event should happen. */
47         bool m_Started{false}; /**< Whether the timer is enabled. */
48         bool m_Running{false}; /**< Whether the timer proc is currently running. */
49
50         void Call();
51         void InternalReschedule(bool completed, double next = -1);
52
53         static void TimerThreadProc();
54
55         friend class TimerHolder;
56 };
57
58 }
59
60 #endif /* TIMER_H */