Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

PollThread.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_PollThread_h_
00003 #define INCLUDED_PollThread_h_
00004 
00005 #ifdef PLATFORM_APERIOS
00006 #  warning PollThread class is not Aperios compatable
00007 #else
00008 
00009 #include "Thread.h"
00010 #include "Shared/TimeET.h"
00011 
00012 //! description of PollThread
00013 class PollThread : public Thread {
00014 public:
00015   //! constructor
00016   PollThread() : Thread(), delay(0L), period(0L), startTime(0L), trackPollTime(false), launching(false), initialPoll(false) {}
00017   //! constructor
00018   explicit PollThread(const TimeET& initial, const TimeET& freq, bool countPollTime, bool autostart=true)
00019   : Thread(), delay(initial), period(freq), startTime(0L), trackPollTime(countPollTime), launching(false), initialPoll(true)
00020   {
00021     if(autostart)
00022       start();
00023   }
00024   //! destructor
00025   ~PollThread() {
00026     if(isRunning()) {
00027       stop();
00028       join();
00029     }
00030   }
00031   
00032   virtual void start();
00033   virtual void stop();
00034   
00035   //! sends a signal to the thread which will interrupt any sleep calls (and trigger interrupted() to be called within the thread)
00036   virtual void interrupt();
00037   
00038   virtual bool getTrackPollTime() { return trackPollTime; } //!< returns #trackPollTime
00039   virtual void setTrackPollTime(bool countPollTime) { trackPollTime=countPollTime; } //!< sets #trackPollTime
00040   
00041 protected:
00042   //! this is the function which will be called at the specified frequency, override it with your own functionality
00043   /*! @return true if run() should continue, false to stop the thread.
00044    *  The default implementation calls runloop() and sets #period to its return value if that value is not -1U.
00045    *  If the value is -1U, it returns false.  Feel free to override this directly, instead of runloop(),
00046    *  particularly if you don't intend to change period dynamically. */
00047   virtual bool poll();
00048   
00049   //! called if a signal is sent while sleeping, should reset #delay to indicate remaining sleep time, relative to startTime
00050   /*! On return, #delay should be set such that #delay-startTime.Age() is remaining sleep time.  In other words,
00051    *  simply set #delay to the #period to maintain previously requested timing.
00052    *  
00053    *  This default implementation will set #delay to the remaining time needed to maintain current period setting.
00054    *  Feel free to override and reset #period (or other member variables) if you need to change timing dynamically.
00055    *  If the period is shortened such that poll() should have already occurred based on time of previous
00056    *  call and the new period (plus any delay value), then poll() will be called immediately upon return. */
00057   virtual void interrupted();
00058   
00059   virtual void * run();
00060   //virtual void cancelled();
00061   
00062   //! called if a SIGALRM is sent (generally, by a call to interrupt())
00063   static void handleInterrupt(int signal);
00064   
00065   TimeET delay; //!< amount of time to delay between call to start() and first call to poll(), or if interrupt occurs after first poll(), amount of time to re-sleep
00066   TimeET period; //!< amount of time between calls to poll() -- if zero or negative, no delay will be made between calls (other than a call to testCancel())
00067   TimeET startTime; //!< the time at which start() was called or the current period began
00068   bool trackPollTime; //!< if true, the time spent in poll() is subtracted from the next sleep time so frequency is fixed; if false, #period is added onto whatever time poll() takes
00069   bool launching; //!< set to true after start until run() is called (can't interrupt before thread has launched)
00070   bool initialPoll; //!< set to true after start until after first call to poll has completed
00071 };
00072 
00073 #endif //Aperios check
00074 
00075 /*! @file
00076  * @brief 
00077  * @author Ethan Tira-Thompson (ejt) (Creator)
00078  *
00079  * $Author: ejt $
00080  * $Name: tekkotsu-3_0 $
00081  * $Revision: 1.7 $
00082  * $State: Exp $
00083  * $Date: 2006/09/16 17:32:39 $
00084  */
00085 
00086 #endif

Tekkotsu v3.0
Generated Wed Oct 4 00:03:45 2006 by Doxygen 1.4.7