aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar tomhudson <tomhudson@chromium.org>2014-07-01 08:06:14 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2014-07-01 08:06:14 -0700
commit60b08a0adfe73f593af62c8d3f55958438360e1b (patch)
treefbd5d0eac47661cf4d4c59566868ff285a7866a5
parent950f89dd20325bf4dfce588d9f64644040787a06 (diff)
Fix race condition in parallel font initialization.
Uses a mutex to guard construction of the singleton, which initialies the non-threadsafe libfontconfig. Without this change, the parallel path ops test runner crashes 6/10 and hangs 2/10 on startup; with this change, 0/10 problems. BUG=skia:2693 R=mtklein@google.com, bungeman@google.com, reed@google.com, tomhudson@google.com Committed: https://skia.googlesource.com/skia/+/df022f5972ae6a2a1d96d15c50eca52cade3abd8 Author: tomhudson@chromium.org Review URL: https://codereview.chromium.org/355573006
-rw-r--r--include/ports/SkFontConfigInterface.h5
-rw-r--r--src/ports/SkFontConfigInterface_android.cpp3
-rw-r--r--src/ports/SkFontConfigInterface_direct.cpp10
-rw-r--r--src/ports/SkFontHost_fontconfig.cpp2
4 files changed, 14 insertions, 6 deletions
diff --git a/include/ports/SkFontConfigInterface.h b/include/ports/SkFontConfigInterface.h
index 8c12a5628a..f548f37d5d 100644
--- a/include/ports/SkFontConfigInterface.h
+++ b/include/ports/SkFontConfigInterface.h
@@ -14,6 +14,8 @@
#include "SkTArray.h"
#include "SkTypeface.h"
+struct SkBaseMutex;
+
/**
* \class SkFontConfigInterface
*
@@ -95,8 +97,9 @@ public:
/**
* Return a singleton instance of a direct subclass that calls into
* libfontconfig. This does not affect the refcnt of the returned instance.
+ * The mutex may be used to guarantee the singleton is only constructed once.
*/
- static SkFontConfigInterface* GetSingletonDirectInterface();
+ static SkFontConfigInterface* GetSingletonDirectInterface(SkBaseMutex* mutex);
// New APIS, which have default impls for now (which do nothing)
diff --git a/src/ports/SkFontConfigInterface_android.cpp b/src/ports/SkFontConfigInterface_android.cpp
index 20e6c5eb84..1f901b9fa9 100644
--- a/src/ports/SkFontConfigInterface_android.cpp
+++ b/src/ports/SkFontConfigInterface_android.cpp
@@ -155,7 +155,8 @@ static SkFontConfigInterfaceAndroid* getSingletonInterface() {
return gFontConfigInterface;
}
-SkFontConfigInterface* SkFontConfigInterface::GetSingletonDirectInterface() {
+SkFontConfigInterface* SkFontConfigInterface::GetSingletonDirectInterface(SkBaseMutex*) {
+ // Doesn't need passed-in mutex because getSingletonInterface() uses one
return getSingletonInterface();
}
diff --git a/src/ports/SkFontConfigInterface_direct.cpp b/src/ports/SkFontConfigInterface_direct.cpp
index dc9afbae1f..c0cfd8fdcf 100644
--- a/src/ports/SkFontConfigInterface_direct.cpp
+++ b/src/ports/SkFontConfigInterface_direct.cpp
@@ -124,9 +124,13 @@ private:
SkMutex mutex_;
};
-SkFontConfigInterface* SkFontConfigInterface::GetSingletonDirectInterface() {
- SK_DECLARE_STATIC_LAZY_PTR(SkFontConfigInterfaceDirect, direct);
- return direct.get();
+SkFontConfigInterface* SkFontConfigInterface::GetSingletonDirectInterface(SkBaseMutex* mutex) {
+ SkAutoMutexAcquire ac(mutex);
+ static SkFontConfigInterfaceDirect* singleton = NULL;
+ if (singleton == NULL) {
+ singleton = SkNEW(SkFontConfigInterfaceDirect);
+ }
+ return singleton;
}
///////////////////////////////////////////////////////////////////////////////
diff --git a/src/ports/SkFontHost_fontconfig.cpp b/src/ports/SkFontHost_fontconfig.cpp
index 07bfbd044c..8abf5cf7f0 100644
--- a/src/ports/SkFontHost_fontconfig.cpp
+++ b/src/ports/SkFontHost_fontconfig.cpp
@@ -50,7 +50,7 @@ static SkFontConfigInterface* RefFCI() {
if (fci) {
return fci;
}
- fci = SkFontConfigInterface::GetSingletonDirectInterface();
+ fci = SkFontConfigInterface::GetSingletonDirectInterface(&gFontConfigInterfaceMutex);
SkFontConfigInterface::SetGlobal(fci);
}
}