Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

TimerExecThread.cc

Go to the documentation of this file.
00001 #include "TimerExecThread.h"
00002 #include "SharedGlobals.h"
00003 #include "Shared/ProjectInterface.h"
00004 #include "Shared/MarkScope.h"
00005 #include "Shared/get_time.h"
00006 #include "Events/EventRouter.h"
00007 
00008 //better to put this here instead of the header
00009 using namespace std; 
00010 
00011 
00012 void TimerExecThread::reset() {
00013   MarkScope sl(startLock);
00014   if((globals->getNextTimer()==-1U || globals->timeScale<=0) && isStarted())
00015     stop();
00016   else if(globals->timeScale>0 && !isStarted())
00017     start();
00018   else if(isStarted()) {
00019     interrupt();
00020   }
00021 }
00022 
00023 long TimerExecThread::calcSleepTime() {
00024   startTime.Set();
00025   unsigned int nextTimer = globals->getNextTimer();
00026   unsigned int now = get_time();
00027   long sleepTime = ( nextTimer <= now ) ? 0 :
00028     static_cast<long>((nextTimer-now)/globals->timeScale);
00029   return sleepTime;
00030 }
00031 
00032 bool TimerExecThread::launched() {
00033   if(globals->timeScale<=0)
00034     return false;
00035   delay=calcSleepTime();
00036   return PollThread::launched();
00037 }
00038 
00039 bool TimerExecThread::poll() {
00040   MarkScope bl(behaviorLock);
00041   //cout << "Poll at " << get_time() << " next timer " << globals->getNextTimer() << " (vs. " << erouter->getNextTimer() << ")" << endl;
00042   //this happens normally:
00043   //ASSERT(get_time()>=globals->getNextTimer(),"TimerExecThread::poll() early (time="<<get_time()<< " vs. nextTimer=" <<globals->getNextTimer()<<")");
00044   try {
00045     erouter->processTimers();
00046   } catch(const std::exception& ex) {
00047     if(!ProjectInterface::uncaughtException(__FILE__,__LINE__,"Occurred during timer processing",&ex))
00048       throw;
00049   } catch(...) {
00050     if(!ProjectInterface::uncaughtException(__FILE__,__LINE__,"Occurred during timer processing",NULL))
00051       throw;
00052   }
00053   globals->setNextTimer(erouter->getNextTimer());
00054   if(globals->getNextTimer()==-1U)
00055     return false;
00056   period=calcSleepTime();
00057   return true;
00058 }
00059 
00060 void TimerExecThread::interrupted() {
00061   delay=calcSleepTime();
00062 }
00063 
00064 
00065 /*! @file
00066  * @brief 
00067  * @author Ethan Tira-Thompson (ejt) (Creator)
00068  */

Tekkotsu Hardware Abstraction Layer 5.1CVS
Generated Mon May 9 05:01:39 2016 by Doxygen 1.6.3