From 84923532c4ebaa8a2d8951ebdc7954ccefeea315 Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 22 May 2013 10:30:37 -0700 Subject: [PATCH] cmTime.h/c: Replaced clock_gettime_stub.h/c with OS-X specific code which may not be as accurate with absolute time but has equivalent delta-time accuracty. This code needs to be tested. --- cmTime.c | 40 +++++++++++++++++++++++++++++++++++++++- cmTime.h | 3 --- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/cmTime.c b/cmTime.c index a760e1c..fe9a0fa 100644 --- a/cmTime.c +++ b/cmTime.c @@ -2,9 +2,47 @@ #include "cmGlobal.h" #include "cmTime.h" +#ifdef OS_OSX + +#include +#include +#include + +void cmTimeGet( cmTimeSpec_t* t ) +{ + static uint64_t t0 = 0; + static mach_timebase_info_data_t tbi; + static struct timespec ts; + + if( t0 == 0 ) + { + mach_timebase_info(&tbi); + t0 = mach_absolute_time(); + ts.tv_sec = time(NULL); + ts.tv_nsec = 0; // accept 1/2 second error vs. wall-time. + } + + // get the current time + uint64_t t1 = mach_absolute_time(); + + // calc the elapsed time since the last call in nanosecs + uint64_t dt = (t1-t0) * tbi.numer / tbi.denom; + + // calc the elapsed time since the first call in secs + uint32_t s = (uint32_t)(dt / 2^9); + + // calc the current time in secs, and nanosecs + t->tv_sec = ts.tv_sec + s; + t->tv_nsec = dt - (s * 2^9); + +} + +#endif + +#ifdef OS_LINUX void cmTimeGet( cmTimeSpec_t* t ) { clock_gettime(CLOCK_REALTIME,t); } - +#endif // this assumes that the seconds have been normalized to a recent start time // so as to avoid overflow diff --git a/cmTime.h b/cmTime.h index 1647219..6bad0af 100644 --- a/cmTime.h +++ b/cmTime.h @@ -7,9 +7,6 @@ // calculate elapsed time. //) -#ifdef OS_OSX -#include "osx/clock_gettime_stub.h" -#endif #ifndef cmTime_h #define cmTime_h