1 //===-- Atomic.cpp - Atomic Operations --------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements atomic operations.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/Atomic.h"
14 #include "llvm/Config/llvm-config.h"
21 // We must include windows.h after intrin.h.
26 #if defined(__GNUC__) || (defined(__IBMCPP__) && __IBMCPP__ >= 1210)
30 void sys::MemoryFence() {
31 #if LLVM_HAS_ATOMICS == 0
34 # if defined(GNU_ATOMICS)
36 # elif defined(_MSC_VER)
39 # error No memory fence implementation for your platform!
44 sys::cas_flag sys::CompareAndSwap(volatile sys::cas_flag* ptr,
45 sys::cas_flag new_value,
46 sys::cas_flag old_value) {
47 #if LLVM_HAS_ATOMICS == 0
48 sys::cas_flag result = *ptr;
49 if (result == old_value)
52 #elif defined(GNU_ATOMICS)
53 return __sync_val_compare_and_swap(ptr, old_value, new_value);
54 #elif defined(_MSC_VER)
55 return InterlockedCompareExchange(ptr, new_value, old_value);
57 # error No compare-and-swap implementation for your platform!