// processclock.h : // #if !defined(AGD_PROCESSCLOCK_H__20BF9E10_0AB6_4173_B84C_B006ED1C9A76__INCLUDED_) #define AGD_PROCESSCLOCK_H__20BF9E10_0AB6_4173_B84C_B006ED1C9A76__INCLUDED_ #include #include #include #include #define _PC_NS_PER_US 1000LL // nanoseconds per microsecond #define _PC_US_PER_MS 1000LL // microseconds per millisecond #define _PC_MS_PER_S 1000LL // milliseconds per second #define _PC_NS_PER_MS (_PC_US_PER_MS * _PC_NS_PER_US) // nanoseconds per millisecond #define _PC_NS_PER_S (_PC_MS_PER_S * _PC_US_PER_MS * _PC_NS_PER_US) // nanoseconds per second #define _PC_US_PER_S (_PC_MS_PER_S * _PC_US_PER_MS) // microseconds per second #define _PC_NUM_STOPCLOCKS 8 typedef long long int pc_time64_t; #ifdef __cplusplus ///////////////////////////////////////////////////////////////////////////// // processclock.h - Declarations: class CProcessClock { public: typedef enum { SR_Ok, SR_TimerUnderrun, SR_Interrupt, SR_Error } SleepResult; typedef struct _STOPCLOCK { struct timespec tsStopclock; #if 0 pc_time64_t nMinElapsed; pc_time64_t nMaxElapsed; #endif }STOPCLOCK, *LPSTOPCLOCK; typedef const STOPCLOCK *LPCSTOPCLOCK; ///////////////////////////////////////////////////////////////////////////// public: CProcessClock(bool bInitAtConstr = false); virtual ~CProcessClock(void); static pc_time64_t GetNanoTick(struct timespec *pts = NULL); inline static pc_time64_t GetMicroTick(struct timespec *pts = NULL){ return GetNanoTick(pts) / _PC_NS_PER_US; } inline static pc_time64_t GetMilliTick(struct timespec *pts = NULL){ return GetNanoTick(pts) / _PC_NS_PER_MS; } inline static pc_time64_t Timespec2NanoSec(const struct timespec *pts){ return (pc_time64_t)pts->tv_sec * _PC_NS_PER_S + (pc_time64_t)pts->tv_nsec; } inline static void NanoSec2Timespec(pc_time64_t n, struct timespec *pts){ if(pts){ pts->tv_sec = (time_t)(n / _PC_NS_PER_S); pts->tv_nsec = (time_t)(n % _PC_NS_PER_S); } } inline static pc_time64_t CompareTimespec(const struct timespec *pts1, const struct timespec *pts2){ pc_time64_t n1 = Timespec2NanoSec(pts1); pc_time64_t n2 = Timespec2NanoSec(pts2); return n1 - n2; } static std::string Interval2String(pc_time64_t nInterval); SleepResult NSleep(pc_time64_t nInterval, bool bResumeOnIntr = true); // interval in nanoseconds inline SleepResult USleep(pc_time64_t nInterval, bool bResumeOnIntr = true){ // interval in microseconds return NSleep(nInterval * _PC_NS_PER_US, bResumeOnIntr); } SleepResult MSleep(pc_time64_t nInterval, bool bResumeOnIntr = true){ // interval in milliseconds return NSleep(nInterval * _PC_NS_PER_MS, bResumeOnIntr); } SleepResult Sleep(pc_time64_t nInterval, bool bResumeOnIntr = true){ // interval in seconds return NSleep(nInterval * _PC_NS_PER_S, bResumeOnIntr); } inline pc_time64_t ClockTrigger(unsigned int nClockIndex = 0){ if(nClockIndex >= _PC_NUM_STOPCLOCKS) return -1; return GetNanoTick(&m_sc[nClockIndex].tsStopclock); } pc_time64_t ClockGetElapsed(unsigned int nClockIndex = 0) const; inline std::string ClockGetElapsedAsString(unsigned int nClockIndex = 0) const { return Interval2String(ClockGetElapsed(nClockIndex)); } void StopclockReset(unsigned int nClockIndex = 0){ if(nClockIndex < _PC_NUM_STOPCLOCKS){ memset(&m_sc[nClockIndex].tsStopclock, 0, sizeof(struct timespec)); } } private: inline pc_time64_t IncTime(struct timespec *pts, pc_time64_t ns) const { pc_time64_t n = Timespec2NanoSec(pts); n += ns; NanoSec2Timespec(n, pts); return n; } private: struct timespec m_tsDueTime; STOPCLOCK m_sc[_PC_NUM_STOPCLOCKS]; bool m_bIsInit; }; ///////////////////////////////////////////////////////////////////////////// #endif // __cplusplus #endif // !defined(AGD_PROCESSCLOCK_H__20BF9E10_0AB6_4173_B84C_B006ED1C9A76__INCLUDED_)