Chromium Code Reviews| 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 { |