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