diff options
author | Benjamin Kramer <benny.kra@googlemail.com> | 2016-06-29 15:04:07 +0000 |
---|---|---|
committer | Benjamin Kramer <benny.kra@googlemail.com> | 2016-06-29 15:04:07 +0000 |
commit | 832d0420781083009f758e01f1df59e4a00003b9 (patch) | |
tree | 8794cd04d14be79af7692fd4f5bca373113ff5dd /llvm/lib/Support/ManagedStatic.cpp | |
parent | a99ccfce1a7e9c789c353e85dc8f2b1b90c34dbe (diff) | |
download | bcm5719-llvm-832d0420781083009f758e01f1df59e4a00003b9.tar.gz bcm5719-llvm-832d0420781083009f758e01f1df59e4a00003b9.zip |
[ManagedStatic] Reimplement double-checked locking with std::atomic.
This gets rid of the memory fence in the hot path (dereferencing the
ManagedStatic), trading for an extra mutex lock in the cold path (when
the ManagedStatic was uninitialized). Since this only happens on the
first accesses it shouldn't matter much. On strict architectures like
x86 this removes any atomic instructions from the hot path.
Also remove the tsan annotations, tsan knows how standard atomics work
so they should be unnecessary now.
llvm-svn: 274131
Diffstat (limited to 'llvm/lib/Support/ManagedStatic.cpp')
-rw-r--r-- | llvm/lib/Support/ManagedStatic.cpp | 15 |
1 files changed, 3 insertions, 12 deletions
diff --git a/llvm/lib/Support/ManagedStatic.cpp b/llvm/lib/Support/ManagedStatic.cpp index bc4fe954e22..7dd31315f90 100644 --- a/llvm/lib/Support/ManagedStatic.cpp +++ b/llvm/lib/Support/ManagedStatic.cpp @@ -13,7 +13,6 @@ #include "llvm/Support/ManagedStatic.h" #include "llvm/Config/config.h" -#include "llvm/Support/Atomic.h" #include "llvm/Support/Mutex.h" #include "llvm/Support/MutexGuard.h" #include "llvm/Support/Threading.h" @@ -42,18 +41,10 @@ void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(), if (llvm_is_multithreaded()) { MutexGuard Lock(*getManagedStaticMutex()); - if (!Ptr) { - void* tmp = Creator(); + if (!Ptr.load(std::memory_order_relaxed)) { + void *Tmp = Creator(); - TsanHappensBefore(this); - sys::MemoryFence(); - - // This write is racy against the first read in the ManagedStatic - // accessors. The race is benign because it does a second read after a - // memory fence, at which point it isn't possible to get a partial value. - TsanIgnoreWritesBegin(); - Ptr = tmp; - TsanIgnoreWritesEnd(); + Ptr.store(Tmp, std::memory_order_release); DeleterFn = Deleter; // Add to list of managed statics. |