Index: include/core/SkInstCnt.h |
diff --git a/include/core/SkInstCnt.h b/include/core/SkInstCnt.h |
index 89bbfa1126d12017b6a77486421baa8ad22f4b46..7e5b454b15d67f4ad7c082a5b2b3af17e1073af5 100644 |
--- a/include/core/SkInstCnt.h |
+++ b/include/core/SkInstCnt.h |
@@ -20,9 +20,16 @@ |
#include "SkTypes.h" |
#if SK_ENABLE_INST_COUNT |
+// Static variables inside member functions below may be defined multiple times |
+// if Skia is being used as a dynamic library. Instance counting should be on |
+// only for static builds. See bug skia:2058. |
+#if defined(SKIA_DLL) |
+#error Instance counting works only when Skia is built as a static library. |
+#endif |
+ |
+#include "SkOnce.h" |
#include "SkTArray.h" |
#include "SkThread.h" |
- |
extern bool gPrintInstCount; |
// The non-root classes just register themselves with their parent |
@@ -38,17 +45,16 @@ extern bool gPrintInstCount; |
#define SK_DECLARE_INST_COUNT_INTERNAL(className, initStep) \ |
class SkInstanceCountHelper { \ |
public: \ |
- typedef int (*PFCheckInstCnt)(int level, bool cleanUp); \ |
SkInstanceCountHelper() { \ |
- static bool gInited; \ |
- if (!gInited) { \ |
- initStep \ |
- GetChildren() = new SkTArray<PFCheckInstCnt>; \ |
- gInited = true; \ |
- } \ |
+ SK_DECLARE_STATIC_ONCE(once); \ |
+ SkOnce(&once, init, 0); \ |
sk_atomic_inc(GetInstanceCountPtr()); \ |
} \ |
\ |
+ static void init(int) { \ |
+ initStep \ |
+ } \ |
+ \ |
SkInstanceCountHelper(const SkInstanceCountHelper&) { \ |
sk_atomic_inc(GetInstanceCountPtr()); \ |
} \ |
@@ -62,11 +68,16 @@ extern bool gPrintInstCount; |
return &gInstanceCount; \ |
} \ |
\ |
- static SkTArray<PFCheckInstCnt>*& GetChildren() { \ |
- static SkTArray<PFCheckInstCnt>* gChildren; \ |
+ static SkTArray<int (*)(int, bool)>*& GetChildren() { \ |
+ static SkTArray<int (*)(int, bool)>* gChildren; \ |
return gChildren; \ |
} \ |
\ |
+ static SkBaseMutex& GetChildrenMutex() { \ |
+ SK_DECLARE_STATIC_MUTEX(childrenMutex); \ |
+ return childrenMutex; \ |
+ } \ |
+ \ |
} fInstanceCountHelper; \ |
\ |
static int32_t GetInstanceCount() { \ |
@@ -86,7 +97,7 @@ extern bool gPrintInstCount; |
if (NULL == SkInstanceCountHelper::GetChildren()) { \ |
return GetInstanceCount(); \ |
} \ |
- SkTArray<int (*)(int, bool)>* children = \ |
+ SkTArray<int (*)(int, bool)>* children = \ |
SkInstanceCountHelper::GetChildren(); \ |
int childCount = children->count(); \ |
int count = GetInstanceCount(); \ |
@@ -105,8 +116,12 @@ extern bool gPrintInstCount; |
} \ |
\ |
static void AddInstChild(int (*childCheckInstCnt)(int, bool)) { \ |
- if (CheckInstanceCount != childCheckInstCnt && \ |
- NULL != SkInstanceCountHelper::GetChildren()) { \ |
+ if (CheckInstanceCount != childCheckInstCnt) { \ |
+ SkAutoMutexAcquire ama(SkInstanceCountHelper::GetChildrenMutex()); \ |
+ if (NULL == SkInstanceCountHelper::GetChildren()) { \ |
+ SkInstanceCountHelper::GetChildren() = \ |
+ new SkTArray<int (*)(int, bool)>; \ |
+ } \ |
SkInstanceCountHelper::GetChildren()->push_back(childCheckInstCnt); \ |
} \ |
} |