Use atomic increment/decrement for reference counting of Type's.
authorOwen Anderson <resistor@mac.com>
Wed, 17 Jun 2009 00:28:49 +0000 (00:28 +0000)
committerOwen Anderson <resistor@mac.com>
Wed, 17 Jun 2009 00:28:49 +0000 (00:28 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73588 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/System/Atomic.h
include/llvm/Type.h
lib/System/Atomic.cpp

index f90a8f62e1c76dd8ce07b1002f1b16e3df9ec251..adbb975298e8131bc634032914fc641e1dc95c11 100644 (file)
@@ -24,8 +24,8 @@ namespace llvm {
     cas_flag CompareAndSwap(volatile cas_flag* ptr,
                             cas_flag new_value,
                             cas_flag old_value);
-    cas_flag AtomicPostIncrement(volatile cas_flag* ptr);
-    cas_flag AtomicPostDecrement(volatile cas_flag* ptr);
+    cas_flag AtomicIncrement(volatile cas_flag* ptr);
+    cas_flag AtomicDecrement(volatile cas_flag* ptr);
   }
 }
 
index 256944f600498327567799eb58e7d703ae59e6f4..d439233d8c05d1d643c50de1a26259b5db8c9a09 100644 (file)
@@ -14,6 +14,7 @@
 #include "llvm/AbstractTypeUser.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/DataTypes.h"
+#include "llvm/System/Atomic.h"
 #include "llvm/ADT/GraphTraits.h"
 #include "llvm/ADT/iterator.h"
 #include <string>
@@ -102,7 +103,7 @@ private:
   /// has no AbstractTypeUsers, the type is deleted.  This is only sensical for
   /// derived types.
   ///
-  mutable unsigned RefCount;
+  mutable sys::cas_flag RefCount;
 
   const Type *getForwardedTypeInternal() const;
 
@@ -337,7 +338,7 @@ public:
 
   void addRef() const {
     assert(isAbstract() && "Cannot add a reference to a non-abstract type!");
-    ++RefCount;
+    sys::AtomicIncrement(&RefCount);
   }
 
   void dropRef() const {
@@ -346,7 +347,8 @@ public:
 
     // If this is the last PATypeHolder using this object, and there are no
     // PATypeHandles using it, the type is dead, delete it now.
-    if (--RefCount == 0 && AbstractTypeUsers.empty())
+    sys::cas_flag OldCount = sys::AtomicDecrement(&RefCount);
+    if (OldCount == 0 && AbstractTypeUsers.empty())
       this->destroy();
   }
   
index 9d8ac925b8900fd5e015d5416fd56eb8867f72a4..5676ca9d62c5ad3fe4e79bc8f3ead78462ca4d11 100644 (file)
@@ -52,7 +52,7 @@ sys::cas_flag sys::CompareAndSwap(volatile sys::cas_flag* ptr,
 #endif
 }
 
-sys::cas_flag sys::AtomicPostIncrement(volatile sys::cas_flag* ptr) {
+sys::cas_flag sys::AtomicIncrement(volatile sys::cas_flag* ptr) {
 #if LLVM_MULTITHREADED==0
   ++(*ptr);
   return *ptr;
@@ -65,7 +65,7 @@ sys::cas_flag sys::AtomicPostIncrement(volatile sys::cas_flag* ptr) {
 #endif
 }
 
-sys::cas_flag sys::AtomicPostDecrement(volatile sys::cas_flag* ptr) {
+sys::cas_flag sys::AtomicDecrement(volatile sys::cas_flag* ptr) {
 #if LLVM_MULTITHREADED==0
   --(*ptr);
   return *ptr;