SDL_systimer.c 5.6 KB

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