Added a macro SDL_TICKS_PASSED() to correctly compare two 32-bit tick values.
authorSam Lantinga <slouken@libsdl.org>
Sun, 20 Oct 2013 20:42:55 -0700
changeset 78576388f5229bb7
parent 7856 cb37776926ad
child 7858 170c9f4c3c28
Added a macro SDL_TICKS_PASSED() to correctly compare two 32-bit tick values.
Went through the code and used the macro and fixed a couple places that were using incorrect timestamp comparisons.
include/SDL_timer.h
src/audio/arts/SDL_artsaudio.c
src/audio/bsd/SDL_bsdaudio.c
src/audio/esd/SDL_esdaudio.c
src/audio/paudio/SDL_paudio.c
src/events/SDL_events.c
src/haptic/windows/SDL_syshaptic.c
src/power/uikit/SDL_syspower.m
src/thread/pthread/SDL_syssem.c
src/video/cocoa/SDL_cocoaevents.m
src/video/x11/SDL_x11events.c
src/video/x11/SDL_x11window.c
     1.1 --- a/include/SDL_timer.h	Sun Oct 20 20:41:30 2013 -0700
     1.2 +++ b/include/SDL_timer.h	Sun Oct 20 20:42:55 2013 -0700
     1.3 @@ -45,6 +45,17 @@
     1.4  extern DECLSPEC Uint32 SDLCALL SDL_GetTicks(void);
     1.5  
     1.6  /**
     1.7 + * \brief Compare SDL ticks values, and return true if A has passed B
     1.8 + *
     1.9 + * e.g. if you want to wait 100 ms, you could do this:
    1.10 + *  Uint32 timeout = SDL_GetTicks() + 100;
    1.11 + *  while (!SDL_TICKS_PASSED(SDL_GetTicks(), timeout)) {
    1.12 + *      ... do work until timeout has elapsed
    1.13 + *  }
    1.14 + */
    1.15 +#define SDL_TICKS_PASSED(A, B)  ((Sint32)((B) - (A)) <= 0)
    1.16 +
    1.17 +/**
    1.18   * \brief Get the current value of the high resolution counter
    1.19   */
    1.20  extern DECLSPEC Uint64 SDLCALL SDL_GetPerformanceCounter(void);
     2.1 --- a/src/audio/arts/SDL_artsaudio.c	Sun Oct 20 20:41:30 2013 -0700
     2.2 +++ b/src/audio/arts/SDL_artsaudio.c	Sun Oct 20 20:42:55 2013 -0700
     2.3 @@ -220,7 +220,7 @@
     2.4  ARTS_Suspend(void)
     2.5  {
     2.6      const Uint32 abortms = SDL_GetTicks() + 3000; /* give up after 3 secs */
     2.7 -    while ( (!SDL_NAME(arts_suspended)()) && (SDL_GetTicks() < abortms) ) {
     2.8 +    while ( (!SDL_NAME(arts_suspended)()) && !SDL_TICKS_PASSED(SDL_GetTicks(), abortms) ) {
     2.9          if ( SDL_NAME(arts_suspend)() ) {
    2.10              break;
    2.11          }
     3.1 --- a/src/audio/bsd/SDL_bsdaudio.c	Sun Oct 20 20:41:30 2013 -0700
     3.2 +++ b/src/audio/bsd/SDL_bsdaudio.c	Sun Oct 20 20:42:55 2013 -0700
     3.3 @@ -125,9 +125,7 @@
     3.4          /* Use timer for general audio synchronization */
     3.5          Sint32 ticks;
     3.6  
     3.7 -        ticks =
     3.8 -            ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) -
     3.9 -            FUDGE_TICKS;
    3.10 +        ticks = ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) - FUDGE_TICKS;
    3.11          if (ticks > 0) {
    3.12              SDL_Delay(ticks);
    3.13          }
     4.1 --- a/src/audio/esd/SDL_esdaudio.c	Sun Oct 20 20:41:30 2013 -0700
     4.2 +++ b/src/audio/esd/SDL_esdaudio.c	Sun Oct 20 20:42:55 2013 -0700
     4.3 @@ -135,8 +135,7 @@
     4.4      }
     4.5  
     4.6      /* Use timer for general audio synchronization */
     4.7 -    ticks =
     4.8 -        ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) - FUDGE_TICKS;
     4.9 +    ticks = ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) - FUDGE_TICKS;
    4.10      if (ticks > 0) {
    4.11          SDL_Delay(ticks);
    4.12      }
     5.1 --- a/src/audio/paudio/SDL_paudio.c	Sun Oct 20 20:41:30 2013 -0700
     5.2 +++ b/src/audio/paudio/SDL_paudio.c	Sun Oct 20 20:42:55 2013 -0700
     5.3 @@ -133,9 +133,7 @@
     5.4          /* Use timer for general audio synchronization */
     5.5          Sint32 ticks;
     5.6  
     5.7 -        ticks =
     5.8 -            ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) -
     5.9 -            FUDGE_TICKS;
    5.10 +        ticks = ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) - FUDGE_TICKS;
    5.11          if (ticks > 0) {
    5.12              SDL_Delay(ticks);
    5.13          }
     6.1 --- a/src/events/SDL_events.c	Sun Oct 20 20:41:30 2013 -0700
     6.2 +++ b/src/events/SDL_events.c	Sun Oct 20 20:42:55 2013 -0700
     6.3 @@ -443,7 +443,7 @@
     6.4                  /* Polling and no events, just return */
     6.5                  return 0;
     6.6              }
     6.7 -            if (timeout > 0 && ((int) (SDL_GetTicks() - expiration) >= 0)) {
     6.8 +            if (timeout > 0 && SDL_TICKS_PASSED(SDL_GetTicks(), expiration)) {
     6.9                  /* Timeout expired and no events */
    6.10                  return 0;
    6.11              }
     7.1 --- a/src/haptic/windows/SDL_syshaptic.c	Sun Oct 20 20:41:30 2013 -0700
     7.2 +++ b/src/haptic/windows/SDL_syshaptic.c	Sun Oct 20 20:42:55 2013 -0700
     7.3 @@ -1567,7 +1567,7 @@
     7.4          SDL_LockMutex(hwdata->mutex);
     7.5          /* If we're currently running and need to stop... */
     7.6          if (hwdata->stopTicks) {
     7.7 -            if ((hwdata->stopTicks != SDL_HAPTIC_INFINITY) && (hwdata->stopTicks < SDL_GetTicks())) {
     7.8 +            if ((hwdata->stopTicks != SDL_HAPTIC_INFINITY) && SDL_TIMESTAMP_PASSED(SDL_GetTicks(), hwdata->stopTicks)) {
     7.9                  XINPUT_VIBRATION vibration = { 0, 0 };
    7.10                  hwdata->stopTicks = 0;
    7.11                  XINPUTSETSTATE(hwdata->userid, &vibration);
     8.1 --- a/src/power/uikit/SDL_syspower.m	Sun Oct 20 20:41:30 2013 -0700
     8.2 +++ b/src/power/uikit/SDL_syspower.m	Sun Oct 20 20:42:55 2013 -0700
     8.3 @@ -38,11 +38,7 @@
     8.4  SDL_UIKit_UpdateBatteryMonitoring(void)
     8.5  {
     8.6      if (SDL_UIKitLastPowerInfoQuery) {
     8.7 -        const Uint32 prev = SDL_UIKitLastPowerInfoQuery;
     8.8 -        const UInt32 now = SDL_GetTicks();
     8.9 -        const UInt32 ticks = now - prev;
    8.10 -        /* if timer wrapped (now < prev), shut down, too. */
    8.11 -        if ((now < prev) || (ticks >= BATTERY_MONITORING_TIMEOUT)) {
    8.12 +        if (SDL_TICKS_PASSED(SDL_GetTicks(), SDL_UIKitLastPowerInfoQuery + BATTERY_MONITORING_TIMEOUT)) {
    8.13              UIDevice *uidev = [UIDevice currentDevice];
    8.14              SDL_assert([uidev isBatteryMonitoringEnabled] == YES);
    8.15              [uidev setBatteryMonitoringEnabled:NO];
     9.1 --- a/src/thread/pthread/SDL_syssem.c	Sun Oct 20 20:41:30 2013 -0700
     9.2 +++ b/src/thread/pthread/SDL_syssem.c	Sun Oct 20 20:42:55 2013 -0700
     9.3 @@ -156,7 +156,7 @@
     9.4  #else
     9.5      end = SDL_GetTicks() + timeout;
     9.6      while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) {
     9.7 -        if ((Sint32)(SDL_GetTicks() - end) >= 0) {
     9.8 +        if (SDL_TICKS_PASSED(SDL_GetTicks(), end)) {
     9.9              break;
    9.10          }
    9.11          SDL_Delay(1);
    10.1 --- a/src/video/cocoa/SDL_cocoaevents.m	Sun Oct 20 20:41:30 2013 -0700
    10.2 +++ b/src/video/cocoa/SDL_cocoaevents.m	Sun Oct 20 20:42:55 2013 -0700
    10.3 @@ -271,7 +271,7 @@
    10.4          SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
    10.5          Uint32 now = SDL_GetTicks();
    10.6          if (!data->screensaver_activity ||
    10.7 -            (int)(now-data->screensaver_activity) >= 30000) {
    10.8 +            SDL_TICKS_PASSED(now, data->screensaver_activity + 30000)) {
    10.9              UpdateSystemActivity(UsrActivity);
   10.10              data->screensaver_activity = now;
   10.11          }
    11.1 --- a/src/video/x11/SDL_x11events.c	Sun Oct 20 20:41:30 2013 -0700
    11.2 +++ b/src/video/x11/SDL_x11events.c	Sun Oct 20 20:42:55 2013 -0700
    11.3 @@ -909,7 +909,7 @@
    11.4              SDL_WindowData *data = videodata->windowlist[i];
    11.5              if (data && data->pending_focus != PENDING_FOCUS_NONE) {
    11.6                  Uint32 now = SDL_GetTicks();
    11.7 -                if ( (int)(data->pending_focus_time-now) <= 0 ) {
    11.8 +                if (SDL_TICKS_PASSED(now, data->pending_focus_time)) {
    11.9                      if ( data->pending_focus == PENDING_FOCUS_IN ) {
   11.10                          X11_DispatchFocusIn(data);
   11.11                      } else {
   11.12 @@ -963,7 +963,7 @@
   11.13      if (_this->suspend_screensaver) {
   11.14          Uint32 now = SDL_GetTicks();
   11.15          if (!data->screensaver_activity ||
   11.16 -            (int) (now - data->screensaver_activity) >= 30000) {
   11.17 +            SDL_TICKS_PASSED(now, data->screensaver_activity + 30000)) {
   11.18              X11_XResetScreenSaver(data->display);
   11.19  
   11.20              #if SDL_USE_LIBDBUS
    12.1 --- a/src/video/x11/SDL_x11window.c	Sun Oct 20 20:41:30 2013 -0700
    12.2 +++ b/src/video/x11/SDL_x11window.c	Sun Oct 20 20:42:55 2013 -0700
    12.3 @@ -66,7 +66,7 @@
    12.4      Uint32 start = SDL_GetTicks();
    12.5  
    12.6      while (!X11_XCheckIfEvent(display, event_return, predicate, arg)) {
    12.7 -        if ((SDL_GetTicks() - start) >= timeoutMS) {
    12.8 +        if (SDL_TICKS_PASSED(SDL_GetTicks(), start + timeoutMS)) {
    12.9              return False;
   12.10          }
   12.11      }