PVData C++
misc/pv/timer.h
00001 /* timer.h */
00002 /*
00003  * Copyright information and license terms for this software can be
00004  * found in the file LICENSE that is included with the distribution
00005  */
00009 #ifndef TIMER_H
00010 #define TIMER_H
00011 #include <memory>
00012 #include <list>
00013 
00014 #include <stddef.h>
00015 #include <stdlib.h>
00016 #include <stddef.h>
00017 #include <string.h>
00018 #include <stdio.h>
00019 
00020 #include <pv/pvType.h>
00021 #include <pv/thread.h>
00022 #include <pv/timeStamp.h>
00023 #include <pv/event.h>
00024 #include <pv/lock.h>
00025 #include <pv/sharedPtr.h>
00026 
00027 #include <shareLib.h>
00028 
00029 namespace epics { namespace pvData { 
00030 
00031 class TimerCallback;
00032 class Timer;
00033 typedef std::tr1::shared_ptr<TimerCallback> TimerCallbackPtr;
00034 typedef std::tr1::shared_ptr<Timer> TimerPtr;
00035 
00040 class epicsShareClass TimerCallback {
00041 public:
00042     POINTER_DEFINITIONS(TimerCallback);
00046     TimerCallback();
00050     virtual ~TimerCallback(){}
00054     virtual void callback() = 0;
00058     virtual void timerStopped() = 0;
00059 private:
00060     epicsTime timeToRun;
00061     double period;
00062     bool onList;
00063     friend class Timer;
00064     struct IncreasingTime;
00065 };
00066 
00071 class epicsShareClass Timer : private Runnable {
00072 public:
00073     POINTER_DEFINITIONS(Timer);
00078     Timer(std::string threadName, ThreadPriority priority);
00079     virtual ~Timer();
00081     void close();
00087     void scheduleAfterDelay(
00088         TimerCallbackPtr const &timerCallback,
00089         double delay);
00096     void schedulePeriodic(
00097         TimerCallbackPtr const &timerCallback,
00098         double delay,
00099         double period);
00105     bool cancel(TimerCallbackPtr const &timerCallback);
00111     bool isScheduled(TimerCallbackPtr const &timerCallback) const;
00116     void dump(std::ostream& o) const;
00117 
00118 private:
00119     virtual void run();
00120 
00121     // call with mutex held
00122     void addElement(TimerCallbackPtr const &timerCallback);
00123 
00124     typedef std::list<TimerCallbackPtr> queue_t;
00125 
00126     mutable Mutex mutex;
00127     queue_t queue;
00128     Event waitForWork;
00129     bool alive;
00130     Thread thread;
00131 };
00132 
00133 epicsShareExtern std::ostream& operator<<(std::ostream& o, const Timer& timer);
00134 
00135 }}
00136 #endif  /* TIMER_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator