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.

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

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

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

342 {
344  SDL_TimerMap *prev, *entry;
345  SDL_bool canceled = SDL_FALSE;
346 
347  /* Find the timer */
349  prev = NULL;
350  for (entry = data->timermap; entry; prev = entry, entry = entry->next) {
351  if (entry->timerID == id) {
352  if (prev) {
353  prev->next = entry->next;
354  } else {
355  data->timermap = entry->next;
356  }
357  break;
358  }
359  }
361 
362  if (entry) {
363  if (!SDL_AtomicGet(&entry->timer->canceled)) {
364  SDL_AtomicSet(&entry->timer->canceled, 1);
365  canceled = SDL_TRUE;
366  }
367  SDL_free(entry);
368  }
369  return canceled;
370 }
#define SDL_LockMutex
SDL_Timer * timer
Definition: SDL_timer.c:45
SDL_TimerMap * timermap
Definition: SDL_timer.c:54
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1967
void SDL_free(void *mem)
struct _SDL_TimerMap * next
Definition: SDL_timer.c:46
#define NULL
Definition: begin_code.h:143
SDL_bool
Definition: SDL_stdinc.h:130
SDL_mutex * timermap_lock
Definition: SDL_timer.c:55
static SDL_TimerData SDL_timer_data
Definition: SDL_timer.c:71
#define SDL_AtomicSet
SDL_atomic_t canceled
Definition: SDL_timer.c:38
#define SDL_AtomicGet
#define SDL_UnlockMutex