sucycletimer.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // sucycletimer.h :
  2. //
  3. #if !defined(AGD_SUCYCLETIMER_H__C9DBE0F2_F287_4369_8033_7209E0C6C839__INCLUDED_)
  4. #define AGD_SUCYCLETIMER_H__C9DBE0F2_F287_4369_8033_7209E0C6C839__INCLUDED_
  5. #include <time.h>
  6. #include <string.h>
  7. /////////////////////////////////////////////////////////////////////////////
  8. // sucycletimer.h - Declarations:
  9. #define _MSEC_PER_SEC 1000LL
  10. #define _MSEC_PER_MIN (_MSEC_PER_SEC * 60LL)
  11. #define _MSEC_PER_HOUR (_MSEC_PER_MIN * 60LL)
  12. #define _USEC_PER_MSEC 1000LL
  13. #define _USEC_PER_SEC (_USEC_PER_MSEC * 1000LL)
  14. #define _USEC_PER_MIN (_USEC_PER_SEC * 60LL)
  15. #define _USEC_PER_HOUR (_USEC_PER_MIN * 60LL)
  16. #define _NSEC_PER_USEC 1000LL
  17. #define _NSEC_PER_MSEC (_NSEC_PER_USEC * 1000LL)
  18. #define _NSEC_PER_SEC (_NSEC_PER_MSEC * 1000LL)
  19. #define _NSEC_PER_MIN (_NSEC_PER_SEC * 60LL)
  20. #define _NSEC_PER_HOUR (_NSEC_PER_MIN * 60LL)
  21. /////////////////////////////////////////////////////////////////////////////
  22. typedef long long cy_time_t;
  23. /////////////////////////////////////////////////////////////////////////////
  24. #ifdef __cplusplus
  25. /////////////////////////////////////////////////////////////////////////////
  26. class CCycleTimer
  27. {
  28. public:
  29. CCycleTimer(unsigned int nMaxCycleIntervalMilliSec1, unsigned int nMaxCycleIntervalMilliSec2 = 0, unsigned int nMaxCycleIntervalMilliSec3 = 0);
  30. CCycleTimer(const struct timespec *pts1, const struct timespec *pts2 = NULL, const struct timespec *pts3 = NULL);
  31. virtual ~CCycleTimer(void);
  32. public:
  33. static cy_time_t GetNanoTick(struct timespec *pts);
  34. inline static cy_time_t GetMicroTick(void){
  35. return CCycleTimer::GetNanoTick(NULL) / _NSEC_PER_USEC;}
  36. inline static cy_time_t GetMilliTick(void){
  37. return CCycleTimer::GetNanoTick(NULL) / _NSEC_PER_MSEC;}
  38. inline static cy_time_t Timespec2NanoSec(const struct timespec &rts){
  39. return (cy_time_t)rts.tv_sec * _NSEC_PER_SEC + (cy_time_t)rts.tv_nsec;}
  40. inline static void NanoSec2Timespec(cy_time_t n, struct timespec &rts){
  41. rts.tv_sec = (time_t)(n / _NSEC_PER_SEC);
  42. rts.tv_nsec = (__syscall_slong_t)(n % _NSEC_PER_SEC);
  43. }
  44. inline static cy_time_t CompareTimespec(const struct timespec &rts1, const struct timespec &rts2){
  45. if(rts1.tv_sec != rts2.tv_sec)
  46. return (cy_time_t)rts1.tv_sec - (cy_time_t)rts2.tv_sec;
  47. else
  48. return (cy_time_t)rts1.tv_nsec - (cy_time_t)rts2.tv_nsec;
  49. }
  50. inline static cy_time_t TimespecDiffNanoSec(const struct timespec &rts1, const struct timespec &rts2){
  51. cy_time_t s = 0;
  52. if(rts1.tv_sec != rts2.tv_sec)
  53. s = ((cy_time_t)rts1.tv_sec - (cy_time_t)rts2.tv_sec) * _NSEC_PER_SEC;
  54. return s + (cy_time_t)rts1.tv_nsec - (cy_time_t)rts2.tv_nsec;
  55. }
  56. inline void Trigger(void){
  57. ::clock_gettime(CLOCK_MONOTONIC, &m_ts);
  58. }
  59. cy_time_t GetNanoSecElapsed(const struct timespec *ptsComparand = NULL) const;
  60. inline cy_time_t GetMicroSecElapsed(const struct timespec *ptsComparand = NULL) const {
  61. return GetNanoSecElapsed(ptsComparand) / _NSEC_PER_USEC;
  62. }
  63. inline cy_time_t GetMilliSecElapsed(const struct timespec *ptsComparand = NULL) const {
  64. return GetNanoSecElapsed(ptsComparand) / _NSEC_PER_MSEC;
  65. }
  66. inline cy_time_t GetRemainingCycleNanoSec1(const struct timespec *ptsComparand = NULL) const {
  67. return m_nMaxCycleNanoSec1 - GetNanoSecElapsed(ptsComparand);
  68. }
  69. inline cy_time_t GetRemainingCycleNanoSec2(const struct timespec *ptsComparand = NULL) const {
  70. return m_nMaxCycleNanoSec2 - GetNanoSecElapsed(ptsComparand);
  71. }
  72. inline cy_time_t GetRemainingCycleNanoSec3(const struct timespec *ptsComparand = NULL) const {
  73. return m_nMaxCycleNanoSec3 - GetNanoSecElapsed(ptsComparand);
  74. }
  75. inline void SetMaxCycleInterval11(unsigned int nMilliSec){
  76. m_nMaxCycleNanoSec1 = nMilliSec * _NSEC_PER_MSEC;
  77. }
  78. inline void SetMaxCycleInterval12(unsigned int nMilliSec){
  79. m_nMaxCycleNanoSec2 = nMilliSec * _NSEC_PER_MSEC;
  80. }
  81. inline void SetMaxCycleInterval13(unsigned int nMilliSec){
  82. m_nMaxCycleNanoSec3 = nMilliSec * _NSEC_PER_MSEC;
  83. }
  84. static void TimespecAddSubNanoSec(struct timespec &rts, cy_time_t nNanoSec);
  85. inline int Sleep1(bool bResumeOnIntr = false) const {
  86. return Sleep(m_nMaxCycleNanoSec1, bResumeOnIntr);
  87. }
  88. inline int Sleep2(bool bResumeOnIntr = false) const {
  89. return Sleep(m_nMaxCycleNanoSec2, bResumeOnIntr);
  90. }
  91. inline int Sleep3(bool bResumeOnIntr = false) const {
  92. return Sleep(m_nMaxCycleNanoSec3, bResumeOnIntr);
  93. }
  94. private:
  95. int Sleep(const cy_time_t &rnMaxCycleNanoSec, bool bResumeOnIntr) const;
  96. private:
  97. struct timespec m_ts;
  98. struct timespec m_res;
  99. cy_time_t m_nMaxCycleNanoSec1;
  100. cy_time_t m_nMaxCycleNanoSec2;
  101. cy_time_t m_nMaxCycleNanoSec3;
  102. };
  103. /////////////////////////////////////////////////////////////////////////////
  104. #endif // __cplusplus
  105. /////////////////////////////////////////////////////////////////////////////
  106. #endif // !defined(AGD_SUCYCLETIMER_H__C9DBE0F2_F287_4369_8033_7209E0C6C839__INCLUDED_)