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