PVData C++  8.0.5
timer.h
1 /* timer.h */
2 /*
3  * Copyright information and license terms for this software can be
4  * found in the file LICENSE that is included with the distribution
5  */
6 /**
7  * @author mrk
8  */
9 #ifndef TIMER_H
10 #define TIMER_H
11 #include <memory>
12 #include <list>
13 
14 #include <stddef.h>
15 #include <stdlib.h>
16 #include <stddef.h>
17 #include <string.h>
18 #include <stdio.h>
19 
20 #include <pv/pvType.h>
21 #include <pv/thread.h>
22 #include <pv/timeStamp.h>
23 #include <pv/event.h>
24 #include <pv/lock.h>
25 #include <pv/sharedPtr.h>
26 
27 #include <shareLib.h>
28 
29 namespace epics { namespace pvData {
30 
31 class TimerCallback;
32 class Timer;
33 typedef std::tr1::shared_ptr<TimerCallback> TimerCallbackPtr;
34 typedef std::tr1::shared_ptr<Timer> TimerPtr;
35 
36 /**
37  * @brief Class that must be implemented by code that makes Timer requests.
38  *
39  */
40 class epicsShareClass TimerCallback {
41 public:
42  POINTER_DEFINITIONS(TimerCallback);
43  /**
44  * Constructor
45  */
46  TimerCallback();
47  /**
48  * Destructor
49  */
50  virtual ~TimerCallback(){}
51  /**
52  * The method that is called when a timer expires.
53  */
54  virtual void callback() = 0;
55  /**
56  * The timer has stopped.
57  */
58  virtual void timerStopped() = 0;
59 private:
60  epicsTime timeToRun;
61  double period;
62  bool onList;
63  friend class Timer;
64  struct IncreasingTime;
65 };
66 
67 /**
68  * @brief Support for delayed or periodic callback execution.
69  *
70  */
71 class epicsShareClass Timer : private Runnable {
72 public:
73  POINTER_DEFINITIONS(Timer);
74  /** Create a new timer queue
75  * @param threadName name for the timer thread.
76  * @param priority thread priority
77  */
78  Timer(std::string threadName, ThreadPriority priority);
79  virtual ~Timer();
80  //! Prevent new callbacks from being scheduled, and cancel pending callbacks
81  void close();
82  /**
83  * schedule a callback after a delay.
84  * @param timerCallback the timerCallback instance.
85  * @param delay number of seconds before calling callback.
86  */
87  void scheduleAfterDelay(
88  TimerCallbackPtr const &timerCallback,
89  double delay);
90  /**
91  * schedule a periodic callback.`
92  * @param timerCallback the timerCallback instance.
93  * @param delay number of seconds before first callback.
94  * @param period time in seconds between each callback.
95  */
96  void schedulePeriodic(
97  TimerCallbackPtr const &timerCallback,
98  double delay,
99  double period);
100  /**
101  * cancel a callback.
102  * @param timerCallback the timerCallback to cancel.
103  * @returns true if the timer was queued, and now is cancelled
104  */
105  bool cancel(TimerCallbackPtr const &timerCallback);
106  /**
107  * Is the callback scheduled to be called?
108  * @param timerCallback the timerCallback.
109  * @return (false,true) if (not, is) scheduled.
110  */
111  bool isScheduled(TimerCallbackPtr const &timerCallback) const;
112  /**
113  * show the elements in the timer queue.
114  * @param o The output stream for the output
115  */
116  void dump(std::ostream& o) const;
117 
118 private:
119  virtual void run();
120 
121  // call with mutex held
122  void addElement(TimerCallbackPtr const &timerCallback);
123 
124  typedef std::list<TimerCallbackPtr> queue_t;
125 
126  mutable Mutex mutex;
127  queue_t queue;
128  Event waitForWork;
129  bool waiting;
130  bool alive;
131  Thread thread;
132 };
133 
134 epicsShareExtern std::ostream& operator<<(std::ostream& o, const Timer& timer);
135 
136 }}
137 #endif /* TIMER_H */
#define POINTER_DEFINITIONS(clazz)
Definition: sharedPtr.h:198