some update for test algoritm

This commit is contained in:
Rémi Heredero 2023-10-19 14:48:10 +02:00
parent 9059d77b3e
commit 268c270a4d
2 changed files with 53 additions and 61 deletions

View File

@ -9,14 +9,31 @@
# Time Algorithm # Time Algorithm
```plantuml ```plantuml
@startuml
(*) --> "relTime = 0" as rt0 @startuml
rt0 --> if goForward then
-left--> [false] "insert" start
else :rTime = 0
-right--> [true] "do Stuff" 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 @enduml
``` ```

View File

@ -34,67 +34,35 @@ XFTimeoutManager::~XFTimeoutManager() {
void XFTimeoutManager::addTimeout(XFTimeout *pNewTimeout) { void XFTimeoutManager::addTimeout(XFTimeout *pNewTimeout) {
const int desireInterval = pNewTimeout->getInterval(); const int dTime = pNewTimeout->getInterval();
int intervalOfTimeout = 0; int rTime = 0;
int ticks; int relInterval = 0;
bool isEnd = true;
int interval = dTime;
this->pMutex_->lock(); 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(); TimeoutList::iterator it = this->timeouts_.begin();
while(intervalOfTimeout < desireInterval) { isEnd = (it == this->timeouts_.end());
if(++it == this->timeouts_.end()){
ticks = (desireInterval-intervalOfTimeout); if(!isEnd){
if(ticks<=0) ticks = 0; relInterval = (*it)->getInterval();
pNewTimeout->setRelTicks(ticks); rTime += relInterval;
this->timeouts_.push_back(pNewTimeout);
this->pMutex_->unlock();
return;
} else {
intervalOfTimeout += (*it)->getRelTicks();
}
} }
if(intervalOfTimeout == desireInterval) { while(!isEnd && ( relInterval <= 0 || (rTime > dTime) )) {
if(++it == this->timeouts_.end()) { isEnd = (++it == this->timeouts_.end());
ticks = (desireInterval-intervalOfTimeout); interval = rTime - dTime;
if(ticks<=0) ticks = 0; if(!isEnd) {
pNewTimeout->setRelTicks(ticks); relInterval = (*it)->getRelTicks();
this->timeouts_.push_back(pNewTimeout); rTime += relInterval;
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++;
}
} }
} }
if(it == this->timeouts_.begin())
// TODO change for take care of 10ms ecart of timeout if(!isEnd) (*it)->substractFromRelTicks(interval);
ticks = (desireInterval-intervalOfTimeout); it--;
ticks = ticks > 0 ? ticks/this->tickInterval_ : 0; pNewTimeout->setRelTicks(interval);
this->timeouts_.insert(it, pNewTimeout);
(*it)->substractFromRelTicks(ticks);
pNewTimeout->setRelTicks(ticks);
this->timeouts_.insert(--it, pNewTimeout);
this->pMutex_->unlock(); this->pMutex_->unlock();
return; return;
@ -131,18 +99,25 @@ void XFTimeoutManager::unscheduleTimeout(int32_t timeoutId, interface::XFBehavio
void XFTimeoutManager::tick() { void XFTimeoutManager::tick() {
if(!this->timeouts_.empty()) { bool isEmpty = this->timeouts_.empty();
int rTime;
if(!isEmpty) {
this->pMutex_->lock(); this->pMutex_->lock();
XFTimeout* timeout = this->timeouts_.front(); XFTimeout* timeout = this->timeouts_.front();
timeout->substractFromRelTicks(tickInterval_); timeout->substractFromRelTicks(tickInterval_);
rTime = timeout->getRelTicks();
while (timeout->getRelTicks() <= 0 ) { while (!isEmpty && (rTime <= 0) ) {
rTime = timeout->getRelTicks();
XFDispatcher::getInstance()->pushEvent(timeout); XFDispatcher::getInstance()->pushEvent(timeout);
this->timeouts_.pop_front(); this->timeouts_.pop_front();
timeout = this->timeouts_.front(); timeout = this->timeouts_.front();
isEmpty = this->timeouts_.empty();
} }