start an non-fonctionnal algoritm

This commit is contained in:
Rémi Heredero 2023-10-19 11:46:19 +02:00
parent 92c2faa858
commit 9059d77b3e

View File

@ -34,9 +34,69 @@ XFTimeoutManager::~XFTimeoutManager() {
void XFTimeoutManager::addTimeout(XFTimeout *pNewTimeout) {
const int desireInterval = pNewTimeout->getInterval();
int intervalOfTimeout = 0;
int ticks;
this->pMutex_->lock();
this->timeouts_.push_back(pNewTimeout);
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++;
}
}
}
// 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);
this->pMutex_->unlock();
return;
}
@ -70,22 +130,24 @@ void XFTimeoutManager::unscheduleTimeout(int32_t timeoutId, interface::XFBehavio
}
void XFTimeoutManager::tick() {
//Trace::out("[DEBUG] - Tick");
if(!this->timeouts_.empty()) {
TimeoutList::iterator it;
this->pMutex_->lock();
for(it=this->timeouts_.begin(); it != this->timeouts_.end(); it++) {
if((*it)->getRelTicks() > 0) {
(*it)->substractFromRelTicks(this->tickInterval_);
} else {
XFEvent* ev = *(it);
XFDispatcher::getInstance()->pushEvent(ev);
it = this->timeouts_.erase(it);
it--;
}
XFTimeout* timeout = this->timeouts_.front();
timeout->substractFromRelTicks(tickInterval_);
while (timeout->getRelTicks() <= 0 ) {
XFDispatcher::getInstance()->pushEvent(timeout);
this->timeouts_.pop_front();
timeout = this->timeouts_.front();
}
this->pMutex_->unlock();
}
}