DelayQueue Class Reference

#include <DelayQueue.hh>

Inheritance diagram for DelayQueue:

Inheritance graph
[legend]
Collaboration diagram for DelayQueue:

Collaboration graph
[legend]

Public Member Functions

 DelayQueue ()
virtual ~DelayQueue ()
void addEntry (DelayQueueEntry *newEntry)
void updateEntry (DelayQueueEntry *entry, DelayInterval newDelay)
void updateEntry (long tokenToFind, DelayInterval newDelay)
void removeEntry (DelayQueueEntry *entry)
DelayQueueEntryremoveEntry (long tokenToFind)
DelayInterval const & timeToNextAlarm ()
void handleAlarm ()
long token ()

Protected Member Functions

virtual void handleTimeout ()

Private Member Functions

DelayQueueEntryhead ()
DelayQueueEntryfindEntryByToken (long token)
void synchronize ()

Private Attributes

EventTime fLastSyncTime

Detailed Description

Definition at line 163 of file DelayQueue.hh.


Constructor & Destructor Documentation

DelayQueue::DelayQueue (  ) 

Definition at line 109 of file DelayQueue.cpp.

References fLastSyncTime, and TimeNow().

00110   : DelayQueueEntry(ETERNITY) {
00111   fLastSyncTime = TimeNow();
00112 }

DelayQueue::~DelayQueue (  )  [virtual]

Definition at line 114 of file DelayQueue.cpp.

References DelayQueueEntry::fNext, and removeEntry().

00114                         {
00115   while (fNext != this) removeEntry(fNext);
00116 }


Member Function Documentation

void DelayQueue::addEntry ( DelayQueueEntry newEntry  ) 

Definition at line 118 of file DelayQueue.cpp.

References DelayQueueEntry::fDeltaTimeRemaining, DelayQueueEntry::fNext, DelayQueueEntry::fPrev, head(), and synchronize().

Referenced by BasicTaskScheduler0::scheduleDelayedTask(), and updateEntry().

00118                                                    {
00119   synchronize();
00120 
00121   DelayQueueEntry* cur = head();
00122   while (newEntry->fDeltaTimeRemaining >= cur->fDeltaTimeRemaining) {
00123     newEntry->fDeltaTimeRemaining -= cur->fDeltaTimeRemaining;
00124     cur = cur->fNext;
00125   }
00126 
00127   cur->fDeltaTimeRemaining -= newEntry->fDeltaTimeRemaining;
00128 
00129   // Add "newEntry" to the queue, just before "cur":
00130   newEntry->fNext = cur;
00131   newEntry->fPrev = cur->fPrev;
00132   cur->fPrev = newEntry->fPrev->fNext = newEntry;
00133 }

void DelayQueue::updateEntry ( DelayQueueEntry entry,
DelayInterval  newDelay 
)

Definition at line 135 of file DelayQueue.cpp.

References addEntry(), DelayQueueEntry::fDeltaTimeRemaining, NULL, and removeEntry().

Referenced by updateEntry().

00135                                                                            {
00136   if (entry == NULL) return;
00137 
00138   removeEntry(entry);
00139   entry->fDeltaTimeRemaining = newDelay;
00140   addEntry(entry);
00141 }

void DelayQueue::updateEntry ( long  tokenToFind,
DelayInterval  newDelay 
)

Definition at line 143 of file DelayQueue.cpp.

References findEntryByToken(), and updateEntry().

00143                                                                      {
00144   DelayQueueEntry* entry = findEntryByToken(tokenToFind);
00145   updateEntry(entry, newDelay);
00146 }

void DelayQueue::removeEntry ( DelayQueueEntry entry  ) 

Definition at line 148 of file DelayQueue.cpp.

References DelayQueueEntry::fDeltaTimeRemaining, DelayQueueEntry::fNext, DelayQueueEntry::fPrev, and NULL.

Referenced by handleAlarm(), removeEntry(), BasicTaskScheduler0::unscheduleDelayedTask(), updateEntry(), and ~DelayQueue().

00148                                                    {
00149   if (entry == NULL || entry->fNext == NULL) return;
00150 
00151   entry->fNext->fDeltaTimeRemaining += entry->fDeltaTimeRemaining;
00152   entry->fPrev->fNext = entry->fNext;
00153   entry->fNext->fPrev = entry->fPrev;
00154   entry->fNext = entry->fPrev = NULL;
00155   // in case we should try to remove it again
00156 }

DelayQueueEntry * DelayQueue::removeEntry ( long  tokenToFind  ) 

Definition at line 158 of file DelayQueue.cpp.

References findEntryByToken(), and removeEntry().

00158                                                          {
00159   DelayQueueEntry* entry = findEntryByToken(tokenToFind);
00160   removeEntry(entry);
00161   return entry;
00162 }

DelayInterval const & DelayQueue::timeToNextAlarm (  ) 

Definition at line 164 of file DelayQueue.cpp.

References DELAY_ZERO, DelayQueueEntry::fDeltaTimeRemaining, head(), and synchronize().

Referenced by BasicTaskScheduler::SingleStep().

00164                                                  {
00165   if (head()->fDeltaTimeRemaining == DELAY_ZERO) return DELAY_ZERO; // a common case
00166 
00167   synchronize();
00168   return head()->fDeltaTimeRemaining;
00169 }

void DelayQueue::handleAlarm (  ) 

Definition at line 171 of file DelayQueue.cpp.

References DELAY_ZERO, DelayQueueEntry::fDeltaTimeRemaining, DelayQueueEntry::handleTimeout(), head(), removeEntry(), and synchronize().

Referenced by BasicTaskScheduler::SingleStep().

00171                              {
00172   if (head()->fDeltaTimeRemaining != DELAY_ZERO) synchronize();
00173 
00174   if (head()->fDeltaTimeRemaining == DELAY_ZERO) {
00175     // This event is due to be handled:
00176     DelayQueueEntry* toRemove = head();
00177     removeEntry(toRemove); // do this first, in case handler accesses queue
00178 
00179     toRemove->handleTimeout();
00180   }
00181 }

DelayQueueEntry* DelayQueue::head (  )  [inline, private]

Definition at line 178 of file DelayQueue.hh.

References DelayQueueEntry::fNext.

Referenced by addEntry(), findEntryByToken(), handleAlarm(), synchronize(), and timeToNextAlarm().

00178 { return fNext; }

DelayQueueEntry * DelayQueue::findEntryByToken ( long  token  )  [private]

Definition at line 183 of file DelayQueue.cpp.

References DelayQueueEntry::fNext, head(), NULL, and DelayQueueEntry::token().

Referenced by removeEntry(), and updateEntry().

00183                                                               {
00184   DelayQueueEntry* cur = head();
00185   while (cur != this) {
00186     if (cur->token() == tokenToFind) return cur;
00187     cur = cur->fNext;
00188   }
00189 
00190   return NULL;
00191 }

void DelayQueue::synchronize (  )  [private]

Definition at line 193 of file DelayQueue.cpp.

References DELAY_ZERO, DelayQueueEntry::fDeltaTimeRemaining, fLastSyncTime, DelayQueueEntry::fNext, head(), and TimeNow().

Referenced by addEntry(), handleAlarm(), and timeToNextAlarm().

00193                              {
00194   // First, figure out how much time has elapsed since the last sync:
00195   EventTime timeNow = TimeNow();
00196   DelayInterval timeSinceLastSync = timeNow - fLastSyncTime;
00197   fLastSyncTime = timeNow;
00198 
00199   // Then, adjust the delay queue for any entries whose time is up:
00200   DelayQueueEntry* curEntry = head();
00201   while (timeSinceLastSync >= curEntry->fDeltaTimeRemaining) {
00202     timeSinceLastSync -= curEntry->fDeltaTimeRemaining;
00203     curEntry->fDeltaTimeRemaining = DELAY_ZERO;
00204     curEntry = curEntry->fNext;
00205   }
00206   curEntry->fDeltaTimeRemaining -= timeSinceLastSync;
00207 }

long DelayQueueEntry::token (  )  [inline, inherited]

Definition at line 142 of file DelayQueue.hh.

References DelayQueueEntry::fToken.

Referenced by findEntryByToken(), and BasicTaskScheduler0::scheduleDelayedTask().

00142                {
00143     return fToken;
00144   }

void DelayQueueEntry::handleTimeout (  )  [protected, virtual, inherited]

Reimplemented in AlarmHandler.

Definition at line 102 of file DelayQueue.cpp.

Referenced by handleAlarm(), and AlarmHandler::handleTimeout().

00102                                     {
00103   delete this;
00104 }


Field Documentation

EventTime DelayQueue::fLastSyncTime [private]

Definition at line 182 of file DelayQueue.hh.

Referenced by DelayQueue(), and synchronize().


The documentation for this class was generated from the following files:
Generated on Tue Oct 7 15:39:37 2008 for live by  doxygen 1.5.2