processclock.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // processclock.h :
  2. //
  3. #if !defined(AGD_PROCESSCLOCK_H__20BF9E10_0AB6_4173_B84C_B006ED1C9A76__INCLUDED_)
  4. #define AGD_PROCESSCLOCK_H__20BF9E10_0AB6_4173_B84C_B006ED1C9A76__INCLUDED_
  5. #include <time.h>
  6. #include <stdint.h>
  7. #include <string>
  8. #define _PC_NS_PER_US 1000LL // nanoseconds per microsecond
  9. #define _PC_US_PER_MS 1000LL // microseconds per millisecond
  10. #define _PC_MS_PER_S 1000LL // milliseconds per second
  11. #define _PC_NS_PER_MS (_PC_US_PER_MS * _PC_NS_PER_US) // nanoseconds per millisecond
  12. #define _PC_NS_PER_S (_PC_MS_PER_S * _PC_US_PER_MS * _PC_NS_PER_US) // nanoseconds per second
  13. #define _PC_US_PER_S (_PC_MS_PER_S * _PC_US_PER_MS) // microseconds per second
  14. #define _PC_NUM_STOPCLOCKS 8
  15. typedef long long int pc_time64_t;
  16. #ifdef __cplusplus
  17. /////////////////////////////////////////////////////////////////////////////
  18. // processclock.h - Declarations:
  19. class CProcessClock
  20. {
  21. public:
  22. typedef enum
  23. {
  24. SR_Ok,
  25. SR_TimerUnderrun,
  26. SR_Interrupt,
  27. SR_Error
  28. }
  29. SleepResult;
  30. typedef struct _STOPCLOCK
  31. {
  32. struct timespec tsStopclock;
  33. #if 0
  34. pc_time64_t nMinElapsed;
  35. pc_time64_t nMaxElapsed;
  36. #endif
  37. }STOPCLOCK, *LPSTOPCLOCK;
  38. typedef const STOPCLOCK *LPCSTOPCLOCK;
  39. /////////////////////////////////////////////////////////////////////////////
  40. public:
  41. CProcessClock(bool bInitAtConstr = false);
  42. virtual ~CProcessClock(void);
  43. static pc_time64_t GetNanoTick(struct timespec *pts = NULL);
  44. inline static pc_time64_t GetMicroTick(struct timespec *pts = NULL){
  45. return GetNanoTick(pts) / _PC_NS_PER_US;
  46. }
  47. inline static pc_time64_t GetMilliTick(struct timespec *pts = NULL){
  48. return GetNanoTick(pts) / _PC_NS_PER_MS;
  49. }
  50. inline static pc_time64_t Timespec2NanoSec(const struct timespec *pts){
  51. return (pc_time64_t)pts->tv_sec * _PC_NS_PER_S + (pc_time64_t)pts->tv_nsec;
  52. }
  53. inline static void NanoSec2Timespec(pc_time64_t n, struct timespec *pts){
  54. if(pts){
  55. pts->tv_sec = (time_t)(n / _PC_NS_PER_S);
  56. pts->tv_nsec = (time_t)(n % _PC_NS_PER_S);
  57. }
  58. }
  59. inline static pc_time64_t CompareTimespec(const struct timespec *pts1, const struct timespec *pts2){
  60. pc_time64_t n1 = Timespec2NanoSec(pts1);
  61. pc_time64_t n2 = Timespec2NanoSec(pts2);
  62. return n1 - n2;
  63. }
  64. static std::string Interval2String(pc_time64_t nInterval);
  65. SleepResult NSleep(pc_time64_t nInterval, bool bResumeOnIntr = true); // interval in nanoseconds
  66. inline SleepResult USleep(pc_time64_t nInterval, bool bResumeOnIntr = true){ // interval in microseconds
  67. return NSleep(nInterval * _PC_NS_PER_US, bResumeOnIntr);
  68. }
  69. SleepResult MSleep(pc_time64_t nInterval, bool bResumeOnIntr = true){ // interval in milliseconds
  70. return NSleep(nInterval * _PC_NS_PER_MS, bResumeOnIntr);
  71. }
  72. SleepResult Sleep(pc_time64_t nInterval, bool bResumeOnIntr = true){ // interval in seconds
  73. return NSleep(nInterval * _PC_NS_PER_S, bResumeOnIntr);
  74. }
  75. inline pc_time64_t ClockTrigger(unsigned int nClockIndex = 0){
  76. if(nClockIndex >= _PC_NUM_STOPCLOCKS)
  77. return -1;
  78. return GetNanoTick(&m_sc[nClockIndex].tsStopclock);
  79. }
  80. pc_time64_t ClockGetElapsed(unsigned int nClockIndex = 0) const;
  81. inline std::string ClockGetElapsedAsString(unsigned int nClockIndex = 0) const {
  82. return Interval2String(ClockGetElapsed(nClockIndex));
  83. }
  84. void StopclockReset(unsigned int nClockIndex = 0){
  85. if(nClockIndex < _PC_NUM_STOPCLOCKS){
  86. memset(&m_sc[nClockIndex].tsStopclock, 0, sizeof(struct timespec));
  87. }
  88. }
  89. private:
  90. inline pc_time64_t IncTime(struct timespec *pts, pc_time64_t ns) const {
  91. pc_time64_t n = Timespec2NanoSec(pts);
  92. n += ns;
  93. NanoSec2Timespec(n, pts);
  94. return n;
  95. }
  96. private:
  97. struct timespec m_tsDueTime;
  98. STOPCLOCK m_sc[_PC_NUM_STOPCLOCKS];
  99. bool m_bIsInit;
  100. };
  101. /////////////////////////////////////////////////////////////////////////////
  102. #endif // __cplusplus
  103. #endif // !defined(AGD_PROCESSCLOCK_H__20BF9E10_0AB6_4173_B84C_B006ED1C9A76__INCLUDED_)