Mercurial > sdl-ios-xcode
diff src/timer/wince/SDL_systimer.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 | 327f181542f1 |
children | b530ef003506 |
line wrap: on
line diff
--- a/src/timer/wince/SDL_systimer.c Thu Jan 27 10:40:17 2011 -0800 +++ b/src/timer/wince/SDL_systimer.c Thu Jan 27 14:45:06 2011 -0800 @@ -24,11 +24,8 @@ #ifdef SDL_TIMER_WINCE #include "../../core/windows/SDL_windows.h" -#include <mmsystem.h> -#include "SDL_thread.h" #include "SDL_timer.h" -#include "../SDL_timer_c.h" static Uint64 start_date; static Uint64 start_ticks; @@ -69,6 +66,14 @@ return ((Sint32) (wce_date() - start_date)); } +/* Recard start-time of application for reference */ +void +SDL_StartTicks(void) +{ + start_date = wce_date(); + start_ticks = wce_ticks(); +} + /* Return time in ms relative to when SDL was started */ Uint32 SDL_GetTicks() @@ -89,122 +94,6 @@ Sleep(ms); } -/* Recard start-time of application for reference */ -void -SDL_StartTicks(void) -{ - start_date = wce_date(); - start_ticks = wce_ticks(); -} - -static UINT WIN_timer; - -#if ( _WIN32_WCE <= 420 ) - -static HANDLE timersThread = 0; -static HANDLE timersQuitEvent = 0; - -DWORD -TimersThreadProc(void *data) -{ - while (WaitForSingleObject(timersQuitEvent, 10) == WAIT_TIMEOUT) { - SDL_ThreadedTimerCheck(); - } - return 0; -} - -int -SDL_SYS_TimerInit(void) -{ - // create a thread to process a threaded timers - // SetTimer does not suit the needs because - // TimerCallbackProc will be called only when WM_TIMER occured - - timersQuitEvent = CreateEvent(0, TRUE, FALSE, 0); - if (!timersQuitEvent) { - SDL_SetError("Cannot create event for timers thread"); - return -1; - } - timersThread = CreateThread(NULL, 0, TimersThreadProc, 0, 0, 0); - if (!timersThread) { - SDL_SetError - ("Cannot create timers thread, check amount of RAM available"); - return -1; - } - SetThreadPriority(timersThread, THREAD_PRIORITY_HIGHEST); - - return (SDL_SetTimerThreaded(1)); -} - -void -SDL_SYS_TimerQuit(void) -{ - SetEvent(timersQuitEvent); - if (WaitForSingleObject(timersThread, 2000) == WAIT_TIMEOUT) - TerminateThread(timersThread, 0); - CloseHandle(timersThread); - CloseHandle(timersQuitEvent); - return; -} - -#else +#endif /* SDL_TIMER_WINCE */ -#pragma comment(lib, "mmtimer.lib") - -/* Data to handle a single periodic alarm */ -static UINT timerID = 0; - -static void CALLBACK -HandleAlarm(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2) -{ - SDL_ThreadedTimerCheck(); -} - - -int -SDL_SYS_TimerInit(void) -{ - MMRESULT result; - - /* Set timer resolution */ - result = timeBeginPeriod(TIMER_RESOLUTION); - if (result != TIMERR_NOERROR) { - SDL_SetError("Warning: Can't set %d ms timer resolution", - TIMER_RESOLUTION); - } - /* Allow 10 ms of drift so we don't chew on CPU */ - timerID = - timeSetEvent(TIMER_RESOLUTION, 1, HandleAlarm, 0, TIME_PERIODIC); - if (!timerID) { - SDL_SetError("timeSetEvent() failed"); - return (-1); - } - return (SDL_SetTimerThreaded(1)); -} - -void -SDL_SYS_TimerQuit(void) -{ - if (timerID) { - timeKillEvent(timerID); - } - timeEndPeriod(TIMER_RESOLUTION); -} - -#endif - -int -SDL_SYS_StartTimer(void) -{ - SDL_SetError("Internal logic error: WinCE uses threaded timer"); - return (-1); -} - -void -SDL_SYS_StopTimer(void) -{ - return; -} - -#endif /* SDL_TIMER_WINCE */ /* vi: set ts=4 sw=4 expandtab: */