Audio plugin host https://kx.studio/carla
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.

162 lines
4.6KB

  1. /*
  2. * Carla time utils
  3. * Copyright (C) 2011-2023 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_TIME_UTILS_HPP_INCLUDED
  18. #define CARLA_TIME_UTILS_HPP_INCLUDED
  19. #include "CarlaUtils.hpp"
  20. #include <ctime>
  21. #ifdef CARLA_OS_WIN
  22. # include <mmsystem.h>
  23. #endif
  24. // --------------------------------------------------------------------------------------------------------------------
  25. // carla_*sleep
  26. /*
  27. * Sleep for 'secs' seconds.
  28. */
  29. static inline
  30. void carla_sleep(const uint secs) noexcept
  31. {
  32. CARLA_SAFE_ASSERT_RETURN(secs > 0,);
  33. try {
  34. #ifdef CARLA_OS_WIN
  35. ::Sleep(secs * 1000);
  36. #else
  37. ::sleep(secs);
  38. #endif
  39. } CARLA_SAFE_EXCEPTION("carla_sleep");
  40. }
  41. /*
  42. * Sleep for 'msecs' milliseconds.
  43. */
  44. static inline
  45. void carla_msleep(const uint msecs) noexcept
  46. {
  47. CARLA_SAFE_ASSERT_RETURN(msecs > 0,);
  48. try {
  49. #ifdef CARLA_OS_WIN
  50. ::Sleep(msecs);
  51. #else
  52. ::usleep(msecs * 1000);
  53. #endif
  54. } CARLA_SAFE_EXCEPTION("carla_msleep");
  55. }
  56. // --------------------------------------------------------------------------------------------------------------------
  57. // carla_gettime_*
  58. /*
  59. * Get a monotonically-increasing time in milliseconds.
  60. */
  61. static inline
  62. time_t carla_gettime_ms() noexcept
  63. {
  64. #if defined(CARLA_OS_MAC)
  65. static const time_t s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000000;
  66. return (clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000000) - s;
  67. #elif defined(CARLA_OS_WIN)
  68. return static_cast<time_t>(timeGetTime());
  69. #else
  70. static struct {
  71. timespec ts;
  72. int r;
  73. time_t ms;
  74. } s = { {}, clock_gettime(CLOCK_MONOTONIC, &s.ts), s.ts.tv_sec * 1000 + s.ts.tv_nsec / 1000000 };
  75. timespec ts;
  76. clock_gettime(CLOCK_MONOTONIC, &ts);
  77. return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000) - s.ms;
  78. #endif
  79. }
  80. /*
  81. * Get a monotonically-increasing time in microseconds.
  82. */
  83. static inline
  84. uint64_t carla_gettime_us() noexcept
  85. {
  86. #if defined(CARLA_OS_MAC)
  87. static const uint64_t s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000;
  88. return (clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000) - s;
  89. #elif defined(CARLA_OS_WIN)
  90. static struct {
  91. LARGE_INTEGER freq;
  92. LARGE_INTEGER counter;
  93. BOOL r1, r2;
  94. } s = { {}, {}, QueryPerformanceFrequency(&s.freq), QueryPerformanceCounter(&s.counter) };
  95. LARGE_INTEGER counter;
  96. QueryPerformanceCounter(&counter);
  97. return (counter.QuadPart - s.counter.QuadPart) * 1000000 / s.freq.QuadPart;
  98. #else
  99. static struct {
  100. timespec ts;
  101. int r;
  102. uint64_t us;
  103. } s = { {}, clock_gettime(CLOCK_MONOTONIC, &s.ts), static_cast<uint64_t>(s.ts.tv_sec * 1000000 +
  104. s.ts.tv_nsec / 1000) };
  105. timespec ts;
  106. clock_gettime(CLOCK_MONOTONIC, &ts);
  107. return (ts.tv_sec * 1000000 + ts.tv_nsec / 1000) - s.us;
  108. #endif
  109. }
  110. /*
  111. * Get a monotonically-increasing time in nanoseconds.
  112. */
  113. static inline
  114. uint64_t carla_gettime_ns() noexcept
  115. {
  116. #if defined(CARLA_OS_MAC)
  117. static const uint64_t s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
  118. return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) - s;
  119. #elif defined(CARLA_OS_WIN)
  120. static struct {
  121. LARGE_INTEGER freq;
  122. LARGE_INTEGER counter;
  123. BOOL r1, r2;
  124. } s = { {}, {}, QueryPerformanceFrequency(&s.freq), QueryPerformanceCounter(&s.counter) };
  125. LARGE_INTEGER counter;
  126. QueryPerformanceCounter(&counter);
  127. return (counter.QuadPart - s.counter.QuadPart) * 1000000000ULL / s.freq.QuadPart;
  128. #else
  129. static struct {
  130. timespec ts;
  131. int r;
  132. uint64_t ns;
  133. } s = { {}, clock_gettime(CLOCK_MONOTONIC, &s.ts), static_cast<uint64_t>(s.ts.tv_sec * 1000000000ULL +
  134. s.ts.tv_nsec) };
  135. timespec ts;
  136. clock_gettime(CLOCK_MONOTONIC, &ts);
  137. return (ts.tv_sec * 1000000000ULL + ts.tv_nsec) - s.ns;
  138. #endif
  139. }
  140. // --------------------------------------------------------------------------------------------------------------------
  141. #endif // CARLA_TIME_UTILS_HPP_INCLUDED