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   if((globals->getNextTimer()==-1U || globals->timeScale<=0) && isStarted())
00014     stop();
00015   else if(globals->timeScale>0 && !isStarted())
00016     start();
00017   else if(isStarted()) {
00018     interrupt();
00019   }
00020 }
00021 
00022 long TimerExecThread::calcSleepTime() {
00023   startTime.Set();
00024   return static_cast<long>((globals->getNextTimer()-get_time())/globals->timeScale);
00025 }
00026 
00027 bool TimerExecThread::launched() {
00028   if(globals->timeScale<=0)
00029     return false;
00030   delay=calcSleepTime();
00031   return PollThread::launched();
00032 }
00033 
00034 bool TimerExecThread::poll() {
00035   MarkScope bl(behaviorLock);
00036   //cout << "Poll at " << get_time() << " next timer " << globals->getNextTimer() << " (vs. " << erouter->getNextTimer() << ")" << endl;
00037   //this happens normally:
00038   //ASSERT(get_time()>=globals->getNextTimer(),"TimerExecThread::poll() early (time="<<get_time()<< " vs. nextTimer=" <<globals->getNextTimer()<<")");
00039   try {
00040     erouter->processTimers();
00041   } catch(const std::exception& ex) {
00042     if(!ProjectInterface::uncaughtException(__FILE__,__LINE__,"Occurred during timer processing",&ex))
00043       throw;
00044   } catch(...) {
00045     if(!ProjectInterface::uncaughtException(__FILE__,__LINE__,"Occurred during timer processing",NULL))
00046       throw;
00047   }
00048   globals->setNextTimer(erouter->getNextTimer());
00049   if(globals->getNextTimer()==-1U)
00050     return false;
00051   period=calcSleepTime();
00052   return true;
00053 }
00054 
00055 void TimerExecThread::interrupted() {
00056   delay=calcSleepTime();
00057 }
00058 
00059 
00060 /*! @file
00061  * @brief 
00062  * @author Ethan Tira-Thompson (ejt) (Creator)
00063  *
00064  * $Author: ejt $
00065  * $Name: tekkotsu-4_0 $
00066  * $Revision: 1.2 $
00067  * $State: Exp $
00068  * $Date: 2007/10/12 16:55:05 $
00069  */

Tekkotsu Hardware Abstraction Layer 4.0
Generated Thu Nov 22 01:00:53 2007 by Doxygen 1.5.4