Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch Coriiander In src\timer\windows\SDL_systimer.c there is an error with regards to timeBeginPeriod and timeEndPeriod. These functions typically get called when no high resolution timer is available, and GetTickCount is not used. According to MSDN (link: http://msdn.microsoft.com/en-us/library/windows/desktop/dd757624(v=vs.85).aspx), for every call to timeBeginPeriod a subsequent call to timeEndPeriod is required. While SDL is currently doing this, it fails to call timeEndPeriod when cleaning up/shutting down SDL. Please note that these functions affect things on a system level. Failing to call timeEndPeriod, disables applications for using WINMM-timers after usage&shutdown of SDL, as effectively they the mechanism is now broken. Solution: Ensure this code gets called when shutting down the timer subsystem: #ifndef USE_GETTICKCOUNT if (!hires_timer_available) { timeSetPeriod(0); } #endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
diff --git a/src/SDL.c b/src/SDL.c
index 9a8da1e..dacd0f7 100644
--- a/src/SDL.c
+++ b/src/SDL.c
@@ -38,7 +38,8 @@
#if !SDL_TIMERS_DISABLED
extern int SDL_TimerInit(void);
extern void SDL_TimerQuit(void);
-extern void SDL_InitTicks(void);
+extern void SDL_TicksInit(void);
+extern void SDL_TicksQuit(void);
#endif
#if SDL_VIDEO_DRIVER_WINDOWS
extern int SDL_HelperWindowCreate(void);
@@ -123,7 +124,7 @@ SDL_InitSubSystem(Uint32 flags)
#endif
#if !SDL_TIMERS_DISABLED
- SDL_InitTicks();
+ SDL_TicksInit();
#endif
if ((flags & SDL_INIT_GAMECONTROLLER)) {
@@ -355,6 +356,10 @@ SDL_Quit(void)
#endif
SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
+#if !SDL_TIMERS_DISABLED
+ SDL_TicksQuit();
+#endif
+
SDL_ClearHints();
SDL_AssertionsQuit();
SDL_LogResetPriorities();
diff --git a/src/test/SDL_test_common.c b/src/test/SDL_test_common.c
index c03da03..9d41a19 100644
--- a/src/test/SDL_test_common.c
+++ b/src/test/SDL_test_common.c
@@ -1542,6 +1542,7 @@ SDLTest_CommonQuit(SDLTest_CommonState * state)
SDL_AudioQuit();
}
SDL_free(state);
+ SDL_Quit();
}
/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/timer/SDL_timer_c.h b/src/timer/SDL_timer_c.h
index 91646d9..8d563cf 100644
--- a/src/timer/SDL_timer_c.h
+++ b/src/timer/SDL_timer_c.h
@@ -26,7 +26,8 @@
#define ROUND_RESOLUTION(X) \
(((X+TIMER_RESOLUTION-1)/TIMER_RESOLUTION)*TIMER_RESOLUTION)
-extern void SDL_InitTicks(void);
+extern void SDL_TicksInit(void);
+extern void SDL_TicksQuit(void);
extern int SDL_TimerInit(void);
extern void SDL_TimerQuit(void);
diff --git a/src/timer/dummy/SDL_systimer.c b/src/timer/dummy/SDL_systimer.c
index 0e2703f..57899ae 100644
--- a/src/timer/dummy/SDL_systimer.c
+++ b/src/timer/dummy/SDL_systimer.c
@@ -27,7 +27,7 @@
static SDL_bool ticks_started = SDL_FALSE;
void
-SDL_InitTicks(void)
+SDL_TicksInit(void)
{
if (ticks_started) {
return;
@@ -35,11 +35,17 @@ SDL_InitTicks(void)
ticks_started = SDL_TRUE;
}
+void
+SDL_TicksQuit(void)
+{
+ ticks_started = SDL_FALSE;
+}
+
Uint32
SDL_GetTicks(void)
{
if (!ticks_started) {
- SDL_InitTicks();
+ SDL_TicksInit();
}
SDL_Unsupported();
diff --git a/src/timer/haiku/SDL_systimer.c b/src/timer/haiku/SDL_systimer.c
index 1500f6b..685f72a 100644
--- a/src/timer/haiku/SDL_systimer.c
+++ b/src/timer/haiku/SDL_systimer.c
@@ -30,7 +30,7 @@ static bigtime_t start;
static SDL_bool ticks_started = SDL_FALSE;
void
-SDL_InitTicks(void)
+SDL_TicksInit(void)
{
if (ticks_started) {
return;
@@ -41,11 +41,17 @@ SDL_InitTicks(void)
start = system_time();
}
+void
+SDL_TicksQuit(void)
+{
+ ticks_started = SDL_FALSE;
+}
+
Uint32
SDL_GetTicks(void)
{
if (!ticks_started) {
- SDL_InitTicks();
+ SDL_TicksInit();
}
return ((system_time() - start) / 1000);
diff --git a/src/timer/psp/SDL_systimer.c b/src/timer/psp/SDL_systimer.c
index 0f05a13..b706f44 100644
--- a/src/timer/psp/SDL_systimer.c
+++ b/src/timer/psp/SDL_systimer.c
@@ -31,7 +31,8 @@
static struct timeval start;
static SDL_bool ticks_started = SDL_FALSE;
-void SDL_InitTicks(void)
+void
+SDL_TicksInit(void)
{
if (ticks_started) {
return;
@@ -41,10 +42,16 @@ void SDL_InitTicks(void)
gettimeofday(&start, NULL);
}
+void
+SDL_TicksQuit(void)
+{
+ ticks_started = SDL_FALSE;
+}
+
Uint32 SDL_GetTicks(void)
{
if (!ticks_started) {
- SDL_InitTicks();
+ SDL_TicksInit();
}
struct timeval now;
diff --git a/src/timer/unix/SDL_systimer.c b/src/timer/unix/SDL_systimer.c
index 7c35bef..0f4c757 100644
--- a/src/timer/unix/SDL_systimer.c
+++ b/src/timer/unix/SDL_systimer.c
@@ -59,7 +59,7 @@ static struct timeval start_tv;
static SDL_bool ticks_started = SDL_FALSE;
void
-SDL_InitTicks(void)
+SDL_TicksInit(void)
{
if (ticks_started) {
return;
@@ -83,12 +83,18 @@ SDL_InitTicks(void)
}
}
+void
+SDL_TicksQuit(void)
+{
+ ticks_started = SDL_FALSE;
+}
+
Uint32
SDL_GetTicks(void)
{
Uint32 ticks;
if (!ticks_started) {
- SDL_InitTicks();
+ SDL_TicksInit();
}
if (has_monotonic_time) {
@@ -117,7 +123,7 @@ SDL_GetPerformanceCounter(void)
{
Uint64 ticks;
if (!ticks_started) {
- SDL_InitTicks();
+ SDL_TicksInit();
}
if (has_monotonic_time) {
@@ -146,7 +152,7 @@ Uint64
SDL_GetPerformanceFrequency(void)
{
if (!ticks_started) {
- SDL_InitTicks();
+ SDL_TicksInit();
}
if (has_monotonic_time) {
diff --git a/src/timer/windows/SDL_systimer.c b/src/timer/windows/SDL_systimer.c
index b1fe301..186233a 100644
--- a/src/timer/windows/SDL_systimer.c
+++ b/src/timer/windows/SDL_systimer.c
@@ -40,7 +40,6 @@ static BOOL hires_timer_available;
static LARGE_INTEGER hires_start_ticks;
/* The number of ticks per second of the high-resolution performance counter */
static LARGE_INTEGER hires_ticks_per_second;
-#endif
static void
timeSetPeriod(UINT uPeriod)
@@ -76,13 +75,15 @@ SDL_TimerResolutionChanged(void *userdata, const char *name, const char *oldValu
}
}
+#endif /* !USE_GETTICKCOUNT */
+
void
-SDL_InitTicks(void)
+SDL_TicksInit(void)
{
if (ticks_started) {
return;
}
- ticks_started = TRUE;
+ ticks_started = SDL_TRUE;
/* Set first ticks value */
#ifdef USE_GETTICKCOUNT
@@ -98,11 +99,26 @@ SDL_InitTicks(void)
hires_timer_available = FALSE;
timeSetPeriod(1); /* use 1 ms timer precision */
start = timeGetTime();
+
+ SDL_AddHintCallback(SDL_HINT_TIMER_RESOLUTION,
+ SDL_TimerResolutionChanged, NULL);
+ }
+#endif
+}
+
+void
+SDL_TicksQuit(void)
+{
+#ifndef USE_GETTICKCOUNT
+ if (!hires_timer_available) {
+ SDL_DelHintCallback(SDL_HINT_TIMER_RESOLUTION,
+ SDL_TimerResolutionChanged, NULL);
+
+ timeSetPeriod(0);
}
#endif
- SDL_AddHintCallback(SDL_HINT_TIMER_RESOLUTION,
- SDL_TimerResolutionChanged, NULL);
+ ticks_started = SDL_FALSE;
}
Uint32
@@ -114,7 +130,7 @@ SDL_GetTicks(void)
#endif
if (!ticks_started) {
- SDL_InitTicks();
+ SDL_TicksInit();
}
#ifdef USE_GETTICKCOUNT
diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index 87e8506..7426e2f 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -432,7 +432,7 @@ SDL_VideoInit(const char *driver_name)
}
#if !SDL_TIMERS_DISABLED
- SDL_InitTicks();
+ SDL_TicksInit();
#endif
/* Start the event loop */