Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
authorSam Lantinga <slouken@libsdl.org>
Sat, 01 Mar 2014 09:50:52 -0800
changeset 8268 3f8410f20405
parent 8267 3e857d08cd02
child 8269 6e18328e25e0
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
src/SDL.c
src/test/SDL_test_common.c
src/timer/SDL_timer_c.h
src/timer/dummy/SDL_systimer.c
src/timer/haiku/SDL_systimer.c
src/timer/psp/SDL_systimer.c
src/timer/unix/SDL_systimer.c
src/timer/windows/SDL_systimer.c
src/video/SDL_video.c
--- a/src/SDL.c	Fri Feb 28 16:24:41 2014 -0800
+++ b/src/SDL.c	Sat Mar 01 09:50:52 2014 -0800
@@ -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 @@
 #endif
 
 #if !SDL_TIMERS_DISABLED
-    SDL_InitTicks();
+    SDL_TicksInit();
 #endif
 
     if ((flags & SDL_INIT_GAMECONTROLLER)) {
@@ -355,6 +356,10 @@
 #endif
     SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
 
+#if !SDL_TIMERS_DISABLED
+    SDL_TicksQuit();
+#endif
+
     SDL_ClearHints();
     SDL_AssertionsQuit();
     SDL_LogResetPriorities();
--- a/src/test/SDL_test_common.c	Fri Feb 28 16:24:41 2014 -0800
+++ b/src/test/SDL_test_common.c	Sat Mar 01 09:50:52 2014 -0800
@@ -1542,6 +1542,7 @@
         SDL_AudioQuit();
     }
     SDL_free(state);
+    SDL_Quit();
 }
 
 /* vi: set ts=4 sw=4 expandtab: */
--- a/src/timer/SDL_timer_c.h	Fri Feb 28 16:24:41 2014 -0800
+++ b/src/timer/SDL_timer_c.h	Sat Mar 01 09:50:52 2014 -0800
@@ -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);
 
--- a/src/timer/dummy/SDL_systimer.c	Fri Feb 28 16:24:41 2014 -0800
+++ b/src/timer/dummy/SDL_systimer.c	Sat Mar 01 09:50:52 2014 -0800
@@ -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 @@
     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();
--- a/src/timer/haiku/SDL_systimer.c	Fri Feb 28 16:24:41 2014 -0800
+++ b/src/timer/haiku/SDL_systimer.c	Sat Mar 01 09:50:52 2014 -0800
@@ -30,7 +30,7 @@
 static SDL_bool ticks_started = SDL_FALSE;
 
 void
-SDL_InitTicks(void)
+SDL_TicksInit(void)
 {
     if (ticks_started) {
         return;
@@ -41,11 +41,17 @@
     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);
--- a/src/timer/psp/SDL_systimer.c	Fri Feb 28 16:24:41 2014 -0800
+++ b/src/timer/psp/SDL_systimer.c	Sat Mar 01 09:50:52 2014 -0800
@@ -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 @@
     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;
--- a/src/timer/unix/SDL_systimer.c	Fri Feb 28 16:24:41 2014 -0800
+++ b/src/timer/unix/SDL_systimer.c	Sat Mar 01 09:50:52 2014 -0800
@@ -59,7 +59,7 @@
 static SDL_bool ticks_started = SDL_FALSE;
 
 void
-SDL_InitTicks(void)
+SDL_TicksInit(void)
 {
     if (ticks_started) {
         return;
@@ -83,12 +83,18 @@
     }
 }
 
+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 @@
 {
     Uint64 ticks;
     if (!ticks_started) {
-        SDL_InitTicks();
+        SDL_TicksInit();
     }
 
     if (has_monotonic_time) {
@@ -146,7 +152,7 @@
 SDL_GetPerformanceFrequency(void)
 {
     if (!ticks_started) {
-        SDL_InitTicks();
+        SDL_TicksInit();
     }
 
     if (has_monotonic_time) {
--- a/src/timer/windows/SDL_systimer.c	Fri Feb 28 16:24:41 2014 -0800
+++ b/src/timer/windows/SDL_systimer.c	Sat Mar 01 09:50:52 2014 -0800
@@ -40,7 +40,6 @@
 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 @@
     }
 }
 
+#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 @@
         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 @@
 #endif
 
     if (!ticks_started) {
-        SDL_InitTicks();
+        SDL_TicksInit();
     }
 
 #ifdef USE_GETTICKCOUNT
--- a/src/video/SDL_video.c	Fri Feb 28 16:24:41 2014 -0800
+++ b/src/video/SDL_video.c	Sat Mar 01 09:50:52 2014 -0800
@@ -432,7 +432,7 @@
     }
 
 #if !SDL_TIMERS_DISABLED
-    SDL_InitTicks();
+    SDL_TicksInit();
 #endif
 
     /* Start the event loop */