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
This commit is contained in:
mtklein 2014-10-13 12:48:16 -07:00 committed by Commit bot
parent 0880686d95
commit c09e2af17f

View File

@ -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
}
}