Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef __CXUTILS_TIMER_H
00041 #define __CXUTILS_TIMER_H
00042
00043 #ifdef WIN32
00044
00045 #else
00046 #include <unistd.h>
00047 #endif
00048
00049 #include <set>
00050 #include <vector>
00051 #include "cxutils/thread.h"
00052
00053 namespace CxUtils
00054 {
00071 class CX_UTILS_DLL Timer
00072 {
00073 public:
00081 class CX_UTILS_DLL Callback
00082 {
00083 public:
00084 typedef std::set<Callback*> Set;
00085
00086
00087
00088
00089
00090
00091
00092 class CX_UTILS_DLL Function
00093 {
00094 public:
00095 typedef std::vector<Function> List;
00096 Function(void (*func)(void *args), void* fargs = NULL);
00097 ~Function();
00098 void Run();
00099 void (*mpFunctionPtr)(void* args);
00100 void *mpFunctionArgs;
00101 };
00102 virtual void ProcessTimerEvent() = 0;
00103 };
00104 Timer();
00105 ~Timer();
00106 int Start(const double frequencyHz, const double hptThresholdHz = 50);
00107 void Stop();
00108 bool RegisterTimerEvent(Callback* timerCallback);
00109 bool RegisterTimerEvent(void (*func)(void *args), void* fargs);
00110 void RemoveTimerEvent(Callback* timerCallback);
00111 void RemoveTimerEvent(void (*func)(void *args), void* fargs);
00112 static double GetTimeMs();
00113 static double GetTimeSeconds();
00114 static void Pause(const unsigned int ms);
00115
00116 inline bool IsActive() const { return mTimerActive; }
00117
00118 inline bool IsShuttingDown() const { return mShutdownFlag; }
00119 void TriggerEvents();
00120 double GetFrequency() const;
00121 bool ChangeFrequency(const double frequencyHz, const double hptThresholdHz = 50);
00122 void SetName(const std::string& name) { mTimerEventThread.SetThreadName(name); }
00123 private:
00124 #ifdef WIN32
00125 class Data;
00126 Data* mpTimerData;
00127 #endif
00128 static void TimerEventThread(void *args);
00129
00130 Thread mTimerEventThread;
00131 Mutex mCallbacksMutex;
00132 Mutex mSleepMutex;
00133 Callback::Set mCallbackObjects;
00134 Callback::Function::List mCallbackFunctions;
00135 volatile unsigned int mDelayTimeMs;
00136 volatile bool mTimerActive;
00137 volatile bool mShutdownFlag;
00138 };
00139
00140 typedef Timer::Callback TimerCallback;
00141 }
00142
00143 #endif
00144