From 584a9318213baf9c5b900d2bdb56aa46f3af191b Mon Sep 17 00:00:00 2001 From: Jeff Hill Date: Wed, 12 Apr 2000 18:03:23 +0000 Subject: [PATCH] removed most of the case statements --- src/libCom/timer/osiTimer.cpp | 121 +++++++++++++--------------------- src/libCom/timer/osiTimer.h | 10 ++- 2 files changed, 48 insertions(+), 83 deletions(-) diff --git a/src/libCom/timer/osiTimer.cpp b/src/libCom/timer/osiTimer.cpp index b24c2cdb0..739483448 100644 --- a/src/libCom/timer/osiTimer.cpp +++ b/src/libCom/timer/osiTimer.cpp @@ -91,7 +91,7 @@ osiTimer::osiTimer (osiTimerQueue & queueIn) : curState (osiTimer::stateIdle), queue (queueIn) { this->queue.mutex.lock (); - this->queue.idle.add (*this); + this->queue.timerLists[stateIdle].add (*this); this->queue.mutex.unlock (); } @@ -104,7 +104,7 @@ osiTimer::osiTimer () : curState (osiTimer::stateIdle), queue (osiDefaultTimerQueue) { this->queue.mutex.lock (); - this->queue.idle.add (*this); + this->queue.timerLists[stateIdle].add (*this); this->queue.mutex.unlock (); } @@ -126,20 +126,7 @@ osiTimer::~osiTimer() if (this == this->queue.pExpireTmr) { this->queue.pExpireTmr = 0; } - switch (this->curState) { - case statePending: - this->queue.pending.remove (*this); - break; - case stateExpired: - this->queue.expired.remove (*this); - break; - case stateIdle: - this->queue.idle.remove (*this); - break; - default: - errlogPrintf ("observed osiTimer is in undefined state?\n"); - break; - } + this->queue.timerLists[this->curState].remove(*this); this->curState = stateLimbo; this->queue.mutex.unlock (); } @@ -163,21 +150,10 @@ void osiTimer::cancel () if (this == this->queue.pExpireTmr) { this->queue.pExpireTmr = 0; } - switch (this->curState) { - case statePending: - this->queue.pending.remove (*this); - this->queue.idle.add (*this); - break; - case stateExpired: - this->queue.expired.remove (*this); - this->queue.idle.add (*this); - break; - case stateIdle: - break; - default: - errlogPrintf ("observed osiTimer is in undefined state?\n"); - break; - } + + this->queue.timerLists[this->curState].remove (*this); + this->curState = stateIdle; + this->queue.timerLists[stateIdle].add (*this); this->queue.mutex.unlock (); @@ -217,32 +193,19 @@ void osiTimer::reschedule (double newDelay) if (this == this->queue.pExpireTmr) { this->queue.pExpireTmr = 0; } - switch (this->curState) { - case statePending: - this->queue.pending.remove (*this); - break; - case stateExpired: - this->queue.expired.remove (*this); - break; - case stateIdle: - this->queue.idle.remove (*this); - break; - default: - errlogPrintf ("observed osiTimer is in undefined state?\n"); - break; - } + this->queue.timerLists[this->curState].remove (*this); this->curState = stateLimbo; this->arm (newDelay); this->queue.mutex.unlock (); } // -// osiTimer::arm() -// NOTE: The osiTimer lock is properly applied externally to this routine -// when it is needed. +// osiTimer::arm () // void osiTimer::arm (double initialDelay) { + bool first; + # ifdef DEBUG unsigned preemptCount=0u; # endif @@ -274,20 +237,22 @@ void osiTimer::arm (double initialDelay) // // **** this should use a binary tree ???? // - tsDLIterBD iter = this->queue.pending.last (); + tsDLIterBD iter = this->queue.timerLists[statePending].last (); while (1) { if ( iter == tsDLIterBD::eol () ) { // // add to the beginning of the list // - this->queue.pending.push (*this); + this->queue.timerLists[statePending].push (*this); + first = true; break; } if ( iter->exp <= this->exp ) { // // add after the item found that expires earlier // - this->queue.pending.insertAfter (*this, *iter); + this->queue.timerLists[statePending].insertAfter (*this, *iter); + first = false; break; } # ifdef DEBUG @@ -314,7 +279,9 @@ void osiTimer::arm (double initialDelay) this->queue.mutex.unlock (); - this->queue.rescheduleEvent.signal (); + if (first) { + this->queue.rescheduleEvent.signal (); + } } // @@ -379,30 +346,29 @@ double osiTimer::timeRemaining () const { double delay; - if ( this->curState == stateLimbo ) { - errlogPrintf ("time remaning fetched on a osiTimer with no queue?\n"); - return DBL_MAX; // queue was destroyed - } this->queue.mutex.lock (); switch (this->curState) { case statePending: - { - double remaining = this->exp - osiTime::getCurrent(); - if ( remaining > 0.0 ) { - delay = remaining; - } - else { - delay = 0.0; - } - break; - } + delay = this->exp - osiTime::getCurrent(); + if ( delay < 0.0 ) { + delay = 0.0; + } + break; + case stateIdle: delay = DBL_MAX; break; + case stateExpired: delay = 0.0; break; + + case stateLimbo: + errlogPrintf ("time remaning fetched on a osiTimer with no queue?\n"); + delay = DBL_MAX; // queue was destroyed + break; + default: errlogPrintf ("saw osiTimer in undefined state\n"); delay = DBL_MAX; @@ -454,7 +420,7 @@ osiTimerQueue::~osiTimerQueue() // // destroy any unexpired timers // - while ( ( pTmr = this->pending.get () ) ) { + while ( ( pTmr = this->timerLists[osiTimer::statePending].get () ) ) { pTmr->curState = osiTimer::stateLimbo; pTmr->destroy (); } @@ -462,7 +428,7 @@ osiTimerQueue::~osiTimerQueue() // // destroy any expired timers // - while ( (pTmr = this->expired.get()) ) { + while ( (pTmr = this->timerLists[osiTimer::stateExpired].get()) ) { pTmr->curState = osiTimer::stateLimbo; pTmr->destroy (); } @@ -501,7 +467,7 @@ double osiTimerQueue::delayToFirstExpire () const this->mutex.lock (); - pTmr = this->pending.first (); + pTmr = this->timerLists[osiTimer::statePending].first (); if (pTmr) { delay = pTmr->timeRemaining (); } @@ -538,16 +504,16 @@ void osiTimerQueue::process () this->inProcess = true; - tsDLIterBD iter = this->pending.first (); + tsDLIterBD iter = this->timerLists[osiTimer::statePending].first (); while ( iter != tsDLIterBD::eol () ) { if (iter->exp >= cur) { break; } tsDLIterBD tmp = iter; ++tmp; - this->pending.remove(*iter); + this->timerLists[osiTimer::statePending].remove(*iter); iter->curState = osiTimer::stateExpired; - this->expired.add(*iter); + this->timerLists[osiTimer::stateExpired].add(*iter); iter = tmp; } @@ -555,10 +521,10 @@ void osiTimerQueue::process () // I am careful to prevent problems if they access the // above list while in an "expire()" call back // - while ( ( pTmr = this->expired.get () ) ) { + while ( ( pTmr = this->timerLists[osiTimer::stateExpired].get () ) ) { pTmr->curState = osiTimer::stateIdle; - this->idle.add (*pTmr); + this->timerLists[osiTimer::stateIdle].add (*pTmr); // // Tag current tmr so that we @@ -584,7 +550,7 @@ void osiTimerQueue::process () if ( this->pExpireTmr == pTmr ) { if ( pTmr->again () ) { - this->idle.remove (*pTmr); + this->timerLists[osiTimer::stateIdle].remove (*pTmr); pTmr->arm (pTmr->delay()); } else { @@ -610,8 +576,9 @@ void osiTimerQueue::show(unsigned level) const { this->mutex.lock(); printf("osiTimerQueue with %u items pending and %u items expired\n", - this->pending.count(), this->expired.count()); - tsDLIterBD iter(this->pending.first()); + this->timerLists[osiTimer::statePending].count(), + this->timerLists[osiTimer::stateExpired].count()); + tsDLIterBD iter(this->timerLists[osiTimer::statePending].first()); while ( iter!=tsDLIterBD::eol() ) { iter->show(level); ++iter; diff --git a/src/libCom/timer/osiTimer.h b/src/libCom/timer/osiTimer.h index 8093e338b..afda7382f 100644 --- a/src/libCom/timer/osiTimer.h +++ b/src/libCom/timer/osiTimer.h @@ -50,7 +50,6 @@ epicsShareExtern osiTimerQueue osiDefaultTimerQueue; * osiTimer */ class osiTimer : public tsDLNode { -friend class osiTimerQueue; public: class noDelaySpecified {}; /* exception */ class noMemory {}; /* exception */ @@ -140,8 +139,9 @@ public: epicsShareFunc virtual ~osiTimer (); private: - - enum state {statePending, stateExpired, stateIdle, stateLimbo}; + friend class osiTimerQueue; + enum state {statePending, stateExpired, stateIdle, + numberOfTimerLists, stateLimbo}; osiTime exp; /* experation time */ state curState; /* current state */ @@ -169,9 +169,7 @@ private: osiMutex mutex; osiEvent rescheduleEvent; osiEvent exitEvent; - tsDLList idle; - tsDLList pending; - tsDLList expired; + tsDLList timerLists [osiTimer::numberOfTimerLists]; osiTimer *pExpireTmr; class osiTimerThread *pMgrThread; unsigned mgrThreadPriority;