| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: Timer.h |
|---|
| 5 | // Purpose: Generic timers which execute arbitrary code when |
|---|
| 6 | // they expire. |
|---|
| 7 | // Created: 5/11/2006 |
|---|
| 8 | // |
|---|
| 9 | // -------------------------------------------------------------------------- |
|---|
| 10 | |
|---|
| 11 | #ifndef TIMER__H |
|---|
| 12 | #define TIMER__H |
|---|
| 13 | |
|---|
| 14 | #ifdef HAVE_SYS_TIME_H |
|---|
| 15 | #include <sys/time.h> |
|---|
| 16 | #endif |
|---|
| 17 | |
|---|
| 18 | #include <vector> |
|---|
| 19 | |
|---|
| 20 | #include "BoxTime.h" |
|---|
| 21 | |
|---|
| 22 | #include "MemLeakFindOn.h" |
|---|
| 23 | |
|---|
| 24 | class Timer; |
|---|
| 25 | |
|---|
| 26 | // -------------------------------------------------------------------------- |
|---|
| 27 | // |
|---|
| 28 | // Class |
|---|
| 29 | // Name: Timers |
|---|
| 30 | // Purpose: Static class to manage all timers and arrange |
|---|
| 31 | // efficient delivery of wakeup signals |
|---|
| 32 | // Created: 19/3/04 |
|---|
| 33 | // |
|---|
| 34 | // -------------------------------------------------------------------------- |
|---|
| 35 | class Timers |
|---|
| 36 | { |
|---|
| 37 | private: |
|---|
| 38 | static std::vector<Timer*>* spTimers; |
|---|
| 39 | static void Reschedule(); |
|---|
| 40 | |
|---|
| 41 | static bool sRescheduleNeeded; |
|---|
| 42 | static void SignalHandler(int iUnused); |
|---|
| 43 | |
|---|
| 44 | public: |
|---|
| 45 | static void Init(); |
|---|
| 46 | static void Cleanup(); |
|---|
| 47 | static void Add (Timer& rTimer); |
|---|
| 48 | static void Remove(Timer& rTimer); |
|---|
| 49 | static void RequestReschedule(); |
|---|
| 50 | static void RescheduleIfNeeded(); |
|---|
| 51 | }; |
|---|
| 52 | |
|---|
| 53 | class Timer |
|---|
| 54 | { |
|---|
| 55 | public: |
|---|
| 56 | Timer(size_t timeoutMillis, const std::string& rName = ""); |
|---|
| 57 | virtual ~Timer(); |
|---|
| 58 | Timer(const Timer &); |
|---|
| 59 | Timer &operator=(const Timer &); |
|---|
| 60 | |
|---|
| 61 | box_time_t GetExpiryTime() { return mExpires; } |
|---|
| 62 | virtual void OnExpire(); |
|---|
| 63 | bool HasExpired() |
|---|
| 64 | { |
|---|
| 65 | Timers::RescheduleIfNeeded(); |
|---|
| 66 | return mExpired; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | const std::string& GetName() const { return mName; } |
|---|
| 70 | |
|---|
| 71 | private: |
|---|
| 72 | box_time_t mExpires; |
|---|
| 73 | bool mExpired; |
|---|
| 74 | std::string mName; |
|---|
| 75 | |
|---|
| 76 | void Start(); |
|---|
| 77 | void Start(int64_t timeoutMillis); |
|---|
| 78 | void Stop(); |
|---|
| 79 | |
|---|
| 80 | #ifdef WIN32 |
|---|
| 81 | HANDLE mTimerHandle; |
|---|
| 82 | static VOID CALLBACK TimerRoutine(PVOID lpParam, |
|---|
| 83 | BOOLEAN TimerOrWaitFired); |
|---|
| 84 | #endif |
|---|
| 85 | }; |
|---|
| 86 | |
|---|
| 87 | #include "MemLeakFindOff.h" |
|---|
| 88 | |
|---|
| 89 | #endif // TIMER__H |
|---|