1 //===- llvm/System/Atomic.h - Atomic Operations -----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the llvm::sys atomic operations.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SYSTEM_ATOMIC_H
15 #define LLVM_SYSTEM_ATOMIC_H
17 #include "llvm/Config/config.h"
28 inline void MemoryFence() {
29 #if LLVM_MULTITHREADED==0
32 # if defined(__GNUC__)
34 # elif defined(_MSC_VER)
37 # error No memory fence implementation for your platform!
42 #if LLVM_MULTITHREADED==0
43 typedef unsigned long cas_flag;
45 inline T CompareAndSwap(volatile T* dest,
52 #elif defined(__GNUC__)
53 typedef unsigned long cas_flag;
55 inline T CompareAndSwap(volatile T* ptr,
58 return __sync_val_compare_and_swap(ptr, old_value, new_value);
60 #elif defined(_MSC_VER)
61 typedef LONG cas_flag;
63 inline T CompareAndSwap(volatile T* ptr,
67 return InterlockedCompareExchange(ptr, new_value, old_value);
68 else if (sizeof(T) == 8)
69 return InterlockedCompareExchange64(ptr, new_value, old_value);
71 assert(0 && "Unsupported compare-and-swap size!");
75 inline T* CompareAndSwap<T*>(volatile T** ptr,
78 return InterlockedCompareExchangePtr(ptr, new_value, old_value);
83 # error No compare-and-swap implementation for your platform!