| 1 |
/** |
| 2 |
* @file protoTimer.cpp |
| 3 |
* |
| 4 |
* @brief A generic timer class which will notify a ProtoTimer::Listener upon timeout. |
| 5 |
*/ |
| 6 |
|
| 7 |
|
| 8 |
#include "protoTimer.h" |
| 9 |
#include "protoDebug.h" |
| 10 |
|
| 11 |
#include <stdio.h> // for getchar() debug |
| 12 |
|
| 13 |
/** |
| 14 |
* @brief Default constructor |
| 15 |
* |
| 16 |
* @param listener(NULL) |
| 17 |
* @param interval(1.0) |
| 18 |
* @param repeat(0) |
| 19 |
* @param repeat_count(0) |
| 20 |
* @param mgr(NULL) |
| 21 |
* @param prev(NULL) |
| 22 |
* @param next(NULL) |
| 23 |
*/ |
| 24 |
ProtoTimer::ProtoTimer() |
| 25 |
: listener(NULL), interval(1.0), repeat(0), repeat_count(0), mgr(NULL) |
| 26 |
#ifndef _SORTED_TIMERS |
| 27 |
,prev(NULL), next(NULL) |
| 28 |
#endif // !_SORTED_TIMERS |
| 29 |
{ |
| 30 |
|
| 31 |
} |
| 32 |
/** |
| 33 |
* Default Destructor |
| 34 |
* |
| 35 |
* Deactivates timer and deletes any associated listener. |
| 36 |
*/ |
| 37 |
|
| 38 |
ProtoTimer::~ProtoTimer() |
| 39 |
{ |
| 40 |
if (IsActive()) Deactivate(); |
| 41 |
if (listener) |
| 42 |
{ |
| 43 |
delete listener; |
| 44 |
listener = NULL; |
| 45 |
} |
| 46 |
} |
| 47 |
/** |
| 48 |
* Reschedules the timer in the timer queue according to the (modified?) timer |
| 49 |
* interval. (also resets repeat count) |
| 50 |
* |
| 51 |
*/ |
| 52 |
bool ProtoTimer::Reschedule() |
| 53 |
{ |
| 54 |
ASSERT(IsActive()); |
| 55 |
if (IsActive()) |
| 56 |
{ |
| 57 |
ProtoTimerMgr* timerMgr = mgr; |
| 58 |
bool updatePending = timerMgr->update_pending; |
| 59 |
timerMgr->update_pending = true; |
| 60 |
timerMgr->DeactivateTimer(*this); |
| 61 |
timerMgr->update_pending = updatePending; |
| 62 |
timerMgr->ActivateTimer(*this); |
| 63 |
return true; |
| 64 |
} |
| 65 |
else |
| 66 |
{ |
| 67 |
PLOG(PL_ERROR, "ProtoTimer::Reschedule() error: timer not active\n"); |
| 68 |
return false; |
| 69 |
} |
| 70 |
} // end ProtoTimer::Reschedule() |
| 71 |
|
| 72 |
/** |
| 73 |
* This method stretches (factor > 1.0) or |
| 74 |
* compresses the timer interval, rescheduling |
| 75 |
* the timer if it is active |
| 76 |
* (note the repeat count is not impacted) |
| 77 |
*/ |
| 78 |
void ProtoTimer::Scale(double factor) |
| 79 |
{ |
| 80 |
if (IsActive()) |
| 81 |
{ |
| 82 |
// Calculate, reschedule and then adjust interval |
| 83 |
double newInterval = factor*interval; |
| 84 |
double timeRemaining = GetTimeRemaining(); |
| 85 |
if (timeRemaining > 0.0) |
| 86 |
{ |
| 87 |
interval = factor*timeRemaining; |
| 88 |
int repeatCountSaved = repeat_count; |
| 89 |
Reschedule(); |
| 90 |
repeat_count = repeatCountSaved; |
| 91 |
} |
| 92 |
interval = newInterval; |
| 93 |
} |
| 94 |
else |
| 95 |
{ |
| 96 |
interval *= factor; |
| 97 |
} |
| 98 |
} // end ProtoTimer::Scale() |
| 99 |
|
| 100 |
void ProtoTimer::Deactivate() |
| 101 |
{ |
| 102 |
ASSERT(IsActive()); |
| 103 |
mgr->DeactivateTimer(*this); |
| 104 |
} |
| 105 |
/** |
| 106 |
* Get the time remaining for the timer according to current "proto time". |
| 107 |
*/ |
| 108 |
double ProtoTimer::GetTimeRemaining() const |
| 109 |
{ |
| 110 |
if (NULL != mgr) |
| 111 |
{ |
| 112 |
ProtoTime currentTime; |
| 113 |
mgr->GetCurrentProtoTime(currentTime); |
| 114 |
double timeRemaining = ProtoTime::Delta(timeout, currentTime); |
| 115 |
if (timeRemaining < 0.0) timeRemaining = 0.0; |
| 116 |
return timeRemaining; |
| 117 |
} |
| 118 |
else |
| 119 |
{ |
| 120 |
return -1.0; |
| 121 |
} |
| 122 |
} // end ProtoTimer::GetTimeRemaining() |
| 123 |
|
| 124 |
/** |
| 125 |
* This class manages ProtoTimer instances when they are |
| 126 |
* "activated". The ProtoDispatcher derives from this to manage |
| 127 |
* ProtoTimers for an application. (The ProtoSimAgent base class |
| 128 |
* contains a ProtoTimerMgr to similarly manage timers for a simulation |
| 129 |
* instance). |
| 130 |
*/ |
| 131 |
ProtoTimerMgr::ProtoTimerMgr() |
| 132 |
: update_pending(false), timeout_scheduled(false), |
| 133 |
#ifdef _SORTED_TIMERS |
| 134 |
timer_list_count(0), |
| 135 |
#else |
| 136 |
long_head(NULL), long_tail(NULL), short_head(NULL), short_tail(NULL), |
| 137 |
#endif // if/else SORTTED_TIMERS |
| 138 |
invoked_timer(NULL) |
| 139 |
{ |
| 140 |
pulse_timer.SetListener(this, &ProtoTimerMgr::OnPulseTimeout); |
| 141 |
pulse_timer.SetInterval(1.0); |
| 142 |
pulse_timer.SetRepeat(-1); |
| 143 |
} |
| 144 |
|
| 145 |
ProtoTimerMgr::~ProtoTimerMgr() |
| 146 |
{ |
| 147 |
// (TBD) Uninstall or halt, deactivate all timers ... |
| 148 |
} |
| 149 |
/** |
| 150 |
* Calls inlined ProtoSystemTime function |
| 151 |
*/ |
| 152 |
void ProtoTimerMgr::GetSystemTime(struct timeval& currentTime) |
| 153 |
{ |
| 154 |
::ProtoSystemTime(currentTime); |
| 155 |
} // end ProtoTimerMgr::GetSystemTime() |
| 156 |
|
| 157 |
const double ProtoTimerMgr::PRECISION_TIME_THRESHOLD = 8.0; |
| 158 |
|
| 159 |
/** |
| 160 |
* |
| 161 |
*/ |
| 162 |
void ProtoTimerMgr::OnSystemTimeout() |
| 163 |
{ |
| 164 |
timeout_scheduled = false; |
| 165 |
bool updateStatus = update_pending; |
| 166 |
update_pending = true; |
| 167 |
ProtoTimer* next = GetShortHead(); |
| 168 |
ProtoTime now; |
| 169 |
GetCurrentProtoTime(now); |
| 170 |
while (next) |
| 171 |
{ |
| 172 |
double delta = ProtoTime::Delta(next->timeout, now); |
| 173 |
//TRACE(" delta = %lf\n", delta); |
| 174 |
// We limit to within a microsecond of accuracy on |
| 175 |
// real-world systems to avoid overzealous attempts |
| 176 |
// at scheduling |
| 177 |
if (delta < 1.0e-06) |
| 178 |
{ |
| 179 |
invoked_timer = next; |
| 180 |
next->DoTimeout(); |
| 181 |
if(invoked_timer== next) |
| 182 |
{ |
| 183 |
if (next->IsActive()) |
| 184 |
{ |
| 185 |
RemoveShortTimer(*next); |
| 186 |
int repeatCount = next->repeat_count; |
| 187 |
if (0 != repeatCount) |
| 188 |
{ |
| 189 |
ReactivateTimer(*next, now); |
| 190 |
if (repeatCount > 0) repeatCount--; |
| 191 |
next->repeat_count = repeatCount; |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
// else timer got deleted or otherwise rescheduled, etc |
| 196 |
invoked_timer = NULL; |
| 197 |
next = GetShortHead(); |
| 198 |
} |
| 199 |
else |
| 200 |
{ |
| 201 |
next = NULL; |
| 202 |
} |
| 203 |
} |
| 204 |
update_pending = updateStatus; |
| 205 |
if (!updateStatus) Update(); |
| 206 |
} // ProtoTimerMgr::OnSystemTimeout() |
| 207 |
|
| 208 |
bool ProtoTimerMgr::OnPulseTimeout(ProtoTimer& /*theTimer*/) |
| 209 |
{ |
| 210 |
ProtoTimer* next = GetLongHead(); |
| 211 |
pulse_mark += 1.0; |
| 212 |
while (NULL != next) |
| 213 |
{ |
| 214 |
double delta = ProtoTime::Delta(next->timeout, pulse_mark); |
| 215 |
if (delta < PRECISION_TIME_THRESHOLD) |
| 216 |
{ |
| 217 |
RemoveLongTimer(*next); |
| 218 |
GetCurrentProtoTime(next->timeout); |
| 219 |
if (delta >= 0.0) |
| 220 |
next->timeout += delta; |
| 221 |
else if (delta < -0.100) |
| 222 |
PLOG(PL_DEBUG, "ProtoTimerMgr: Warning! real time failure interval:%lf (delta:%lf)\n", |
| 223 |
next->GetInterval(), delta); |
| 224 |
InsertShortTimer(*next); |
| 225 |
next = GetLongHead(); |
| 226 |
} |
| 227 |
else |
| 228 |
{ |
| 229 |
break; |
| 230 |
} |
| 231 |
} |
| 232 |
if (NULL == GetLongHead()) |
| 233 |
{ |
| 234 |
DeactivateTimer(pulse_timer); |
| 235 |
return false; |
| 236 |
} |
| 237 |
else |
| 238 |
{ |
| 239 |
return true; |
| 240 |
} |
| 241 |
} // end ProtoTimerMgr::OnPulseTimeout() |
| 242 |
|
| 243 |
void ProtoTimerMgr::ActivateTimer(ProtoTimer& theTimer) |
| 244 |
{ |
| 245 |
ASSERT(!theTimer.IsActive()); |
| 246 |
double timerInterval = theTimer.GetInterval(); |
| 247 |
if (PRECISION_TIME_THRESHOLD > timerInterval) |
| 248 |
{ |
| 249 |
GetCurrentProtoTime(theTimer.timeout); |
| 250 |
theTimer.timeout += timerInterval; |
| 251 |
InsertShortTimer(theTimer); |
| 252 |
} |
| 253 |
else |
| 254 |
{ |
| 255 |
if (!pulse_timer.IsActive()) |
| 256 |
{ |
| 257 |
GetCurrentProtoTime(pulse_mark); |
| 258 |
bool updateStatus = update_pending; |
| 259 |
update_pending = true; |
| 260 |
ActivateTimer(pulse_timer); |
| 261 |
update_pending = updateStatus; |
| 262 |
} |
| 263 |
theTimer.timeout = pulse_mark; |
| 264 |
double delta = timerInterval + 1.0 - pulse_timer.GetTimeRemaining(); |
| 265 |
ASSERT(delta >= 0.0); |
| 266 |
theTimer.timeout += delta; |
| 267 |
InsertLongTimer(theTimer); |
| 268 |
} |
| 269 |
theTimer.repeat_count = theTimer.repeat; |
| 270 |
if (!update_pending) Update(); |
| 271 |
} // end ProtoTimerMgr::ActivateTimer() |
| 272 |
|
| 273 |
void ProtoTimerMgr::ReactivateTimer(ProtoTimer& theTimer, const ProtoTime& now) |
| 274 |
{ |
| 275 |
double timerInterval = theTimer.GetInterval(); |
| 276 |
if (PRECISION_TIME_THRESHOLD > timerInterval) |
| 277 |
{ |
| 278 |
//TRACE("incrementing timer timeout %lu:%lu by %lf\n", theTimer.timeout.sec(), theTimer.timeout.usec(), timerInterval); |
| 279 |
theTimer.timeout += timerInterval; |
| 280 |
//TRACE(" new timeout %lu:%lu\n", theTimer.timeout.sec(), theTimer.timeout.usec()); |
| 281 |
double delta = ProtoTime::Delta(theTimer.timeout, now); |
| 282 |
if (delta < -0.100 ) |
| 283 |
{ |
| 284 |
GetCurrentProtoTime(theTimer.timeout); |
| 285 |
PLOG(PL_DEBUG, "ProtoTimerMgr: Warning! real time failure interval:%lf (delta:%lf)\n", |
| 286 |
timerInterval, delta); |
| 287 |
} |
| 288 |
InsertShortTimer(theTimer); |
| 289 |
} |
| 290 |
else |
| 291 |
{ |
| 292 |
if (!pulse_timer.IsActive()) |
| 293 |
{ |
| 294 |
GetCurrentProtoTime(pulse_mark); |
| 295 |
bool updateStatus = update_pending; |
| 296 |
update_pending = true; |
| 297 |
ActivateTimer(pulse_timer); |
| 298 |
update_pending = updateStatus; |
| 299 |
} |
| 300 |
GetPulseTime(theTimer.timeout); |
| 301 |
theTimer.timeout += timerInterval; |
| 302 |
InsertLongTimer(theTimer); |
| 303 |
} |
| 304 |
if (!update_pending) Update(); |
| 305 |
} // end ProtoTimerMgr::ReactivateTimer(() |
| 306 |
|
| 307 |
void ProtoTimerMgr::DeactivateTimer(ProtoTimer& theTimer) |
| 308 |
{ |
| 309 |
if (theTimer.mgr == this) |
| 310 |
{ |
| 311 |
if (theTimer.is_precise) |
| 312 |
{ |
| 313 |
// See if timer being removed is currently being "invoked" |
| 314 |
if (&theTimer == invoked_timer) |
| 315 |
invoked_timer = NULL; |
| 316 |
RemoveShortTimer(theTimer); |
| 317 |
} |
| 318 |
else |
| 319 |
{ |
| 320 |
RemoveLongTimer(theTimer); |
| 321 |
if (NULL == GetLongHead()) |
| 322 |
{ |
| 323 |
bool updateStatus = update_pending; |
| 324 |
update_pending = true; |
| 325 |
DeactivateTimer(pulse_timer); |
| 326 |
update_pending = updateStatus; |
| 327 |
} |
| 328 |
} |
| 329 |
if (!update_pending) Update(); |
| 330 |
} |
| 331 |
} // end ProtoTimerMgr::DeactivateTimer() |
| 332 |
|
| 333 |
void ProtoTimerMgr::Update() |
| 334 |
{ |
| 335 |
if (NULL == GetShortHead()) |
| 336 |
{ |
| 337 |
// REMOVE existing scheduled system timeout if applicable |
| 338 |
if (timeout_scheduled) |
| 339 |
{ |
| 340 |
if (!UpdateSystemTimer(ProtoTimer::REMOVE, -1.0)) |
| 341 |
PLOG(PL_ERROR, "ProtoTimerMgr::Update() error: scheduled system timeout REMOVE failure\n"); |
| 342 |
timeout_scheduled = false; |
| 343 |
} |
| 344 |
} |
| 345 |
else if (timeout_scheduled) |
| 346 |
{ |
| 347 |
// MODIFY existing scheduled system timeout if different |
| 348 |
if (scheduled_timeout != GetShortHead()->timeout) |
| 349 |
{ |
| 350 |
if (UpdateSystemTimer(ProtoTimer::MODIFY, GetShortHead()->GetTimeRemaining())) |
| 351 |
{ |
| 352 |
scheduled_timeout = GetShortHead()->timeout; |
| 353 |
} |
| 354 |
else // (TBD) if MODIFY fails, do we still have a system timeout ??? |
| 355 |
{ |
| 356 |
PLOG(PL_ERROR, "ProtoTimerMgr::Update() error: scheduled system timeout MODIFY failure\n"); |
| 357 |
timeout_scheduled = false; // (TBD) ??? |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
else |
| 362 |
{ |
| 363 |
// INSTALL new scheduled system timeout |
| 364 |
if (UpdateSystemTimer(ProtoTimer::INSTALL, GetShortHead()->GetTimeRemaining())) |
| 365 |
{ |
| 366 |
scheduled_timeout = GetShortHead()->timeout; |
| 367 |
timeout_scheduled = true; |
| 368 |
} |
| 369 |
else |
| 370 |
{ |
| 371 |
PLOG(PL_ERROR, "ProtoTimerMgr::Update() error: scheduled system timeout INSTALL failure\n"); |
| 372 |
} |
| 373 |
} |
| 374 |
} // end ProtoTimerMgr::Update() |
| 375 |
|
| 376 |
#ifdef _SORTED_TIMERS |
| 377 |
void ProtoTimerMgr::InsertShortTimer(ProtoTimer& theTimer) |
| 378 |
{ |
| 379 |
theTimer.mgr = this; |
| 380 |
theTimer.is_precise = true; |
| 381 |
const int LINKED_LIST_MAX = 16; |
| 382 |
bool addToList; |
| 383 |
if (timer_list_count < LINKED_LIST_MAX) |
| 384 |
{ |
| 385 |
if (timer_table.IsEmpty() || (theTimer.timeout <= timer_table.GetHead()->timeout)) |
| 386 |
{ |
| 387 |
addToList = true; |
| 388 |
timer_list_count++; |
| 389 |
} |
| 390 |
else |
| 391 |
{ |
| 392 |
// there's room in the list but the timer is bigger than table head |
| 393 |
addToList = false; |
| 394 |
} |
| 395 |
} |
| 396 |
else if (LINKED_LIST_MAX > 0) |
| 397 |
{ |
| 398 |
ProtoTimer* listTail = timer_list.GetTail(); |
| 399 |
if (theTimer.timeout < listTail->timeout) |
| 400 |
{ |
| 401 |
timer_list.Remove(*listTail); |
| 402 |
listTail->UpdateKey(); |
| 403 |
timer_table.Insert(*listTail); |
| 404 |
addToList = true; |
| 405 |
} |
| 406 |
else |
| 407 |
{ |
| 408 |
addToList = false; |
| 409 |
} |
| 410 |
} |
| 411 |
else |
| 412 |
{ |
| 413 |
addToList = false; |
| 414 |
} |
| 415 |
if (addToList) |
| 416 |
{ |
| 417 |
ProtoTimerList::Iterator iterator(timer_list); |
| 418 |
ProtoTimer* nextTimer; |
| 419 |
|
| 420 |
// Thus code (as stands), searches for the timer insertion |
| 421 |
// point from the front of the list. The assumption is that |
| 422 |
// applications will generally have a small number of high speed |
| 423 |
// timers being updated frequently and other, longer term, timers |
| 424 |
// being updated less frequently that end up at the end of the list. |
| 425 |
// Thus, searching from the front is more efficient on average |
| 426 |
// (Note that the "timer_list" linked list is only used for a |
| 427 |
// modest number of timers and the "timer_table" ProtoSortedTree |
| 428 |
// is used to maintain low insertion cost for cases of large |
| 429 |
// numbers of timers). |
| 430 |
// |
| 431 |
// NOTE: There is additional code here that, if uncommented, will |
| 432 |
// search the "timer_list" from the front _and_ the back. This |
| 433 |
// approach is not expected to provide significant benefit in general, |
| 434 |
// but the commented code is provided for someone to evaluate as desired. |
| 435 |
// (Also the "LINKER_LIST_MAX" constant could be changed to adjust |
| 436 |
// the balance of "timer_list" entries vs. "timer_table" entries. |
| 437 |
//ProtoTimerList::Iterator riterator(timer_list, true); |
| 438 |
//ProtoTimer* prevTimer = NULL; |
| 439 |
while (true) |
| 440 |
{ |
| 441 |
nextTimer = iterator.GetNextItem(); |
| 442 |
if ((NULL == nextTimer) || (theTimer.timeout <= nextTimer->timeout)) |
| 443 |
break; |
| 444 |
// Note we know riterator.GetPrevItem() will be non-NULL at this point |
| 445 |
//nextTimer = prevTimer; |
| 446 |
//prevTimer = riterator.GetPrevItem(); |
| 447 |
//if (prevTimer->timeout <= theTimer.timeout) |
| 448 |
// break; |
| 449 |
} |
| 450 |
theTimer.InvalidateKey(); |
| 451 |
if (NULL == nextTimer) |
| 452 |
timer_list.Append(theTimer); |
| 453 |
else |
| 454 |
timer_list.Insert(theTimer, *nextTimer); |
| 455 |
} |
| 456 |
else |
| 457 |
{ |
| 458 |
theTimer.UpdateKey(); |
| 459 |
timer_table.Insert(theTimer); |
| 460 |
} |
| 461 |
|
| 462 |
while (timer_list_count < LINKED_LIST_MAX) |
| 463 |
{ |
| 464 |
// Move any timers that can fit from table to list. This is essentially a |
| 465 |
// deferred action that would be done in RemoveShortTimer() but doing it |
| 466 |
// here allows for a timer in the "timer_list" to be quickly removed and |
| 467 |
// reinserted without unnecessarily migrating a timer from and then back |
| 468 |
// to the "timer_table" |
| 469 |
ProtoTimer* next = timer_table.RemoveHead(); |
| 470 |
if (NULL != next) |
| 471 |
{ |
| 472 |
next->InvalidateKey(); |
| 473 |
timer_list.Append(*next); |
| 474 |
timer_list_count++; |
| 475 |
} |
| 476 |
else |
| 477 |
{ |
| 478 |
break; |
| 479 |
} |
| 480 |
} |
| 481 |
} // end ProtoTimerMgr::InsertShortTimer() |
| 482 |
|
| 483 |
void ProtoTimerMgr::RemoveShortTimer(ProtoTimer& theTimer) |
| 484 |
{ |
| 485 |
if (theTimer.KeyIsValid()) |
| 486 |
{ |
| 487 |
timer_table.Remove(theTimer); |
| 488 |
} |
| 489 |
else |
| 490 |
{ |
| 491 |
timer_list.Remove(theTimer); |
| 492 |
timer_list_count--; |
| 493 |
} |
| 494 |
theTimer.mgr = NULL; |
| 495 |
} // end ProtoTimerMgr::RemoveShortTimer() |
| 496 |
|
| 497 |
void ProtoTimerMgr::InsertLongTimer(ProtoTimer& theTimer) |
| 498 |
{ |
| 499 |
theTimer.mgr = this; |
| 500 |
theTimer.is_precise = false; |
| 501 |
theTimer.UpdateKey(); |
| 502 |
long_timer_table.Insert(theTimer); |
| 503 |
} // end ProtoTimerMgr::InsertLongTimer() |
| 504 |
|
| 505 |
void ProtoTimerMgr::RemoveLongTimer(ProtoTimer& theTimer) |
| 506 |
{ |
| 507 |
long_timer_table.Remove(theTimer); |
| 508 |
theTimer.mgr = NULL; |
| 509 |
} // end ProtoTimerMgr::InsertLongTimer() |
| 510 |
|
| 511 |
#else |
| 512 |
|
| 513 |
void ProtoTimerMgr::InsertShortTimer(ProtoTimer& theTimer) |
| 514 |
{ |
| 515 |
unsigned breakCount = 0; |
| 516 |
ProtoTimer* next = short_head; |
| 517 |
theTimer.mgr = this; |
| 518 |
theTimer.is_precise = true; |
| 519 |
while(next) |
| 520 |
{ |
| 521 |
double delta = ProtoTime::Delta(theTimer.timeout, next->timeout); |
| 522 |
if (delta <= 0.0) |
| 523 |
{ |
| 524 |
theTimer.next = next; |
| 525 |
if(NULL != (theTimer.prev = next->prev)) |
| 526 |
theTimer.prev->next = &theTimer; |
| 527 |
else |
| 528 |
short_head = &theTimer; |
| 529 |
next->prev = &theTimer; |
| 530 |
return; |
| 531 |
} |
| 532 |
else |
| 533 |
{ |
| 534 |
next = next->next; |
| 535 |
} |
| 536 |
breakCount++; |
| 537 |
if(breakCount == 10)//go ahead and try to add the entry to the end of the list |
| 538 |
//if(breakCount == 100000)//go ahead and try to add the entry to the end of the list |
| 539 |
{ |
| 540 |
if(InsertShortTimerReverse(theTimer)) |
| 541 |
{ |
| 542 |
//The entry was succesfully added to the end so we are done |
| 543 |
return; |
| 544 |
} |
| 545 |
} |
| 546 |
//ASSERT(breakCount < 5000); |
| 547 |
} |
| 548 |
if (NULL != (theTimer.prev = short_tail)) |
| 549 |
short_tail->next = &theTimer; |
| 550 |
else |
| 551 |
short_head = &theTimer; |
| 552 |
short_tail = &theTimer; |
| 553 |
theTimer.next = NULL; |
| 554 |
} // end ProtoTimerMgr::InsertShortTimer() |
| 555 |
|
| 556 |
bool ProtoTimerMgr::InsertShortTimerReverse(ProtoTimer& theTimer) |
| 557 |
{ |
| 558 |
unsigned breakCount = 0; |
| 559 |
ProtoTimer* prev = short_tail; |
| 560 |
theTimer.mgr = this; |
| 561 |
theTimer.is_precise = true; |
| 562 |
while(prev) |
| 563 |
{ |
| 564 |
double delta = ProtoTime::Delta(theTimer.timeout, prev->timeout); |
| 565 |
if (delta > 0.0) |
| 566 |
{ |
| 567 |
if(NULL == (theTimer.next = prev->next)) |
| 568 |
short_tail = &theTimer; |
| 569 |
else |
| 570 |
theTimer.next->prev = &theTimer; |
| 571 |
theTimer.prev = prev; |
| 572 |
prev->next = &theTimer; |
| 573 |
//DMSG(0,"bunny Reverse breakcount was %d with timeout time %f\n",breakCount,theTimer.timeout.GetValue()); |
| 574 |
return true; |
| 575 |
} |
| 576 |
else |
| 577 |
{ |
| 578 |
prev = prev->prev; |
| 579 |
} |
| 580 |
breakCount++; |
| 581 |
if(breakCount == 10) |
| 582 |
{ |
| 583 |
return false; |
| 584 |
} |
| 585 |
} |
| 586 |
if (NULL == (theTimer.next = short_head)) |
| 587 |
short_tail = &theTimer; |
| 588 |
else |
| 589 |
short_head->prev = &theTimer; |
| 590 |
short_head = &theTimer; |
| 591 |
theTimer.prev = NULL; |
| 592 |
return true; |
| 593 |
} // end ProtoTimerMgr::InsertShortTimerReverse() |
| 594 |
|
| 595 |
void ProtoTimerMgr::RemoveShortTimer(ProtoTimer& theTimer) |
| 596 |
{ |
| 597 |
if (theTimer.prev) |
| 598 |
theTimer.prev->next = theTimer.next; |
| 599 |
else |
| 600 |
short_head = theTimer.next; |
| 601 |
if (theTimer.next) |
| 602 |
theTimer.next->prev = theTimer.prev; |
| 603 |
else |
| 604 |
short_tail = theTimer.prev; |
| 605 |
theTimer.mgr = NULL; |
| 606 |
} // end ProtoTimerMgr::RemoveShortTimer() |
| 607 |
|
| 608 |
void ProtoTimerMgr::InsertLongTimer(ProtoTimer& theTimer) |
| 609 |
{ |
| 610 |
unsigned breakCount = 0; |
| 611 |
ProtoTimer* next = long_head; |
| 612 |
theTimer.mgr = this; |
| 613 |
theTimer.is_precise = false; |
| 614 |
while(next) |
| 615 |
{ |
| 616 |
double delta = ProtoTime::Delta(theTimer.timeout, next->timeout); |
| 617 |
if (delta <= 0.0) |
| 618 |
{ |
| 619 |
theTimer.next = next; |
| 620 |
if((theTimer.prev = next->prev)) |
| 621 |
theTimer.prev->next = &theTimer; |
| 622 |
else |
| 623 |
long_head = &theTimer; |
| 624 |
next->prev = &theTimer; |
| 625 |
return; |
| 626 |
} |
| 627 |
else |
| 628 |
{ |
| 629 |
next = next->next; |
| 630 |
} |
| 631 |
breakCount++; |
| 632 |
if(breakCount == 10)//go ahead and try to add the entry to the end of the list |
| 633 |
{ |
| 634 |
if(InsertLongTimerReverse(theTimer)) |
| 635 |
{ |
| 636 |
//The entry was succesfully added to the end so we are done |
| 637 |
return; |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
if ((theTimer.prev = long_tail)) |
| 642 |
long_tail->next = &theTimer; |
| 643 |
else |
| 644 |
long_head = &theTimer; |
| 645 |
long_tail = &theTimer; |
| 646 |
theTimer.next = NULL; |
| 647 |
} // end ProtoTimerMgr::InsertLongTimer() |
| 648 |
|
| 649 |
bool ProtoTimerMgr::InsertLongTimerReverse(ProtoTimer& theTimer) |
| 650 |
{ |
| 651 |
unsigned breakCount = 0; |
| 652 |
ProtoTimer* prev = long_tail; |
| 653 |
theTimer.mgr = this; |
| 654 |
theTimer.is_precise = false; |
| 655 |
while(prev) |
| 656 |
{ |
| 657 |
double delta = ProtoTime::Delta(theTimer.timeout, prev->timeout); |
| 658 |
if (delta > 0.0) |
| 659 |
{ |
| 660 |
if(NULL == (theTimer.next = prev->next)) |
| 661 |
long_tail = &theTimer; |
| 662 |
else |
| 663 |
theTimer.next->prev = &theTimer; |
| 664 |
theTimer.prev = prev; |
| 665 |
prev->next = &theTimer; |
| 666 |
return true; |
| 667 |
} |
| 668 |
else |
| 669 |
{ |
| 670 |
prev = prev->prev; |
| 671 |
} |
| 672 |
breakCount++; |
| 673 |
if(breakCount == 10) |
| 674 |
{ |
| 675 |
return false; |
| 676 |
} |
| 677 |
} |
| 678 |
if (NULL == (theTimer.next = long_head)) |
| 679 |
long_tail = &theTimer; |
| 680 |
else |
| 681 |
long_head->prev = &theTimer; |
| 682 |
long_head = &theTimer; |
| 683 |
theTimer.prev = NULL; |
| 684 |
return true; |
| 685 |
} // end ProtoTimerMgr::InsertLongTimerReverse() |
| 686 |
|
| 687 |
void ProtoTimerMgr::RemoveLongTimer(ProtoTimer& theTimer) |
| 688 |
{ |
| 689 |
if (theTimer.prev) |
| 690 |
theTimer.prev->next = theTimer.next; |
| 691 |
else |
| 692 |
long_head = theTimer.next; |
| 693 |
if (theTimer.next) |
| 694 |
theTimer.next->prev = theTimer.prev; |
| 695 |
else |
| 696 |
long_tail = theTimer.prev; |
| 697 |
theTimer.mgr = NULL; |
| 698 |
} // end ProtoTimerMgr::RemoveLongTimer() |
| 699 |
|
| 700 |
#endif // if/else _SORTED_TIMERS |