[libcxx-commits] [libcxx] r357413 - Fix PR#41323 'Race condition in steady_clock::now for _LIBCPP_WIN32API'. thanks to Ivan Afanasyev for the report.

Marshall Clow via libcxx-commits libcxx-commits at lists.llvm.org
Mon Apr 1 10:23:30 PDT 2019


Author: marshall
Date: Mon Apr  1 10:23:30 2019
New Revision: 357413

URL: http://llvm.org/viewvc/llvm-project?rev=357413&view=rev
Log:
Fix PR#41323 'Race condition in steady_clock::now for _LIBCPP_WIN32API'. thanks to Ivan Afanasyev for the report.

Modified:
    libcxx/trunk/src/chrono.cpp

Modified: libcxx/trunk/src/chrono.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/chrono.cpp?rev=357413&r1=357412&r2=357413&view=diff
==============================================================================
--- libcxx/trunk/src/chrono.cpp (original)
+++ libcxx/trunk/src/chrono.cpp Mon Apr  1 10:23:30 2019
@@ -177,16 +177,25 @@ steady_clock::now() _NOEXCEPT
 
 #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));
 }
 




More information about the libcxx-commits mailing list