SDL_systimer.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_TIMER_UNIX
  20. #include <stdio.h>
  21. #include <sys/time.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include "../SDL_timer_c.h"
  25. #ifdef SDL_PLATFORM_EMSCRIPTEN
  26. #include <emscripten.h>
  27. #endif
  28. /* The clock_gettime provides monotonous time, so we should use it if
  29. it's available. The clock_gettime function is behind ifdef
  30. for __USE_POSIX199309
  31. Tommi Kyntola ([email protected]) 27/09/2005
  32. */
  33. /* Reworked monotonic clock to not assume the current system has one
  34. as not all linux kernels provide a monotonic clock (yeah recent ones
  35. probably do)
  36. Also added macOS Monotonic clock support
  37. Based on work in https://github.com/ThomasHabets/monotonic_clock
  38. */
  39. #if defined(HAVE_NANOSLEEP) || defined(HAVE_CLOCK_GETTIME)
  40. #include <time.h>
  41. #endif
  42. #ifdef SDL_PLATFORM_APPLE
  43. #include <mach/mach_time.h>
  44. #endif
  45. // Use CLOCK_MONOTONIC_RAW, if available, which is not subject to adjustment by NTP
  46. #ifdef HAVE_CLOCK_GETTIME
  47. // Older Android phones have a buggy CLOCK_MONOTONIC_RAW, use CLOCK_MONOTONIC
  48. // See fix: https://github.com/torvalds/linux/commit/dbb236c1ceb697a559e0694ac4c9e7b9131d0b16
  49. #if defined(CLOCK_MONOTONIC_RAW) && !defined(__ANDROID__)
  50. #define SDL_MONOTONIC_CLOCK CLOCK_MONOTONIC_RAW
  51. #else
  52. #define SDL_MONOTONIC_CLOCK CLOCK_MONOTONIC
  53. #endif
  54. #endif
  55. // The first ticks value of the application
  56. #if !defined(HAVE_CLOCK_GETTIME) && defined(SDL_PLATFORM_APPLE)
  57. mach_timebase_info_data_t mach_base_info;
  58. #endif
  59. static bool checked_monotonic_time = false;
  60. static bool has_monotonic_time = false;
  61. static void CheckMonotonicTime(void)
  62. {
  63. #ifdef HAVE_CLOCK_GETTIME
  64. struct timespec value;
  65. if (clock_gettime(SDL_MONOTONIC_CLOCK, &value) == 0) {
  66. has_monotonic_time = true;
  67. }
  68. #elif defined(SDL_PLATFORM_APPLE)
  69. if (mach_timebase_info(&mach_base_info) == 0) {
  70. has_monotonic_time = true;
  71. }
  72. #endif
  73. checked_monotonic_time = true;
  74. }
  75. Uint64 SDL_GetPerformanceCounter(void)
  76. {
  77. Uint64 ticks;
  78. if (!checked_monotonic_time) {
  79. CheckMonotonicTime();
  80. }
  81. if (has_monotonic_time) {
  82. #ifdef HAVE_CLOCK_GETTIME
  83. struct timespec now;
  84. clock_gettime(SDL_MONOTONIC_CLOCK, &now);
  85. ticks = now.tv_sec;
  86. ticks *= SDL_NS_PER_SECOND;
  87. ticks += now.tv_nsec;
  88. #elif defined(SDL_PLATFORM_APPLE)
  89. ticks = mach_absolute_time();
  90. #else
  91. SDL_assert(false);
  92. ticks = 0;
  93. #endif
  94. } else {
  95. struct timeval now;
  96. gettimeofday(&now, NULL);
  97. ticks = now.tv_sec;
  98. ticks *= SDL_US_PER_SECOND;
  99. ticks += now.tv_usec;
  100. }
  101. return ticks;
  102. }
  103. Uint64 SDL_GetPerformanceFrequency(void)
  104. {
  105. if (!checked_monotonic_time) {
  106. CheckMonotonicTime();
  107. }
  108. if (has_monotonic_time) {
  109. #ifdef HAVE_CLOCK_GETTIME
  110. return SDL_NS_PER_SECOND;
  111. #elif defined(SDL_PLATFORM_APPLE)
  112. Uint64 freq = mach_base_info.denom;
  113. freq *= SDL_NS_PER_SECOND;
  114. freq /= mach_base_info.numer;
  115. return freq;
  116. #endif
  117. }
  118. return SDL_US_PER_SECOND;
  119. }
  120. void SDL_SYS_DelayNS(Uint64 ns)
  121. {
  122. int was_error;
  123. #ifdef HAVE_NANOSLEEP
  124. struct timespec tv, remaining;
  125. #else
  126. struct timeval tv;
  127. Uint64 then, now, elapsed;
  128. #endif
  129. #ifdef SDL_PLATFORM_EMSCRIPTEN
  130. if (emscripten_has_asyncify() && SDL_GetHintBoolean(SDL_HINT_EMSCRIPTEN_ASYNCIFY, true)) {
  131. // pseudo-synchronous pause, used directly or through e.g. SDL_WaitEvent
  132. emscripten_sleep(ns / SDL_NS_PER_MS);
  133. return;
  134. }
  135. #endif
  136. // Set the timeout interval
  137. #ifdef HAVE_NANOSLEEP
  138. remaining.tv_sec = (time_t)(ns / SDL_NS_PER_SECOND);
  139. remaining.tv_nsec = (long)(ns % SDL_NS_PER_SECOND);
  140. #else
  141. then = SDL_GetTicksNS();
  142. #endif
  143. do {
  144. errno = 0;
  145. #ifdef HAVE_NANOSLEEP
  146. tv.tv_sec = remaining.tv_sec;
  147. tv.tv_nsec = remaining.tv_nsec;
  148. was_error = nanosleep(&tv, &remaining);
  149. #else
  150. // Calculate the time interval left (in case of interrupt)
  151. now = SDL_GetTicksNS();
  152. elapsed = (now - then);
  153. then = now;
  154. if (elapsed >= ns) {
  155. break;
  156. }
  157. ns -= elapsed;
  158. tv.tv_sec = (ns / SDL_NS_PER_SECOND);
  159. tv.tv_usec = SDL_NS_TO_US(ns % SDL_NS_PER_SECOND);
  160. was_error = select(0, NULL, NULL, NULL, &tv);
  161. #endif // HAVE_NANOSLEEP
  162. } while (was_error && (errno == EINTR));
  163. }
  164. #endif // SDL_TIMER_UNIX