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