Use double-checked locking for this lazy initialization.
authorOwen Anderson <resistor@mac.com>
Wed, 17 Jun 2009 22:53:57 +0000 (22:53 +0000)
committerOwen Anderson <resistor@mac.com>
Wed, 17 Jun 2009 22:53:57 +0000 (22:53 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73653 91177308-0d34-0410-b5e6-96231b3b80d8

lib/VMCore/Type.cpp

index b815eec665b3c1b45a778dbd1fefcac3d2f99a26..0add0af3b849f00d84d549b7397b17c27ece9f78 100644 (file)
@@ -453,8 +453,27 @@ void DerivedType::dropAllTypeUses() {
   if (NumContainedTys != 0) {
     // The type must stay abstract.  To do this, we insert a pointer to a type
     // that will never get resolved, thus will always be abstract.
-    static Type *AlwaysOpaqueTy = OpaqueType::get();
-    static PATypeHolder Holder(AlwaysOpaqueTy);
+    static Type *AlwaysOpaqueTy = 0;
+    static PATypeHolder* Holder = 0;
+    if (!AlwaysOpaqueTy) {
+      if (llvm_is_multithreaded()) {
+        llvm_acquire_global_lock();
+      
+        if (!AlwaysOpaqueTy) {
+          Type *tmp = OpaqueType::get();
+          PATypeHolder* tmp2 = new PATypeHolder(AlwaysOpaqueTy);
+          sys::MemoryFence();
+          AlwaysOpaqueTy = tmp;
+          Holder = tmp2;
+        }
+      
+        llvm_release_global_lock();
+      } else {
+        AlwaysOpaqueTy = OpaqueType::get();
+        Holder = new PATypeHolder(AlwaysOpaqueTy);
+      } 
+    }
+    
     ContainedTys[0] = AlwaysOpaqueTy;
 
     // Change the rest of the types to be Int32Ty's.  It doesn't matter what we