]> granicus.if.org Git - icinga2/blob - lib/base/timer.hpp
Fix another crash in Timer::Call
[icinga2] / lib / base / timer.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #ifndef TIMER_H
21 #define TIMER_H
22
23 #include "base/i2-base.hpp"
24 #include "base/object.hpp"
25 #include <boost/signals2.hpp>
26
27 namespace icinga {
28
29 /**
30  * A timer that periodically triggers an event.
31  *
32  * @ingroup base
33  */
34 class I2_BASE_API Timer : public Object
35 {
36 public:
37         DECLARE_PTR_TYPEDEFS(Timer);
38
39         Timer(void);
40         ~Timer(void);
41
42         void SetInterval(double interval);
43         double GetInterval(void) const;
44
45         static void AdjustTimers(double adjustment);
46
47         void Start(void);
48         void Stop(bool wait = false);
49
50         void Reschedule(double next = -1);
51         double GetNext(void) const;
52
53         boost::signals2::signal<void(const Timer::Ptr&)> OnTimerExpired;
54
55         class Holder {
56         public:
57                 Holder(Timer *timer)
58                         : m_Timer(timer)
59                 { }
60
61                 inline Timer *GetObject(void) const
62                 {
63                         return m_Timer;
64                 }
65
66                 inline double GetNextUnlocked(void) const
67                 {
68                         return m_Timer->m_Next;
69                 }
70
71                 operator Timer *(void) const
72                 {
73                         return m_Timer;
74                 }
75
76         private:
77                 Timer *m_Timer;
78         };
79
80 private:
81         double m_Interval; /**< The interval of the timer. */
82         double m_Next; /**< When the next event should happen. */
83         bool m_Started; /**< Whether the timer is enabled. */
84         bool m_Running; /**< Whether the timer proc is currently running. */
85
86         void Call();
87         void InternalReschedule(bool completed, double next = -1);
88
89         static void TimerThreadProc(void);
90
91         static void Initialize(void);
92         static void Uninitialize(void);
93
94         friend class Application;
95 };
96
97 }
98
99 #endif /* TIMER_H */