]> gitweb.factorcode.org Git - factor.git/commitdiff
workaround for QueryPerformanceCounter: if the high part of the count doesn't increme...
authorDoug Coleman <doug.coleman@gmail.com>
Mon, 14 Dec 2009 02:42:51 +0000 (20:42 -0600)
committerDoug Coleman <doug.coleman@gmail.com>
Mon, 14 Dec 2009 02:42:51 +0000 (20:42 -0600)
vm/os-windows-nt.cpp

index 796834a9c4529582586199b4493f7d6b618cc8b3..7890ec5ff693c11a51d35950dd062090faf105af 100755 (executable)
@@ -36,10 +36,15 @@ u64 system_micros()
                - EPOCH_OFFSET) / 10;
 }
 
+/* On VirtualBox, QueryPerformanceCounter does not increment
+the high part every time the low part overflows.  Workaround. */
 u64 nano_count()
 {
        LARGE_INTEGER count;
        LARGE_INTEGER frequency;
+       static u32 hi_correction = 0;
+       static u32 hi = 0xffffffff;
+       static u32 lo = 0xffffffff;
        BOOL ret;
        ret = QueryPerformanceCounter(&count);
        if(ret == 0)
@@ -47,7 +52,14 @@ u64 nano_count()
        ret = QueryPerformanceFrequency(&frequency);
        if(ret == 0)
                fatal_error("QueryPerformanceFrequency", 0);
-       
+
+       if((u32)count.LowPart < lo && (u32)count.HighPart == hi)
+               hi_correction++;
+
+       hi = count.HighPart;
+       lo = count.LowPart;
+       count.HighPart += hi_correction;
+
        return count.QuadPart*(1000000000/frequency.QuadPart);
 }