Chromium Code Reviews| Index: Source/wtf/Threading.h |
| diff --git a/Source/wtf/Threading.h b/Source/wtf/Threading.h |
| index 97d4cb390ae0734e9b214a95207e47713bebe7d3..e89dcb793295befc0b136be712fa72e6a6c9d766 100644 |
| --- a/Source/wtf/Threading.h |
| +++ b/Source/wtf/Threading.h |
| @@ -30,14 +30,25 @@ |
| #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. Make use of double-checked locking to reduce overhead. |
| +#define AtomicallyInitializedStaticReference(T, name, initializer) \ |
|
Noel Gordon
2015/01/24 01:20:06
To "reduce overhead" relative to what?
sof
2015/01/24 06:20:06
To what's on the left here in the diff? :) Doesn't
Noel Gordon
2015/01/24 12:08:24
Think _we_ understand the motivation :) but other
sof
2015/01/24 12:40:05
Sounds sensible; doesn't the description here expl
|
| + /* Init to nullptr is thread-safe on all implementations. */ \ |
| + static void* name##Pointer = nullptr; \ |
| + if (!WTF::acquireLoad(&name##Pointer)) { \ |
|
Noel Gordon
2015/01/24 01:20:06
Blink prefers the early return. Any reason not to
sof
2015/01/24 06:20:06
You have to bind the reference, and at the scope t
Noel Gordon
2015/01/24 12:08:24
Yes I see your point, @51 it seems, so ok.
|
| + WTF::lockAtomicallyInitializedStaticMutex(); \ |
| + if (!WTF::acquireLoad(&name##Pointer)) { \ |
| + WTF::RemoveConst<T>::Type* initializerResult = initializer; \ |
| + WTF::releaseStore(&name##Pointer, initializerResult); \ |
| + } \ |
| + WTF::unlockAtomicallyInitializedStaticMutex(); \ |
| + } \ |
| + T& name = *static_cast<T*>(name##Pointer) |
| namespace WTF { |