1.1 --- a/src/timer/linux/SDL_systimer.c Tue Oct 11 17:33:03 2005 +0000
1.2 +++ b/src/timer/linux/SDL_systimer.c Tue Oct 11 18:16:12 2005 +0000
1.3 @@ -34,6 +34,16 @@
1.4 #include <string.h>
1.5 #include <errno.h>
1.6
1.7 +/* The clock_gettime provides monotonous time, so we should use it if
1.8 + it's available. The clock_gettime function is behind ifdef
1.9 + for __USE_POSIX199309
1.10 + Tommi Kyntola (tommi.kyntola@ray.fi) 27/09/2005
1.11 +*/
1.12 +#if (defined _POSIX_TIMERS && _POSIX_TIMERS > 0)
1.13 +#include <time.h>
1.14 +#define USE_CLOCK_GETTIME
1.15 +#endif
1.16 +
1.17 #include "SDL_error.h"
1.18 #include "SDL_timer.h"
1.19 #include "SDL_timer_c.h"
1.20 @@ -86,6 +96,19 @@
1.21 float cpu_mhz;
1.22 unsigned long long tsc_start;
1.23 unsigned long long tsc_end;
1.24 +/* Slight code doubling here for the sake of readability */
1.25 +#ifdef USE_CLOCK_GETTIME
1.26 + struct timespec tv_start, tv_end;
1.27 + long usec_delay;
1.28 +
1.29 + rdtsc(tsc_start);
1.30 + clock_gettime(CLOCK_MONOTONIC,&tv_start);
1.31 + sleep(1);
1.32 + rdtsc(tsc_end);
1.33 + clock_gettime(CLOCK_MONOTONIC,&tv_end);
1.34 + usec_delay = (1000000000L * (tv_end.tv_sec - tv_start.tv_sec) +
1.35 + (tv_end.tv_nsec - tv_start.tv_nsec)) / 1000;
1.36 +#else
1.37 struct timeval tv_start, tv_end;
1.38 long usec_delay;
1.39
1.40 @@ -96,6 +119,7 @@
1.41 gettimeofday(&tv_end, NULL);
1.42 usec_delay = 1000000L * (tv_end.tv_sec - tv_start.tv_sec) +
1.43 (tv_end.tv_usec - tv_start.tv_usec);
1.44 +#endif /* USE_CLOCK_GETTIME */
1.45 cpu_mhz = (float)(tsc_end-tsc_start) / usec_delay;
1.46 #if 0
1.47 printf("cpu MHz\t\t: %.3f\n", cpu_mhz);
1.48 @@ -106,7 +130,11 @@
1.49 #else
1.50
1.51 /* The first ticks value of the application */
1.52 +#ifdef USE_CLOCK_GETTIME
1.53 +static struct timespec start;
1.54 +#else
1.55 static struct timeval start;
1.56 +#endif /* USE_CLOCK_GETTIME */
1.57
1.58 #endif /* USE_RDTSC */
1.59
1.60 @@ -119,9 +147,11 @@
1.61 cpu_mhz1000 = calc_cpu_mhz() * 1000.0f;
1.62 }
1.63 rdtsc(start);
1.64 +#elif defined(USE_CLOCK_GETTIME)
1.65 + clock_gettime(CLOCK_MONOTONIC,&start);
1.66 #else
1.67 gettimeofday(&start, NULL);
1.68 -#endif /* USE_RDTSC */
1.69 +#endif
1.70 }
1.71
1.72 Uint32 SDL_GetTicks (void)
1.73 @@ -133,14 +163,19 @@
1.74 }
1.75 rdtsc(now);
1.76 return (Uint32)((now-start)/cpu_mhz1000);
1.77 +#elif defined(USE_CLOCK_GETTIME)
1.78 + Uint32 ticks;
1.79 + struct timespec now;
1.80 + clock_gettime(CLOCK_MONOTONIC,&now);
1.81 + ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_nsec-start.tv_nsec)/1000000;
1.82 + return(ticks);
1.83 #else
1.84 + Uint32 ticks;
1.85 struct timeval now;
1.86 - Uint32 ticks;
1.87 -
1.88 gettimeofday(&now, NULL);
1.89 ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000;
1.90 return(ticks);
1.91 -#endif /* USE_RDTSC */
1.92 +#endif
1.93 }
1.94
1.95 void SDL_Delay (Uint32 ms)