From c09e2af17fab03d3d36c20e5201a560c3e4c233e Mon Sep 17 00:00:00 2001 From: mtklein Date: Mon, 13 Oct 2014 12:48:16 -0700 Subject: Fix race in supports_simd(). Local statics are not thread safe in Chrome. Use an SkLazyPtr instead. See https://code.google.com/p/chromium/issues/detail?id=418041 BUG=418041 Review URL: https://codereview.chromium.org/655573002 --- src/opts/opts_check_x86.cpp | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/opts/opts_check_x86.cpp b/src/opts/opts_check_x86.cpp index 55eb843312..8fec2ba69c 100644 --- a/src/opts/opts_check_x86.cpp +++ b/src/opts/opts_check_x86.cpp @@ -16,6 +16,7 @@ #include "SkBlitRow_opts_SSE4.h" #include "SkBlurImage_opts_SSE2.h" #include "SkBlurImage_opts_SSE4.h" +#include "SkLazyPtr.h" #include "SkMorphology_opts.h" #include "SkMorphology_opts_SSE2.h" #include "SkRTConf.h" @@ -79,22 +80,29 @@ static inline void getcpuid(int info_type, int info[4]) { /* Fetch the SIMD level directly from the CPU, at run-time. * Only checks the levels needed by the optimizations in this file. */ -static int get_SIMD_level() { - int cpu_info[4] = { 0 }; - +namespace { // get_SIMD_level() technically must have external linkage, so no static. +int* get_SIMD_level() { + int cpu_info[4] = { 0, 0, 0, 0 }; getcpuid(1, cpu_info); + + int* level = SkNEW(int); + if ((cpu_info[2] & (1<<20)) != 0) { - return SK_CPU_SSE_LEVEL_SSE42; + *level = SK_CPU_SSE_LEVEL_SSE42; } else if ((cpu_info[2] & (1<<19)) != 0) { - return SK_CPU_SSE_LEVEL_SSE41; + *level = SK_CPU_SSE_LEVEL_SSE41; } else if ((cpu_info[2] & (1<<9)) != 0) { - return SK_CPU_SSE_LEVEL_SSSE3; + *level = SK_CPU_SSE_LEVEL_SSSE3; } else if ((cpu_info[3] & (1<<26)) != 0) { - return SK_CPU_SSE_LEVEL_SSE2; + *level = SK_CPU_SSE_LEVEL_SSE2; } else { - return 0; + *level = 0; } + return level; } +} // namespace + +SK_DECLARE_STATIC_LAZY_PTR(int, gSIMDLevel, get_SIMD_level); /* Verify that the requested SIMD level is supported in the build. * If not, check if the platform supports it. @@ -115,8 +123,7 @@ static inline bool supports_simd(int minLevel) { */ return false; #else - static int gSIMDLevel = get_SIMD_level(); - return (minLevel <= gSIMDLevel); + return minLevel <= *gSIMDLevel.get(); #endif } } -- cgit v1.2.3