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
```plantuml
@startuml
(*) --> "relTime = 0" as rt0
rt0 --> if goForward then
-left--> [false] "insert"
else
-right--> [true] "do Stuff"
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
```

View File

@ -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();
}
}
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++;
isEnd = (it == this->timeouts_.end());
if(!isEnd){
relInterval = (*it)->getInterval();
rTime += relInterval;
}
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();
}