Mesa (main): c11: Implement timespec_get on win32 properly when not available

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Thu Jun 9 18:00:21 UTC 2022


Module: Mesa
Branch: main
Commit: 65d5ee4012842646f157693d179a76d9071989f6
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=65d5ee4012842646f157693d179a76d9071989f6

Author: Yonggang Luo <luoyonggang at gmail.com>
Date:   Wed Apr 20 00:22:24 2022 +0800

c11: Implement timespec_get on win32 properly when not available

Signed-off-by: Yonggang Luo <luoyonggang at gmail.com>
Reviewed-by: Jason Ekstrand <jason.ekstrand at collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/15497>

---

 src/c11/impl/time.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/src/c11/impl/time.c b/src/c11/impl/time.c
index 7c9cf6eead8..8a44412b963 100644
--- a/src/c11/impl/time.c
+++ b/src/c11/impl/time.c
@@ -11,18 +11,36 @@
 
 #if defined(_WIN32) && !defined(__CYGWIN__)
 
-#include <assert.h>
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN 1
+#endif
+#include <windows.h>
 
 int
 timespec_get(struct timespec *ts, int base)
 {
-    assert(ts != NULL);
+/* difference between 1970 and 1601 */
+#define _TIMESPEC_IMPL_UNIX_EPOCH_IN_TICKS 116444736000000000ull
+/* 1 tick is 100 nanoseconds */
+#define _TIMESPEC_IMPL_TICKS_PER_SECONDS 10000000ull
+    if (!ts)
+        return 0;
     if (base == TIME_UTC) {
-        ts->tv_sec = time(NULL);
-        ts->tv_nsec = 0;
+        FILETIME ft;
+        ULARGE_INTEGER date;
+        LONGLONG ticks;
+
+        GetSystemTimeAsFileTime(&ft);
+        date.HighPart = ft.dwHighDateTime;
+        date.LowPart = ft.dwLowDateTime;
+        ticks = (LONGLONG)(date.QuadPart - _TIMESPEC_IMPL_UNIX_EPOCH_IN_TICKS);
+        ts->tv_sec = ticks / _TIMESPEC_IMPL_TICKS_PER_SECONDS;
+        ts->tv_nsec = (ticks % _TIMESPEC_IMPL_TICKS_PER_SECONDS) * 100;
         return base;
     }
     return 0;
+#undef _TIMESPEC_IMPL_UNIX_EPOCH_IN_TICKS
+#undef _TIMESPEC_IMPL_TICKS_PER_SECONDS
 }
 
 #else



More information about the mesa-commit mailing list