From 334143d304cdf6827dbdeaceca0f28d74707e95c Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Wed, 1 Jul 2009 17:22:27 +0000 Subject: [PATCH] I give up on trying to use reader/writer locks for recursive type refinement. Use a recursive mutex instead, which will (in theory) generate more contention, but is really a much more natural fit for what's going on during recursive type refinement. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@74618 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/Type.cpp | 98 +++++++++++++-------------------------------- 1 file changed, 27 insertions(+), 71 deletions(-) diff --git a/lib/VMCore/Type.cpp b/lib/VMCore/Type.cpp index 5df7f120505..40d75170491 100644 --- a/lib/VMCore/Type.cpp +++ b/lib/VMCore/Type.cpp @@ -43,8 +43,8 @@ AbstractTypeUser::~AbstractTypeUser() {} // Type Class Implementation //===----------------------------------------------------------------------===// -// Reader/writer lock used for guarding access to the type maps. -static ManagedStatic > TypeMapLock; +// Lock used for guarding access to the type maps. +static ManagedStatic > TypeMapLock; // Recursive lock used for guarding access to AbstractTypeUsers. // NOTE: The true template parameter means this will no-op when we're not in @@ -1006,23 +1006,13 @@ const IntegerType *IntegerType::get(unsigned NumBits) { // First, see if the type is already in the table, for which // a reader lock suffices. - TypeMapLock->reader_acquire(); + sys::SmartScopedLock L(&*TypeMapLock); ITy = IntegerTypes->get(IVT); - TypeMapLock->reader_release(); if (!ITy) { - // OK, not in the table, get a writer lock. - sys::SmartScopedWriter Writer(&*TypeMapLock); - ITy = IntegerTypes->get(IVT); - - // We need to _recheck_ the table in case someone - // put it in between when we released the reader lock - // and when we gained the writer lock! - if (!ITy) { - // Value not found. Derive a new type! - ITy = new IntegerType(NumBits); - IntegerTypes->add(IVT, ITy); - } + // Value not found. Derive a new type! + ITy = new IntegerType(NumBits); + IntegerTypes->add(IVT, ITy); } #ifdef DEBUG_MERGE_TYPES DOUT << "Derived new type: " << *ITy << "\n"; @@ -1089,23 +1079,14 @@ FunctionType *FunctionType::get(const Type *ReturnType, FunctionValType VT(ReturnType, Params, isVarArg); FunctionType *FT = 0; - TypeMapLock->reader_acquire(); + sys::SmartScopedLock L(&*TypeMapLock); FT = FunctionTypes->get(VT); - TypeMapLock->reader_release(); if (!FT) { - sys::SmartScopedWriter Writer(&*TypeMapLock); - - // Have to check again here, because it might have - // been inserted between when we release the reader - // lock and when we acquired the writer lock. - FT = FunctionTypes->get(VT); - if (!FT) { - FT = (FunctionType*) operator new(sizeof(FunctionType) + - sizeof(PATypeHandle)*(Params.size()+1)); - new (FT) FunctionType(ReturnType, Params, isVarArg); - FunctionTypes->add(VT, FT); - } + FT = (FunctionType*) operator new(sizeof(FunctionType) + + sizeof(PATypeHandle)*(Params.size()+1)); + new (FT) FunctionType(ReturnType, Params, isVarArg); + FunctionTypes->add(VT, FT); } #ifdef DEBUG_MERGE_TYPES @@ -1148,19 +1129,12 @@ ArrayType *ArrayType::get(const Type *ElementType, uint64_t NumElements) { ArrayValType AVT(ElementType, NumElements); ArrayType *AT = 0; - TypeMapLock->reader_acquire(); + sys::SmartScopedLock L(&*TypeMapLock); AT = ArrayTypes->get(AVT); - TypeMapLock->reader_release(); - + if (!AT) { - sys::SmartScopedWriter Writer(&*TypeMapLock); - - // Recheck. Might have changed between release and acquire. - AT = ArrayTypes->get(AVT); - if (!AT) { - // Value not found. Derive a new type! - ArrayTypes->add(AVT, AT = new ArrayType(ElementType, NumElements)); - } + // Value not found. Derive a new type! + ArrayTypes->add(AVT, AT = new ArrayType(ElementType, NumElements)); } #ifdef DEBUG_MERGE_TYPES DOUT << "Derived new type: " << *AT << "\n"; @@ -1214,17 +1188,11 @@ VectorType *VectorType::get(const Type *ElementType, unsigned NumElements) { VectorValType PVT(ElementType, NumElements); VectorType *PT = 0; - TypeMapLock->reader_acquire(); + sys::SmartScopedLock L(&*TypeMapLock); PT = VectorTypes->get(PVT); - TypeMapLock->reader_release(); if (!PT) { - sys::SmartScopedWriter Writer(&*TypeMapLock); - PT = VectorTypes->get(PVT); - // Recheck. Might have changed between release and acquire. - if (!PT) { - VectorTypes->add(PVT, PT = new VectorType(ElementType, NumElements)); - } + VectorTypes->add(PVT, PT = new VectorType(ElementType, NumElements)); } #ifdef DEBUG_MERGE_TYPES DOUT << "Derived new type: " << *PT << "\n"; @@ -1282,21 +1250,15 @@ StructType *StructType::get(const std::vector &ETypes, StructValType STV(ETypes, isPacked); StructType *ST = 0; - TypeMapLock->reader_acquire(); + sys::SmartScopedLock L(&*TypeMapLock); ST = StructTypes->get(STV); - TypeMapLock->reader_release(); if (!ST) { - sys::SmartScopedWriter Writer(&*TypeMapLock); - ST = StructTypes->get(STV); - // Recheck. Might have changed between release and acquire. - if (!ST) { - // Value not found. Derive a new type! - ST = (StructType*) operator new(sizeof(StructType) + - sizeof(PATypeHandle) * ETypes.size()); - new (ST) StructType(ETypes, isPacked); - StructTypes->add(STV, ST); - } + // Value not found. Derive a new type! + ST = (StructType*) operator new(sizeof(StructType) + + sizeof(PATypeHandle) * ETypes.size()); + new (ST) StructType(ETypes, isPacked); + StructTypes->add(STV, ST); } #ifdef DEBUG_MERGE_TYPES DOUT << "Derived new type: " << *ST << "\n"; @@ -1367,18 +1329,12 @@ PointerType *PointerType::get(const Type *ValueType, unsigned AddressSpace) { PointerType *PT = 0; - TypeMapLock->reader_acquire(); + sys::SmartScopedLock L(&*TypeMapLock); PT = PointerTypes->get(PVT); - TypeMapLock->reader_release(); if (!PT) { - sys::SmartScopedWriter Writer(&*TypeMapLock); - PT = PointerTypes->get(PVT); - // Recheck. Might have changed between release and acquire. - if (!PT) { - // Value not found. Derive a new type! - PointerTypes->add(PVT, PT = new PointerType(ValueType, AddressSpace)); - } + // Value not found. Derive a new type! + PointerTypes->add(PVT, PT = new PointerType(ValueType, AddressSpace)); } #ifdef DEBUG_MERGE_TYPES DOUT << "Derived new type: " << *PT << "\n"; @@ -1532,7 +1488,7 @@ void DerivedType::unlockedRefineAbstractTypeTo(const Type *NewType) { void DerivedType::refineAbstractTypeTo(const Type *NewType) { // All recursive calls will go through unlockedRefineAbstractTypeTo, // to avoid deadlock problems. - sys::SmartScopedWriter Writer(&*TypeMapLock); + sys::SmartScopedLock L(&*TypeMapLock); unlockedRefineAbstractTypeTo(NewType); }