Mercurial > sdl-ios-xcode
diff src/SDL_compat.c @ 5113:481dabb098ef
Improved timer implementation
The new timer model is formalized as using a separate thread to handle timer callbacks. This was the case on almost every platform before, but it's now a requirement, and simplifies the implementation and makes it perform consistently across platforms.
Goals:
* Minimize timer thread blocking
* Dispatch timers as accurately as possible
* SDL_AddTimer() and SDL_RemoveTimer() are completely threadsafe
* SDL_RemoveTimer() doesn't crash with a timer that's expired or removed
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Thu, 27 Jan 2011 14:45:06 -0800 |
parents | da347bfed240 |
children | c8e049de174c |
line wrap: on
line diff
--- a/src/SDL_compat.c Thu Jan 27 10:40:17 2011 -0800 +++ b/src/SDL_compat.c Thu Jan 27 14:45:06 2011 -0800 @@ -1760,6 +1760,31 @@ return previous; } +static Uint32 +SDL_SetTimerCallback(Uint32 interval, void* param) +{ + return ((SDL_OldTimerCallback)param)(interval); +} + +int +SDL_SetTimer(Uint32 interval, SDL_OldTimerCallback callback) +{ + static SDL_TimerID compat_timer; + + if (compat_timer) { + SDL_RemoveTimer(compat_timer); + compat_timer = 0; + } + + if (interval && callback) { + compat_timer = SDL_AddTimer(interval, SDL_SetTimerCallback, callback); + if (!compat_timer) { + return -1; + } + } + return 0; +} + int SDL_putenv(const char *_var) {