SDL_timer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  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. #ifndef SDL_timer_h_
  19. #define SDL_timer_h_
  20. /**
  21. * # CategoryTimer
  22. *
  23. * SDL time management routines.
  24. */
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_error.h>
  27. #include <SDL3/SDL_begin_code.h>
  28. /* Set up for C function definitions, even when using C++ */
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /* SDL time constants */
  33. #define SDL_MS_PER_SECOND 1000
  34. #define SDL_US_PER_SECOND 1000000
  35. #define SDL_NS_PER_SECOND 1000000000LL
  36. #define SDL_NS_PER_MS 1000000
  37. #define SDL_NS_PER_US 1000
  38. #define SDL_SECONDS_TO_NS(S) (((Uint64)(S)) * SDL_NS_PER_SECOND)
  39. #define SDL_NS_TO_SECONDS(NS) ((NS) / SDL_NS_PER_SECOND)
  40. #define SDL_MS_TO_NS(MS) (((Uint64)(MS)) * SDL_NS_PER_MS)
  41. #define SDL_NS_TO_MS(NS) ((NS) / SDL_NS_PER_MS)
  42. #define SDL_US_TO_NS(US) (((Uint64)(US)) * SDL_NS_PER_US)
  43. #define SDL_NS_TO_US(NS) ((NS) / SDL_NS_PER_US)
  44. /**
  45. * Get the number of milliseconds since SDL library initialization.
  46. *
  47. * \returns an unsigned 64-bit value representing the number of milliseconds
  48. * since the SDL library initialized.
  49. *
  50. * \threadsafety It is safe to call this function from any thread.
  51. *
  52. * \since This function is available since SDL 3.1.3.
  53. */
  54. extern SDL_DECLSPEC Uint64 SDLCALL SDL_GetTicks(void);
  55. /**
  56. * Get the number of nanoseconds since SDL library initialization.
  57. *
  58. * \returns an unsigned 64-bit value representing the number of nanoseconds
  59. * since the SDL library initialized.
  60. *
  61. * \threadsafety It is safe to call this function from any thread.
  62. *
  63. * \since This function is available since SDL 3.1.3.
  64. */
  65. extern SDL_DECLSPEC Uint64 SDLCALL SDL_GetTicksNS(void);
  66. /**
  67. * Get the current value of the high resolution counter.
  68. *
  69. * This function is typically used for profiling.
  70. *
  71. * The counter values are only meaningful relative to each other. Differences
  72. * between values can be converted to times by using
  73. * SDL_GetPerformanceFrequency().
  74. *
  75. * \returns the current counter value.
  76. *
  77. * \threadsafety It is safe to call this function from any thread.
  78. *
  79. * \since This function is available since SDL 3.1.3.
  80. *
  81. * \sa SDL_GetPerformanceFrequency
  82. */
  83. extern SDL_DECLSPEC Uint64 SDLCALL SDL_GetPerformanceCounter(void);
  84. /**
  85. * Get the count per second of the high resolution counter.
  86. *
  87. * \returns a platform-specific count per second.
  88. *
  89. * \threadsafety It is safe to call this function from any thread.
  90. *
  91. * \since This function is available since SDL 3.1.3.
  92. *
  93. * \sa SDL_GetPerformanceCounter
  94. */
  95. extern SDL_DECLSPEC Uint64 SDLCALL SDL_GetPerformanceFrequency(void);
  96. /**
  97. * Wait a specified number of milliseconds before returning.
  98. *
  99. * This function waits a specified number of milliseconds before returning. It
  100. * waits at least the specified time, but possibly longer due to OS
  101. * scheduling.
  102. *
  103. * \param ms the number of milliseconds to delay.
  104. *
  105. * \threadsafety It is safe to call this function from any thread.
  106. *
  107. * \since This function is available since SDL 3.1.3.
  108. */
  109. extern SDL_DECLSPEC void SDLCALL SDL_Delay(Uint32 ms);
  110. /**
  111. * Wait a specified number of nanoseconds before returning.
  112. *
  113. * This function waits a specified number of nanoseconds before returning. It
  114. * waits at least the specified time, but possibly longer due to OS
  115. * scheduling.
  116. *
  117. * \param ns the number of nanoseconds to delay.
  118. *
  119. * \threadsafety It is safe to call this function from any thread.
  120. *
  121. * \since This function is available since SDL 3.1.3.
  122. */
  123. extern SDL_DECLSPEC void SDLCALL SDL_DelayNS(Uint64 ns);
  124. /**
  125. * Wait a specified number of nanoseconds before returning.
  126. *
  127. * This function waits a specified number of nanoseconds before returning. It
  128. * will attempt to wait as close to the requested time as possible, busy
  129. * waiting if necessary, but could return later due to OS scheduling.
  130. *
  131. * \param ns the number of nanoseconds to delay.
  132. *
  133. * \threadsafety It is safe to call this function from any thread.
  134. *
  135. * \since This function is available since SDL 3.2.0.
  136. */
  137. extern SDL_DECLSPEC void SDLCALL SDL_DelayPrecise(Uint64 ns);
  138. /**
  139. * Definition of the timer ID type.
  140. *
  141. * \since This datatype is available since SDL 3.1.3.
  142. */
  143. typedef Uint32 SDL_TimerID;
  144. /**
  145. * Function prototype for the millisecond timer callback function.
  146. *
  147. * The callback function is passed the current timer interval and returns the
  148. * next timer interval, in milliseconds. If the returned value is the same as
  149. * the one passed in, the periodic alarm continues, otherwise a new alarm is
  150. * scheduled. If the callback returns 0, the periodic alarm is canceled and
  151. * will be removed.
  152. *
  153. * \param userdata an arbitrary pointer provided by the app through
  154. * SDL_AddTimer, for its own use.
  155. * \param timerID the current timer being processed.
  156. * \param interval the current callback time interval.
  157. * \returns the new callback time interval, or 0 to disable further runs of
  158. * the callback.
  159. *
  160. * \threadsafety SDL may call this callback at any time from a background
  161. * thread; the application is responsible for locking resources
  162. * the callback touches that need to be protected.
  163. *
  164. * \since This datatype is available since SDL 3.1.3.
  165. *
  166. * \sa SDL_AddTimer
  167. */
  168. typedef Uint32 (SDLCALL *SDL_TimerCallback)(void *userdata, SDL_TimerID timerID, Uint32 interval);
  169. /**
  170. * Call a callback function at a future time.
  171. *
  172. * The callback function is passed the current timer interval and the user
  173. * supplied parameter from the SDL_AddTimer() call and should return the next
  174. * timer interval. If the value returned from the callback is 0, the timer is
  175. * canceled and will be removed.
  176. *
  177. * The callback is run on a separate thread, and for short timeouts can
  178. * potentially be called before this function returns.
  179. *
  180. * Timers take into account the amount of time it took to execute the
  181. * callback. For example, if the callback took 250 ms to execute and returned
  182. * 1000 (ms), the timer would only wait another 750 ms before its next
  183. * iteration.
  184. *
  185. * Timing may be inexact due to OS scheduling. Be sure to note the current
  186. * time with SDL_GetTicksNS() or SDL_GetPerformanceCounter() in case your
  187. * callback needs to adjust for variances.
  188. *
  189. * \param interval the timer delay, in milliseconds, passed to `callback`.
  190. * \param callback the SDL_TimerCallback function to call when the specified
  191. * `interval` elapses.
  192. * \param userdata a pointer that is passed to `callback`.
  193. * \returns a timer ID or 0 on failure; call SDL_GetError() for more
  194. * information.
  195. *
  196. * \threadsafety It is safe to call this function from any thread.
  197. *
  198. * \since This function is available since SDL 3.1.3.
  199. *
  200. * \sa SDL_AddTimerNS
  201. * \sa SDL_RemoveTimer
  202. */
  203. extern SDL_DECLSPEC SDL_TimerID SDLCALL SDL_AddTimer(Uint32 interval, SDL_TimerCallback callback, void *userdata);
  204. /**
  205. * Function prototype for the nanosecond timer callback function.
  206. *
  207. * The callback function is passed the current timer interval and returns the
  208. * next timer interval, in nanoseconds. If the returned value is the same as
  209. * the one passed in, the periodic alarm continues, otherwise a new alarm is
  210. * scheduled. If the callback returns 0, the periodic alarm is canceled and
  211. * will be removed.
  212. *
  213. * \param userdata an arbitrary pointer provided by the app through
  214. * SDL_AddTimer, for its own use.
  215. * \param timerID the current timer being processed.
  216. * \param interval the current callback time interval.
  217. * \returns the new callback time interval, or 0 to disable further runs of
  218. * the callback.
  219. *
  220. * \threadsafety SDL may call this callback at any time from a background
  221. * thread; the application is responsible for locking resources
  222. * the callback touches that need to be protected.
  223. *
  224. * \since This datatype is available since SDL 3.1.3.
  225. *
  226. * \sa SDL_AddTimerNS
  227. */
  228. typedef Uint64 (SDLCALL *SDL_NSTimerCallback)(void *userdata, SDL_TimerID timerID, Uint64 interval);
  229. /**
  230. * Call a callback function at a future time.
  231. *
  232. * The callback function is passed the current timer interval and the user
  233. * supplied parameter from the SDL_AddTimerNS() call and should return the
  234. * next timer interval. If the value returned from the callback is 0, the
  235. * timer is canceled and will be removed.
  236. *
  237. * The callback is run on a separate thread, and for short timeouts can
  238. * potentially be called before this function returns.
  239. *
  240. * Timers take into account the amount of time it took to execute the
  241. * callback. For example, if the callback took 250 ns to execute and returned
  242. * 1000 (ns), the timer would only wait another 750 ns before its next
  243. * iteration.
  244. *
  245. * Timing may be inexact due to OS scheduling. Be sure to note the current
  246. * time with SDL_GetTicksNS() or SDL_GetPerformanceCounter() in case your
  247. * callback needs to adjust for variances.
  248. *
  249. * \param interval the timer delay, in nanoseconds, passed to `callback`.
  250. * \param callback the SDL_TimerCallback function to call when the specified
  251. * `interval` elapses.
  252. * \param userdata a pointer that is passed to `callback`.
  253. * \returns a timer ID or 0 on failure; call SDL_GetError() for more
  254. * information.
  255. *
  256. * \threadsafety It is safe to call this function from any thread.
  257. *
  258. * \since This function is available since SDL 3.1.3.
  259. *
  260. * \sa SDL_AddTimer
  261. * \sa SDL_RemoveTimer
  262. */
  263. extern SDL_DECLSPEC SDL_TimerID SDLCALL SDL_AddTimerNS(Uint64 interval, SDL_NSTimerCallback callback, void *userdata);
  264. /**
  265. * Remove a timer created with SDL_AddTimer().
  266. *
  267. * \param id the ID of the timer to remove.
  268. * \returns true on success or false on failure; call SDL_GetError() for more
  269. * information.
  270. *
  271. * \threadsafety It is safe to call this function from any thread.
  272. *
  273. * \since This function is available since SDL 3.1.3.
  274. *
  275. * \sa SDL_AddTimer
  276. */
  277. extern SDL_DECLSPEC bool SDLCALL SDL_RemoveTimer(SDL_TimerID id);
  278. /* Ends C function definitions when using C++ */
  279. #ifdef __cplusplus
  280. }
  281. #endif
  282. #include <SDL3/SDL_close_code.h>
  283. #endif /* SDL_timer_h_ */