Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
authorSam Lantinga <slouken@libsdl.org>
Sat, 01 Mar 2014 09:50:52 -0800
changeset 82683f8410f20405
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
     1.1 --- a/src/SDL.c	Fri Feb 28 16:24:41 2014 -0800
     1.2 +++ b/src/SDL.c	Sat Mar 01 09:50:52 2014 -0800
     1.3 @@ -38,7 +38,8 @@
     1.4  #if !SDL_TIMERS_DISABLED
     1.5  extern int SDL_TimerInit(void);
     1.6  extern void SDL_TimerQuit(void);
     1.7 -extern void SDL_InitTicks(void);
     1.8 +extern void SDL_TicksInit(void);
     1.9 +extern void SDL_TicksQuit(void);
    1.10  #endif
    1.11  #if SDL_VIDEO_DRIVER_WINDOWS
    1.12  extern int SDL_HelperWindowCreate(void);
    1.13 @@ -123,7 +124,7 @@
    1.14  #endif
    1.15  
    1.16  #if !SDL_TIMERS_DISABLED
    1.17 -    SDL_InitTicks();
    1.18 +    SDL_TicksInit();
    1.19  #endif
    1.20  
    1.21      if ((flags & SDL_INIT_GAMECONTROLLER)) {
    1.22 @@ -355,6 +356,10 @@
    1.23  #endif
    1.24      SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
    1.25  
    1.26 +#if !SDL_TIMERS_DISABLED
    1.27 +    SDL_TicksQuit();
    1.28 +#endif
    1.29 +
    1.30      SDL_ClearHints();
    1.31      SDL_AssertionsQuit();
    1.32      SDL_LogResetPriorities();
     2.1 --- a/src/test/SDL_test_common.c	Fri Feb 28 16:24:41 2014 -0800
     2.2 +++ b/src/test/SDL_test_common.c	Sat Mar 01 09:50:52 2014 -0800
     2.3 @@ -1542,6 +1542,7 @@
     2.4          SDL_AudioQuit();
     2.5      }
     2.6      SDL_free(state);
     2.7 +    SDL_Quit();
     2.8  }
     2.9  
    2.10  /* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/timer/SDL_timer_c.h	Fri Feb 28 16:24:41 2014 -0800
     3.2 +++ b/src/timer/SDL_timer_c.h	Sat Mar 01 09:50:52 2014 -0800
     3.3 @@ -26,7 +26,8 @@
     3.4  #define ROUND_RESOLUTION(X) \
     3.5      (((X+TIMER_RESOLUTION-1)/TIMER_RESOLUTION)*TIMER_RESOLUTION)
     3.6  
     3.7 -extern void SDL_InitTicks(void);
     3.8 +extern void SDL_TicksInit(void);
     3.9 +extern void SDL_TicksQuit(void);
    3.10  extern int SDL_TimerInit(void);
    3.11  extern void SDL_TimerQuit(void);
    3.12  
     4.1 --- a/src/timer/dummy/SDL_systimer.c	Fri Feb 28 16:24:41 2014 -0800
     4.2 +++ b/src/timer/dummy/SDL_systimer.c	Sat Mar 01 09:50:52 2014 -0800
     4.3 @@ -27,7 +27,7 @@
     4.4  static SDL_bool ticks_started = SDL_FALSE;
     4.5  
     4.6  void
     4.7 -SDL_InitTicks(void)
     4.8 +SDL_TicksInit(void)
     4.9  {
    4.10      if (ticks_started) {
    4.11          return;
    4.12 @@ -35,11 +35,17 @@
    4.13      ticks_started = SDL_TRUE;
    4.14  }
    4.15  
    4.16 +void
    4.17 +SDL_TicksQuit(void)
    4.18 +{
    4.19 +    ticks_started = SDL_FALSE;
    4.20 +}
    4.21 +
    4.22  Uint32
    4.23  SDL_GetTicks(void)
    4.24  {
    4.25      if (!ticks_started) {
    4.26 -        SDL_InitTicks();
    4.27 +        SDL_TicksInit();
    4.28      }
    4.29  
    4.30      SDL_Unsupported();
     5.1 --- a/src/timer/haiku/SDL_systimer.c	Fri Feb 28 16:24:41 2014 -0800
     5.2 +++ b/src/timer/haiku/SDL_systimer.c	Sat Mar 01 09:50:52 2014 -0800
     5.3 @@ -30,7 +30,7 @@
     5.4  static SDL_bool ticks_started = SDL_FALSE;
     5.5  
     5.6  void
     5.7 -SDL_InitTicks(void)
     5.8 +SDL_TicksInit(void)
     5.9  {
    5.10      if (ticks_started) {
    5.11          return;
    5.12 @@ -41,11 +41,17 @@
    5.13      start = system_time();
    5.14  }
    5.15  
    5.16 +void
    5.17 +SDL_TicksQuit(void)
    5.18 +{
    5.19 +    ticks_started = SDL_FALSE;
    5.20 +}
    5.21 +
    5.22  Uint32
    5.23  SDL_GetTicks(void)
    5.24  {
    5.25      if (!ticks_started) {
    5.26 -        SDL_InitTicks();
    5.27 +        SDL_TicksInit();
    5.28      }
    5.29  
    5.30      return ((system_time() - start) / 1000);
     6.1 --- a/src/timer/psp/SDL_systimer.c	Fri Feb 28 16:24:41 2014 -0800
     6.2 +++ b/src/timer/psp/SDL_systimer.c	Sat Mar 01 09:50:52 2014 -0800
     6.3 @@ -31,7 +31,8 @@
     6.4  static struct timeval start;
     6.5  static SDL_bool ticks_started = SDL_FALSE;
     6.6  
     6.7 -void SDL_InitTicks(void)
     6.8 +void
     6.9 +SDL_TicksInit(void)
    6.10  {
    6.11      if (ticks_started) {
    6.12          return;
    6.13 @@ -41,10 +42,16 @@
    6.14      gettimeofday(&start, NULL);
    6.15  }
    6.16  
    6.17 +void
    6.18 +SDL_TicksQuit(void)
    6.19 +{
    6.20 +    ticks_started = SDL_FALSE;
    6.21 +}
    6.22 +
    6.23  Uint32 SDL_GetTicks(void)
    6.24  {
    6.25      if (!ticks_started) {
    6.26 -        SDL_InitTicks();
    6.27 +        SDL_TicksInit();
    6.28      }
    6.29  
    6.30      struct timeval now;
     7.1 --- a/src/timer/unix/SDL_systimer.c	Fri Feb 28 16:24:41 2014 -0800
     7.2 +++ b/src/timer/unix/SDL_systimer.c	Sat Mar 01 09:50:52 2014 -0800
     7.3 @@ -59,7 +59,7 @@
     7.4  static SDL_bool ticks_started = SDL_FALSE;
     7.5  
     7.6  void
     7.7 -SDL_InitTicks(void)
     7.8 +SDL_TicksInit(void)
     7.9  {
    7.10      if (ticks_started) {
    7.11          return;
    7.12 @@ -83,12 +83,18 @@
    7.13      }
    7.14  }
    7.15  
    7.16 +void
    7.17 +SDL_TicksQuit(void)
    7.18 +{
    7.19 +    ticks_started = SDL_FALSE;
    7.20 +}
    7.21 +
    7.22  Uint32
    7.23  SDL_GetTicks(void)
    7.24  {
    7.25      Uint32 ticks;
    7.26      if (!ticks_started) {
    7.27 -        SDL_InitTicks();
    7.28 +        SDL_TicksInit();
    7.29      }
    7.30  
    7.31      if (has_monotonic_time) {
    7.32 @@ -117,7 +123,7 @@
    7.33  {
    7.34      Uint64 ticks;
    7.35      if (!ticks_started) {
    7.36 -        SDL_InitTicks();
    7.37 +        SDL_TicksInit();
    7.38      }
    7.39  
    7.40      if (has_monotonic_time) {
    7.41 @@ -146,7 +152,7 @@
    7.42  SDL_GetPerformanceFrequency(void)
    7.43  {
    7.44      if (!ticks_started) {
    7.45 -        SDL_InitTicks();
    7.46 +        SDL_TicksInit();
    7.47      }
    7.48  
    7.49      if (has_monotonic_time) {
     8.1 --- a/src/timer/windows/SDL_systimer.c	Fri Feb 28 16:24:41 2014 -0800
     8.2 +++ b/src/timer/windows/SDL_systimer.c	Sat Mar 01 09:50:52 2014 -0800
     8.3 @@ -40,7 +40,6 @@
     8.4  static LARGE_INTEGER hires_start_ticks;
     8.5  /* The number of ticks per second of the high-resolution performance counter */
     8.6  static LARGE_INTEGER hires_ticks_per_second;
     8.7 -#endif
     8.8  
     8.9  static void
    8.10  timeSetPeriod(UINT uPeriod)
    8.11 @@ -76,13 +75,15 @@
    8.12      }
    8.13  }
    8.14  
    8.15 +#endif /* !USE_GETTICKCOUNT */
    8.16 +
    8.17  void
    8.18 -SDL_InitTicks(void)
    8.19 +SDL_TicksInit(void)
    8.20  {
    8.21      if (ticks_started) {
    8.22          return;
    8.23      }
    8.24 -    ticks_started = TRUE;
    8.25 +    ticks_started = SDL_TRUE;
    8.26  
    8.27      /* Set first ticks value */
    8.28  #ifdef USE_GETTICKCOUNT
    8.29 @@ -98,11 +99,26 @@
    8.30          hires_timer_available = FALSE;
    8.31          timeSetPeriod(1);     /* use 1 ms timer precision */
    8.32          start = timeGetTime();
    8.33 +
    8.34 +        SDL_AddHintCallback(SDL_HINT_TIMER_RESOLUTION,
    8.35 +                            SDL_TimerResolutionChanged, NULL);
    8.36 +    }
    8.37 +#endif
    8.38 +}
    8.39 +
    8.40 +void
    8.41 +SDL_TicksQuit(void)
    8.42 +{
    8.43 +#ifndef USE_GETTICKCOUNT
    8.44 +    if (!hires_timer_available) {
    8.45 +        SDL_DelHintCallback(SDL_HINT_TIMER_RESOLUTION,
    8.46 +                            SDL_TimerResolutionChanged, NULL);
    8.47 +
    8.48 +        timeSetPeriod(0);
    8.49      }
    8.50  #endif
    8.51  
    8.52 -    SDL_AddHintCallback(SDL_HINT_TIMER_RESOLUTION,
    8.53 -                        SDL_TimerResolutionChanged, NULL);
    8.54 +    ticks_started = SDL_FALSE;
    8.55  }
    8.56  
    8.57  Uint32
    8.58 @@ -114,7 +130,7 @@
    8.59  #endif
    8.60  
    8.61      if (!ticks_started) {
    8.62 -        SDL_InitTicks();
    8.63 +        SDL_TicksInit();
    8.64      }
    8.65  
    8.66  #ifdef USE_GETTICKCOUNT
     9.1 --- a/src/video/SDL_video.c	Fri Feb 28 16:24:41 2014 -0800
     9.2 +++ b/src/video/SDL_video.c	Sat Mar 01 09:50:52 2014 -0800
     9.3 @@ -432,7 +432,7 @@
     9.4      }
     9.5  
     9.6  #if !SDL_TIMERS_DISABLED
     9.7 -    SDL_InitTicks();
     9.8 +    SDL_TicksInit();
     9.9  #endif
    9.10  
    9.11      /* Start the event loop */