SDL_systimer.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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 HAVE_NANOSLEEP || 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. #if 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. #if 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
  74. SDL_GetPerformanceCounter(void)
  75. {
  76. Uint64 ticks;
  77. if (!checked_monotonic_time) {
  78. CheckMonotonicTime();
  79. }
  80. if (has_monotonic_time) {
  81. #if HAVE_CLOCK_GETTIME
  82. struct timespec now;
  83. clock_gettime(SDL_MONOTONIC_CLOCK, &now);
  84. ticks = now.tv_sec;
  85. ticks *= SDL_NS_PER_SECOND;
  86. ticks += now.tv_nsec;
  87. #elif defined(__APPLE__)
  88. ticks = mach_absolute_time();
  89. #else
  90. SDL_assert(SDL_FALSE);
  91. ticks = 0;
  92. #endif
  93. } else {
  94. struct timeval now;
  95. gettimeofday(&now, NULL);
  96. ticks = now.tv_sec;
  97. ticks *= SDL_US_PER_SECOND;
  98. ticks += now.tv_usec;
  99. }
  100. return ticks;
  101. }
  102. Uint64
  103. SDL_GetPerformanceFrequency(void)
  104. {
  105. if (!checked_monotonic_time) {
  106. CheckMonotonicTime();
  107. }
  108. if (has_monotonic_time) {
  109. #if HAVE_CLOCK_GETTIME
  110. return SDL_NS_PER_SECOND;
  111. #elif defined(__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_DelayNS(Uint64 ns)
  121. {
  122. int was_error;
  123. #if HAVE_NANOSLEEP
  124. struct timespec tv, remaining;
  125. #else
  126. struct timeval tv;
  127. Uint64 then, now, elapsed;
  128. #endif
  129. #ifdef __EMSCRIPTEN__
  130. if (emscripten_has_asyncify() && SDL_GetHintBoolean(SDL_HINT_EMSCRIPTEN_ASYNCIFY, SDL_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. #if 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. #if 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 */