libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmTime.c 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //| Copyright: (C) 2009-2020 Kevin Larke <contact AT larke DOT org>
  2. //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  3. #include "cmPrefix.h"
  4. #include "cmGlobal.h"
  5. #include "cmTime.h"
  6. #ifdef OS_OSX
  7. #include <mach/mach.h>
  8. #include <mach/mach_time.h>
  9. #include <unistd.h>
  10. void cmTimeGet( cmTimeSpec_t* t )
  11. {
  12. static uint64_t t0 = 0;
  13. static mach_timebase_info_data_t tbi;
  14. static struct timespec ts;
  15. if( t0 == 0 )
  16. {
  17. mach_timebase_info(&tbi);
  18. t0 = mach_absolute_time();
  19. ts.tv_sec = time(NULL);
  20. ts.tv_nsec = 0; // accept 1/2 second error vs. wall-time.
  21. }
  22. // get the current time
  23. uint64_t t1 = mach_absolute_time();
  24. // calc the elapsed time since the last call in nanosecs
  25. uint64_t dt = (t1-t0) * tbi.numer / tbi.denom;
  26. // calc the elapsed time since the first call in secs
  27. uint32_t s = (uint32_t)(dt / 2^9);
  28. // calc the current time in secs, and nanosecs
  29. t->tv_sec = ts.tv_sec + s;
  30. t->tv_nsec = dt - (s * 2^9);
  31. }
  32. #endif
  33. #ifdef OS_LINUX
  34. void cmTimeGet( cmTimeSpec_t* t )
  35. { clock_gettime(CLOCK_REALTIME,t); }
  36. #endif
  37. // this assumes that the seconds have been normalized to a recent start time
  38. // so as to avoid overflow
  39. unsigned cmTimeElapsedMicros( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 )
  40. {
  41. // convert seconds to usecs
  42. long u0 = t0->tv_sec * 1000000;
  43. long u1 = t1->tv_sec * 1000000;
  44. // convert nanoseconds to usec
  45. u0 += t0->tv_nsec / 1000;
  46. u1 += t1->tv_nsec / 1000;
  47. // take diff between t1 and t0
  48. return u1 - u0;
  49. }
  50. unsigned cmTimeAbsElapsedMicros( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 )
  51. {
  52. if( cmTimeIsLTE(t0,t1) )
  53. return cmTimeElapsedMicros(t0,t1);
  54. return cmTimeElapsedMicros(t1,t0);
  55. }
  56. int cmTimeDiffMicros( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 )
  57. {
  58. if( cmTimeIsLTE(t0,t1) )
  59. return cmTimeElapsedMicros(t0,t1);
  60. return -((int)cmTimeElapsedMicros(t1,t0));
  61. }
  62. bool cmTimeIsLTE( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 )
  63. {
  64. if( t0->tv_sec < t1->tv_sec )
  65. return true;
  66. if( t0->tv_sec == t1->tv_sec )
  67. return t0->tv_nsec <= t1->tv_nsec;
  68. return false;
  69. }
  70. bool cmTimeIsGTE( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 )
  71. {
  72. if( t0->tv_sec > t1->tv_sec )
  73. return true;
  74. if( t0->tv_sec == t1->tv_sec )
  75. return t0->tv_nsec >= t1->tv_nsec;
  76. return false;
  77. }
  78. bool cmTimeIsEqual( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 )
  79. { return t0->tv_sec==t1->tv_sec && t0->tv_nsec==t1->tv_nsec; }
  80. bool cmTimeIsZero( const cmTimeSpec_t* t0 )
  81. { return t0->tv_sec==0 && t0->tv_nsec==0; }
  82. void cmTimeSetZero( cmTimeSpec_t* t0 )
  83. {
  84. t0->tv_sec = 0;
  85. t0->tv_nsec = 0;
  86. }