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

Reid Kleckner rnk at google.com
Mon Oct 13 14:57:45 PDT 2014


Thanks, I think this is almost done.

================
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
+}
----------------
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.

================
Comment at: include/llvm/Support/Threading.h:18
@@ -17,1 +17,3 @@
 
+#include "llvm/Config/config.h"
+
----------------
beanz wrote:
> rnk wrote:
> > We can only include llvm/Config/config.h from .cpp files, not .h files.
> Really? We do include it in headers like Unix.h and WindowsSupport.h. I'm not really sure how to implement this without access to the HAVE_PTHREAD_ONCE define.
I should've said .h files in llvm/include/, since those are exported to users, while config.h is not installed. The .h files in llvm/lib/ like Unix.h can use config.h.

================
Comment at: include/llvm/Support/Threading.h:47
@@ +46,3 @@
+  {                                                                            \
+    static volatile sys::cas_flag initialized = 0;                             \
+    sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0);           \
----------------
beanz wrote:
> rnk wrote:
> > I don't think we want to commit this double checked locking. I think we can simplify this to:
> > 
> >   if Windows:
> >     use InitOnceExecuteOnce in lib/Support/Windows/Threading.inc
> >   else:
> >     use a C++11 thread-safe static local in lib/Support/Unix/Threading.inc
> I'm not sure this is the right breakdown. I believe MSVC has std::call_once, so I was hoping to do a breakdown based on the availability of <mutex>, and pthread_once.
That won't cover the mingw64 with no pthread emulation case, though. I think we can use thread-safe statics in that case, though.

http://reviews.llvm.org/D5473






More information about the llvm-commits mailing list