rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

commit 103211b3cfe0a78287a01fb662f56b66add4593d
parent da0effe709b9dbeb8752a278f819078c8bb8fbba
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date:   Tue, 27 Oct 2015 18:02:59 +0100

Clock specifics for MACOSX

Diffstat:
Msrc/clock_time.c | 78++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
1 file changed, 48 insertions(+), 30 deletions(-)

diff --git a/src/clock_time.c b/src/clock_time.c @@ -18,11 +18,15 @@ #if defined(COMPILER_CL) || (defined(MINGW) && defined(ARCH_32BITS)) #define CLOCK_TIME_WINDOWS +#elif defined(OS_MACH) + #define CLOCK_TIME_MACH #endif #ifdef CLOCK_TIME_WINDOWS #include "io_c99.h" /* snprintf support */ #include <Windows.h> +#elif defined(CLOCK_TIME_MACH) + #include <mach/mach_time.h> #else #include <time.h> #endif @@ -31,12 +35,12 @@ #include <string.h> #define TIME_TO_NSEC(Time) ((Time)->nsec + (Time)->sec * 1000000000L) -#define NSEC_PER_USEC (int64_t)1000 -#define NSEC_PER_MSEC ((int64_t)1000 * NSEC_PER_USEC) -#define NSEC_PER_SEC ((int64_t)1000 * NSEC_PER_MSEC) -#define NSEC_PER_MIN ((int64_t)60 * NSEC_PER_SEC) -#define NSEC_PER_HOUR ((int64_t)60 * NSEC_PER_MIN) -#define NSEC_PER_DAY ((int64_t)24 * NSEC_PER_HOUR) +#define NSEC_PER_USEC__ (int64_t)1000 +#define NSEC_PER_MSEC__ ((int64_t)1000 * NSEC_PER_USEC__) +#define NSEC_PER_SEC__ ((int64_t)1000 * NSEC_PER_MSEC__) +#define NSEC_PER_MIN__ ((int64_t)60 * NSEC_PER_SEC__) +#define NSEC_PER_HOUR__ ((int64_t)60 * NSEC_PER_MIN__) +#define NSEC_PER_DAY__ ((int64_t)24 * NSEC_PER_HOUR__) void time_current(struct time* t) @@ -54,6 +58,20 @@ time_current(struct time* t) t->nsec = (int64_t)((double)time.QuadPart*1000000000.0/(double)tmp.QuadPart); t->sec = t->nsec / 1000000000; t->nsec = t->nsec - t->sec * 1000000000; +#elif defined(CLOCK_TIME_MACH) + static double orwl_timebase = 0.0; + static uint64_t orwl_timestart = 0; + double diff; + if (!orwl_timestart) { + mach_timebase_info_data_t tb = { 0 }; + mach_timebase_info(&tb); + orwl_timebase = tb.numer; + orwl_timebase /= tb.denom; + orwl_timestart = mach_absolute_time(); + } + diff = (double)(mach_absolute_time() - orwl_timestart) * orwl_timebase; + t->sec = (int64_t)diff / 1000000000; + t->nsec = (int64_t)diff - t->sec * 1000000000; #else struct timespec time; int err = 0; (void)err; @@ -100,22 +118,22 @@ time_val(const struct time* time, enum time_unit unit) /* Do nothing. */ break; case TIME_USEC: - val /= NSEC_PER_USEC; + val /= NSEC_PER_USEC__; break; case TIME_MSEC: - val /= NSEC_PER_MSEC; + val /= NSEC_PER_MSEC__; break; case TIME_SEC: - val /= NSEC_PER_SEC; + val /= NSEC_PER_SEC__; break; case TIME_MIN: - val /= NSEC_PER_MIN; + val /= NSEC_PER_MIN__; break; case TIME_HOUR: - val /= NSEC_PER_HOUR; + val /= NSEC_PER_HOUR__; break; case TIME_DAY: - val /= NSEC_PER_DAY; + val /= NSEC_PER_DAY__; break; default: ASSERT(0); break; } @@ -160,34 +178,34 @@ time_dump time_nsec = TIME_TO_NSEC(time); if(flag & TIME_DAY) { - const int64_t nb_days = time_nsec / NSEC_PER_DAY; + const int64_t nb_days = time_nsec / NSEC_PER_DAY__; if(nb_days) DUMP(nb_days, "day"); - time_nsec -= nb_days * NSEC_PER_DAY; + time_nsec -= nb_days * NSEC_PER_DAY__; } if(flag & TIME_HOUR) { - const int64_t nb_hours = time_nsec / NSEC_PER_HOUR; + const int64_t nb_hours = time_nsec / NSEC_PER_HOUR__; if(nb_hours) DUMP(nb_hours, "hour"); - time_nsec -= nb_hours * NSEC_PER_HOUR; + time_nsec -= nb_hours * NSEC_PER_HOUR__; } if(flag & TIME_MIN) { - const int64_t nb_mins = time_nsec / NSEC_PER_MIN; + const int64_t nb_mins = time_nsec / NSEC_PER_MIN__; if(nb_mins) DUMP(nb_mins, "min"); - time_nsec -= nb_mins * NSEC_PER_MIN; + time_nsec -= nb_mins * NSEC_PER_MIN__; } if(flag & TIME_SEC) { - const int64_t nb_secs = time_nsec / NSEC_PER_SEC; + const int64_t nb_secs = time_nsec / NSEC_PER_SEC__; if(nb_secs) DUMP(nb_secs, "sec"); - time_nsec -= nb_secs * NSEC_PER_SEC; + time_nsec -= nb_secs * NSEC_PER_SEC__; } if(flag & TIME_MSEC) { - const int64_t nb_msecs = time_nsec / NSEC_PER_MSEC; + const int64_t nb_msecs = time_nsec / NSEC_PER_MSEC__; if(nb_msecs) DUMP(nb_msecs, "msec"); - time_nsec -= nb_msecs * NSEC_PER_MSEC; + time_nsec -= nb_msecs * NSEC_PER_MSEC__; } if(flag & TIME_USEC) { - const int64_t nb_usecs = time_nsec / NSEC_PER_USEC; + const int64_t nb_usecs = time_nsec / NSEC_PER_USEC__; if(nb_usecs) DUMP(nb_usecs, "usec"); - time_nsec -= nb_usecs * NSEC_PER_USEC; + time_nsec -= nb_usecs * NSEC_PER_USEC__; } if(flag & TIME_NSEC) { if(time_nsec) DUMP(time_nsec, "nsec"); @@ -211,10 +229,10 @@ time_dump #undef DUMP } -#undef NSEC_PER_USEC -#undef NSEC_PER_MSEC -#undef NSEC_PER_SEC -#undef NSEC_PER_MIN -#undef NSEC_PER_HOUR -#undef NSEC_PER_DAY +#undef NSEC_PER_USEC__ +#undef NSEC_PER_MSEC__ +#undef NSEC_PER_SEC__ +#undef NSEC_PER_MIN__ +#undef NSEC_PER_HOUR__ +#undef NSEC_PER_DAY__