Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/lib/sysdep/os/win/wposix/wtime.cpp
/* Copyright (C) 2010 Wildfire Games. | /* Copyright (C) 2020 Wildfire Games. | ||||
* | * | ||||
* Permission is hereby granted, free of charge, to any person obtaining | * Permission is hereby granted, free of charge, to any person obtaining | ||||
* a copy of this software and associated documentation files (the | * a copy of this software and associated documentation files (the | ||||
* "Software"), to deal in the Software without restriction, including | * "Software"), to deal in the Software without restriction, including | ||||
* without limitation the rights to use, copy, modify, merge, publish, | * without limitation the rights to use, copy, modify, merge, publish, | ||||
* distribute, sublicense, and/or sell copies of the Software, and to | * distribute, sublicense, and/or sell copies of the Software, and to | ||||
* permit persons to whom the Software is furnished to do so, subject to | * permit persons to whom the Software is furnished to do so, subject to | ||||
* the following conditions: | * the following conditions: | ||||
Show All 9 Lines | |||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||||
*/ | */ | ||||
/* | /* | ||||
* emulate POSIX time functionality on Windows. | * emulate POSIX time functionality on Windows. | ||||
*/ | */ | ||||
// note: clock_gettime et al. have been removed. callers should use the | |||||
// WHRT directly, rather than needlessly translating s -> ns -> s, | |||||
// which costs time and accuracy. | |||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#include "lib/sysdep/os/win/wposix/wtime.h" | #include "lib/sysdep/os/win/wposix/wtime.h" | ||||
#include "lib/sysdep/os/win/wposix/wposix_internal.h" | #include "lib/sysdep/os/win/wposix/wposix_internal.h" | ||||
#include "lib/sysdep/os/win/whrt/whrt.h" | |||||
WINIT_REGISTER_MAIN_INIT(wtime_Init); // whrt -> wtime | WINIT_REGISTER_MAIN_INIT(wtime_Init); // whrt -> wtime | ||||
// NT system time and FILETIME are hectonanoseconds since Jan. 1, 1601 UTC. | // NT system time and FILETIME are hectonanoseconds since Jan. 1, 1601 UTC. | ||||
// SYSTEMTIME is a struct containing month, year, etc. | // SYSTEMTIME is a struct containing month, year, etc. | ||||
static const long _1e3 = 1000; | static const long _1e3 = 1000; | ||||
static const long _1e6 = 1000000; | static const long _1e6 = 1000000; | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | |||||
static void LatchInitialSystemTime() | static void LatchInitialSystemTime() | ||||
{ | { | ||||
FILETIME ft; | FILETIME ft; | ||||
GetSystemTimeAsFileTime(&ft); | GetSystemTimeAsFileTime(&ft); | ||||
const u64 hns = u64_from_FILETIME(&ft); | const u64 hns = u64_from_FILETIME(&ft); | ||||
stInitial_ns = (hns - posix_epoch_hns) * 100; | stInitial_ns = (hns - posix_epoch_hns) * 100; | ||||
} | } | ||||
// return nanoseconds since POSIX epoch. | |||||
// algorithm: add current HRT value to the startup system time | |||||
static i64 CurrentSystemTime_ns() | |||||
{ | |||||
const i64 ns = stInitial_ns + i64(whrt_Time() * _1e9); | |||||
return ns; | |||||
} | |||||
static timespec TimespecFromNs(i64 ns) | static timespec TimespecFromNs(i64 ns) | ||||
{ | { | ||||
timespec ts; | timespec ts; | ||||
ts.tv_sec = (time_t)((ns / _1e9) & 0xFFFFFFFF); | ts.tv_sec = (time_t)((ns / _1e9) & 0xFFFFFFFF); | ||||
ts.tv_nsec = (long)(ns % _1e9); | ts.tv_nsec = (long)(ns % _1e9); | ||||
return ts; | return ts; | ||||
} | } | ||||
static size_t MsFromTimespec(const timespec& ts) | static size_t MsFromTimespec(const timespec& ts) | ||||
{ | { | ||||
i64 ms = ts.tv_sec; // avoid overflow | i64 ms = ts.tv_sec; // avoid overflow | ||||
ms *= _1e3; | ms *= _1e3; | ||||
ms += ts.tv_nsec / _1e6; | ms += ts.tv_nsec / _1e6; | ||||
return size_t(ms); | return size_t(ms); | ||||
} | } | ||||
//----------------------------------------------------------------------------- | |||||
int clock_gettime(clockid_t clock, struct timespec* ts) | |||||
{ | |||||
ENSURE(clock == CLOCK_REALTIME || clock == CLOCK_MONOTONIC); | |||||
const i64 ns = CurrentSystemTime_ns(); | |||||
*ts = TimespecFromNs(ns); | |||||
return 0; | |||||
} | |||||
int clock_getres(clockid_t clock, struct timespec* ts) | |||||
{ | |||||
ENSURE(clock == CLOCK_REALTIME || clock == CLOCK_MONOTONIC); | |||||
const double resolution = whrt_Resolution(); | |||||
const i64 ns = i64(resolution * 1e9); | |||||
*ts = TimespecFromNs(ns); | |||||
return 0; | |||||
} | |||||
int nanosleep(const struct timespec* rqtp, struct timespec* /* rmtp */) | int nanosleep(const struct timespec* rqtp, struct timespec* /* rmtp */) | ||||
{ | { | ||||
const DWORD ms = (DWORD)MsFromTimespec(*rqtp); | const DWORD ms = (DWORD)MsFromTimespec(*rqtp); | ||||
if(ms) | if(ms) | ||||
Sleep(ms); | Sleep(ms); | ||||
return 0; | return 0; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 473 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator