Fix PR#41323 'Race condition in steady_clock::now for _LIBCPP_WIN32API'. thanks to Ivan Afanasyev for the report.
llvm-svn: 357413
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: ecad92b0680716857d135ef40bbf595354f35867
diff --git a/src/chrono.cpp b/src/chrono.cpp
index ea0a638..1758e6c 100644
--- a/src/chrono.cpp
+++ b/src/chrono.cpp
@@ -177,16 +177,25 @@
#elif defined(_LIBCPP_WIN32API)
+// https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says:
+// If the function fails, the return value is zero. <snip>
+// On systems that run Windows XP or later, the function will always succeed
+// and will thus never return zero.
+
+static LARGE_INTEGER
+__QueryPerformanceFrequency()
+{
+ LARGE_INTEGER val;
+ (void) QueryPerformanceFrequency(&val);
+ return val;
+}
+
steady_clock::time_point
steady_clock::now() _NOEXCEPT
{
- static LARGE_INTEGER freq;
- static BOOL initialized = FALSE;
- if (!initialized)
- initialized = QueryPerformanceFrequency(&freq); // always succceeds
+ static const LARGE_INTEGER freq = __QueryPerformanceFrequency();
- LARGE_INTEGER counter;
- QueryPerformanceCounter(&counter);
+ LARGE_INTEGER counter = __QueryPerformanceFrequency();
return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
}