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,
|  |  | B
| ) |  | ((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.

Referenced by SDL_PrivateSendMouseButton(), SDL_SemWaitTimeout(), and SDL_WaitEventTimeout().

## 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.

References SDL_TimerData::active, SDL_Timer::callback, callback(), SDL_Timer::canceled, SDL_TimerData::freelist, SDL_Timer::interval, SDL_TimerData::lock, SDL_Timer::next, SDL_TimerMap::next, SDL_TimerData::nextID, SDL_Timer::param, SDL_TimerData::pending, 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_TimerData::sem, SDL_TimerMap::timer, SDL_Timer::timerID, SDL_TimerMap::timerID, SDL_TimerData::timermap, and SDL_TimerData::timermap_lock.

{
SDL_TimerData *data = &SDL_timer_data;
SDL_Timer *timer;
SDL_TimerMap *entry;
SDL_AtomicLock(&data->lock);
if (!SDL_AtomicGet(&data->active)) {
if (SDL_TimerInit() < 0) {
SDL_AtomicUnlock(&data->lock);
return 0;
}
}
timer = data->freelist;
if (timer) {
data->freelist = timer->next;
}
SDL_AtomicUnlock(&data->lock);
if (timer) {
SDL_RemoveTimer(timer->timerID);
} else {
timer = (SDL_Timer *)SDL_malloc(sizeof(*timer));
if (!timer) {
SDL_OutOfMemory();
return 0;
}
}
timer->timerID = SDL_AtomicIncRef(&data->nextID);
timer->callback = callback;
timer->param = param;
timer->interval = interval;
timer->scheduled = SDL_GetTicks() + interval;
SDL_AtomicSet(&timer->canceled, 0);
entry = (SDL_TimerMap *)SDL_malloc(sizeof(*entry));
if (!entry) {
SDL_free(timer);
SDL_OutOfMemory();
return 0;
}
entry->timer = timer;
entry->timerID = timer->timerID;
SDL_LockMutex(data->timermap_lock);
entry->next = data->timermap;
data->timermap = entry;
SDL_UnlockMutex(data->timermap_lock);
/* Add the timer to the pending list for the timer thread */
SDL_AtomicLock(&data->lock);
timer->next = data->pending;
data->pending = timer;
SDL_AtomicUnlock(&data->lock);
/* Wake up the timer thread if necessary */
SDL_SemPost(data->sem);
return entry->timerID;
}
SDL_LockMutex
#define SDL_LockMutex
Definition: SDL_dynapi_overrides.h:260
SDL_TimerMap::timer
SDL_Timer * timer
Definition: SDL_timer.c:45
SDL_TimerData::timermap
SDL_TimerMap * timermap
Definition: SDL_timer.c:54
SDL_AtomicLock
#define SDL_AtomicLock
Definition: SDL_dynapi_overrides.h:64
SDL_Timer::interval
Uint32 interval
Definition: SDL_timer.c:36
SDL_TimerInit
int SDL_TimerInit(void)
Definition: SDL_timer.c:207
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDL_TimerData::nextID
SDL_atomic_t nextID
Definition: SDL_timer.c:53
SDL_SemPost
#define SDL_SemPost
Definition: SDL_dynapi_overrides.h:269
SDL_AtomicUnlock
#define SDL_AtomicUnlock
Definition: SDL_dynapi_overrides.h:65
SDL_RemoveTimer
SDL_bool SDL_RemoveTimer(SDL_TimerID id)
Remove a timer knowing its ID.
Definition: SDL_timer.c:342
SDL_TimerMap
Definition: SDL_timer.c:42
SDL_Timer::next
struct _SDL_Timer * next
Definition: SDL_timer.c:39
SDL_TimerData::active
SDL_atomic_t active
Definition: SDL_timer.c:65
SDL_GetTicks
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
SDL_Timer::scheduled
Uint32 scheduled
Definition: SDL_timer.c:37
SDL_Timer::callback
SDL_TimerCallback callback
Definition: SDL_timer.c:34
SDL_Timer::timerID
int timerID
Definition: SDL_timer.c:33
callback
static Uint32 callback(Uint32 interval, void *param)
Definition: testtimer.c:34
SDL_TimerData::sem
SDL_sem * sem
Definition: SDL_timer.c:62
SDL_AtomicIncRef
#define SDL_AtomicIncRef(a)
Increment an atomic variable used as a reference count.
Definition: SDL_atomic.h:234
SDL_TimerMap::next
struct _SDL_TimerMap * next
Definition: SDL_timer.c:46
SDL_Timer::param
void * param
Definition: SDL_timer.c:35
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_TimerData::freelist
SDL_Timer * freelist
Definition: SDL_timer.c:64
SDL_TimerData::timermap_lock
SDL_mutex * timermap_lock
Definition: SDL_timer.c:55
SDL_TimerData::pending
SDL_Timer * pending
Definition: SDL_timer.c:63
SDL_Timer
Definition: SDL_timer.c:31
SDL_timer_data
static SDL_TimerData SDL_timer_data
Definition: SDL_timer.c:71
SDL_AtomicSet
#define SDL_AtomicSet
Definition: SDL_dynapi_overrides.h:67
SDL_Timer::canceled
SDL_atomic_t canceled
Definition: SDL_timer.c:38
SDL_AtomicGet
#define SDL_AtomicGet
Definition: SDL_dynapi_overrides.h:68
SDL_UnlockMutex
#define SDL_UnlockMutex
Definition: SDL_dynapi_overrides.h:262
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:374
SDL_TimerData::lock
SDL_SpinLock lock
Definition: SDL_timer.c:61
param
GLfloat param
Definition: SDL_opengl_glext.h:370
SDL_TimerData
Definition: SDL_timer.c:50
SDL_TimerMap::timerID
int timerID
Definition: SDL_timer.c:44

## 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.

## 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_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.

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, SDL_TimerMap::timerID, SDL_TimerData::timermap, and SDL_TimerData::timermap_lock.

Referenced by SDL_AddTimer().

{
SDL_TimerData *data = &SDL_timer_data;
SDL_TimerMap *prev, *entry;
SDL_bool canceled = SDL_FALSE;
/* Find the timer */
SDL_LockMutex(data->timermap_lock);
prev = NULL;
for (entry = data->timermap; entry; prev = entry, entry = entry->next) {
if (entry->timerID == id) {
if (prev) {
prev->next = entry->next;
} else {
data->timermap = entry->next;
}
break;
}
}
SDL_UnlockMutex(data->timermap_lock);
if (entry) {
if (!SDL_AtomicGet(&entry->timer->canceled)) {
SDL_AtomicSet(&entry->timer->canceled, 1);
canceled = SDL_TRUE;
}
SDL_free(entry);
}
return canceled;
}
SDL_LockMutex
#define SDL_LockMutex
Definition: SDL_dynapi_overrides.h:260
SDL_TimerMap::timer
SDL_Timer * timer
Definition: SDL_timer.c:45
SDL_TRUE
Definition: SDL_stdinc.h:142
SDL_TimerData::timermap
SDL_TimerMap * timermap
Definition: SDL_timer.c:54
SDL_FALSE
Definition: SDL_stdinc.h:141
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDL_TimerMap
Definition: SDL_timer.c:42
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
SDL_TimerMap::next
struct _SDL_TimerMap * next
Definition: SDL_timer.c:46
NULL
#define NULL
Definition: begin_code.h:164
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:139
SDL_TimerData::timermap_lock
SDL_mutex * timermap_lock
Definition: SDL_timer.c:55
SDL_timer_data
static SDL_TimerData SDL_timer_data
Definition: SDL_timer.c:71
SDL_AtomicSet
#define SDL_AtomicSet
Definition: SDL_dynapi_overrides.h:67
SDL_Timer::canceled
SDL_atomic_t canceled
Definition: SDL_timer.c:38
SDL_AtomicGet
#define SDL_AtomicGet
Definition: SDL_dynapi_overrides.h:68
SDL_UnlockMutex
#define SDL_UnlockMutex
Definition: SDL_dynapi_overrides.h:262
SDL_TimerData
Definition: SDL_timer.c:50
SDL_TimerMap::timerID
int timerID
Definition: SDL_timer.c:44