SDL_timer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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. /**
  34. * Number of milliseconds in a second.
  35. *
  36. * This is always 1000.
  37. *
  38. * \since This macro is available since SDL 3.1.3.
  39. */
  40. #define SDL_MS_PER_SECOND 1000
  41. /**
  42. * Number of microseconds in a second.
  43. *
  44. * This is always 1000000.
  45. *
  46. * \since This macro is available since SDL 3.1.3.
  47. */
  48. #define SDL_US_PER_SECOND 1000000
  49. /**
  50. * Number of nanoseconds in a second.
  51. *
  52. * This is always 1000000000.
  53. *
  54. * \since This macro is available since SDL 3.1.3.
  55. */
  56. #define SDL_NS_PER_SECOND 1000000000LL
  57. /**
  58. * Number of nanoseconds in a millisecond.
  59. *
  60. * This is always 1000000.
  61. *
  62. * \since This macro is available since SDL 3.1.3.
  63. */
  64. #define SDL_NS_PER_MS 1000000
  65. /**
  66. * Number of nanoseconds in a microsecond.
  67. *
  68. * This is always 1000.
  69. *
  70. * \since This macro is available since SDL 3.1.3.
  71. */
  72. #define SDL_NS_PER_US 1000
  73. /**
  74. * Convert seconds to nanoseconds.
  75. *
  76. * This only converts whole numbers, not fractional seconds.
  77. *
  78. * \param S the number of seconds to convert.
  79. * \returns S, expressed in nanoseconds.
  80. *
  81. * \threadsafety It is safe to call this macro from any thread.
  82. *
  83. * \since This macro is available since SDL 3.1.3.
  84. */
  85. #define SDL_SECONDS_TO_NS(S) (((Uint64)(S)) * SDL_NS_PER_SECOND)
  86. /**
  87. * Convert nanoseconds to seconds.
  88. *
  89. * This performs a division, so the results can be dramatically different if
  90. * `NS` is an integer or floating point value.
  91. *
  92. * \param NS the number of nanoseconds to convert.
  93. * \returns NS, expressed in seconds.
  94. *
  95. * \threadsafety It is safe to call this macro from any thread.
  96. *
  97. * \since This macro is available since SDL 3.1.3.
  98. */
  99. #define SDL_NS_TO_SECONDS(NS) ((NS) / SDL_NS_PER_SECOND)
  100. /**
  101. * Convert milliseconds to nanoseconds.
  102. *
  103. * This only converts whole numbers, not fractional milliseconds.
  104. *
  105. * \param MS the number of milliseconds to convert.
  106. * \returns MS, expressed in nanoseconds.
  107. *
  108. * \threadsafety It is safe to call this macro from any thread.
  109. *
  110. * \since This macro is available since SDL 3.1.3.
  111. */
  112. #define SDL_MS_TO_NS(MS) (((Uint64)(MS)) * SDL_NS_PER_MS)
  113. /**
  114. * Convert nanoseconds to milliseconds.
  115. *
  116. * This performs a division, so the results can be dramatically different if
  117. * `NS` is an integer or floating point value.
  118. *
  119. * \param NS the number of nanoseconds to convert.
  120. * \returns NS, expressed in milliseconds.
  121. *
  122. * \threadsafety It is safe to call this macro from any thread.
  123. *
  124. * \since This macro is available since SDL 3.1.3.
  125. */
  126. #define SDL_NS_TO_MS(NS) ((NS) / SDL_NS_PER_MS)
  127. /**
  128. * Convert microseconds to nanoseconds.
  129. *
  130. * This only converts whole numbers, not fractional microseconds.
  131. *
  132. * \param US the number of microseconds to convert.
  133. * \returns US, expressed in nanoseconds.
  134. *
  135. * \threadsafety It is safe to call this macro from any thread.
  136. *
  137. * \since This macro is available since SDL 3.1.3.
  138. */
  139. #define SDL_US_TO_NS(US) (((Uint64)(US)) * SDL_NS_PER_US)
  140. /**
  141. * Convert nanoseconds to microseconds.
  142. *
  143. * This performs a division, so the results can be dramatically different if
  144. * `NS` is an integer or floating point value.
  145. *
  146. * \param NS the number of nanoseconds to convert.
  147. * \returns NS, expressed in microseconds.
  148. *
  149. * \threadsafety It is safe to call this macro from any thread.
  150. *
  151. * \since This macro is available since SDL 3.1.3.
  152. */
  153. #define SDL_NS_TO_US(NS) ((NS) / SDL_NS_PER_US)
  154. /**
  155. * Get the number of milliseconds since SDL library initialization.
  156. *
  157. * \returns an unsigned 64-bit value representing the number of milliseconds
  158. * since the SDL library initialized.
  159. *
  160. * \threadsafety It is safe to call this function from any thread.
  161. *
  162. * \since This function is available since SDL 3.1.3.
  163. */
  164. extern SDL_DECLSPEC Uint64 SDLCALL SDL_GetTicks(void);
  165. /**
  166. * Get the number of nanoseconds since SDL library initialization.
  167. *
  168. * \returns an unsigned 64-bit value representing the number of nanoseconds
  169. * since the SDL library initialized.
  170. *
  171. * \threadsafety It is safe to call this function from any thread.
  172. *
  173. * \since This function is available since SDL 3.1.3.
  174. */
  175. extern SDL_DECLSPEC Uint64 SDLCALL SDL_GetTicksNS(void);
  176. /**
  177. * Get the current value of the high resolution counter.
  178. *
  179. * This function is typically used for profiling.
  180. *
  181. * The counter values are only meaningful relative to each other. Differences
  182. * between values can be converted to times by using
  183. * SDL_GetPerformanceFrequency().
  184. *
  185. * \returns the current counter value.
  186. *
  187. * \threadsafety It is safe to call this function from any thread.
  188. *
  189. * \since This function is available since SDL 3.1.3.
  190. *
  191. * \sa SDL_GetPerformanceFrequency
  192. */
  193. extern SDL_DECLSPEC Uint64 SDLCALL SDL_GetPerformanceCounter(void);
  194. /**
  195. * Get the count per second of the high resolution counter.
  196. *
  197. * \returns a platform-specific count per second.
  198. *
  199. * \threadsafety It is safe to call this function from any thread.
  200. *
  201. * \since This function is available since SDL 3.1.3.
  202. *
  203. * \sa SDL_GetPerformanceCounter
  204. */
  205. extern SDL_DECLSPEC Uint64 SDLCALL SDL_GetPerformanceFrequency(void);
  206. /**
  207. * Wait a specified number of milliseconds before returning.
  208. *
  209. * This function waits a specified number of milliseconds before returning. It
  210. * waits at least the specified time, but possibly longer due to OS
  211. * scheduling.
  212. *
  213. * \param ms the number of milliseconds to delay.
  214. *
  215. * \threadsafety It is safe to call this function from any thread.
  216. *
  217. * \since This function is available since SDL 3.1.3.
  218. */
  219. extern SDL_DECLSPEC void SDLCALL SDL_Delay(Uint32 ms);
  220. /**
  221. * Wait a specified number of nanoseconds before returning.
  222. *
  223. * This function waits a specified number of nanoseconds before returning. It
  224. * waits at least the specified time, but possibly longer due to OS
  225. * scheduling.
  226. *
  227. * \param ns the number of nanoseconds to delay.
  228. *
  229. * \threadsafety It is safe to call this function from any thread.
  230. *
  231. * \since This function is available since SDL 3.1.3.
  232. */
  233. extern SDL_DECLSPEC void SDLCALL SDL_DelayNS(Uint64 ns);
  234. /**
  235. * Wait a specified number of nanoseconds before returning.
  236. *
  237. * This function waits a specified number of nanoseconds before returning. It
  238. * will attempt to wait as close to the requested time as possible, busy
  239. * waiting if necessary, but could return later due to OS scheduling.
  240. *
  241. * \param ns the number of nanoseconds to delay.
  242. *
  243. * \threadsafety It is safe to call this function from any thread.
  244. *
  245. * \since This function is available since SDL 3.1.6.
  246. */
  247. extern SDL_DECLSPEC void SDLCALL SDL_DelayPrecise(Uint64 ns);
  248. /**
  249. * Definition of the timer ID type.
  250. *
  251. * \since This datatype is available since SDL 3.1.3.
  252. */
  253. typedef Uint32 SDL_TimerID;
  254. /**
  255. * Function prototype for the millisecond timer callback function.
  256. *
  257. * The callback function is passed the current timer interval and returns the
  258. * next timer interval, in milliseconds. If the returned value is the same as
  259. * the one passed in, the periodic alarm continues, otherwise a new alarm is
  260. * scheduled. If the callback returns 0, the periodic alarm is canceled and
  261. * will be removed.
  262. *
  263. * \param userdata an arbitrary pointer provided by the app through
  264. * SDL_AddTimer, for its own use.
  265. * \param timerID the current timer being processed.
  266. * \param interval the current callback time interval.
  267. * \returns the new callback time interval, or 0 to disable further runs of
  268. * the callback.
  269. *
  270. * \threadsafety SDL may call this callback at any time from a background
  271. * thread; the application is responsible for locking resources
  272. * the callback touches that need to be protected.
  273. *
  274. * \since This datatype is available since SDL 3.1.3.
  275. *
  276. * \sa SDL_AddTimer
  277. */
  278. typedef Uint32 (SDLCALL *SDL_TimerCallback)(void *userdata, SDL_TimerID timerID, Uint32 interval);
  279. /**
  280. * Call a callback function at a future time.
  281. *
  282. * The callback function is passed the current timer interval and the user
  283. * supplied parameter from the SDL_AddTimer() call and should return the next
  284. * timer interval. If the value returned from the callback is 0, the timer is
  285. * canceled and will be removed.
  286. *
  287. * The callback is run on a separate thread, and for short timeouts can
  288. * potentially be called before this function returns.
  289. *
  290. * Timers take into account the amount of time it took to execute the
  291. * callback. For example, if the callback took 250 ms to execute and returned
  292. * 1000 (ms), the timer would only wait another 750 ms before its next
  293. * iteration.
  294. *
  295. * Timing may be inexact due to OS scheduling. Be sure to note the current
  296. * time with SDL_GetTicksNS() or SDL_GetPerformanceCounter() in case your
  297. * callback needs to adjust for variances.
  298. *
  299. * \param interval the timer delay, in milliseconds, passed to `callback`.
  300. * \param callback the SDL_TimerCallback function to call when the specified
  301. * `interval` elapses.
  302. * \param userdata a pointer that is passed to `callback`.
  303. * \returns a timer ID or 0 on failure; call SDL_GetError() for more
  304. * information.
  305. *
  306. * \threadsafety It is safe to call this function from any thread.
  307. *
  308. * \since This function is available since SDL 3.1.3.
  309. *
  310. * \sa SDL_AddTimerNS
  311. * \sa SDL_RemoveTimer
  312. */
  313. extern SDL_DECLSPEC SDL_TimerID SDLCALL SDL_AddTimer(Uint32 interval, SDL_TimerCallback callback, void *userdata);
  314. /**
  315. * Function prototype for the nanosecond timer callback function.
  316. *
  317. * The callback function is passed the current timer interval and returns the
  318. * next timer interval, in nanoseconds. If the returned value is the same as
  319. * the one passed in, the periodic alarm continues, otherwise a new alarm is
  320. * scheduled. If the callback returns 0, the periodic alarm is canceled and
  321. * will be removed.
  322. *
  323. * \param userdata an arbitrary pointer provided by the app through
  324. * SDL_AddTimer, for its own use.
  325. * \param timerID the current timer being processed.
  326. * \param interval the current callback time interval.
  327. * \returns the new callback time interval, or 0 to disable further runs of
  328. * the callback.
  329. *
  330. * \threadsafety SDL may call this callback at any time from a background
  331. * thread; the application is responsible for locking resources
  332. * the callback touches that need to be protected.
  333. *
  334. * \since This datatype is available since SDL 3.1.3.
  335. *
  336. * \sa SDL_AddTimerNS
  337. */
  338. typedef Uint64 (SDLCALL *SDL_NSTimerCallback)(void *userdata, SDL_TimerID timerID, Uint64 interval);
  339. /**
  340. * Call a callback function at a future time.
  341. *
  342. * The callback function is passed the current timer interval and the user
  343. * supplied parameter from the SDL_AddTimerNS() call and should return the
  344. * next timer interval. If the value returned from the callback is 0, the
  345. * timer is canceled and will be removed.
  346. *
  347. * The callback is run on a separate thread, and for short timeouts can
  348. * potentially be called before this function returns.
  349. *
  350. * Timers take into account the amount of time it took to execute the
  351. * callback. For example, if the callback took 250 ns to execute and returned
  352. * 1000 (ns), the timer would only wait another 750 ns before its next
  353. * iteration.
  354. *
  355. * Timing may be inexact due to OS scheduling. Be sure to note the current
  356. * time with SDL_GetTicksNS() or SDL_GetPerformanceCounter() in case your
  357. * callback needs to adjust for variances.
  358. *
  359. * \param interval the timer delay, in nanoseconds, passed to `callback`.
  360. * \param callback the SDL_TimerCallback function to call when the specified
  361. * `interval` elapses.
  362. * \param userdata a pointer that is passed to `callback`.
  363. * \returns a timer ID or 0 on failure; call SDL_GetError() for more
  364. * information.
  365. *
  366. * \threadsafety It is safe to call this function from any thread.
  367. *
  368. * \since This function is available since SDL 3.1.3.
  369. *
  370. * \sa SDL_AddTimer
  371. * \sa SDL_RemoveTimer
  372. */
  373. extern SDL_DECLSPEC SDL_TimerID SDLCALL SDL_AddTimerNS(Uint64 interval, SDL_NSTimerCallback callback, void *userdata);
  374. /**
  375. * Remove a timer created with SDL_AddTimer().
  376. *
  377. * \param id the ID of the timer to remove.
  378. * \returns true on success or false on failure; call SDL_GetError() for more
  379. * information.
  380. *
  381. * \threadsafety It is safe to call this function from any thread.
  382. *
  383. * \since This function is available since SDL 3.1.3.
  384. *
  385. * \sa SDL_AddTimer
  386. */
  387. extern SDL_DECLSPEC bool SDLCALL SDL_RemoveTimer(SDL_TimerID id);
  388. /* Ends C function definitions when using C++ */
  389. #ifdef __cplusplus
  390. }
  391. #endif
  392. #include <SDL3/SDL_close_code.h>
  393. #endif /* SDL_timer_h_ */