[libcxx-commits] [libcxx] 3ec634e - [libcxx] Use GetSystemTimePreciseAsFileTime() if available

Martin Storsjö via libcxx-commits libcxx-commits at lists.llvm.org
Fri Aug 27 10:12:06 PDT 2021


Author: Sizhe Zhao
Date: 2021-08-27T20:11:29+03:00
New Revision: 3ec634e65a02d5f443cc982e9e00ec4e51143d07

URL: https://github.com/llvm/llvm-project/commit/3ec634e65a02d5f443cc982e9e00ec4e51143d07
DIFF: https://github.com/llvm/llvm-project/commit/3ec634e65a02d5f443cc982e9e00ec4e51143d07.diff

LOG: [libcxx] Use GetSystemTimePreciseAsFileTime() if available

We will try to use GetSystemTimePreciseAsFileTime if possible.
Reference: https://sourceforge.net/p/mingw-w64/mingw-w64/ci/59195b2d7fe26549f70969b0dd487293819f023e/.

Reviewed By: compnerd, #libc, mstorsjo, ldionne

Differential Revision: https://reviews.llvm.org/D104987

Added: 
    

Modified: 
    libcxx/src/chrono.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/src/chrono.cpp b/libcxx/src/chrono.cpp
index 13b375947b4d9..6f5d2ba7649b2 100644
--- a/libcxx/src/chrono.cpp
+++ b/libcxx/src/chrono.cpp
@@ -63,6 +63,28 @@ namespace chrono
 
 #if defined(_LIBCPP_WIN32API)
 
+#if _WIN32_WINNT < _WIN32_WINNT_WIN8
+
+namespace {
+
+typedef void(WINAPI *GetSystemTimeAsFileTimePtr)(LPFILETIME);
+
+class GetSystemTimeInit {
+public:
+  GetSystemTimeInit() {
+    fp = (GetSystemTimeAsFileTimePtr)GetProcAddress(
+        GetModuleHandleW(L"kernel32.dll"), "GetSystemTimePreciseAsFileTime");
+    if (fp == nullptr)
+      fp = GetSystemTimeAsFileTime;
+  }
+  GetSystemTimeAsFileTimePtr fp;
+};
+
+GetSystemTimeInit GetSystemTimeAsFileTimeFunc _LIBCPP_INIT_PRIORITY_MAX;
+} // namespace
+
+#endif
+
 static system_clock::time_point __libcpp_system_clock_now() {
   // FILETIME is in 100ns units
   using filetime_duration =
@@ -74,10 +96,13 @@ static system_clock::time_point __libcpp_system_clock_now() {
   static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
 
   FILETIME ft;
-#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
+#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)) || \
+    (_WIN32_WINNT >= _WIN32_WINNT_WIN10)
   GetSystemTimePreciseAsFileTime(&ft);
-#else
+#elif !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
   GetSystemTimeAsFileTime(&ft);
+#else
+  GetSystemTimeAsFileTimeFunc.fp(&ft);
 #endif
 
   filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |


        


More information about the libcxx-commits mailing list