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"
27 inline void MemoryFence() {
28 #if LLVM_MULTITHREADED==0
31 # if defined(__GNUC__)
33 # elif defined(_MSC_VER)
36 # error No memory fence implementation for your platform!
41 #if LLVM_MULTITHREADED==0
42 typedef unsigned long cas_flag;
44 inline T CompareAndSwap(volatile T* dest,
51 #elif defined(__GNUC__)
52 typedef unsigned long cas_flag;
54 inline T CompareAndSwap(volatile T* ptr,
57 return __sync_val_compare_and_swap(ptr, old_value, new_value);
59 #elif defined(_MSC_VER)
60 typedef LONG cas_flag;
62 inline T CompareAndSwap(volatile T* ptr,
66 return InterlockedCompareExchange(ptr, new_value, old_value);
67 else if (sizeof(T) == 8)
68 return InterlockedCompareExchange64(ptr, new_value, old_value);
70 assert(0 && "Unsupported compare-and-swap size!");
74 inline T* CompareAndSwap<T*>(volatile T** ptr,
77 return InterlockedCompareExchangePtr(ptr, new_value, old_value);
82 # error No compare-and-swap implementation for your platform!