Reduce contention on the Attributes lock by using atomic operations for reference...
[oota-llvm.git] / lib / VMCore / Attributes.cpp
index 91c7320a708b5b3e880bc2183fe644b2a5c8d538..fb1a9b8cd5fbdf28aeceb7241bd417669428c146 100644 (file)
@@ -15,6 +15,7 @@
 #include "llvm/Type.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/FoldingSet.h"
+#include "llvm/System/Atomic.h"
 #include "llvm/System/Mutex.h"
 #include "llvm/Support/Streams.h"
 #include "llvm/Support/ManagedStatic.h"
@@ -97,7 +98,7 @@ Attributes Attribute::typeIncompatible(const Type *Ty) {
 
 namespace llvm {
 class AttributeListImpl : public FoldingSetNode {
-  unsigned RefCount;
+  sys::cas_flag RefCount;
   
   // AttributesList is uniqued, these should not be publicly available.
   void operator=(const AttributeListImpl &); // Do not implement
@@ -111,8 +112,11 @@ public:
     RefCount = 0;
   }
   
-  void AddRef() { ++RefCount; }
-  void DropRef() { if (--RefCount == 0) delete this; }
+  void AddRef() { sys::AtomicIncrement(&RefCount); }
+  void DropRef() {
+    sys::cas_flag old = sys::AtomicDecrement(&RefCount);
+    if (old == 0) delete this;
+  }
   
   void Profile(FoldingSetNodeID &ID) const {
     Profile(ID, Attrs.data(), Attrs.size());