[libcxx-commits] [PATCH] D104987: Use GetSystemTimePreciseAsFileTime() if available
Sizhe Zhao via Phabricator via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Jul 9 17:59:21 PDT 2021
Prince213 updated this revision to Diff 357678.
Prince213 removed a subscriber: libc++.
Prince213 added a comment.
Try to detect if GetSystemTimePreciseAsFileTime is available at runtime when it is not present at compile time.
Users might target a lower version of Windows, but the same code might later in time run in an recent version of Windows, where GetSystemTimePreciseAsFileTime is available. We'll try to detect this situation at runtime.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D104987/new/
https://reviews.llvm.org/D104987
Files:
libcxx/src/chrono.cpp
Index: libcxx/src/chrono.cpp
===================================================================
--- libcxx/src/chrono.cpp
+++ libcxx/src/chrono.cpp
@@ -63,6 +63,20 @@
#if defined(_LIBCPP_WIN32API)
+#if (_WIN32_WINNT < _WIN32_WINNT_WIN8 || !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)) && \
+ _WIN32_WINNT < _WIN32_WINNT_WIN10
+
+typedef void(WINAPI* FP)(LPFILETIME);
+
+static FP init_system_clock() {
+ FP fp = (FP)GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetSystemTimePreciseAsFileTime");
+ if (nullptr == fp)
+ return GetSystemTimeAsFileTime;
+ return fp;
+}
+
+#endif
+
static system_clock::time_point __libcpp_system_clock_now() {
// FILETIME is in 100ns units
using filetime_duration =
@@ -74,10 +88,12 @@
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
- GetSystemTimeAsFileTime(&ft);
+ static FP fp = init_system_clock();
+ fp(&ft);
#endif
filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D104987.357678.patch
Type: text/x-patch
Size: 1334 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210710/9dbcc748/attachment.bin>
More information about the libcxx-commits
mailing list