SDL_systimer.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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 __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 __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. #ifdef CLOCK_MONOTONIC_RAW
  48. #define SDL_MONOTONIC_CLOCK CLOCK_MONOTONIC_RAW
  49. #else
  50. #define SDL_MONOTONIC_CLOCK CLOCK_MONOTONIC
  51. #endif
  52. #endif
  53. /* The first ticks value of the application */
  54. #if !defined(HAVE_CLOCK_GETTIME) && defined(__APPLE__)
  55. mach_timebase_info_data_t mach_base_info;
  56. #endif
  57. static SDL_bool checked_monotonic_time = SDL_FALSE;
  58. static SDL_bool has_monotonic_time = SDL_FALSE;
  59. static void CheckMonotonicTime(void)
  60. {
  61. #ifdef HAVE_CLOCK_GETTIME
  62. struct timespec value;
  63. if (clock_gettime(SDL_MONOTONIC_CLOCK, &value) == 0) {
  64. has_monotonic_time = SDL_TRUE;
  65. } else
  66. #elif defined(__APPLE__)
  67. if (mach_timebase_info(&mach_base_info) == 0) {
  68. has_monotonic_time = SDL_TRUE;
  69. }
  70. #endif
  71. checked_monotonic_time = SDL_TRUE;
  72. }
  73. Uint64 SDL_GetPerformanceCounter(void)
  74. {
  75. Uint64 ticks;
  76. if (!checked_monotonic_time) {
  77. CheckMonotonicTime();
  78. }
  79. if (has_monotonic_time) {
  80. #ifdef HAVE_CLOCK_GETTIME
  81. struct timespec now;
  82. clock_gettime(SDL_MONOTONIC_CLOCK, &now);
  83. ticks = now.tv_sec;
  84. ticks *= SDL_NS_PER_SECOND;
  85. ticks += now.tv_nsec;
  86. #elif defined(__APPLE__)
  87. ticks = mach_absolute_time();
  88. #else
  89. SDL_assert(SDL_FALSE);
  90. ticks = 0;
  91. #endif
  92. } else {
  93. struct timeval now;
  94. gettimeofday(&now, NULL);
  95. ticks = now.tv_sec;
  96. ticks *= SDL_US_PER_SECOND;
  97. ticks += now.tv_usec;
  98. }
  99. return ticks;
  100. }
  101. Uint64 SDL_GetPerformanceFrequency(void)
  102. {
  103. if (!checked_monotonic_time) {
  104. CheckMonotonicTime();
  105. }
  106. if (has_monotonic_time) {
  107. #ifdef HAVE_CLOCK_GETTIME
  108. return SDL_NS_PER_SECOND;
  109. #elif defined(__APPLE__)
  110. Uint64 freq = mach_base_info.denom;
  111. freq *= SDL_NS_PER_SECOND;
  112. freq /= mach_base_info.numer;
  113. return freq;
  114. #endif
  115. }
  116. return SDL_US_PER_SECOND;
  117. }
  118. void SDL_DelayNS(Uint64 ns)
  119. {
  120. int was_error;
  121. #ifdef HAVE_NANOSLEEP
  122. struct timespec tv, remaining;
  123. #else
  124. struct timeval tv;
  125. Uint64 then, now, elapsed;
  126. #endif
  127. #ifdef __EMSCRIPTEN__
  128. if (emscripten_has_asyncify() && SDL_GetHintBoolean(SDL_HINT_EMSCRIPTEN_ASYNCIFY, SDL_TRUE)) {
  129. /* pseudo-synchronous pause, used directly or through e.g. SDL_WaitEvent */
  130. emscripten_sleep(ns / SDL_NS_PER_MS);
  131. return;
  132. }
  133. #endif
  134. /* Set the timeout interval */
  135. #ifdef HAVE_NANOSLEEP
  136. remaining.tv_sec = (time_t)(ns / SDL_NS_PER_SECOND);
  137. remaining.tv_nsec = (long)(ns % SDL_NS_PER_SECOND);
  138. #else
  139. then = SDL_GetTicksNS();
  140. #endif
  141. do {
  142. errno = 0;
  143. #ifdef HAVE_NANOSLEEP
  144. tv.tv_sec = remaining.tv_sec;
  145. tv.tv_nsec = remaining.tv_nsec;
  146. was_error = nanosleep(&tv, &remaining);
  147. #else
  148. /* Calculate the time interval left (in case of interrupt) */
  149. now = SDL_GetTicksNS();
  150. elapsed = (now - then);
  151. then = now;
  152. if (elapsed >= ns) {
  153. break;
  154. }
  155. ns -= elapsed;
  156. tv.tv_sec = (ns / SDL_NS_PER_SECOND);
  157. tv.tv_usec = SDL_NS_TO_US(ns % SDL_NS_PER_SECOND);
  158. was_error = select(0, NULL, NULL, NULL, &tv);
  159. #endif /* HAVE_NANOSLEEP */
  160. } while (was_error && (errno == EINTR));
  161. }
  162. #endif /* SDL_TIMER_UNIX */