util/os_time: Safe os_time_get_nano for Windows
authorJames Park <jpark37@lagfreegames.com>
Tue, 3 Nov 2020 18:03:13 +0000 (10:03 -0800)
committerMarge Bot <eric+marge@anholt.net>
Thu, 26 Nov 2020 07:58:56 +0000 (07:58 +0000)
Avoid small possibility of reading torn write on 32-bit platforms.

If frequency caching is desired, it's probably better to initialize from
C++ and extern "C" instead. It's not a tremendous optimization though.

Reviewed-by: Jose Fonseca <jfonseca@vmware.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7138>

src/util/os_time.c

index 92fc363..d2edd88 100644 (file)
@@ -67,11 +67,10 @@ os_time_get_nano(void)
 
 #elif DETECT_OS_WINDOWS
 
-   static LARGE_INTEGER frequency;
+   LARGE_INTEGER frequency;
    LARGE_INTEGER counter;
    int64_t secs, nanosecs;
-   if(!frequency.QuadPart)
-      QueryPerformanceFrequency(&frequency);
+   QueryPerformanceFrequency(&frequency);
    QueryPerformanceCounter(&counter);
    /* Compute seconds and nanoseconds parts separately to
     * reduce severity of precision loss.