Repository navigation
Expand file tree
/
Copy pathtimer.ipp
More file actions
312 lines (255 loc) · 6.56 KB
/
Copy pathtimer.ipp
File metadata and controls
312 lines (255 loc) · 6.56 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
This source file is part of KBEngine
For the latest info, see http://www.kbengine.org/
Copyright (c) 2008-2012 KBEngine.
KBEngine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KBEngine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
*/
namespace KBEngine {
template<class TIME_STAMP>
TimersT<TIME_STAMP>::TimersT():
timeQueue_(),
pProcessingNode_( NULL ),
lastProcessTime_( 0 ),
numCancelled_( 0 )
{
}
template<class TIME_STAMP>
TimersT<TIME_STAMP>::~TimersT()
{
this->clear();
}
template <class TIME_STAMP>
TimerHandle TimersT< TIME_STAMP >::add( TimeStamp startTime,
TimeStamp interval, TimerHandler * pHandler, void * pUser )
{
Time * pTime = new Time( *this, startTime, interval, pHandler, pUser );
timeQueue_.push( pTime );
return TimerHandle( pTime );
}
template <class TIME_STAMP>
void TimersT< TIME_STAMP >::onCancel()
{
++numCancelled_;
// If there are too many cancelled timers in the queue (more than half),
// these are flushed from the queue immediately.
if (numCancelled_ * 2 > int(timeQueue_.size()))
{
this->purgeCancelledTimes();
}
}
template <class TIME_STAMP>
void TimersT< TIME_STAMP >::clear(bool shouldCallCancel)
{
int maxLoopCount = timeQueue_.size();
while (!timeQueue_.empty())
{
Time * pTime = timeQueue_.unsafePopBack();
if (!pTime->isCancelled() && shouldCallCancel)
{
--numCancelled_;
pTime->cancel();
if (--maxLoopCount == 0)
{
shouldCallCancel = false;
}
}
else if (pTime->isCancelled())
{
--numCancelled_;
}
delete pTime;
}
numCancelled_ = 0;
timeQueue_ = PriorityQueue();
}
template <class TIME>
class IsNotCancelled
{
public:
bool operator()( const TIME * pTime )
{
return !pTime->isCancelled();
}
};
template <class TIME_STAMP>
void TimersT< TIME_STAMP >::purgeCancelledTimes()
{
typename PriorityQueue::Container & container = timeQueue_.container();
typename PriorityQueue::Container::iterator newEnd =
std::partition( container.begin(), container.end(),
IsNotCancelled< Time >() );
for (typename PriorityQueue::Container::iterator iter = newEnd;
iter != container.end();
++iter)
{
delete *iter;
}
const int numPurged = (container.end() - newEnd);
numCancelled_ -= numPurged;
KBE_ASSERT( (numCancelled_ == 0) || (numCancelled_ == 1) );
container.erase( newEnd, container.end() );
timeQueue_.heapify();
}
template <class TIME_STAMP>
int TimersT< TIME_STAMP >::process(TimeStamp now)
{
int numFired = 0;
while ((!timeQueue_.empty()) && (
timeQueue_.top()->time() <= now ||
timeQueue_.top()->isCancelled()))
{
Time * pTime = pProcessingNode_ = timeQueue_.top();
timeQueue_.pop();
if (!pTime->isCancelled())
{
++numFired;
pTime->triggerTimer();
}
if (!pTime->isCancelled())
{
timeQueue_.push( pTime );
}
else
{
delete pTime;
KBE_ASSERT( numCancelled_ > 0 );
--numCancelled_;
}
}
pProcessingNode_ = NULL;
lastProcessTime_ = now;
return numFired;
}
template <class TIME_STAMP>
bool TimersT< TIME_STAMP >::legal(TimerHandle handle) const
{
typedef Time * const * TimeIter;
Time * pTime = static_cast< Time* >( handle.time() );
if (pTime == NULL)
{
return false;
}
if (pTime == pProcessingNode_)
{
return true;
}
TimeIter begin = &timeQueue_.top();
TimeIter end = begin + timeQueue_.size();
for (TimeIter it = begin; it != end; it++)
{
if (*it == pTime)
{
return true;
}
}
return false;
}
template <class TIME_STAMP>
TIME_STAMP TimersT< TIME_STAMP >::nextExp(TimeStamp now) const
{
if (timeQueue_.empty() ||
now > timeQueue_.top()->time())
{
return 0;
}
return timeQueue_.top()->time() - now;
}
template <class TIME_STAMP>
bool TimersT< TIME_STAMP >::getTimerInfo( TimerHandle handle,
TimeStamp & time,
TimeStamp & interval,
void * & pUser ) const
{
Time * pTime = static_cast< Time * >( handle.time() );
if (!pTime->isCancelled())
{
time = pTime->time();
interval = pTime->interval();
pUser = pTime->getUserData();
return true;
}
return false;
}
template <class TIME_STAMP>
TIME_STAMP
TimersT< TIME_STAMP >::timerDeliveryTime(TimerHandle handle) const
{
Time * pTime = static_cast< Time * >( handle.time() );
return pTime->deliveryTime();
}
template <class TIME_STAMP>
TIME_STAMP
TimersT< TIME_STAMP >::timerIntervalTime(TimerHandle handle) const
{
Time * pTime = static_cast< Time * >( handle.time() );
return pTime->interval();
}
template <class TIME_STAMP>
TIME_STAMP & TimersT< TIME_STAMP >::timerIntervalTime( TimerHandle handle )
{
Time * pTime = static_cast< Time * >( handle.time() );
return pTime->intervalRef();
}
inline TimeBase::TimeBase(TimersBase & owner, TimerHandler * pHandler, void * pUserData) :
owner_(owner),
pHandler_(pHandler),
pUserData_(pUserData),
state_(TIME_PENDING)
{
pHandler->incTimerRegisterCount();
}
inline void TimeBase::cancel()
{
if (this->isCancelled()){
return;
}
KBE_ASSERT((state_ == TIME_PENDING) || (state_ == TIME_EXECUTING));
state_ = TIME_CANCELLED;
if (pHandler_){
pHandler_->release(TimerHandle(this), pUserData_);
pHandler_ = NULL;
}
owner_.onCancel();
}
template <class TIME_STAMP>
TimersT< TIME_STAMP >::Time::Time( TimersBase & owner,
TimeStamp startTime, TimeStamp interval,
TimerHandler * _pHandler, void * _pUser ) :
TimeBase(owner, _pHandler, _pUser),
time_(startTime),
interval_(interval)
{
}
template <class TIME_STAMP>
TIME_STAMP TimersT< TIME_STAMP >::Time::deliveryTime() const
{
return this->isExecuting() ? (time_ + interval_) : time_;
}
template <class TIME_STAMP>
void TimersT< TIME_STAMP >::Time::triggerTimer()
{
if (!this->isCancelled())
{
state_ = TIME_EXECUTING;
pHandler_->handleTimeout( TimerHandle( this ), pUserData_ );
if ((interval_ == 0) && !this->isCancelled())
{
this->cancel();
}
}
if (!this->isCancelled())
{
time_ += interval_;
state_ = TIME_PENDING;
}
}
}