[PATCH] Removing the static destructor from ManagedStatic.cpp by controlling the allocation and de-allocaation of the mutex.

Chris Bieneman beanz at apple.com
Mon Oct 13 15:04:39 PDT 2014


================
Comment at: include/llvm/Support/Threading.h:51-69
@@ +50,21 @@
+#else
+  static volatile sys::cas_flag initialized = 0;
+  sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0);
+  if (old_val == 0) {
+    UserFn();
+    sys::MemoryFence();
+    TsanIgnoreWritesBegin();
+    TsanHappensBefore(&initialized);
+    initialized = 2;
+    TsanIgnoreWritesEnd();
+  } else {
+    sys::cas_flag tmp = initialized;
+    sys::MemoryFence();
+    while (tmp != 2) {
+      tmp = initialized;
+      sys::MemoryFence();
+    }
+  }
+  TsanHappensAfter(&initialized);
+#endif
+}
----------------
rnk wrote:
> mingw64 should have thread safe statics, so I think this can just be:
>   struct InitOnceWrapper {
>     InitOnceWrapper() { UserFn(); }
>   };
>   static InitOnceWrapper InitOnceVar;
> 
> I don't actually understand memory barriers, so I'd rather rely on standard primitives.
Doesn't MingW64 have <mutex>? I think we only need to fall back to the custom code on MingW32.

http://reviews.llvm.org/D5473






More information about the llvm-commits mailing list