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

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: 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
Index: Source/wtf/Threading.h
diff --git a/Source/wtf/Threading.h b/Source/wtf/Threading.h
index 97d4cb390ae0734e9b214a95207e47713bebe7d3..61207e33ed5eeceea06da42ba9f4ad92b4c541d3 100644
--- a/Source/wtf/Threading.h
+++ b/Source/wtf/Threading.h
@@ -30,14 +30,23 @@
#ifndef Threading_h
#define Threading_h
+#include "wtf/Atomics.h"
+#include "wtf/TypeTraits.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.
+#define AtomicallyInitializedStaticReference(T, name, initializer) \
+ /* Double-checked locking; init to nullptr is thread-safe on all implementations. */ \
+ static WTF::RemoveConst<T>::Type* name##_pointer = nullptr; \
haraken 2015/01/21 15:23:33 _pointer => Pointer
sof 2015/01/21 15:51:38 Thanks, switched.
+ if (!WTF::acquireLoad(reinterpret_cast<void* const*>(&name##_pointer))) { \
+ WTF::lockAtomicallyInitializedStaticMutex(); \
+ if (!WTF::acquireLoad(reinterpret_cast<void* const*>(&name##_pointer))) \
+ WTF::releaseStore(reinterpret_cast<void**>(&name##_pointer), initializer); \
+ WTF::unlockAtomicallyInitializedStaticMutex(); \
+ } \
+ T& name = *name##_pointer
namespace WTF {

Powered by Google App Engine
This is Rietveld 408576698