From 466c0cec3f6ef8b11427d1cfef963dad48d05971 Mon Sep 17 00:00:00 2001 From: John Date: Tue, 10 Feb 2026 15:22:50 +0000 Subject: [PATCH] Added MSVC utime_now implementation Remove gettimeofday msvc fix f f2 --- common/time_util.c | 33 +++++++++++++++++++++++++++++++++ common/time_util.h | 14 +------------- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/common/time_util.c b/common/time_util.c index 7a25f424..52c1bcb1 100644 --- a/common/time_util.c +++ b/common/time_util.c @@ -29,6 +29,25 @@ either expressed or implied, of the Regents of The University of Michigan. #include #include "time_util.h" +#ifdef _MSC_VER + +static INIT_ONCE profiler_initd = INIT_ONCE_STATIC_INIT; // static-initialization struct +static volatile LONGLONG profiler_perf_frequency; + +static BOOL __stdcall profiler_init(PINIT_ONCE init_once, PVOID parameter, LPVOID *context) +{ + (void) init_once; + (void) parameter; + (void) context; + + LARGE_INTEGER freq; + QueryPerformanceFrequency(&freq); + profiler_perf_frequency = freq.QuadPart; + return true; +} + +#endif + struct timeutil_rest { int64_t acc_time; @@ -48,9 +67,23 @@ void timeutil_rest_destroy(timeutil_rest_t *rest) int64_t utime_now() // blacklist-ignore { + #ifdef _MSC_VER + + // initialize profiler (will only run once and block for all threads) + InitOnceExecuteOnce(&profiler_initd, profiler_init, NULL, NULL); + + LARGE_INTEGER counter; + + QueryPerformanceCounter(&counter); + + // convert to microseconds + return (int64_t) (counter.QuadPart * 1000000LL) / profiler_perf_frequency; + + #else struct timeval tv; gettimeofday (&tv, NULL); // blacklist-ignore return (int64_t) tv.tv_sec * 1000000 + tv.tv_usec; + #endif } int64_t utime_get_seconds(int64_t v) diff --git a/common/time_util.h b/common/time_util.h index a2ead4ca..be7ec5f2 100644 --- a/common/time_util.h +++ b/common/time_util.h @@ -40,19 +40,7 @@ either expressed or implied, of the Regents of The University of Michigan. typedef long long suseconds_t; #endif -#ifdef _MSC_VER - -inline int gettimeofday(struct timeval* tp, void* tzp) -{ - (void)tzp; - - unsigned long t; - t = time(NULL); - tp->tv_sec = t / 1000; - tp->tv_usec = t % 1000; - return 0; -} -#else +#ifndef _MSC_VER #include #include #endif