SDL_systimer.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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.h"
  25. #include "SDL_hints.h"
  26. #include "../SDL_timer_c.h"
  27. #ifdef __EMSCRIPTEN__
  28. #include <emscripten.h>
  29. #endif
  30. /* The clock_gettime provides monotonous time, so we should use it if
  31. it's available. The clock_gettime function is behind ifdef
  32. for __USE_POSIX199309
  33. Tommi Kyntola ([email protected]) 27/09/2005
  34. */
  35. /* Reworked monotonic clock to not assume the current system has one
  36. as not all linux kernels provide a monotonic clock (yeah recent ones
  37. probably do)
  38. Also added OS X Monotonic clock support
  39. Based on work in https://github.com/ThomasHabets/monotonic_clock
  40. */
  41. #if HAVE_NANOSLEEP || HAVE_CLOCK_GETTIME || HAVE_CLOCK_GETTIME_NSEC_NP
  42. #include <time.h>
  43. #endif
  44. #ifdef __APPLE__
  45. #include <mach/mach_time.h>
  46. #endif
  47. /* Use CLOCK_MONOTONIC_RAW, if available, which is not subject to adjustment by NTP */
  48. #if HAVE_CLOCK_GETTIME || HAVE_CLOCK_GETTIME_NSEC_NP
  49. #ifdef CLOCK_MONOTONIC_RAW
  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 HAVE_CLOCK_GETTIME
  57. static struct timespec start_ts;
  58. #elif defined(__APPLE__) && !HAVE_CLOCK_GETTIME_NSEC_NP
  59. static uint64_t start_mach;
  60. mach_timebase_info_data_t mach_base_info;
  61. #endif
  62. static SDL_bool has_monotonic_time = SDL_FALSE;
  63. static struct timeval start_tv;
  64. static SDL_bool ticks_started = SDL_FALSE;
  65. void
  66. SDL_TicksInit(void)
  67. {
  68. if (ticks_started) {
  69. return;
  70. }
  71. ticks_started = SDL_TRUE;
  72. /* Set first ticks value */
  73. #if HAVE_CLOCK_GETTIME
  74. if (clock_gettime(SDL_MONOTONIC_CLOCK, &start_ts) == 0) {
  75. has_monotonic_time = SDL_TRUE;
  76. } else
  77. #elif defined(__APPLE__)
  78. #if !HAVE_CLOCK_GETTIME_NSEC_NP
  79. kern_return_t ret = mach_timebase_info(&mach_base_info);
  80. if (ret == 0) {
  81. has_monotonic_time = SDL_TRUE;
  82. start_mach = mach_absolute_time();
  83. } else
  84. #else
  85. if (clock_gettime_nsec_np(SDL_MONOTONIC_CLOCK) > 0) {
  86. has_monotonic_time = SDL_TRUE;
  87. } else
  88. #endif
  89. #endif
  90. {
  91. gettimeofday(&start_tv, NULL);
  92. }
  93. }
  94. void
  95. SDL_TicksQuit(void)
  96. {
  97. ticks_started = SDL_FALSE;
  98. }
  99. Uint32
  100. SDL_GetTicks(void)
  101. {
  102. Uint32 ticks;
  103. if (!ticks_started) {
  104. SDL_TicksInit();
  105. }
  106. if (has_monotonic_time) {
  107. #if HAVE_CLOCK_GETTIME
  108. struct timespec now;
  109. clock_gettime(SDL_MONOTONIC_CLOCK, &now);
  110. ticks = (Uint32)((now.tv_sec - start_ts.tv_sec) * 1000 + (now.tv_nsec - start_ts.tv_nsec) / 1000000);
  111. #elif defined(__APPLE__)
  112. #if !HAVE_CLOCK_GETTIME_NSEC_NP
  113. uint64_t now = mach_absolute_time();
  114. ticks = (Uint32)((((now - start_mach) * mach_base_info.numer) / mach_base_info.denom) / 1000000);
  115. #else
  116. ticks = (Uint32)clock_gettime_nsec_np(SDL_MONOTONIC_CLOCK);
  117. #endif
  118. #else
  119. SDL_assert(SDL_FALSE);
  120. ticks = 0;
  121. #endif
  122. } else {
  123. struct timeval now;
  124. gettimeofday(&now, NULL);
  125. ticks = (Uint32)((now.tv_sec - start_tv.tv_sec) * 1000 + (now.tv_usec - start_tv.tv_usec) / 1000);
  126. }
  127. return (ticks);
  128. }
  129. Uint64
  130. SDL_GetPerformanceCounter(void)
  131. {
  132. Uint64 ticks;
  133. if (!ticks_started) {
  134. SDL_TicksInit();
  135. }
  136. if (has_monotonic_time) {
  137. #if HAVE_CLOCK_GETTIME
  138. struct timespec now;
  139. clock_gettime(SDL_MONOTONIC_CLOCK, &now);
  140. ticks = now.tv_sec;
  141. ticks *= 1000000000;
  142. ticks += now.tv_nsec;
  143. #elif defined(__APPLE__)
  144. #if !HAVE_CLOCK_GETTIME_NSEC_NP
  145. ticks = mach_absolute_time();
  146. #else
  147. ticks = (Uint32)clock_gettime_nsec_np(SDL_MONOTONIC_CLOCK);
  148. #endif
  149. #else
  150. SDL_assert(SDL_FALSE);
  151. ticks = 0;
  152. #endif
  153. } else {
  154. struct timeval now;
  155. gettimeofday(&now, NULL);
  156. ticks = now.tv_sec;
  157. ticks *= 1000000;
  158. ticks += now.tv_usec;
  159. }
  160. return (ticks);
  161. }
  162. Uint64
  163. SDL_GetPerformanceFrequency(void)
  164. {
  165. if (!ticks_started) {
  166. SDL_TicksInit();
  167. }
  168. if (has_monotonic_time) {
  169. #if HAVE_CLOCK_GETTIME
  170. return 1000000000;
  171. #elif defined(__APPLE__)
  172. #if !HAVE_CLOCK_GETTIME_NSEC_NP
  173. Uint64 freq = mach_base_info.denom;
  174. freq *= 1000000000;
  175. freq /= mach_base_info.numer;
  176. return freq;
  177. #else
  178. return 1000000000;
  179. #endif
  180. #endif
  181. }
  182. return 1000000;
  183. }
  184. void
  185. SDL_Delay(Uint32 ms)
  186. {
  187. #ifdef __EMSCRIPTEN__
  188. if (emscripten_has_asyncify() && SDL_GetHintBoolean(SDL_HINT_EMSCRIPTEN_ASYNCIFY, SDL_TRUE)) {
  189. /* pseudo-synchronous pause, used directly or through e.g. SDL_WaitEvent */
  190. emscripten_sleep(ms);
  191. return;
  192. }
  193. #endif
  194. int was_error;
  195. #if HAVE_NANOSLEEP
  196. struct timespec elapsed, tv;
  197. #else
  198. struct timeval tv;
  199. Uint32 then, now, elapsed;
  200. #endif
  201. /* Set the timeout interval */
  202. #if HAVE_NANOSLEEP
  203. elapsed.tv_sec = ms / 1000;
  204. elapsed.tv_nsec = (ms % 1000) * 1000000;
  205. #else
  206. then = SDL_GetTicks();
  207. #endif
  208. do {
  209. errno = 0;
  210. #if HAVE_NANOSLEEP
  211. tv.tv_sec = elapsed.tv_sec;
  212. tv.tv_nsec = elapsed.tv_nsec;
  213. was_error = nanosleep(&tv, &elapsed);
  214. #else
  215. /* Calculate the time interval left (in case of interrupt) */
  216. now = SDL_GetTicks();
  217. elapsed = (now - then);
  218. then = now;
  219. if (elapsed >= ms) {
  220. break;
  221. }
  222. ms -= elapsed;
  223. tv.tv_sec = ms / 1000;
  224. tv.tv_usec = (ms % 1000) * 1000;
  225. was_error = select(0, NULL, NULL, NULL, &tv);
  226. #endif /* HAVE_NANOSLEEP */
  227. } while (was_error && (errno == EINTR));
  228. }
  229. #endif /* SDL_TIMER_UNIX */
  230. /* vi: set ts=4 sw=4 expandtab: */