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