2009-05-20 20:26:15 +02:00
|
|
|
//===-- Atomic.cpp - Atomic Operations --------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2014-06-20 03:36:00 +02:00
|
|
|
// This file implements atomic operations.
|
2009-05-20 20:26:15 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-11-29 19:16:10 +01:00
|
|
|
#include "llvm/Support/Atomic.h"
|
2011-12-23 00:04:07 +01:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2009-05-20 20:26:15 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
2014-05-06 02:57:33 +02:00
|
|
|
#include <Intrin.h>
|
2009-05-20 20:26:15 +02:00
|
|
|
#include <windows.h>
|
2009-06-02 19:35:55 +02:00
|
|
|
#undef MemoryFence
|
2009-05-20 20:26:15 +02:00
|
|
|
#endif
|
|
|
|
|
2012-11-02 21:54:45 +01:00
|
|
|
#if defined(__GNUC__) || (defined(__IBMCPP__) && __IBMCPP__ >= 1210)
|
|
|
|
#define GNU_ATOMICS
|
|
|
|
#endif
|
|
|
|
|
2009-05-20 20:26:15 +02:00
|
|
|
void sys::MemoryFence() {
|
2011-09-19 22:43:23 +02:00
|
|
|
#if LLVM_HAS_ATOMICS == 0
|
2009-05-20 20:26:15 +02:00
|
|
|
return;
|
|
|
|
#else
|
2012-11-02 21:54:45 +01:00
|
|
|
# if defined(GNU_ATOMICS)
|
2009-05-20 20:26:15 +02:00
|
|
|
__sync_synchronize();
|
|
|
|
# elif defined(_MSC_VER)
|
|
|
|
MemoryBarrier();
|
|
|
|
# else
|
|
|
|
# error No memory fence implementation for your platform!
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-06-23 22:17:22 +02:00
|
|
|
sys::cas_flag sys::CompareAndSwap(volatile sys::cas_flag* ptr,
|
|
|
|
sys::cas_flag new_value,
|
|
|
|
sys::cas_flag old_value) {
|
2011-09-19 22:43:23 +02:00
|
|
|
#if LLVM_HAS_ATOMICS == 0
|
2009-06-23 22:17:22 +02:00
|
|
|
sys::cas_flag result = *ptr;
|
2009-05-20 21:01:50 +02:00
|
|
|
if (result == old_value)
|
|
|
|
*ptr = new_value;
|
2009-05-20 20:26:15 +02:00
|
|
|
return result;
|
2012-11-02 21:54:45 +01:00
|
|
|
#elif defined(GNU_ATOMICS)
|
2009-05-20 20:26:15 +02:00
|
|
|
return __sync_val_compare_and_swap(ptr, old_value, new_value);
|
|
|
|
#elif defined(_MSC_VER)
|
2009-05-20 21:06:49 +02:00
|
|
|
return InterlockedCompareExchange(ptr, new_value, old_value);
|
2009-05-20 20:26:15 +02:00
|
|
|
#else
|
|
|
|
# error No compare-and-swap implementation for your platform!
|
|
|
|
#endif
|
2009-06-03 13:54:28 +02:00
|
|
|
}
|