-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.c
287 lines (242 loc) · 7.45 KB
/
timer.c
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* timer.c: Threaded timer class
*
* See the README file for copyright information and how to reach the author.
*
*/
#include <sys/time.h>
#include <vdr/config.h>
#include <vdr/tools.h>
#include <vdr/thread.h>
#include "timer.h"
//#define XINELIBOUTPUT_DEBUG
//#define XINELIBOUTPUT_DEBUG_STDOUT
#ifdef XINELIBOUTPUT_DEBUG
# include "logdefs.h"
#else
# define TRACE(x)
# define TRACEF(x)
#endif
#define time_ms() cTimeMs::Now()
// ---------------------------- cTimerThreadEvent ----------------------------
class cTimerThreadEvent : public cListObject {
public:
cTimerThreadEvent(cTimerCallback *Handler, unsigned int TimeoutMs,
bool DeleteOnCancel = false) :
m_Handler(Handler),
m_DeleteOnCancel(DeleteOnCancel),
m_TimeoutMs(TimeoutMs)
{
m_NextEventTime = time_ms();
UpdateEventTime();
}
~cTimerThreadEvent()
{
if(m_DeleteOnCancel && m_Handler)
delete m_Handler;
}
void UpdateEventTime()
{
m_NextEventTime += m_TimeoutMs;
}
int TimeToNextEvent(void)
{
return m_NextEventTime - time_ms();
}
virtual bool operator< (const cListObject &ListObject)
{
const cTimerThreadEvent *o = (cTimerThreadEvent *)&ListObject;
return m_NextEventTime<o->m_NextEventTime;
}
virtual int Compare(const cListObject &ListObject) const
{
const cTimerThreadEvent *o = (cTimerThreadEvent *)&ListObject;
if(m_NextEventTime<o->m_NextEventTime)
return -1;
else if(m_NextEventTime>o->m_NextEventTime)
return 1;
return 0;
}
cTimerCallback *m_Handler;
protected:
bool m_DeleteOnCancel;
unsigned int m_TimeoutMs;
int64_t m_NextEventTime;
};
// ------------------------------- cTimerThread ------------------------------
class cTimerThread : public cThread {
private:
cTimerThread(cTimerThread&); // copy not allowed
static cMutex m_InstanceLock;
static cTimerThread *m_Instance; // singleton
cMutex m_Lock;
cCondVar m_Signal;
cList<cTimerThreadEvent> m_Events;
cTimerThreadEvent *m_RunningEvent;
bool m_Finished;
bool m_HandlerRunning;
cTimerThread() :
m_RunningEvent(NULL),
m_Finished(false),
m_HandlerRunning(false)
{
}
virtual ~cTimerThread()
{
m_Lock.Lock();
cTimerThreadEvent *ev;
while(NULL != (ev = m_Events.First())) {
m_Events.Del(ev,true);
}
m_Lock.Unlock();
m_Signal.Broadcast();
Cancel(1);
}
protected:
virtual void Action()
{
TRACEF("cTimerThread::Action");
m_Lock.Lock();
while(m_Events.First()) {
m_Signal.TimedWait(m_Lock,
max(1, m_Events.First()->TimeToNextEvent()));
TRACE("cTimerThread::Action waked up");
while(NULL != (m_RunningEvent = m_Events.First()) &&
m_RunningEvent->TimeToNextEvent() <= 0) {
TRACE("cTimerThread::Action calling handler");
m_HandlerRunning=true;
// m_Lock.Unlock();
// - can't unlock or running timer handler may be deleted while
// executing (or thread may be killed by Delete)
bool result = m_RunningEvent->m_Handler->TimerEvent();
// m_Lock.Lock();
m_HandlerRunning=false;
if(!result) {
if(m_RunningEvent) { // check if event was cancelled in handler...
TRACE("cTimerThread::Action handler cancelled timer");
m_Events.Del(m_RunningEvent, true);
}
} else {
if(m_RunningEvent) {
TRACE("cTimerThread::Action timer re-scheduled");
m_RunningEvent->UpdateEventTime();
m_Events.Sort();
}
}
m_RunningEvent = NULL;
}
}
m_Finished = true;
m_Lock.Unlock();
}
void Add(cTimerThreadEvent *Event)
{
TRACEF("cTimerThread::Add");
//m_Events.Del(Event, false);
Event->Unlink();
Del(Event->m_Handler);
m_Events.Add(Event);
m_Events.Sort();
}
bool Del(cTimerCallback *Handler, void *TargetId=NULL,
bool inDestructor=false)
{
TRACEF("cTimerThread::Del");
cTimerThreadEvent *ev = m_Events.First();
while(ev) {
if(ev->m_Handler == Handler ||
(TargetId && ev->m_Handler->TargetId() == TargetId) ||
(Handler && ev->m_Handler->is(Handler,Handler->size()))) {
cTimerThreadEvent *nev = m_Events.Next(ev);
if(inDestructor) ev->m_Handler=NULL;
m_Events.Del(ev, true);
ev = nev;
} else
ev = m_Events.Next(ev);
}
if(m_RunningEvent &&
(m_RunningEvent->m_Handler == Handler ||
m_RunningEvent->m_Handler->TargetId() == TargetId))
m_RunningEvent = NULL;
return !m_HandlerRunning && !m_RunningEvent && !m_Events.First();
}
public:
static void AddEvent(cTimerCallback *Handler, unsigned int TimeoutMs,
bool DeleteOnCancel=false)
{
TRACEF("cTimerThread::AddEvent");
m_InstanceLock.Lock();
if(m_Instance && m_Instance->m_Finished) {
delete m_Instance;
m_Instance = NULL;
}
if(!m_Instance) {
m_Instance = new cTimerThread;
m_Instance->m_Lock.Lock();
m_Instance->Start();
} else {
m_Instance->m_Lock.Lock();
m_Instance->m_Signal.Broadcast();
}
m_Instance->Add(new cTimerThreadEvent(Handler, max(1U,TimeoutMs),
DeleteOnCancel));
m_Instance->m_Lock.Unlock();
m_InstanceLock.Unlock();
}
static void CancelEvent(cTimerCallback *Handler, void *TargetId = NULL,
bool inDestructor=false)
{
TRACEF("cTimerThread::CancelEvent");
m_InstanceLock.Lock();
if(m_Instance && !m_Instance->m_Finished) {
m_Instance->m_Lock.Lock();
if(m_Instance->Del(Handler, TargetId, inDestructor) && !inDestructor) {
m_Instance->m_Lock.Unlock();
delete m_Instance;
m_Instance = NULL;
} else
m_Instance->m_Lock.Unlock();
}
m_InstanceLock.Unlock();
}
};
cMutex cTimerThread::m_InstanceLock;
cTimerThread *cTimerThread::m_Instance = NULL;
// ------------------------------ cTimerCallback -----------------------------
cTimerCallback::~cTimerCallback()
{
TRACEF("cTimerCallback::~cTimerCallback");
cTimerThread::CancelEvent(this, NULL, true);
}
void cTimerCallback::Set(cTimerCallback *handler, unsigned int TimeoutMs)
{
TRACEF("cTimerCallback::Set");
cTimerThread::AddEvent(handler, TimeoutMs);
}
void cTimerCallback::Cancel(cTimerCallback *handler)
{
TRACEF("cTimerCallback::Cancel");
cTimerThread::CancelEvent(handler);
}
// ------------------------------- cTimerEvent -------------------------------
//cTimerEvent::cTimerEvent(unsigned int TimeoutMs)
//{
// TRACEF("cTimerEvent::cTimerEvent");
//// cTimerThread::AddEvent(this, TimeoutMs, true);
//}
void cTimerEvent::AddEvent(unsigned int TimeoutMs)
{
TRACEF("cTimerEvent::AddEvent");
cTimerThread::AddEvent(this, TimeoutMs, true);
}
void cTimerEvent::Cancel(cTimerEvent *&event)
{
TRACEF("cTimerEvent::Cancel");
cTimerThread::CancelEvent(event);
event = NULL;
}
void cTimerEvent::CancelAll(void *Target)
{
TRACEF("cTimerEvent::CancelAll");
cTimerThread::CancelEvent(NULL, Target);
}