Mercurial > sdl-ios-xcode
comparison src/timer/windows/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 | e337f792c6a7 |
comparison
equal
deleted
inserted
replaced
5112:0846f18eb625 | 5113:481dabb098ef |
---|---|
22 #include "SDL_config.h" | 22 #include "SDL_config.h" |
23 | 23 |
24 #ifdef SDL_TIMER_WINDOWS | 24 #ifdef SDL_TIMER_WINDOWS |
25 | 25 |
26 #include "../../core/windows/SDL_windows.h" | 26 #include "../../core/windows/SDL_windows.h" |
27 #include <mmsystem.h> | |
28 | 27 |
29 #include "SDL_timer.h" | 28 #include "SDL_timer.h" |
30 #include "../SDL_timer_c.h" | |
31 | 29 |
32 #ifdef _WIN32_WCE | 30 #ifdef _WIN32_WCE |
33 #error This is WinCE. Please use src/timer/wince/SDL_systimer.c instead. | 31 #error This is WinCE. Please use src/timer/wince/SDL_systimer.c instead. |
34 #endif | 32 #endif |
35 | 33 |
104 SDL_Delay(Uint32 ms) | 102 SDL_Delay(Uint32 ms) |
105 { | 103 { |
106 Sleep(ms); | 104 Sleep(ms); |
107 } | 105 } |
108 | 106 |
109 /* Data to handle a single periodic alarm */ | |
110 static UINT timerID = 0; | |
111 | |
112 static void CALLBACK | |
113 HandleAlarm(UINT uID, UINT uMsg, DWORD_PTR dwUser, | |
114 DWORD_PTR dw1, DWORD_PTR dw2) | |
115 { | |
116 SDL_ThreadedTimerCheck(); | |
117 } | |
118 | |
119 | |
120 int | |
121 SDL_SYS_TimerInit(void) | |
122 { | |
123 MMRESULT result; | |
124 | |
125 /* Set timer resolution */ | |
126 result = timeBeginPeriod(TIMER_RESOLUTION); | |
127 if (result != TIMERR_NOERROR) { | |
128 SDL_SetError("Warning: Can't set %d ms timer resolution", | |
129 TIMER_RESOLUTION); | |
130 } | |
131 /* Allow 10 ms of drift so we don't chew on CPU */ | |
132 timerID = | |
133 timeSetEvent(TIMER_RESOLUTION, 1, HandleAlarm, 0, TIME_PERIODIC); | |
134 if (!timerID) { | |
135 SDL_SetError("timeSetEvent() failed"); | |
136 return (-1); | |
137 } | |
138 return (SDL_SetTimerThreaded(1)); | |
139 } | |
140 | |
141 void | |
142 SDL_SYS_TimerQuit(void) | |
143 { | |
144 if (timerID) { | |
145 timeKillEvent(timerID); | |
146 } | |
147 timeEndPeriod(TIMER_RESOLUTION); | |
148 } | |
149 | |
150 int | |
151 SDL_SYS_StartTimer(void) | |
152 { | |
153 SDL_SetError("Internal logic error: Win32 uses threaded timer"); | |
154 return (-1); | |
155 } | |
156 | |
157 void | |
158 SDL_SYS_StopTimer(void) | |
159 { | |
160 return; | |
161 } | |
162 | |
163 #endif /* SDL_TIMER_WINDOWS */ | 107 #endif /* SDL_TIMER_WINDOWS */ |
164 | 108 |
165 /* vi: set ts=4 sw=4 expandtab: */ | 109 /* vi: set ts=4 sw=4 expandtab: */ |