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

typedef int 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) {
304  SDL_OutOfMemory();
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);
318  SDL_OutOfMemory();
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 }

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.

◆ 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 }

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

SDL_LockMutex
#define SDL_LockMutex
Definition: SDL_dynapi_overrides.h:260
NULL
#define NULL
Definition: begin_code.h:167
SDL_AtomicLock
#define SDL_AtomicLock
Definition: SDL_dynapi_overrides.h:64
SDL_TimerInit
int SDL_TimerInit(void)
Definition: SDL_timer.c:207
SDL_TimerMap
Definition: SDL_timer.c:42
SDL_AtomicIncRef
#define SDL_AtomicIncRef(a)
Increment an atomic variable used as a reference count.
Definition: SDL_atomic.h:252
SDL_TimerMap::timer
SDL_Timer * timer
Definition: SDL_timer.c:45
callback
static Uint32 callback(Uint32 interval, void *param)
Definition: testtimer.c:34
SDL_Timer::next
struct _SDL_Timer * next
Definition: SDL_timer.c:39
SDL_Timer
Definition: SDL_timer.c:31
SDL_Timer::param
void * param
Definition: SDL_timer.c:35
SDL_SemPost
#define SDL_SemPost
Definition: SDL_dynapi_overrides.h:269
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1973
SDL_TimerMap::next
struct _SDL_TimerMap * next
Definition: SDL_timer.c:46
SDL_AtomicUnlock
#define SDL_AtomicUnlock
Definition: SDL_dynapi_overrides.h:65
SDL_Timer::timerID
int timerID
Definition: SDL_timer.c:33
SDL_RemoveTimer
SDL_bool SDL_RemoveTimer(SDL_TimerID id)
Remove a timer knowing its ID.
Definition: SDL_timer.c:342
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
SDL_FALSE
Definition: SDL_stdinc.h:163
SDL_GetTicks
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
param
GLfloat param
Definition: SDL_opengl_glext.h:370
SDL_Timer::callback
SDL_TimerCallback callback
Definition: SDL_timer.c:34
SDL_TimerMap::timerID
int timerID
Definition: SDL_timer.c:44
SDL_Timer::interval
Uint32 interval
Definition: SDL_timer.c:36
SDL_TimerData
Definition: SDL_timer.c:50
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:161
SDL_TRUE
Definition: SDL_stdinc.h:164
SDL_timer_data
static SDL_TimerData SDL_timer_data
Definition: SDL_timer.c:71
SDL_Timer::scheduled
Uint32 scheduled
Definition: SDL_timer.c:37
SDL_AtomicSet
#define SDL_AtomicSet
Definition: SDL_dynapi_overrides.h:67
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:374
SDL_AtomicGet
#define SDL_AtomicGet
Definition: SDL_dynapi_overrides.h:68
SDL_UnlockMutex
#define SDL_UnlockMutex
Definition: SDL_dynapi_overrides.h:262
SDL_Timer::canceled
SDL_atomic_t canceled
Definition: SDL_timer.c:38