00001 00032 #ifndef LL_TIMER_H 00033 #define LL_TIMER_H 00034 00035 #if LL_LINUX || LL_DARWIN 00036 #include <sys/time.h> 00037 #endif 00038 00039 #include "stdtypes.h" 00040 00041 // units conversions 00042 #ifndef USEC_PER_SEC 00043 const U32 USEC_PER_SEC = 1000000; 00044 #endif 00045 const U32 SEC_PER_MIN = 60; 00046 const U32 MIN_PER_HOUR = 60; 00047 const U32 USEC_PER_MIN = USEC_PER_SEC * SEC_PER_MIN; 00048 const U32 USEC_PER_HOUR = USEC_PER_MIN * MIN_PER_HOUR; 00049 const U32 SEC_PER_HOUR = SEC_PER_MIN * MIN_PER_HOUR; 00050 const F64 SEC_PER_USEC = 1.0 / (F64) USEC_PER_SEC; 00051 00052 class LLTimer 00053 { 00054 public: 00055 static LLTimer *sTimer; // global timer 00056 00057 protected: 00058 U64 mLastClockCount; 00059 U64 mExpirationTicks; 00060 BOOL mStarted; 00061 00062 public: 00063 LLTimer(); 00064 ~LLTimer(); 00065 00066 static void initClass() { if (!sTimer) sTimer = new LLTimer; } 00067 static void cleanupClass() { delete sTimer; sTimer = NULL; } 00068 00069 // Return a high precision number of seconds since the start of 00070 // this application instance. 00071 static F64 getElapsedSeconds() 00072 { 00073 return sTimer->getElapsedTimeF64(); 00074 } 00075 00076 // Return a high precision usec since epoch 00077 static U64 getTotalTime(); 00078 00079 // Return a high precision seconds since epoch 00080 static F64 getTotalSeconds(); 00081 00082 00083 // MANIPULATORS 00084 void start() { reset(); mStarted = TRUE; } 00085 void stop() { mStarted = FALSE; } 00086 void reset(); // Resets the timer 00087 void setLastClockCount(U64 current_count); // Sets the timer so that the next elapsed call will be relative to this time 00088 void setTimerExpirySec(F32 expiration); 00089 BOOL checkExpirationAndReset(F32 expiration); 00090 BOOL hasExpired() const; 00091 F32 getElapsedTimeAndResetF32(); // Returns elapsed time in seconds with reset 00092 F64 getElapsedTimeAndResetF64(); 00093 00094 F32 getRemainingTimeF32() const; 00095 00096 static BOOL knownBadTimer(); 00097 00098 // ACCESSORS 00099 F32 getElapsedTimeF32() const; // Returns elapsed time in seconds 00100 F64 getElapsedTimeF64() const; // Returns elapsed time in seconds 00101 00102 BOOL getStarted() const { return mStarted; } 00103 00104 00105 static U64 getCurrentClockCount(); // Returns the raw clockticks 00106 }; 00107 00108 // 00109 // Various functions for initializing/accessing clock and timing stuff. Don't use these without REALLY knowing how they work. 00110 // 00111 U64 get_clock_count(); 00112 F64 calc_clock_frequency(U32 msecs); 00113 void update_clock_frequencies(); 00114 00115 // Sleep for milliseconds 00116 void ms_sleep(U32 ms); 00117 00118 // Returns the correct UTC time in seconds, like time(NULL). 00119 // Useful on the viewer, which may have its local clock set wrong. 00120 U32 time_corrected(); 00121 00122 // Correction factor used by time_corrected() above. 00123 extern S32 gUTCOffset; 00124 00125 // Is the current computer (in its current time zone) 00126 // observing daylight savings time? 00127 BOOL is_daylight_savings(); 00128 00129 // Converts internal "struct tm" time buffer to Pacific Standard/Daylight Time 00130 // Usage: 00131 // S32 utc_time; 00132 // utc_time = time_corrected(); 00133 // struct tm* internal_time = utc_to_pacific_time(utc_time, gDaylight); 00134 struct tm* utc_to_pacific_time(S32 utc_time, BOOL pacific_daylight_time); 00135 00136 void microsecondsToTimecodeString(U64 current_time, char *tcstring); 00137 void secondsToTimecodeString(F32 current_time, char *tcstring); 00138 00139 // class for scheduling a function to be called at a given frequency (approximate, inprecise) 00140 class LLEventTimer 00141 { 00142 public: 00143 LLEventTimer(F32 period); // period is the amount of time between each call to tick() in seconds 00144 virtual ~LLEventTimer(); 00145 00146 //function to be called at the supplied frequency 00147 // Normally return FALSE; TRUE will delete the timer after the function returns. 00148 virtual BOOL tick() = 0; 00149 00150 static void updateClass(); 00151 00152 protected: 00153 LLTimer mEventTimer; 00154 F32 mPeriod; 00155 00156 private: 00157 //list of active timers 00158 static std::list<LLEventTimer*> sActiveList; 00159 }; 00160 00161 #endif