add win32 impl for oc_clock_time()

This commit is contained in:
Reuben Dunnington 2023-08-22 13:40:39 -07:00
parent 5a4fba96fa
commit 9d1d8e5306
Signed by: rdunnington
GPG Key ID: 4EC5290E704FD482
1 changed files with 37 additions and 4 deletions

View File

@ -18,6 +18,8 @@ extern "C"
static u64 __performanceCounterFreq = 0; static u64 __performanceCounterFreq = 0;
const u64 WIN_EPOCH_TO_UNIX_EPOCH_100NS = 116444736000000000; // number of 100ns from Jan 1, 1601 (windows epoch) to Jan 1, 1970 (unix epoch)
void oc_clock_init() void oc_clock_init()
{ {
LARGE_INTEGER freq; LARGE_INTEGER freq;
@ -26,6 +28,10 @@ extern "C"
} }
f64 oc_clock_time(oc_clock_kind clock) f64 oc_clock_time(oc_clock_kind clock)
{
switch(clock)
{
case OC_CLOCK_MONOTONIC:
{ {
LARGE_INTEGER counter; LARGE_INTEGER counter;
QueryPerformanceCounter(&counter); QueryPerformanceCounter(&counter);
@ -34,6 +40,33 @@ extern "C"
return (time); return (time);
} }
case OC_CLOCK_UPTIME:
{
LONGLONG unbiasedTime100Ns;
if(QueryUnbiasedInterruptTime(&unbiasedTime100Ns))
{
f64 unbiasedTimeSecs = ((f64)unbiasedTime100Ns) / (10 * 1000 * 1000);
return unbiasedTimeSecs;
}
oc_log_error("OC_CLOCK_UPTIME syscall failed with error: %d", GetLastError());
return 0;
}
case OC_CLOCK_DATE:
{
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
u64 systemTime100Ns = ((u64)ft.dwHighDateTime << 32) | (u64)ft.dwLowDateTime;
u64 timestamp100Ns = systemTime100Ns - WIN_EPOCH_TO_UNIX_EPOCH_100NS;
f64 timeSecs = ((f64)timestamp100Ns) / (10 * 1000 * 1000);
return timeSecs;
}
}
return 0.0;
}
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"
#endif #endif