From 1ef960b01b3d76d9dac19a0d38d71dd03bbb9f21 Mon Sep 17 00:00:00 2001 From: bungeman Date: Fri, 25 Jul 2014 15:11:03 -0700 Subject: [PATCH] Fix thread unsafe mutex initialization. BUG=skia:2779 Committed: https://skia.googlesource.com/skia/+/d6aeb6dc8fe21066f1a2c4813a4256a3acd3edf5 R=robertphillips@google.com, mtklein@google.com, reed@android.com, bsalomon@google.com Author: bungeman@google.com Review URL: https://codereview.chromium.org/419113002 --- src/ports/SkMutex_pthread.h | 12 +++++++----- src/ports/SkMutex_win.h | 4 +--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ports/SkMutex_pthread.h b/src/ports/SkMutex_pthread.h index 3bf3628588..9aaa0612e7 100644 --- a/src/ports/SkMutex_pthread.h +++ b/src/ports/SkMutex_pthread.h @@ -89,11 +89,13 @@ private: #define SK_BASE_MUTEX_INIT { PTHREAD_MUTEX_INITIALIZER, SkDEBUGCODE(0) } // Using POD-style initialization prevents the generation of a static initializer. +// // Without magic statics there are no thread safety guarantees on initialization -// of local statics (even POD). -// As a result, it is illegal to SK_DECLARE_STATIC_MUTEX in a function. -#define SK_DECLARE_STATIC_MUTEX(name) \ - static inline void SK_MACRO_APPEND_LINE(name)(){} \ - static SkBaseMutex name = SK_BASE_MUTEX_INIT +// of local statics (even POD). As a result, it is illegal to use +// SK_DECLARE_STATIC_MUTEX in a function. +// +// Because SkBaseMutex is not a primitive, a static SkBaseMutex cannot be +// initialized in a class with this macro. +#define SK_DECLARE_STATIC_MUTEX(name) namespace {} static SkBaseMutex name = SK_BASE_MUTEX_INIT #endif diff --git a/src/ports/SkMutex_win.h b/src/ports/SkMutex_win.h index ccad063c27..fe06336a90 100644 --- a/src/ports/SkMutex_win.h +++ b/src/ports/SkMutex_win.h @@ -74,8 +74,6 @@ class SkMutex : public SkBaseMutex { }; // Windows currently provides no documented means of POD initializing a CRITICAL_SECTION. // As a result, it is illegal to SK_DECLARE_STATIC_MUTEX in a function. -#define SK_DECLARE_STATIC_MUTEX(name) \ - static inline void SK_MACRO_APPEND_LINE(name)(){} \ - static SkBaseMutex name +#define SK_DECLARE_STATIC_MUTEX(name) namespace{} static SkBaseMutex name #endif