diff options
author | Owen Anderson <resistor@mac.com> | 2009-06-17 22:23:31 +0000 |
---|---|---|
committer | Owen Anderson <resistor@mac.com> | 2009-06-17 22:23:31 +0000 |
commit | d06a035bb535d48b3b98b439acd6bb4ca443e1b4 (patch) | |
tree | 3c35a8c5fdda5c891e2350046748f14276158d83 | |
parent | 1dc3bc0978813f6402e37023b6d0d279b81f8b37 (diff) | |
download | external_llvm-d06a035bb535d48b3b98b439acd6bb4ca443e1b4.zip external_llvm-d06a035bb535d48b3b98b439acd6bb4ca443e1b4.tar.gz external_llvm-d06a035bb535d48b3b98b439acd6bb4ca443e1b4.tar.bz2 |
Protect the GC table in Function.cpp
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73647 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/llvm/System/RWMutex.h | 14 | ||||
-rw-r--r-- | lib/VMCore/Function.cpp | 44 |
2 files changed, 39 insertions, 19 deletions
diff --git a/include/llvm/System/RWMutex.h b/include/llvm/System/RWMutex.h index 6ac7aa3..8967e9e 100644 --- a/include/llvm/System/RWMutex.h +++ b/include/llvm/System/RWMutex.h @@ -79,6 +79,20 @@ namespace llvm /// @} }; + /// ScopedReader - RAII acquisition of a writer lock + struct ScopedReader { + RWMutex* mutex; + + explicit ScopedReader(RWMutex* m) { + mutex = m; + mutex->reader_acquire(); + } + + ~ScopedReader() { + mutex->reader_release(); + } + }; + /// ScopedWriter - RAII acquisition of a writer lock struct ScopedWriter { RWMutex* mutex; diff --git a/lib/VMCore/Function.cpp b/lib/VMCore/Function.cpp index 54bd895..96c6792 100644 --- a/lib/VMCore/Function.cpp +++ b/lib/VMCore/Function.cpp @@ -16,7 +16,9 @@ #include "llvm/IntrinsicInst.h" #include "llvm/CodeGen/ValueTypes.h" #include "llvm/Support/LeakDetector.h" +#include "llvm/Support/ManagedStatic.h" #include "llvm/Support/StringPool.h" +#include "llvm/System/RWMutex.h" #include "SymbolTableListTraitsImpl.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/StringExtras.h" @@ -228,38 +230,42 @@ void Function::removeAttribute(unsigned i, Attributes attr) { // allocating an additional word in Function for programs which do not use GC // (i.e., most programs) at the cost of increased overhead for clients which do // use GC. -static DenseMap<const Function*,PooledStringPtr> *GCNames; -static StringPool *GCNamePool; +static ManagedStatic<DenseMap<const Function*,PooledStringPtr> > GCNames; +static ManagedStatic<StringPool> GCNamePool; +static ManagedStatic<sys::RWMutex> GCLock; bool Function::hasGC() const { - return GCNames && GCNames->count(this); + if (llvm_is_multithreaded()) { + sys::ScopedReader Reader(&*GCLock); + return GCNames->count(this); + } else + return GCNames->count(this); } const char *Function::getGC() const { assert(hasGC() && "Function has no collector"); - return *(*GCNames)[this]; + + if (llvm_is_multithreaded()) { + sys::ScopedReader Reader(&*GCLock); + return *(*GCNames)[this]; + } else + return *(*GCNames)[this]; } void Function::setGC(const char *Str) { - if (!GCNamePool) - GCNamePool = new StringPool(); - if (!GCNames) - GCNames = new DenseMap<const Function*,PooledStringPtr>(); - (*GCNames)[this] = GCNamePool->intern(Str); + if (llvm_is_multithreaded()) { + sys::ScopedWriter Writer(&*GCLock); + (*GCNames)[this] = GCNamePool->intern(Str); + } else + (*GCNames)[this] = GCNamePool->intern(Str); } void Function::clearGC() { - if (GCNames) { + if (llvm_is_multithreaded()) { + sys::ScopedWriter Writer(&*GCLock); + GCNames->erase(this); + } else GCNames->erase(this); - if (GCNames->empty()) { - delete GCNames; - GCNames = 0; - if (GCNamePool->empty()) { - delete GCNamePool; - GCNamePool = 0; - } - } - } } /// copyAttributesFrom - copy all additional attributes (those not needed to |