util.h: restore times()

This commit is contained in:
Przemyslaw Skibinski 2017-02-21 07:33:45 +01:00
parent da4a0f30af
commit 1b59333c82

View File

@ -124,13 +124,14 @@ extern "C" {
UTIL_STATIC void UTIL_getTime(UTIL_time_t* x) { *x = mach_absolute_time(); }
UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_freq_t rate, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return (((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom))/1000ULL; }
UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_freq_t rate, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return ((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom); }
#elif (_POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK) /* defined in <unistd.h> */
typedef struct timespec UTIL_freq_t;
typedef struct timespec UTIL_time_t;
UTIL_STATIC void UTIL_initTimer(UTIL_freq_t* clockResolution) { clock_getres(CLOCK_MONOTONIC, clockResolution); }
UTIL_STATIC void UTIL_getTime(UTIL_time_t* x) { if (clock_gettime(CLOCK_MONOTONIC, x) == -1) { fprintf(stderr, "clock_gettime error"); }; }
UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_freq_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return (1000000000ULL * (clockEnd.tv_sec - clockStart.tv_sec) + (clockEnd.tv_nsec - clockStart.tv_nsec))/1000ULL; }
UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_freq_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd.tv_sec - clockStart.tv_sec) + (clockEnd.tv_nsec - clockStart.tv_nsec); }
#elif (PLATFORM_POSIX_VERSION >= 200112L)
#include <sys/times.h> /* times */
typedef U64 UTIL_freq_t;
typedef U64 UTIL_time_t;
UTIL_STATIC void UTIL_initTimer(UTIL_freq_t* ticksPerSecond) { *ticksPerSecond=sysconf(_SC_CLK_TCK); }
UTIL_STATIC void UTIL_getTime(UTIL_time_t* x) { struct tms junk; clock_t newTicks = (clock_t) times(&junk); (void)junk; *x = (UTIL_time_t)newTicks; }
UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_freq_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / ticksPerSecond; }
UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_freq_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / ticksPerSecond; }
#else /* relies on standard C (note : clock_t measurements can be wrong when using multi-threading) */
typedef clock_t UTIL_freq_t;
typedef clock_t UTIL_time_t;