From 711d3dc7c87ebc0c37777fbcf7282ed573331ca2 Mon Sep 17 00:00:00 2001 From: Minglei Jin Date: Thu, 28 Jan 2021 20:22:39 +0800 Subject: [PATCH] clock_gettime/win: first round clock_gettime for windows --- deps/rmonotonic/src/clock_gettime_win.c | 72 +++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 deps/rmonotonic/src/clock_gettime_win.c diff --git a/deps/rmonotonic/src/clock_gettime_win.c b/deps/rmonotonic/src/clock_gettime_win.c new file mode 100644 index 0000000000..98a1baaf39 --- /dev/null +++ b/deps/rmonotonic/src/clock_gettime_win.c @@ -0,0 +1,72 @@ +#if defined(_WIN32) || defined(_WIN64) +#include +#include + +#define MS_PER_SEC 1000ULL // MS = milliseconds +#define US_PER_MS 1000ULL // US = microseconds +#define HNS_PER_US 10ULL // HNS = hundred-nanoseconds (e.g., 1 hns = 100 ns) +#define NS_PER_US 1000ULL + +#define HNS_PER_SEC (MS_PER_SEC * US_PER_MS * HNS_PER_US) +#define NS_PER_HNS (100ULL) // NS = nanoseconds +#define NS_PER_SEC (MS_PER_SEC * US_PER_MS * NS_PER_US) + +int clock_gettime_monotonic(struct timespec *tv) +{ + static LARGE_INTEGER ticksPerSec; + LARGE_INTEGER ticks; + double seconds; + + if (!ticksPerSec.QuadPart) { + QueryPerformanceFrequency(&ticksPerSec); + if (!ticksPerSec.QuadPart) { + errno = ENOTSUP; + return -1; + } + } + + QueryPerformanceCounter(&ticks); + + seconds = (double) ticks.QuadPart / (double) ticksPerSec.QuadPart; + tv->tv_sec = (time_t)seconds; + tv->tv_nsec = (long)((ULONGLONG)(seconds * NS_PER_SEC) % NS_PER_SEC); + + return 0; +} + +int clock_gettime_realtime(struct timespec *tv) +{ + FILETIME ft; + ULARGE_INTEGER hnsTime; + + GetSystemTimeAsFileTime(&ft); + + hnsTime.LowPart = ft.dwLowDateTime; + hnsTime.HighPart = ft.dwHighDateTime; + + // To get POSIX Epoch as baseline, subtract the number of hns intervals from Jan 1, 1601 to Jan 1, 1970. + hnsTime.QuadPart -= (11644473600ULL * HNS_PER_SEC); + + // modulus by hns intervals per second first, then convert to ns, as not to lose resolution + tv->tv_nsec = (long) ((hnsTime.QuadPart % HNS_PER_SEC) * NS_PER_HNS); + tv->tv_sec = (long) (hnsTime.QuadPart / HNS_PER_SEC); + + return 0; +} + +int clock_gettime(clockid_t type, struct timespec *tp) +{ + if (type == CLOCK_MONOTONIC) + { + return clock_gettime_monotonic(tp); + } + else if (type == CLOCK_REALTIME) + { + return clock_gettime_realtime(tp); + } + + errno = ENOTSUP; + return -1; +} + +#endif -- GitLab