Remove SkOnce from function local initializations
SkOnce was used in several places for simple, thread-safe
singleton initialization. This pattern can be replaced by
magic statics introduced in C++11, which provide the
same thread-safety guarantees with simpler code.
This also follows Chromium's guidelines
https://chromium.googlesource.com/chromium/src/+/lkgr/docs/static_initializers.md
Change-Id: I092ed5a924c27b405be7132c6ea12cd9084c191c
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1066596
Reviewed-by: Daniel Dilan <danieldilan@google.com>
Auto-Submit: Kaylee Lubick <kjlubick@google.com>
Commit-Queue: Kaylee Lubick <kjlubick@google.com>
diff --git a/modules/canvaskit/bidi_bindings.cpp b/modules/canvaskit/bidi_bindings.cpp
index be75a4a..0bd12e3 100644
--- a/modules/canvaskit/bidi_bindings.cpp
+++ b/modules/canvaskit/bidi_bindings.cpp
@@ -5,7 +5,7 @@
* found in the LICENSE file.
*/
#include "include/core/SkString.h"
-#include "include/private/base/SkOnce.h"
+
#include "modules/skunicode/include/SkUnicode.h"
#if defined(SK_UNICODE_BIDI_IMPLEMENTATION)
@@ -37,10 +37,8 @@
class CodeUnitsPlaceholder { };
static sk_sp<SkUnicode> getBidiUnicode() {
- static sk_sp<SkUnicode> unicode;
- static SkOnce once;
- once([] { unicode = SkUnicodes::Bidi::Make(); });
- return unicode;
+ static SkUnicode* unicode = SkUnicodes::Bidi::Make().release();
+ return sk_ref_sp(unicode);
}
EMSCRIPTEN_BINDINGS(Bidi) {
diff --git a/src/core/SkData.cpp b/src/core/SkData.cpp
index 4573b6e..84d1d67 100644
--- a/src/core/SkData.cpp
+++ b/src/core/SkData.cpp
@@ -11,7 +11,6 @@
#include "include/core/SkStream.h"
#include "include/private/base/SkAssert.h"
#include "include/private/base/SkMalloc.h"
-#include "include/private/base/SkOnce.h"
#include "src/core/SkOSFile.h"
#include "src/core/SkStreamPriv.h"
@@ -125,10 +124,7 @@
///////////////////////////////////////////////////////////////////////////////
sk_sp<SkData> SkData::MakeEmpty() {
- static SkOnce once;
- static SkData* empty;
-
- once([]{ empty = new SkData({}, nullptr, nullptr); });
+ static SkData* empty = new SkData({}, nullptr, nullptr);
return sk_ref_sp(empty);
}
diff --git a/src/core/SkDataTable.cpp b/src/core/SkDataTable.cpp
index d15e65b..bcab1dd 100644
--- a/src/core/SkDataTable.cpp
+++ b/src/core/SkDataTable.cpp
@@ -10,7 +10,6 @@
#include "include/core/SkRefCnt.h"
#include "include/private/base/SkAssert.h"
#include "include/private/base/SkMalloc.h"
-#include "include/private/base/SkOnce.h"
#include <cstring>
@@ -83,9 +82,7 @@
///////////////////////////////////////////////////////////////////////////////
sk_sp<SkDataTable> SkDataTable::MakeEmpty() {
- static SkDataTable* singleton;
- static SkOnce once;
- once([]{ singleton = new SkDataTable(); });
+ static SkDataTable* singleton = new SkDataTable();
return sk_ref_sp(singleton);
}
diff --git a/src/core/SkMessageBus.h b/src/core/SkMessageBus.h
index 3b78793..5cb07bd 100644
--- a/src/core/SkMessageBus.h
+++ b/src/core/SkMessageBus.h
@@ -14,7 +14,6 @@
#include "include/core/SkTypes.h"
#include "include/private/base/SkMutex.h"
#include "include/private/base/SkNoncopyable.h"
-#include "include/private/base/SkOnce.h"
#include "include/private/base/SkTArray.h"
#include "include/private/base/SkTDArray.h"
@@ -71,14 +70,12 @@
// This must go in a single .cpp file, not some .h, or we risk creating more than one global
// SkMessageBus per type when using shared libraries. NOTE: at most one per file will compile.
-#define DECLARE_SKMESSAGEBUS_MESSAGE(Message, IDType, AllowCopyableMessage) \
- template <> \
- SkMessageBus<Message, IDType, AllowCopyableMessage>* \
- SkMessageBus<Message, IDType, AllowCopyableMessage>::Get() { \
- static SkOnce once; \
- static SkMessageBus<Message, IDType, AllowCopyableMessage>* bus; \
- once([] { bus = new SkMessageBus<Message, IDType, AllowCopyableMessage>(); }); \
- return bus; \
+#define DECLARE_SKMESSAGEBUS_MESSAGE(Message, IDType, AllowCopyableMessage) \
+ template <> \
+ SkMessageBus<Message, IDType, AllowCopyableMessage>* \
+ SkMessageBus<Message, IDType, AllowCopyableMessage>::Get() { \
+ static auto* bus = new SkMessageBus<Message, IDType, AllowCopyableMessage>(); \
+ return bus; \
}
// ----------------------- Implementation of SkMessageBus::Inbox -----------------------
diff --git a/src/ports/SkFontConfigInterface_direct_factory.cpp b/src/ports/SkFontConfigInterface_direct_factory.cpp
index 1923478..a44f7f2 100644
--- a/src/ports/SkFontConfigInterface_direct_factory.cpp
+++ b/src/ports/SkFontConfigInterface_direct_factory.cpp
@@ -5,12 +5,9 @@
* found in the LICENSE file.
*/
-#include "include/private/base/SkOnce.h"
#include "src/ports/SkFontConfigInterface_direct.h"
SkFontConfigInterface* SkFontConfigInterface::GetSingletonDirectInterface() {
- static SkFontConfigInterface* singleton;
- static SkOnce once;
- once([]{ singleton = new SkFontConfigInterfaceDirect(nullptr); });
+ static SkFontConfigInterface* singleton = new SkFontConfigInterfaceDirect(nullptr);
return singleton;
}
diff --git a/src/utils/SkEventTracer.cpp b/src/utils/SkEventTracer.cpp
index dcc35dc..7d28155 100644
--- a/src/utils/SkEventTracer.cpp
+++ b/src/utils/SkEventTracer.cpp
@@ -7,8 +7,6 @@
#include "include/utils/SkEventTracer.h"
-#include "include/private/base/SkOnce.h"
-
#include <stdlib.h>
#include <atomic>
@@ -64,8 +62,6 @@
if (auto tracer = gUserTracer.load(std::memory_order_acquire)) {
return tracer;
}
- static SkOnce once;
- static SkDefaultEventTracer* defaultTracer;
- once([] { defaultTracer = new SkDefaultEventTracer; });
+ static SkDefaultEventTracer* defaultTracer = new SkDefaultEventTracer;
return defaultTracer;
}
diff --git a/tools/ganesh/gl/glx/CreatePlatformGLTestContext_glx.cpp b/tools/ganesh/gl/glx/CreatePlatformGLTestContext_glx.cpp
index 9f984e3..4545b22 100644
--- a/tools/ganesh/gl/glx/CreatePlatformGLTestContext_glx.cpp
+++ b/tools/ganesh/gl/glx/CreatePlatformGLTestContext_glx.cpp
@@ -86,9 +86,7 @@
private:
Display* fDisplay;
};
- static std::unique_ptr<AutoDisplay> ad;
- static SkOnce once;
- once([] { ad = std::make_unique<AutoDisplay>(); });
+ static std::unique_ptr<AutoDisplay> ad = std::make_unique<AutoDisplay>();
return ad->display();
}