Now that we have atomics support properly detected by configure,
[oota-llvm.git] / include / llvm / System / Atomic.h
index d062f995c98500d14f97dbde5663e461e183be61..8bae46e25458b31e35c6c40a9074b0e67a223c3f 100644 (file)
 #ifndef LLVM_SYSTEM_ATOMIC_H
 #define LLVM_SYSTEM_ATOMIC_H
 
-#include <stdint.h>
-
-#if defined(__APPLE__)
-#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ == 0)
-#include <libkern/OSAtomic.h>
-#endif
-#elif LLVM_ON_WIN32
+#if defined(_MSC_VER)
 #include <windows.h>
 #endif
 
 namespace llvm {
   namespace sys {
     
-#if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
     inline void MemoryFence() {
-      return;
-    }
-    
-    typedef uint32_t cas_flag;
+#if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
+#  if defined(__GNUC__)
+      __asm__ __volatile__("" : : : "memory");
+#  elif defined(_MSC_VER)
+      __asm { };
+#  else
+#    error No memory fence implementation for your platform!
+#  endif
+#else
+#  if defined(__GNUC__)
+      __sync_synchronize();
+#  elif defined(_MSC_VER)
+      MemoryBarrier();
+#  else
+#    error No memory fence implementation for your platform!
+#  endif
+#endif
+}
+
+#if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
+    typedef unsigned long cas_flag;
     inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
       cas_flag result = *dest;
       if (result == c)
         *dest = exc;
       return result;
     }
-    
-#elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
-    inline void MemoryFence() {
-      __sync_synchronize();
+#elif defined(__GNUC__)
+    typedef unsigned long cas_flag;
+    inline cas_flag CompareAndSwap(cas_flag* ptr,
+                                   cas_flag new_value,
+                                   cas_flag old_value) {
+      return __sync_val_compare_and_swap(ptr, old_value, new_value);
     }
-    
-    typedef volatile uint32_t cas_flag;
-    inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
-      return __sync_val_compare_and_swap(dest, exc, c);
-    }
-    
-#elif defined(__APPLE__)
-    inline void MemoryFence() {
-      OSMemoryBarrier();
-    }
-    
-    typedef volatile int32_t cas_flag;
-    inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
-      cas_flag old = *dest;
-      OSAtomicCompareAndSwap32(c, exc, dest);
-      return old;
-    }
-#elif defined(LLVM_ON_WIN32)
-#warning Memory fence implementation requires Windows 2003 or later.
-    inline void MemoryFence() {
-      MemoryBarrier();
-    }
-    
-    typedef volatile long cas_flag;
-    inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
-      return _InterlockedCompareExchange(dest, exc, c);
+#elif defined(_MSC_VER) && _M_IX86 > 400
+    typedef LONG cas_flag;
+    inline cas_flag CompareAndSwap(cas_flag* ptr,
+                                   cas_flag new_value,
+                                   cas_flag old_value) {
+      return InterlockedCompareExchange(addr, new_value, old_value);
     }
 #else
-#error No memory atomics implementation for your platform!
+#  error No compare-and-swap implementation for your platform!
 #endif
-    
+
   }
 }
 
-#endif
\ No newline at end of file
+#endif