SDL_timer.h 14 KB

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