Don't relax org or align. They change size as the relaxation happens, but they
[oota-llvm.git] / lib / Support / ManagedStatic.cpp
index c365e013c6fccbbae1d32c017be546c7276cefeb..c767c15e71c9d853aa573b22b3518df138b7d12f 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Support/ManagedStatic.h"
+#include "llvm/Config/config.h"
+#include "llvm/Support/Atomic.h"
 #include <cassert>
 using namespace llvm;
 
 static const ManagedStaticBase *StaticList = 0;
 
-void ManagedStaticBase::RegisterManagedStatic(void *ObjPtr,
+void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
                                               void (*Deleter)(void*)) const {
-  assert(Ptr == 0 && DeleterFn == 0 && Next == 0 &&
-         "Partially init static?");
-  Ptr = ObjPtr;
-  DeleterFn = Deleter;
+  if (llvm_is_multithreaded()) {
+    llvm_acquire_global_lock();
+
+    if (Ptr == 0) {
+      void* tmp = Creator ? Creator() : 0;
+
+      sys::MemoryFence();
+      Ptr = tmp;
+      DeleterFn = Deleter;
+      
+      // Add to list of managed statics.
+      Next = StaticList;
+      StaticList = this;
+    }
+
+    llvm_release_global_lock();
+  } else {
+    assert(Ptr == 0 && DeleterFn == 0 && Next == 0 &&
+           "Partially initialized ManagedStatic!?");
+    Ptr = Creator ? Creator() : 0;
+    DeleterFn = Deleter;
   
-  // Add to list of managed statics.
-  Next = StaticList;
-  StaticList = this;
+    // Add to list of managed statics.
+    Next = StaticList;
+    StaticList = this;
+  }
 }
 
 void ManagedStaticBase::destroy() const {
@@ -49,5 +69,7 @@ void ManagedStaticBase::destroy() const {
 void llvm::llvm_shutdown() {
   while (StaticList)
     StaticList->destroy();
+
+  if (llvm_is_multithreaded()) llvm_stop_multithreaded();
 }