Add an RAII ScopedWriter, which allows one to acquire a writer lock for the duration of a scope. Simplify a lot of uses of

writer locks in Constants.cpp by using it.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73630 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Owen Anderson 2009-06-17 20:34:43 +00:00
parent 31c36f02f2
commit dd561e1539
2 changed files with 72 additions and 41 deletions

View File

@ -78,6 +78,20 @@ namespace llvm
void operator=(const RWMutex &); void operator=(const RWMutex &);
/// @} /// @}
}; };
/// ScopedWriter - RAII acquisition of a writer lock
struct ScopedWriter {
RWMutex* mutex;
explicit ScopedWriter(RWMutex* m) {
mutex = m;
mutex->writer_acquire();
}
~ScopedWriter() {
mutex->writer_release();
}
};
} }
} }

View File

@ -306,12 +306,11 @@ ConstantInt *ConstantInt::get(const APInt& V) {
ConstantsLock->reader_release(); ConstantsLock->reader_release();
if (!Slot) { if (!Slot) {
ConstantsLock->writer_acquire(); sys::ScopedWriter Writer(&*ConstantsLock);
ConstantInt *&Slot = (*IntConstants)[Key]; ConstantInt *&Slot = (*IntConstants)[Key];
if (!Slot) { if (!Slot) {
Slot = new ConstantInt(ITy, V); Slot = new ConstantInt(ITy, V);
} }
ConstantsLock->writer_release();
} }
return Slot; return Slot;
@ -421,7 +420,7 @@ ConstantFP *ConstantFP::get(const APFloat &V) {
ConstantsLock->reader_release(); ConstantsLock->reader_release();
if (!Slot) { if (!Slot) {
ConstantsLock->writer_acquire(); sys::ScopedWriter Writer(&*ConstantsLock);
Slot = (*FPConstants)[Key]; Slot = (*FPConstants)[Key];
if (!Slot) { if (!Slot) {
const Type *Ty; const Type *Ty;
@ -441,7 +440,6 @@ ConstantFP *ConstantFP::get(const APFloat &V) {
Slot = new ConstantFP(Ty, V); Slot = new ConstantFP(Ty, V);
} }
ConstantsLock->writer_release();
} }
return Slot; return Slot;
@ -1240,7 +1238,7 @@ public:
ConstantsLock->reader_release(); ConstantsLock->reader_release();
if (!Result) { if (!Result) {
ConstantsLock->writer_acquire(); sys::ScopedWriter Writer(&*ConstantsLock);
I = Map.find(Lookup); I = Map.find(Lookup);
// Is it in the map? // Is it in the map?
if (I != Map.end()) if (I != Map.end())
@ -1270,7 +1268,6 @@ public:
} }
} }
} }
ConstantsLock->writer_release();
} }
return Result; return Result;
@ -1419,9 +1416,11 @@ public:
// If the type became concrete without being refined to any other existing // If the type became concrete without being refined to any other existing
// type, we just remove ourselves from the ATU list. // type, we just remove ourselves from the ATU list.
void typeBecameConcrete(const DerivedType *AbsTy) { void typeBecameConcrete(const DerivedType *AbsTy) {
if (llvm_is_multithreaded()) ConstantsLock->writer_acquire(); if (llvm_is_multithreaded()) {
AbsTy->removeAbstractTypeUser(this); sys::ScopedWriter Writer(&*ConstantsLock);
if (llvm_is_multithreaded()) ConstantsLock->writer_release(); AbsTy->removeAbstractTypeUser(this);
} else
AbsTy->removeAbstractTypeUser(this);
} }
void dump() const { void dump() const {
@ -1723,9 +1722,12 @@ Constant *ConstantVector::get(const std::vector<Constant*> &V) {
// destroyConstant - Remove the constant from the constant table... // destroyConstant - Remove the constant from the constant table...
// //
void ConstantVector::destroyConstant() { void ConstantVector::destroyConstant() {
if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
VectorConstants->remove(this); if (llvm_is_multithreaded()) {
if (llvm_is_multithreaded()) ConstantsLock->writer_release(); sys::ScopedWriter Write(&*ConstantsLock);
VectorConstants->remove(this);
} else
VectorConstants->remove(this);
destroyConstantImpl(); destroyConstantImpl();
} }
@ -1796,9 +1798,11 @@ ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
// destroyConstant - Remove the constant from the constant table... // destroyConstant - Remove the constant from the constant table...
// //
void ConstantPointerNull::destroyConstant() { void ConstantPointerNull::destroyConstant() {
if (llvm_is_multithreaded()) ConstantsLock->writer_acquire(); if (llvm_is_multithreaded()) {
NullPtrConstants->remove(this); sys::ScopedWriter Writer(&*ConstantsLock);
if (llvm_is_multithreaded()) ConstantsLock->writer_release(); NullPtrConstants->remove(this);
} else
NullPtrConstants->remove(this);
destroyConstantImpl(); destroyConstantImpl();
} }
@ -1857,21 +1861,33 @@ MDString::MDString(const char *begin, const char *end)
static ManagedStatic<StringMap<MDString*> > MDStringCache; static ManagedStatic<StringMap<MDString*> > MDStringCache;
MDString *MDString::get(const char *StrBegin, const char *StrEnd) { MDString *MDString::get(const char *StrBegin, const char *StrEnd) {
if (llvm_is_multithreaded()) ConstantsLock->writer_acquire(); if (llvm_is_multithreaded()) {
StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue(StrBegin, sys::ScopedWriter Writer(&*ConstantsLock);
StrEnd); StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue(
MDString *&S = Entry.getValue(); StrBegin, StrEnd);
if (!S) S = new MDString(Entry.getKeyData(), MDString *&S = Entry.getValue();
Entry.getKeyData() + Entry.getKeyLength()); if (!S) S = new MDString(Entry.getKeyData(),
Entry.getKeyData() + Entry.getKeyLength());
if (llvm_is_multithreaded()) ConstantsLock->writer_release();
return S; return S;
} else {
StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue(
StrBegin, StrEnd);
MDString *&S = Entry.getValue();
if (!S) S = new MDString(Entry.getKeyData(),
Entry.getKeyData() + Entry.getKeyLength());
return S;
}
} }
void MDString::destroyConstant() { void MDString::destroyConstant() {
if (llvm_is_multithreaded()) ConstantsLock->writer_acquire(); if (llvm_is_multithreaded()) {
MDStringCache->erase(MDStringCache->find(StrBegin, StrEnd)); sys::ScopedWriter Writer(&*ConstantsLock);
if (llvm_is_multithreaded()) ConstantsLock->writer_release(); MDStringCache->erase(MDStringCache->find(StrBegin, StrEnd));
} else
MDStringCache->erase(MDStringCache->find(StrBegin, StrEnd));
destroyConstantImpl(); destroyConstantImpl();
} }
@ -1903,14 +1919,13 @@ MDNode *MDNode::get(Value*const* Vals, unsigned NumVals) {
ConstantsLock->reader_release(); ConstantsLock->reader_release();
if (!N) { if (!N) {
ConstantsLock->writer_acquire(); sys::ScopedWriter Writer(&*ConstantsLock);
N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint); N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
if (!N) { if (!N) {
// InsertPoint will have been set by the FindNodeOrInsertPos call. // InsertPoint will have been set by the FindNodeOrInsertPos call.
MDNode *N = new(0) MDNode(Vals, NumVals); MDNode *N = new(0) MDNode(Vals, NumVals);
MDNodeSet->InsertNode(N, InsertPoint); MDNodeSet->InsertNode(N, InsertPoint);
} }
ConstantsLock->writer_release();
} }
return N; return N;
@ -1927,9 +1942,12 @@ MDNode *MDNode::get(Value*const* Vals, unsigned NumVals) {
} }
void MDNode::destroyConstant() { void MDNode::destroyConstant() {
if (llvm_is_multithreaded()) ConstantsLock->writer_acquire(); if (llvm_is_multithreaded()) {
MDNodeSet->RemoveNode(this); sys::ScopedWriter Writer(&*ConstantsLock);
if (llvm_is_multithreaded()) ConstantsLock->writer_release(); MDNodeSet->RemoveNode(this);
} else
MDNodeSet->RemoveNode(this);
destroyConstantImpl(); destroyConstantImpl();
} }
@ -2793,9 +2811,12 @@ Constant *ConstantExpr::getZeroValueForNegationExpr(const Type *Ty) {
// destroyConstant - Remove the constant from the constant table... // destroyConstant - Remove the constant from the constant table...
// //
void ConstantExpr::destroyConstant() { void ConstantExpr::destroyConstant() {
if (llvm_is_multithreaded()) ConstantsLock->writer_acquire(); if (llvm_is_multithreaded()) {
ExprConstants->remove(this); sys::ScopedWriter Writer(&*ConstantsLock);
if (llvm_is_multithreaded()) ConstantsLock->writer_release(); ExprConstants->remove(this);
} else
ExprConstants->remove(this);
destroyConstantImpl(); destroyConstantImpl();
} }
@ -2859,7 +2880,7 @@ void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Replacement = ConstantAggregateZero::get(getType()); Replacement = ConstantAggregateZero::get(getType());
} else { } else {
// Check to see if we have this array type already. // Check to see if we have this array type already.
if (llvm_is_multithreaded()) ConstantsLock->writer_acquire(); sys::ScopedWriter Writer(&*ConstantsLock);
bool Exists; bool Exists;
ArrayConstantsTy::MapTy::iterator I = ArrayConstantsTy::MapTy::iterator I =
ArrayConstants->InsertOrGetItem(Lookup, Exists); ArrayConstants->InsertOrGetItem(Lookup, Exists);
@ -2885,10 +2906,8 @@ void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
if (getOperand(i) == From) if (getOperand(i) == From)
setOperand(i, ToC); setOperand(i, ToC);
} }
if (llvm_is_multithreaded()) ConstantsLock->writer_release();
return; return;
} }
if (llvm_is_multithreaded()) ConstantsLock->writer_release();
} }
// Otherwise, I do need to replace this with an existing value. // Otherwise, I do need to replace this with an existing value.
@ -2937,7 +2956,7 @@ void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Replacement = ConstantAggregateZero::get(getType()); Replacement = ConstantAggregateZero::get(getType());
} else { } else {
// Check to see if we have this array type already. // Check to see if we have this array type already.
if (llvm_is_multithreaded()) ConstantsLock->writer_acquire(); sys::ScopedWriter Writer(&*ConstantsLock);
bool Exists; bool Exists;
StructConstantsTy::MapTy::iterator I = StructConstantsTy::MapTy::iterator I =
StructConstants->InsertOrGetItem(Lookup, Exists); StructConstants->InsertOrGetItem(Lookup, Exists);
@ -2953,10 +2972,8 @@ void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
// Update to the new value. // Update to the new value.
setOperand(OperandToUpdate, ToC); setOperand(OperandToUpdate, ToC);
if (llvm_is_multithreaded()) ConstantsLock->writer_release();
return; return;
} }
if (llvm_is_multithreaded()) ConstantsLock->writer_release();
} }
assert(Replacement != this && "I didn't contain From!"); assert(Replacement != this && "I didn't contain From!");