Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5127)

Unified Diff: Source/wtf/Threading.h

Issue 794223003: Cheaper thread-safe atomic initialization of static references. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Avoid using reinterpret_cast<void*> Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/wtf/HashTable.cpp ('k') | Source/wtf/text/TextEncoding.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/wtf/Threading.h
diff --git a/Source/wtf/Threading.h b/Source/wtf/Threading.h
index 97d4cb390ae0734e9b214a95207e47713bebe7d3..0953a7b40d122a3e004d75a59c41ef3e5db113ef 100644
--- a/Source/wtf/Threading.h
+++ b/Source/wtf/Threading.h
@@ -30,14 +30,22 @@
#ifndef Threading_h
#define Threading_h
+#include "wtf/Atomics.h"
#include "wtf/WTFExport.h"
#include <stdint.h>
-// For portability, we do not use thread-safe statics natively supported by some compilers (e.g. gcc).
-#define AtomicallyInitializedStatic(T, name) \
- WTF::lockAtomicallyInitializedStaticMutex(); \
- static T name; \
- WTF::unlockAtomicallyInitializedStaticMutex();
+// For portability, we do not make use of C++11 thread-safe statics, as supported
+// by some toolchains. Make use of double-checked locking to reduce overhead.
+#define AtomicallyInitializedStaticReference(T, name, initializer) \
+ /* Init to nullptr is thread-safe on all implementations. */ \
+ static void* name##Pointer = nullptr; \
+ if (!WTF::acquireLoad(&name##Pointer)) { \
+ WTF::lockAtomicallyInitializedStaticMutex(); \
+ if (!WTF::acquireLoad(&name##Pointer)) \
+ WTF::releaseStore(&name##Pointer, initializer); \
Jeffrey Yasskin 2015/01/21 19:36:06 It probably makes sense to use: if (!WTF::acquire
sof 2015/01/21 20:42:54 The extra type check is definitely worth it; added
+ WTF::unlockAtomicallyInitializedStaticMutex(); \
+ } \
+ T& name = *static_cast<T*>(name##Pointer)
namespace WTF {
« no previous file with comments | « Source/wtf/HashTable.cpp ('k') | Source/wtf/text/TextEncoding.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698