[Mesa-dev] [PATCH 1/8] gallium/u_cpu_detect: fix a race condition on initialization

Marek Olšák maraeo at gmail.com
Thu Sep 6 04:02:22 UTC 2018


From: Marek Olšák <marek.olsak at amd.com>

---
 src/gallium/auxiliary/util/u_cpu_detect.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/src/gallium/auxiliary/util/u_cpu_detect.c b/src/gallium/auxiliary/util/u_cpu_detect.c
index 14003aa7692..93cf58011db 100644
--- a/src/gallium/auxiliary/util/u_cpu_detect.c
+++ b/src/gallium/auxiliary/util/u_cpu_detect.c
@@ -29,20 +29,21 @@
  * CPU feature detection.
  *
  * @author Dennis Smit
  * @author Based on the work of Eric Anholt <anholt at FreeBSD.org>
  */
 
 #include "pipe/p_config.h"
 
 #include "u_debug.h"
 #include "u_cpu_detect.h"
+#include "c11/threads.h"
 
 #if defined(PIPE_ARCH_PPC)
 #if defined(PIPE_OS_APPLE)
 #include <sys/sysctl.h>
 #else
 #include <signal.h>
 #include <setjmp.h>
 #endif
 #endif
 
@@ -359,28 +360,23 @@ check_os_arm_support(void)
              util_cpu_caps.has_neon = (hwcap >> 12) & 1;
              break;
           }
        }
        close (fd);
     }
 #endif /* PIPE_OS_LINUX */
 }
 #endif /* PIPE_ARCH_ARM */
 
-void
-util_cpu_detect(void)
+static void
+util_cpu_detect_once(void)
 {
-   static boolean util_cpu_detect_initialized = FALSE;
-
-   if(util_cpu_detect_initialized)
-      return;
-
    memset(&util_cpu_caps, 0, sizeof util_cpu_caps);
 
    /* Count the number of CPUs in system */
 #if defined(PIPE_OS_WINDOWS)
    {
       SYSTEM_INFO system_info;
       GetSystemInfo(&system_info);
       util_cpu_caps.nr_cpus = system_info.dwNumberOfProcessors;
    }
 #elif defined(PIPE_OS_UNIX) && defined(_SC_NPROCESSORS_ONLN)
@@ -554,13 +550,19 @@ util_cpu_detect(void)
       debug_printf("util_cpu_caps.has_avx512dq = %u\n", util_cpu_caps.has_avx512dq);
       debug_printf("util_cpu_caps.has_avx512ifma = %u\n", util_cpu_caps.has_avx512ifma);
       debug_printf("util_cpu_caps.has_avx512pf = %u\n", util_cpu_caps.has_avx512pf);
       debug_printf("util_cpu_caps.has_avx512er = %u\n", util_cpu_caps.has_avx512er);
       debug_printf("util_cpu_caps.has_avx512cd = %u\n", util_cpu_caps.has_avx512cd);
       debug_printf("util_cpu_caps.has_avx512bw = %u\n", util_cpu_caps.has_avx512bw);
       debug_printf("util_cpu_caps.has_avx512vl = %u\n", util_cpu_caps.has_avx512vl);
       debug_printf("util_cpu_caps.has_avx512vbmi = %u\n", util_cpu_caps.has_avx512vbmi);
    }
 #endif
+}
+
+static once_flag cpu_once_flag = ONCE_FLAG_INIT;
 
-   util_cpu_detect_initialized = TRUE;
+void
+util_cpu_detect(void)
+{
+   call_once(&cpu_once_flag, util_cpu_detect_once);
 }
-- 
2.17.1



More information about the mesa-dev mailing list