SDL_systimer.c 5.3 KB

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