some update for test algoritm
This commit is contained in:
		@@ -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();
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user