Removing the static initializer in ManagedStatic.cpp by using llvm_call_once to initi...
[oota-llvm.git] / lib / Support / Windows / Threading.inc
diff --git a/lib/Support/Windows/Threading.inc b/lib/Support/Windows/Threading.inc
new file mode 100644 (file)
index 0000000..099c6b2
--- /dev/null
@@ -0,0 +1,19 @@
+#include <winbase.h>
+
+void llvm::call_once(once_flag &flag, void (*fptr)(void)) {
+  while (flag != Done) {
+    if (flag == Wait) {
+      ::Sleep(1);
+      continue;
+    }
+
+    sys::cas_flag old_val = sys::CompareAndSwap(&flag, Wait, Uninitialized);
+    if (old_val == Uninitialized) {
+      fptr();
+      sys::MemoryFence();
+      flag = Done;
+      return;
+    }
+  }
+  sys::MemoryFence();
+}