2009-05-21 02:26:15 +08: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 09:36:00 +08:00
|
|
|
// This file implements atomic operations.
|
2009-05-21 02:26:15 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/Atomic.h"
|
2011-12-23 07:04:07 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2009-05-21 02:26:15 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
2014-05-06 08:57:33 +08:00
|
|
|
#include <Intrin.h>
|
2017-06-06 20:43:20 +08:00
|
|
|
|
|
|
|
// We must include windows.h after Intrin.h.
|
2009-05-21 02:26:15 +08:00
|
|
|
#include <windows.h>
|
2009-06-03 01:35:55 +08:00
|
|
|
#undef MemoryFence
|
2009-05-21 02:26:15 +08:00
|
|
|
#endif
|
|
|
|
|
2012-11-03 04:54:45 +08:00
|
|
|
#if defined(__GNUC__) || (defined(__IBMCPP__) && __IBMCPP__ >= 1210)
|
|
|
|
#define GNU_ATOMICS
|
|
|
|
#endif
|
|
|
|
|
2009-05-21 02:26:15 +08:00
|
|
|
void sys::MemoryFence() {
|
2011-09-20 04:43:23 +08:00
|
|
|
#if LLVM_HAS_ATOMICS == 0
|
2009-05-21 02:26:15 +08:00
|
|
|
return;
|
|
|
|
#else
|
2012-11-03 04:54:45 +08:00
|
|
|
# if defined(GNU_ATOMICS)
|
2009-05-21 02:26:15 +08:00
|
|
|
__sync_synchronize();
|
|
|
|
# elif defined(_MSC_VER)
|
|
|
|
MemoryBarrier();
|
|
|
|
# else
|
|
|
|
# error No memory fence implementation for your platform!
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-06-24 04:17:22 +08:00
|
|
|
sys::cas_flag sys::CompareAndSwap(volatile sys::cas_flag* ptr,
|
|
|
|
sys::cas_flag new_value,
|
|
|
|
sys::cas_flag old_value) {
|
2011-09-20 04:43:23 +08:00
|
|
|
#if LLVM_HAS_ATOMICS == 0
|
2009-06-24 04:17:22 +08:00
|
|
|
sys::cas_flag result = *ptr;
|
2009-05-21 03:01:50 +08:00
|
|
|
if (result == old_value)
|
|
|
|
*ptr = new_value;
|
2009-05-21 02:26:15 +08:00
|
|
|
return result;
|
2012-11-03 04:54:45 +08:00
|
|
|
#elif defined(GNU_ATOMICS)
|
2009-05-21 02:26:15 +08:00
|
|
|
return __sync_val_compare_and_swap(ptr, old_value, new_value);
|
|
|
|
#elif defined(_MSC_VER)
|
2009-05-21 03:06:49 +08:00
|
|
|
return InterlockedCompareExchange(ptr, new_value, old_value);
|
2009-05-21 02:26:15 +08:00
|
|
|
#else
|
|
|
|
# error No compare-and-swap implementation for your platform!
|
|
|
|
#endif
|
2009-06-03 19:54:28 +08:00
|
|
|
}
|