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 (intptr_t tokenToFind, DelayInterval newDelay)
void removeEntry (DelayQueueEntry *entry)
DelayQueueEntryremoveEntry (intptr_t tokenToFind)
DelayInterval const & timeToNextAlarm ()
void handleAlarm ()
intptr_t token ()

Protected Member Functions

virtual void handleTimeout ()

Private Member Functions

DelayQueueEntryhead ()
DelayQueueEntryfindEntryByToken (intptr_t token)
void synchronize ()

Private Attributes

EventTime fLastSyncTime

Detailed Description

Definition at line 160 of file DelayQueue.hh.


Constructor & Destructor Documentation

DelayQueue::DelayQueue (  ) 

Definition at line 110 of file DelayQueue.cpp.

References fLastSyncTime, and TimeNow().

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

DelayQueue::~DelayQueue (  )  [virtual]

Definition at line 115 of file DelayQueue.cpp.

References DelayQueueEntry::fNext, and removeEntry().

00115                         {
00116   while (fNext != this) {
00117     DelayQueueEntry* entryToRemove = fNext;
00118     removeEntry(entryToRemove);
00119     delete entryToRemove;
00120   }
00121 }


Member Function Documentation

void DelayQueue::addEntry ( DelayQueueEntry newEntry  ) 

Definition at line 123 of file DelayQueue.cpp.

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

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

00123                                                    {
00124   synchronize();
00125 
00126   DelayQueueEntry* cur = head();
00127   while (newEntry->fDeltaTimeRemaining >= cur->fDeltaTimeRemaining) {
00128     newEntry->fDeltaTimeRemaining -= cur->fDeltaTimeRemaining;
00129     cur = cur->fNext;
00130   }
00131 
00132   cur->fDeltaTimeRemaining -= newEntry->fDeltaTimeRemaining;
00133 
00134   // Add "newEntry" to the queue, just before "cur":
00135   newEntry->fNext = cur;
00136   newEntry->fPrev = cur->fPrev;
00137   cur->fPrev = newEntry->fPrev->fNext = newEntry;
00138 }

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

Definition at line 140 of file DelayQueue.cpp.

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

Referenced by updateEntry().

00140                                                                            {
00141   if (entry == NULL) return;
00142 
00143   removeEntry(entry);
00144   entry->fDeltaTimeRemaining = newDelay;
00145   addEntry(entry);
00146 }

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

Definition at line 148 of file DelayQueue.cpp.

References findEntryByToken(), and updateEntry().

00148                                                                          {
00149   DelayQueueEntry* entry = findEntryByToken(tokenToFind);
00150   updateEntry(entry, newDelay);
00151 }

void DelayQueue::removeEntry ( DelayQueueEntry entry  ) 

Definition at line 153 of file DelayQueue.cpp.

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

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

00153                                                    {
00154   if (entry == NULL || entry->fNext == NULL) return;
00155 
00156   entry->fNext->fDeltaTimeRemaining += entry->fDeltaTimeRemaining;
00157   entry->fPrev->fNext = entry->fNext;
00158   entry->fNext->fPrev = entry->fPrev;
00159   entry->fNext = entry->fPrev = NULL;
00160   // in case we should try to remove it again
00161 }

DelayQueueEntry * DelayQueue::removeEntry ( intptr_t  tokenToFind  ) 

Definition at line 163 of file DelayQueue.cpp.

References findEntryByToken(), and removeEntry().

00163                                                              {
00164   DelayQueueEntry* entry = findEntryByToken(tokenToFind);
00165   removeEntry(entry);
00166   return entry;
00167 }

DelayInterval const & DelayQueue::timeToNextAlarm (  ) 

Definition at line 169 of file DelayQueue.cpp.

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

Referenced by BasicTaskScheduler::SingleStep().

00169                                                  {
00170   if (head()->fDeltaTimeRemaining == DELAY_ZERO) return DELAY_ZERO; // a common case
00171 
00172   synchronize();
00173   return head()->fDeltaTimeRemaining;
00174 }

void DelayQueue::handleAlarm (  ) 

Definition at line 176 of file DelayQueue.cpp.

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

Referenced by BasicTaskScheduler::SingleStep().

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

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

Definition at line 175 of file DelayQueue.hh.

References DelayQueueEntry::fNext.

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

00175 { return fNext; }

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

Definition at line 188 of file DelayQueue.cpp.

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

Referenced by removeEntry(), and updateEntry().

00188                                                                   {
00189   DelayQueueEntry* cur = head();
00190   while (cur != this) {
00191     if (cur->token() == tokenToFind) return cur;
00192     cur = cur->fNext;
00193   }
00194 
00195   return NULL;
00196 }

void DelayQueue::synchronize (  )  [private]

Definition at line 198 of file DelayQueue.cpp.

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

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

00198                              {
00199   // First, figure out how much time has elapsed since the last sync:
00200   EventTime timeNow = TimeNow();
00201   if (timeNow < fLastSyncTime) {
00202     // The system clock has apparently gone back in time; reset our sync time and return:
00203     fLastSyncTime  = timeNow;
00204     return;
00205   }
00206   DelayInterval timeSinceLastSync = timeNow - fLastSyncTime;
00207   fLastSyncTime = timeNow;
00208 
00209   // Then, adjust the delay queue for any entries whose time is up:
00210   DelayQueueEntry* curEntry = head();
00211   while (timeSinceLastSync >= curEntry->fDeltaTimeRemaining) {
00212     timeSinceLastSync -= curEntry->fDeltaTimeRemaining;
00213     curEntry->fDeltaTimeRemaining = DELAY_ZERO;
00214     curEntry = curEntry->fNext;
00215   }
00216   curEntry->fDeltaTimeRemaining -= timeSinceLastSync;
00217 }

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

Definition at line 139 of file DelayQueue.hh.

References DelayQueueEntry::fToken.

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

00139                    {
00140     return fToken;
00141   }

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

Reimplemented in AlarmHandler.

Definition at line 103 of file DelayQueue.cpp.

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

00103                                     {
00104   delete this;
00105 }


Field Documentation

EventTime DelayQueue::fLastSyncTime [private]

Definition at line 179 of file DelayQueue.hh.

Referenced by DelayQueue(), and synchronize().


The documentation for this class was generated from the following files:
Generated on Thu Feb 2 23:54:46 2012 for live by  doxygen 1.5.2