forked from willamowius/gnugk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gktimer.cxx
159 lines (137 loc) · 3.81 KB
/
gktimer.cxx
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* gktimer.cxx
*
* Generic support for time-based events.
*
* Copyright (c) 2004, Michal Zygmuntowicz
* Copyright (c) 2006-2017, Jan Willamowius
*
* This work is published under the GNU Public License version 2 (GPLv2)
* see file COPYING for details.
* We also explicitly grant the right to link this code
* with the OpenH323/H323Plus and OpenSSL library.
*
*/
#include <ptlib.h>
#include "stl_supp.h"
#include "gktimer.h"
using std::find;
/// A timer that calls a simple void function on its expiration
class GkVoidFuncTimer : public GkTimer
{
public:
GkVoidFuncTimer(
const PTime & expirationTime,
void (*timerFunc)()
) : GkTimer(expirationTime), m_timerFunc(timerFunc) { }
GkVoidFuncTimer(
const PTime & expirationTime,
long interval,
void (*timerFunc)()
) : GkTimer(expirationTime, interval), m_timerFunc(timerFunc) { }
protected:
virtual void OnTimerExpired() { if (m_timerFunc) (*m_timerFunc)(); }
private:
GkVoidFuncTimer();
private:
void (*m_timerFunc)(); /// a simple timer function
};
/// A timer that calls a simple one arg function on its expiration
class GkOneArgFuncTimer : public GkTimer
{
public:
GkOneArgFuncTimer(
const PTime & expirationTime,
void (*timerFunc)(GkTimer*)
) : GkTimer(expirationTime), m_timerFunc(timerFunc) { }
GkOneArgFuncTimer(
const PTime & expirationTime,
long interval,
void (*timerFunc)(GkTimer*)
) : GkTimer(expirationTime, interval), m_timerFunc(timerFunc) { }
protected:
virtual void OnTimerExpired() { if (m_timerFunc) (*m_timerFunc)(this); }
private:
GkOneArgFuncTimer();
private:
void (*m_timerFunc)(GkTimer*); /// a simple timer function
};
const GkTimerManager::GkTimerHandle GkTimerManager::INVALID_HANDLE = NULL;
GkTimerManager::GkTimerManager()
{
}
GkTimerManager::GkTimerHandle GkTimerManager::RegisterTimer(
void (*timerFunc)(), /// timer function
const PTime & tm /// timer expiration time
)
{
GkTimer* const t = new GkVoidFuncTimer(tm, timerFunc);
PWaitAndSignal lock(m_timersMutex);
m_timers.push_back(t);
return t;
}
GkTimerManager::GkTimerHandle GkTimerManager::RegisterTimer(
void (*timerFunc)(), /// timer function
const PTime & tm, /// the first expiration time
long interval /// timer interval (seconds)
)
{
GkTimer * const t = new GkVoidFuncTimer(tm, interval, timerFunc);
PWaitAndSignal lock(m_timersMutex);
m_timers.push_back(t);
return t;
}
GkTimerManager::GkTimerHandle GkTimerManager::RegisterTimer(
void (*timerFunc)(GkTimer*), /// timer function
const PTime & tm /// timer expiration time
)
{
GkTimer * const t = new GkOneArgFuncTimer(tm, timerFunc);
PWaitAndSignal lock(m_timersMutex);
m_timers.push_back(t);
return t;
}
GkTimerManager::GkTimerHandle GkTimerManager::RegisterTimer(
void (*timerFunc)(GkTimer*), /// timer function
const PTime & tm, /// the first timer expiration time
long interval /// timer interval (seconds)
)
{
GkTimer * const t = new GkOneArgFuncTimer(tm, interval, timerFunc);
PWaitAndSignal lock(m_timersMutex);
m_timers.push_back(t);
return t;
}
bool GkTimerManager::UnregisterTimer(GkTimerManager::GkTimerHandle timer)
{
PWaitAndSignal lock(m_timersMutex);
std::list<GkTimerHandle>::iterator i = find(m_timers.begin(), m_timers.end(), timer);
if (i != m_timers.end()) {
m_timers.erase(i);
delete timer;
return true;
} else
return false;
}
void GkTimerManager::CheckTimers()
{
PWaitAndSignal lock(m_timersMutex);
std::list<GkTimerHandle>::iterator i = m_timers.begin();
while (i != m_timers.end()) {
GkTimer * timer = *i++;
if (timer->IsExpired()) {
timer->SetFired(true);
timer->OnTimerExpired();
if (timer->IsPeriodic())
timer->SetExpirationTime(timer->GetExpirationTime()
+ PTimeInterval(timer->GetInterval() * 1000)
);
}
}
}
GkTimerManager::~GkTimerManager()
{
PWaitAndSignal lock(m_timersMutex);
DeleteObjectsInContainer(m_timers);
m_timers.clear();
}