some update for test algoritm
This commit is contained in:
		
							
								
								
									
										27
									
								
								readme.md
									
									
									
									
									
								
							
							
						
						
									
										27
									
								
								readme.md
									
									
									
									
									
								
							| @@ -9,14 +9,31 @@ | |||||||
| # Time Algorithm | # Time Algorithm | ||||||
|  |  | ||||||
| ```plantuml | ```plantuml | ||||||
|  |  | ||||||
| @startuml | @startuml | ||||||
| (*) --> "relTime = 0" as rt0 |  | ||||||
| rt0 --> if goForward then | start | ||||||
| -left--> [false] "insert" | :rTime = 0 | ||||||
| else | relInterval = 0 | ||||||
| -right--> [true] "do Stuff" | 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 | ||||||
|  |  | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
| @@ -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) { |  | ||||||
|         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 |     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(); | ||||||
|  |  | ||||||
|         } |         } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user