diff --git a/readme.md b/readme.md index d7e374a..6bf7691 100644 --- a/readme.md +++ b/readme.md @@ -9,14 +9,31 @@ # Time Algorithm ```plantuml -@startuml -(*) --> "relTime = 0" as rt0 -rt0 --> if goForward then --left--> [false] "insert" -else --right--> [true] "do Stuff" + +@startuml + +start +:rTime = 0 +relInterval = 0 +isEnd = it == list.end(); +if (!isEnd) then (not end) + :relInterval = it.getRelTicks() + rTime += relInterval; + + while ( !isEnd && ( relInterval <= 0 || (rTime < dTime) ) is (goForward) + :isEnd = (++it == list.end()); + if (!isEnd) then (not end) + :relInterval = it.getRelTicks() + rTime += relInterval; + endif + endwhile + +endif +: insert ; + @enduml + ``` diff --git a/src/simplified/xf/port/common/timeoutmanager.cpp b/src/simplified/xf/port/common/timeoutmanager.cpp index 0356cc7..f5ec382 100644 --- a/src/simplified/xf/port/common/timeoutmanager.cpp +++ b/src/simplified/xf/port/common/timeoutmanager.cpp @@ -34,67 +34,35 @@ XFTimeoutManager::~XFTimeoutManager() { void XFTimeoutManager::addTimeout(XFTimeout *pNewTimeout) { - const int desireInterval = pNewTimeout->getInterval(); - int intervalOfTimeout = 0; - int ticks; + const int dTime = pNewTimeout->getInterval(); + int rTime = 0; + int relInterval = 0; + bool isEnd = true; + int interval = dTime; this->pMutex_->lock(); - - if(this->timeouts_.empty()) { - ticks = (desireInterval-intervalOfTimeout); - if(ticks<=0) ticks = 0; - pNewTimeout->setRelTicks(ticks); - this->timeouts_.push_back(pNewTimeout); - this->pMutex_->unlock(); - return; - } - TimeoutList::iterator it = this->timeouts_.begin(); - while(intervalOfTimeout < desireInterval) { - if(++it == this->timeouts_.end()){ - ticks = (desireInterval-intervalOfTimeout); - if(ticks<=0) ticks = 0; - pNewTimeout->setRelTicks(ticks); - this->timeouts_.push_back(pNewTimeout); - this->pMutex_->unlock(); - return; - } else { - intervalOfTimeout += (*it)->getRelTicks(); - } + isEnd = (it == this->timeouts_.end()); + + if(!isEnd){ + relInterval = (*it)->getInterval(); + rTime += relInterval; } - if(intervalOfTimeout == desireInterval) { - if(++it == this->timeouts_.end()) { - ticks = (desireInterval-intervalOfTimeout); - if(ticks<=0) ticks = 0; - pNewTimeout->setRelTicks(ticks); - this->timeouts_.push_back(pNewTimeout); - this->pMutex_->unlock(); - return; - } else { - - while((*it)->getRelTicks() == 0) { - if(++it == this->timeouts_.end()) { - ticks = (desireInterval-intervalOfTimeout); - if(ticks<=0) ticks = 0; - pNewTimeout->setRelTicks(ticks); - this->timeouts_.push_back(pNewTimeout); - this->pMutex_->unlock(); - return; - } - it++; - } - + while(!isEnd && ( relInterval <= 0 || (rTime > dTime) )) { + isEnd = (++it == this->timeouts_.end()); + interval = rTime - dTime; + if(!isEnd) { + relInterval = (*it)->getRelTicks(); + rTime += relInterval; } } + if(it == this->timeouts_.begin()) - // TODO change for take care of 10ms ecart of timeout - ticks = (desireInterval-intervalOfTimeout); - ticks = ticks > 0 ? ticks/this->tickInterval_ : 0; - - (*it)->substractFromRelTicks(ticks); - pNewTimeout->setRelTicks(ticks); - this->timeouts_.insert(--it, pNewTimeout); + if(!isEnd) (*it)->substractFromRelTicks(interval); + it--; + pNewTimeout->setRelTicks(interval); + this->timeouts_.insert(it, pNewTimeout); this->pMutex_->unlock(); return; @@ -131,18 +99,25 @@ void XFTimeoutManager::unscheduleTimeout(int32_t timeoutId, interface::XFBehavio void XFTimeoutManager::tick() { - if(!this->timeouts_.empty()) { + bool isEmpty = this->timeouts_.empty(); + int rTime; + + if(!isEmpty) { this->pMutex_->lock(); XFTimeout* timeout = this->timeouts_.front(); timeout->substractFromRelTicks(tickInterval_); + rTime = timeout->getRelTicks(); - while (timeout->getRelTicks() <= 0 ) { + while (!isEmpty && (rTime <= 0) ) { + + rTime = timeout->getRelTicks(); XFDispatcher::getInstance()->pushEvent(timeout); this->timeouts_.pop_front(); timeout = this->timeouts_.front(); + isEmpty = this->timeouts_.empty(); }