processclock.h 4.0 KB

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