1 //===-- Atomic.cpp - 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 implements atomic operations.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Atomic.h"
15 #include "llvm/Config/llvm-config.h"
22 // We must include windows.h after intrin.h.
27 #if defined(__GNUC__) || (defined(__IBMCPP__) && __IBMCPP__ >= 1210)
31 void sys::MemoryFence() {
32 #if LLVM_HAS_ATOMICS == 0
35 # if defined(GNU_ATOMICS)
37 # elif defined(_MSC_VER)
40 # error No memory fence implementation for your platform!
45 sys::cas_flag
sys::CompareAndSwap(volatile sys::cas_flag
* ptr
,
46 sys::cas_flag new_value
,
47 sys::cas_flag old_value
) {
48 #if LLVM_HAS_ATOMICS == 0
49 sys::cas_flag result
= *ptr
;
50 if (result
== old_value
)
53 #elif defined(GNU_ATOMICS)
54 return __sync_val_compare_and_swap(ptr
, old_value
, new_value
);
55 #elif defined(_MSC_VER)
56 return InterlockedCompareExchange(ptr
, new_value
, old_value
);
58 # error No compare-and-swap implementation for your platform!