[PATCH xserver 4/4] os: Use 64-bit tick count on Windows when available

Jeff Smith whydoubt at gmail.com
Wed Dec 27 04:10:55 UTC 2017


On Windows, GetTickCount() returns a value that wraps after 49.7 days.
Windows Vista and later offer GetTickCount64(), which does not have that
issue.

In places that can make use of more than 32 bits from the tick counter,
use a 64-bit counter if availabile.

Signed-off-by: Jeff Smith <whydoubt at gmail.com>
---
 os/utils.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/os/utils.c b/os/utils.c
index f3d0f71..f147bcf 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -442,6 +442,7 @@ ForceClockId(clockid_t forced_clockid)
 #endif
 
 #if (defined WIN32 && defined __MINGW32__) || defined(__CYGWIN__)
+typedef ULONGLONG (WINAPI *LPFN_GETTICKCOUNT64)();
 CARD32
 GetTimeInMillis(void)
 {
@@ -450,6 +451,18 @@ GetTimeInMillis(void)
 CARD64
 GetTimeInMicros(void)
 {
+    static Bool firstTime = TRUE;
+    static LPFN_GETTICKCOUNT64 fnGetTickCount64 = NULL;
+
+    if (firstTime) {
+        HMODULE module = GetModuleHandle(TEXT("kernel32"));
+        fnGetTickCount64 =
+            (LPFN_GETTICKCOUNT64) GetProcAddress(module, "GetTickCount64");
+        firstTime = FALSE;
+    }
+    if (NULL != fnGetTickCount64)
+        return (CARD64) fnGetTickCount64() * 1000;
+
     return (CARD64) GetTickCount() * 1000;
 }
 #elif defined(MONOTONIC_CLOCK)
-- 
2.9.4



More information about the xorg-devel mailing list