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