From 89dfa2bc22577c758cb8badd34e4f177192a6eff Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Tue, 26 Nov 2019 00:45:45 +0000 Subject: [PATCH] [util] Add platform-specific clock implementation MinGW calls to GetSystemTimeAsFileTime via gettimeofday for std::chrono::high_resolution_clock::now, This is horribly slow and inaccurate. There are also issues if MinGW is not built with libstdc++ at the same time that can cause the precision to be bad enough to cause massive hangs. This effectively works around that as well. Relevant: ValveSoftware/Proton#3198 --- src/util/util_time.h | 50 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/util/util_time.h diff --git a/src/util/util_time.h b/src/util/util_time.h new file mode 100644 index 00000000..3eca03bb --- /dev/null +++ b/src/util/util_time.h @@ -0,0 +1,50 @@ +#pragma once + +#include +#include + +#ifdef _WIN32 +#include +#endif + +namespace dxvk { + +#ifdef _WIN32 + struct high_resolution_clock { + static constexpr bool is_steady = true; + + using rep = int64_t; + using period = std::nano; + using duration = std::chrono::nanoseconds; + using time_point = std::chrono::time_point; + + static inline time_point now() noexcept { + // Keep the frequency static, this doesn't change at all. + static const int64_t freq = getFrequency(); + const int64_t counter = getCounter(); + + const int64_t whole = (counter / freq) * period::den; + const int64_t part = (counter % freq) * period::den / freq; + + return time_point(duration(whole + part)); + } + + static inline int64_t getFrequency() { + LARGE_INTEGER freq; + QueryPerformanceFrequency(&freq); + + return freq.QuadPart; + } + + static inline int64_t getCounter() { + LARGE_INTEGER count; + QueryPerformanceCounter(&count); + + return count.QuadPart; + } + }; +#else + using clock = std::chrono::high_resolution_clock; +#endif + +} \ No newline at end of file