TRC Library for FRC (2011) 2011
TRC Library Code Project

C:/Users/Michael/Frc/2011/code/trclib/TrcTimer.h

Go to the documentation of this file.
00001 #if 0
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 #endif
00015 
00016 #ifndef _TRCTIMER_H
00017 #define _TRCTIMER_H
00018 
00019 #ifdef MOD_ID
00020     #undef MOD_ID
00021 #endif
00022 #define MOD_ID                  MOD_TIMER
00023 #ifdef MOD_NAME
00024     #undef MOD_NAME
00025 #endif
00026 #define MOD_NAME                "TrcTimer"
00027 
00034 class TimerNotify
00035 {
00036 public:
00043     virtual
00044     void
00045     NotifyTimer(
00046         __in void *context
00047         ) = 0;
00048 };  //class TimerNotify
00049 
00054 class TrcTimer
00055 {
00056 private:
00057     TimerNotify *m_notify;
00058     void        *m_context;
00059     SEM_ID       m_semaphore;
00060     Notifier    *m_notifier;
00061 
00065     void
00066     TimerExpired(
00067         void
00068         )
00069     {
00070         TLevel(EVENT);
00071         TEnter();
00072 
00073         CRITICAL_REGION(m_semaphore)
00074         {
00075             if (m_notify != NULL)
00076             {
00077                 m_notify->NotifyTimer(m_context);
00078                 m_notify = NULL;
00079             }
00080         }
00081         END_REGION;
00082 
00083         TExit();
00084     }   //TimerExpired
00085 
00093     static
00094     void
00095     CallTimerExpired(
00096         __in void *timer
00097         )
00098     {
00099         TLevel(EVENT);
00100         TEnterMsg(("timer=%p", timer));
00101         ((TrcTimer *)timer)->TimerExpired();
00102         TExit();
00103     }   //CallTimerExpired
00104 
00105 public:
00109     TrcTimer(
00110         void
00111         ): m_notify(NULL),
00112            m_context(NULL)
00113     {
00114         TLevel(INIT);
00115         TEnter();
00116 
00117         m_semaphore = semBCreate(SEM_Q_PRIORITY, SEM_FULL);
00118         m_notifier = new Notifier(TrcTimer::CallTimerExpired, this);
00119 
00120         TExit();
00121     }   //TrcTimer
00122 
00126     ~TrcTimer(
00127         void
00128         )
00129     {
00130         TLevel(INIT);
00131         TEnter();
00132 
00133         semFlush(m_semaphore);
00134         SAFE_DELETE(m_notifier);
00135 
00136         TExit();
00137     }   //~TrcTimer
00138 
00148     bool
00149     SetTimer(
00150         __in double       delay,
00151         __in TimerNotify *notify,
00152         __in void        *context = NULL
00153         )
00154     {
00155         bool rc = true;
00156         TLevel(API);
00157         TEnterMsg(("delay=%f,notify=%p,context=%p", delay, notify, context));
00158 
00159         CRITICAL_REGION(m_semaphore)
00160         {
00161             if (m_notify != NULL)
00162             {
00163                 TErr(("There is already a pending timer."));
00164                 rc = false;
00165             }
00166             else
00167             {
00168                 m_notify = notify;
00169                 m_context = context;
00170                 m_notifier->StartSingle(delay);
00171             }
00172         }
00173         END_REGION;
00174 
00175         TExitMsg(("=%d", rc));
00176         return rc;
00177     }   //SetTimer
00178 
00182     void
00183     CancelTimer(
00184         void
00185         )
00186     {
00187         TLevel(API);
00188         TEnter();
00189 
00190         m_notifier->Stop();
00191         m_notify = NULL;
00192         m_context = NULL;
00193 
00194         TExit();
00195     }   //CancelTimer
00196 };  //class TrcTimer
00197 
00198 #endif  //ifndef _TRCTIMER_H
 All Classes Files Functions Variables Defines