Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/QNX4/TimerThread.cpp

Go to the documentation of this file.
00001 #ifndef __J2K__TimerThread_CPP__
00002 #define __J2K__TimerThread_CPP__
00003 
00004 /*
00005    Simple periodically executed thread.  
00006    Overrides MsgThread with its own message.
00007 
00008    You are able to add a response table to this thread, 
00009    but I really don't recommend it unless you're sure 
00010    it won't impact on the response time.
00011 
00012    Note that the timer is message number -1.  
00013    It invokes the virtual function tick()
00014 */
00015 
00016 #include <j2k/Fred/QNX/TimerThread.hpp>
00017 
00018 const int TimerThread::defaultMsg = -1;
00019 
00020 TimerThread::TimerThread()
00021   : MsgThread(),
00022     Timer_ID( -1 ),
00023     Proxy_ID( -1 )
00024     { }
00025 
00026 TimerThread::~TimerThread() {
00027   stop();
00028 }
00029 
00030 /*
00031  * Purpose:  Arm the timer thread.  It will fire at the next period.
00032  *
00033  * Input:    period  : time period of timer.  Timer will then fire
00034  *                     every period.  First fire is after one period.
00035  *
00036  *           algorithm, priority, stack_size for the Thread.
00037  *
00038  * Output:   PID of the thread.
00039  */
00040 
00041 pid_t  TimerThread::start( timespec&  period,
00042                            int        algorithm,
00043                            int        priority,
00044                            size_t     stack_size  )
00045 {
00046 
00047   if ( MsgThread::start( algorithm, priority, stack_size ) < 0 ) {
00048     return -1;
00049   }
00050 
00051   // Create a Proxy
00052   Proxy_ID = qnx_proxy_attach( Thread::pid,
00053                                (char*)&defaultMsg,
00054                                sizeof(&defaultMsg),
00055                                priority             );
00056 
00057   if ( Proxy_ID < 0 ) {
00058     stop();
00059     return -1;
00060   }
00061 
00062   // Create a timer
00063   struct sigevent evt;
00064   evt.sigev_signo = -Proxy_ID;
00065   Timer_ID = timer_create( CLOCK_REALTIME, &evt );
00066 
00067   if ( Timer_ID < 0 ) {
00068     stop();
00069     return -1;
00070   }
00071 
00072   // Set the Timer
00073   itimerspec timer;
00074   timer.it_value    = period;
00075   timer.it_interval = period;
00076 
00077   if ( timer_settime( timer_ID, 0, &timer, NULL ) < 0 ) {
00078     stop();
00079     return -1;
00080   }
00081 
00082   return Thread::pid;
00083 }
00084 
00085 /*
00086  * Purpose:  Stop the Thread and clean up.
00087  */
00088 int TimerThread::stop()
00089 {
00090   // Kill the running Timer
00091   if ( Timer_ID >= 0 ) {
00092     timer_delete( Timer_ID );
00093     timer_ID = -1;
00094   }
00095 
00096   // Kill Timer Proxy
00097   if ( Proxy_ID >= 0 ) {
00098     qnx_proxy_detach( Proxy_ID );
00099     Proxy_ID = -1;
00100   }
00101 
00102   // Let previous parent clean up
00103   return MsgThread::stop();
00104 }
00105 
00106 
00107 /*
00108  * Purpose:  Filters the timeout message (ID -1)
00109  *
00110  * Input:    msg     : Message recieved
00111  *           data    : Message data
00112  *           pid     : PID of sender
00113  *
00114  * Output:   Returns 1, if we are filtering the message 
00115  *           (i.e., pid = Proxy_ID and msg = -1)
00116  */
00117 
00118 int TimerThread::message_filter( int msg, char* data, pid_t pid )
00119 {
00120   if ( pid == Proxy_ID  &&  msg == defaultMsg ) {
00121     tick();
00122     return 1;
00123   }
00124   return 0;
00125 }
00126 
00127 int MsgSort( void* msg, void* ptrThis, pid_t rPid, void* p )
00128 {
00129   if ( rPid == Proxy_ID && (*((int*)msg)) == MSG_TIMER_TICK ) {
00130     p = NULL;
00131     Tick();   // The message was valid call Tick()
00132     return MSG_TIMER_TICK;
00133   }
00134   return 0;
00135 }
00136 
00137 #endif

Generated on Sun Oct 14 18:46:42 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001