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   return static_cast<long>((globals->getNextTimer()-get_time())/globals->timeScale);
00026 }
00027 
00028 bool TimerExecThread::launched() {
00029   if(globals->timeScale<=0)
00030     return false;
00031   delay=calcSleepTime();
00032   return PollThread::launched();
00033 }
00034 
00035 bool TimerExecThread::poll() {
00036   MarkScope bl(behaviorLock);
00037   //cout << "Poll at " << get_time() << " next timer " << globals->getNextTimer() << " (vs. " << erouter->getNextTimer() << ")" << endl;
00038   //this happens normally:
00039   //ASSERT(get_time()>=globals->getNextTimer(),"TimerExecThread::poll() early (time="<<get_time()<< " vs. nextTimer=" <<globals->getNextTimer()<<")");
00040   try {
00041     erouter->processTimers();
00042   } catch(const std::exception& ex) {
00043     if(!ProjectInterface::uncaughtException(__FILE__,__LINE__,"Occurred during timer processing",&ex))
00044       throw;
00045   } catch(...) {
00046     if(!ProjectInterface::uncaughtException(__FILE__,__LINE__,"Occurred during timer processing",NULL))
00047       throw;
00048   }
00049   globals->setNextTimer(erouter->getNextTimer());
00050   if(globals->getNextTimer()==-1U)
00051     return false;
00052   period=calcSleepTime();
00053   return true;
00054 }
00055 
00056 void TimerExecThread::interrupted() {
00057   delay=calcSleepTime();
00058 }
00059 
00060 
00061 /*! @file
00062  * @brief 
00063  * @author Ethan Tira-Thompson (ejt) (Creator)
00064  */

Tekkotsu Hardware Abstraction Layer 5.1CVS
Generated Sat May 4 06:37:22 2013 by Doxygen 1.6.3