From: Owen Anderson Date: Thu, 14 May 2009 21:24:15 +0000 (+0000) Subject: Add CompareAndSwap. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=96938130801568c0a7d1e9fe3c09747d60168c8d;p=oota-llvm.git Add CompareAndSwap. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@71795 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/System/Atomic.h b/include/llvm/System/Atomic.h index 7db31d52dce..3f788074d9b 100644 --- a/include/llvm/System/Atomic.h +++ b/include/llvm/System/Atomic.h @@ -11,34 +11,70 @@ // //===----------------------------------------------------------------------===// +#ifndef LLVM_SYSTEM_ATOMIC_H +#define LLVM_SYSTEM_ATOMIC_H + #include "llvm/Config/config.h" +#include #ifdef __APPLE__ -#include + #elif LLVM_ON_WIN32 #include #endif -#ifndef LLVM_SYSTEM_ATOMIC_H -#define LLVM_SYSTEM_ATOMIC_H - namespace llvm { namespace sys { - inline void MemoryFence() { + #if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0 + inline void MemoryFence() { return; + } + + typedef uint32_t 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(); + } + + 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 UInt32 cas_flag; + inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) { + cas_flag old = *dest; + OSCompareAndSwap(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); + } #else -#warning No memory fence implementation found for you platform! +#error No memory atomics implementation for your platform! #endif - } + } }