CARMA C++
ScheduledTimer.h
1 #ifndef CARMA_UTIL_SCHEDULED_TIMER_H
2 #define CARMA_UTIL_SCHEDULED_TIMER_H
3 
4 #include <ctime>
5 
6 #include "carma/util/PthreadMutex.h"
7 #include "carma/util/PthreadCond.h"
8 
9 namespace carma {
10 namespace util {
11 
19 
21  public:
22 
31  void WaitForNextFireTime( );
32 
39 
40  protected:
41  typedef struct ::timespec Timespec;
42 
43  static void sanityCheckTimespec( const Timespec & ts );
44 
45  explicit ScheduledTimer( bool preflight );
46 
47  virtual ~ScheduledTimer( );
48 
49  void SetNextFireTime( const Timespec & nextFireAbsTime );
50 
51  void SetNextFireTimeAndWait( const Timespec & nextFireAbsTime );
52 
53  void refreshNextFireAbsTimeIfNeeded( Timespec * nextFireAbsTime );
54 
55  virtual Timespec
56  CalculateNextFireAbsoluteTime( const Timespec & fireAbsTime ) = 0;
57 
58  private:
59  typedef enum {
60  FIRE_ABS_TIME_STATE_INVALID,
61  FIRE_ABS_TIME_STATE_NEXT,
62  FIRE_ABS_TIME_STATE_LAST
63  } FireAbsTimeState;
64 
65  FireAbsTimeState fireAbsTimeState_;
66  Timespec fireAbsTime_;
67  PthreadCond cond_;
68  PthreadMutex mutex_;
69  bool interruptRequested_;
70 };
71 
72 
73 } // namespace util
74 } // namespace carma
75 
76 #endif
A simple wrapper class that makes use of ::pthread_cond_t easier in a C++ world.
Definition: PthreadCond.h:43
Abstract base class for PthreadCond::TimedWait based timers.
void WaitForNextFireTime()
Block (the thread of the caller) until the next fire time for the timer arrives.
A simple wrapper class that makes use of ::pthread_mutex_t easier in a C++ world. ...
Definition: PthreadMutex.h:41
void InterruptPresentOrNextWait()
Interrupt the wait that is presently occurring on this timer object or if no such wait is presently o...