SDL 2.0
SDL_timer.h File Reference
#include "SDL_stdinc.h"
#include "SDL_error.h"
#include "begin_code.h"
#include "close_code.h"
+ Include dependency graph for SDL_timer.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define SDL_TICKS_PASSED(A, B)   ((Sint32)((B) - (A)) <= 0)
 Compare SDL ticks values, and return true if A has passed B. More...
 

Typedefs

typedef Uint32(* SDL_TimerCallback) (Uint32 interval, void *param)
 
typedef int SDL_TimerID
 

Functions

Uint32 SDL_GetTicks (void)
 Get the number of milliseconds since the SDL library initialization. More...
 
Uint64 SDL_GetPerformanceCounter (void)
 Get the current value of the high resolution counter. More...
 
Uint64 SDL_GetPerformanceFrequency (void)
 Get the count per second of the high resolution counter. More...
 
void SDL_Delay (Uint32 ms)
 Wait a specified number of milliseconds before returning. More...
 
SDL_TimerID SDL_AddTimer (Uint32 interval, SDL_TimerCallback callback, void *param)
 Add a new timer to the pool of timers already running. More...
 
SDL_bool SDL_RemoveTimer (SDL_TimerID id)
 Remove a timer knowing its ID. More...
 

Detailed Description

Header for the SDL time management routines.

Definition in file SDL_timer.h.

Macro Definition Documentation

◆ SDL_TICKS_PASSED

#define SDL_TICKS_PASSED (   A,
 
)    ((Sint32)((B) - (A)) <= 0)

Compare SDL ticks values, and return true if A has passed B.

e.g. if you want to wait 100 ms, you could do this: Uint32 timeout = SDL_GetTicks() + 100; while (!SDL_TICKS_PASSED(SDL_GetTicks(), timeout)) { ... do work until timeout has elapsed }

Definition at line 56 of file SDL_timer.h.

Typedef Documentation

◆ SDL_TimerCallback

typedef Uint32(* SDL_TimerCallback) (Uint32 interval, void *param)

Function prototype for the timer callback function.

The callback function is passed the current timer interval and returns the next timer interval. If the returned value is the same as the one passed in, the periodic alarm continues, otherwise a new alarm is scheduled. If the callback returns 0, the periodic alarm is cancelled.

Definition at line 81 of file SDL_timer.h.

◆ SDL_TimerID

SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 Uint32 return SDL_AudioCVT SDL_AudioFormat Uint8 int SDL_AudioFormat Uint8 int return Uint8 const Uint8 Uint32 int const char return return return return return return return return Uint32 return Uint32 SDL_Event return SDL_Event int return SDL_EventFilter void SDL_EventFilter void SDL_EventFilter void int return const char const char return SDL_JoystickGUID return int return int return SDL_GameController return int return const char return SDL_GameController SDL_GameControllerAxis return const char return SDL_GameController SDL_GameControllerButton return SDL_GameController SDL_RWops return SDL_TouchID SDL_RWops return int return int return return SDL_Joystick return SDL_Haptic SDL_Haptic return SDL_Haptic return SDL_Haptic SDL_HapticEffect return SDL_Haptic int Uint32 return SDL_Haptic int SDL_Haptic int return SDL_Haptic return SDL_Haptic return SDL_Haptic return SDL_Haptic return const char const char return const char SDL_HintCallback void int return SDL_Joystick return SDL_Joystick return const char return SDL_Joystick return SDL_Joystick return SDL_Joystick return int return SDL_Joystick int return SDL_Joystick int return return return SDL_Scancode return SDL_Scancode return SDL_Keycode return return const char return void int SDL_LogPriority SDL_LogOutputFunction void Uint32 const char const char SDL_Window return int int return SDL_Window int int return SDL_Surface int int return SDL_Cursor return int return SDL_mutex return SDL_mutex return Uint32 return SDL_sem return SDL_sem Uint32 return SDL_sem return SDL_cond SDL_cond return SDL_cond SDL_mutex Uint32 return Uint32 int Uint32 Uint32 Uint32 Uint32 return Uint32 return int return SDL_Palette const SDL_Color int int return const SDL_PixelFormat Uint8 Uint8 Uint8 return Uint32 const SDL_PixelFormat Uint8 Uint8 Uint8 float Uint16 int int return const SDL_Rect const SDL_Rect SDL_Rect return const SDL_Point int const SDL_Rect SDL_Rect return return int int Uint32 SDL_Window SDL_Renderer return SDL_Surface return SDL_Renderer SDL_RendererInfo return SDL_Renderer Uint32 int int int return SDL_Texture Uint32 int int int return SDL_Texture Uint8 Uint8 Uint8 return SDL_Texture Uint8 return SDL_Texture SDL_BlendMode return SDL_Texture const SDL_Rect const Uint8 int const Uint8 int const Uint8 int return SDL_Texture SDL_Renderer SDL_Texture return SDL_Renderer int int return SDL_Renderer const SDL_Rect return SDL_Renderer const SDL_Rect return SDL_Renderer float float return SDL_Renderer Uint8 Uint8 Uint8 Uint8 return SDL_Renderer SDL_BlendMode return SDL_Renderer return SDL_Renderer const SDL_Point int return SDL_Renderer const SDL_Point int return SDL_Renderer const SDL_Rect int return SDL_Renderer const SDL_Rect int return SDL_Renderer SDL_Texture const SDL_Rect const SDL_Rect const double const SDL_Point const SDL_RendererFlip return SDL_Renderer SDL_Renderer SDL_Texture return void int return return SDL_RWops return SDL_RWops return SDL_RWops return SDL_RWops return SDL_RWops Uint16 return SDL_RWops Uint32 return SDL_RWops Uint64 return const char unsigned int unsigned int unsigned int unsigned int Uint32 return SDL_Window SDL_Surface SDL_WindowShapeMode return size_t return void size_t return const char return void size_t size_t int(*) a int return int return int size_t return size_t return const wchar_t return const wchar_t size_t return const char size_t return const char size_t return char return char return const char int return int char int return long char int return Sint64 char int return const char return const char char int return const char char int return const char char return const char const char size_t return const char const char size_t return double return double return double return double return double return double return double int return float return const char const char return SDL_iconv_t const char size_t char size_t return Uint32 int int int Uint32 Uint32 Uint32 Uint32 return SDL_Surface SDL_Surface return SDL_RWops int return SDL_Surface int return SDL_Surface Uint32 return SDL_Surface Uint8 Uint8 Uint8 return SDL_Surface Uint8 return SDL_Surface SDL_BlendMode return SDL_Surface SDL_Rect SDL_Surface Uint32 Uint32 return SDL_Surface const SDL_Rect Uint32 return SDL_Surface const SDL_Rect SDL_Surface SDL_Rect return SDL_Surface const SDL_Rect SDL_Surface const SDL_Rect return SDL_Surface SDL_Rect SDL_Surface SDL_Rect return SDL_Thread return SDL_Thread return SDL_Thread int return SDL_TLSID const void void(*) return return Uint32 SDL_TimerID

Definition of the timer ID type.

Definition at line 86 of file SDL_timer.h.

Function Documentation

◆ SDL_AddTimer()

SDL_TimerID SDL_AddTimer ( Uint32  interval,
SDL_TimerCallback  callback,
void param 
)

Add a new timer to the pool of timers already running.

Returns
A timer ID, or 0 when an error occurs.

Definition at line 279 of file SDL_timer.c.

280{
282 SDL_Timer *timer;
283 SDL_TimerMap *entry;
284
285 SDL_AtomicLock(&data->lock);
286 if (!SDL_AtomicGet(&data->active)) {
287 if (SDL_TimerInit() < 0) {
288 SDL_AtomicUnlock(&data->lock);
289 return 0;
290 }
291 }
292
293 timer = data->freelist;
294 if (timer) {
295 data->freelist = timer->next;
296 }
297 SDL_AtomicUnlock(&data->lock);
298
299 if (timer) {
300 SDL_RemoveTimer(timer->timerID);
301 } else {
302 timer = (SDL_Timer *)SDL_malloc(sizeof(*timer));
303 if (!timer) {
305 return 0;
306 }
307 }
308 timer->timerID = SDL_AtomicIncRef(&data->nextID);
309 timer->callback = callback;
310 timer->param = param;
311 timer->interval = interval;
312 timer->scheduled = SDL_GetTicks() + interval;
313 SDL_AtomicSet(&timer->canceled, 0);
314
315 entry = (SDL_TimerMap *)SDL_malloc(sizeof(*entry));
316 if (!entry) {
317 SDL_free(timer);
319 return 0;
320 }
321 entry->timer = timer;
322 entry->timerID = timer->timerID;
323
324 SDL_LockMutex(data->timermap_lock);
325 entry->next = data->timermap;
326 data->timermap = entry;
327 SDL_UnlockMutex(data->timermap_lock);
328
329 /* Add the timer to the pending list for the timer thread */
330 SDL_AtomicLock(&data->lock);
331 timer->next = data->pending;
332 data->pending = timer;
333 SDL_AtomicUnlock(&data->lock);
334
335 /* Wake up the timer thread if necessary */
336 SDL_SemPost(data->sem);
337
338 return entry->timerID;
339}
#define SDL_AtomicIncRef(a)
Increment an atomic variable used as a reference count.
Definition: SDL_atomic.h:252
#define SDL_AtomicSet
#define SDL_AtomicLock
#define SDL_SemPost
#define SDL_LockMutex
#define SDL_malloc
#define SDL_AtomicUnlock
#define SDL_free
#define SDL_AtomicGet
#define SDL_UnlockMutex
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
GLfloat param
static SDL_TimerData SDL_timer_data
Definition: SDL_timer.c:71
SDL_bool SDL_RemoveTimer(SDL_TimerID id)
Remove a timer knowing its ID.
Definition: SDL_timer.c:342
int SDL_TimerInit(void)
Definition: SDL_timer.c:207
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
Uint32 scheduled
Definition: SDL_timer.c:37
SDL_atomic_t canceled
Definition: SDL_timer.c:38
struct _SDL_Timer * next
Definition: SDL_timer.c:39
void * param
Definition: SDL_timer.c:35
SDL_TimerCallback callback
Definition: SDL_timer.c:34
Uint32 interval
Definition: SDL_timer.c:36
int timerID
Definition: SDL_timer.c:33
struct _SDL_TimerMap * next
Definition: SDL_timer.c:46
SDL_Timer * timer
Definition: SDL_timer.c:45
static Uint32 callback(Uint32 interval, void *param)
Definition: testtimer.c:34

References SDL_Timer::callback, callback(), SDL_Timer::canceled, SDL_Timer::interval, SDL_Timer::next, SDL_TimerMap::next, SDL_Timer::param, SDL_Timer::scheduled, SDL_AtomicGet, SDL_AtomicIncRef, SDL_AtomicLock, SDL_AtomicSet, SDL_AtomicUnlock, SDL_free, SDL_GetTicks(), SDL_LockMutex, SDL_malloc, SDL_OutOfMemory, SDL_RemoveTimer(), SDL_SemPost, SDL_timer_data, SDL_TimerInit(), SDL_UnlockMutex, SDL_TimerMap::timer, SDL_Timer::timerID, and SDL_TimerMap::timerID.

◆ SDL_Delay()

void SDL_Delay ( Uint32  ms)

Wait a specified number of milliseconds before returning.

◆ SDL_GetPerformanceCounter()

Uint64 SDL_GetPerformanceCounter ( void  )

Get the current value of the high resolution counter.

Definition at line 513 of file SDL_dynapi_procs.h.

◆ SDL_GetPerformanceFrequency()

Uint64 SDL_GetPerformanceFrequency ( void  )

Get the count per second of the high resolution counter.

Referenced by main(), render(), and timer_getPerformanceFrequency().

◆ SDL_GetTicks()

Uint32 SDL_GetTicks ( void  )

Get the number of milliseconds since the SDL library initialization.

Note
This value wraps if the program runs for more than ~49 days.

Referenced by ConfigureBinding(), loop(), main(), runAdder(), RunFIFOTest(), SDL_AddTimer(), SDL_PrivateGameControllerButton(), SDL_PrivateSendMouseButton(), SDL_PushEvent(), SDL_SemWaitTimeout(), SDL_TimerThread(), SDL_WaitEventTimeout(), TestWaitTimeout(), timer_delayAndGetTicks(), and WatchJoystick().

◆ SDL_RemoveTimer()

SDL_bool SDL_RemoveTimer ( SDL_TimerID  id)

Remove a timer knowing its ID.

Returns
A boolean value indicating success or failure.
Warning
It is not safe to remove a timer multiple times.

Definition at line 342 of file SDL_timer.c.

343{
345 SDL_TimerMap *prev, *entry;
346 SDL_bool canceled = SDL_FALSE;
347
348 /* Find the timer */
349 SDL_LockMutex(data->timermap_lock);
350 prev = NULL;
351 for (entry = data->timermap; entry; prev = entry, entry = entry->next) {
352 if (entry->timerID == id) {
353 if (prev) {
354 prev->next = entry->next;
355 } else {
356 data->timermap = entry->next;
357 }
358 break;
359 }
360 }
361 SDL_UnlockMutex(data->timermap_lock);
362
363 if (entry) {
364 if (!SDL_AtomicGet(&entry->timer->canceled)) {
365 SDL_AtomicSet(&entry->timer->canceled, 1);
366 canceled = SDL_TRUE;
367 }
368 SDL_free(entry);
369 }
370 return canceled;
371}
SDL_bool
Definition: SDL_stdinc.h:162
@ SDL_TRUE
Definition: SDL_stdinc.h:164
@ SDL_FALSE
Definition: SDL_stdinc.h:163
#define NULL
Definition: begin_code.h:167

References SDL_Timer::canceled, SDL_TimerMap::next, NULL, SDL_AtomicGet, SDL_AtomicSet, SDL_FALSE, SDL_free, SDL_LockMutex, SDL_timer_data, SDL_TRUE, SDL_UnlockMutex, SDL_TimerMap::timer, and SDL_TimerMap::timerID.

Referenced by SDL_AddTimer().