2009-05-20 18:26:15 +00:00
|
|
|
//===-- Atomic.cpp - Atomic Operations --------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-05-20 18:26:15 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2014-06-20 01:36:00 +00:00
|
|
|
// This file implements atomic operations.
|
2009-05-20 18:26:15 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-11-29 18:16:10 +00:00
|
|
|
#include "llvm/Support/Atomic.h"
|
2011-12-22 23:04:07 +00:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2009-05-20 18:26:15 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
2017-08-03 20:10:47 +00:00
|
|
|
#include <intrin.h>
|
2017-06-06 12:43:20 +00:00
|
|
|
|
2017-08-03 20:10:47 +00:00
|
|
|
// We must include windows.h after intrin.h.
|
2009-05-20 18:26:15 +00:00
|
|
|
#include <windows.h>
|
2009-06-02 17:35:55 +00:00
|
|
|
#undef MemoryFence
|
2009-05-20 18:26:15 +00:00
|
|
|
#endif
|
|
|
|
|
2012-11-02 20:54:45 +00:00
|
|
|
#if defined(__GNUC__) || (defined(__IBMCPP__) && __IBMCPP__ >= 1210)
|
|
|
|
#define GNU_ATOMICS
|
|
|
|
#endif
|
|
|
|
|
2009-05-20 18:26:15 +00:00
|
|
|
void sys::MemoryFence() {
|
2011-09-19 20:43:23 +00:00
|
|
|
#if LLVM_HAS_ATOMICS == 0
|
2009-05-20 18:26:15 +00:00
|
|
|
return;
|
|
|
|
#else
|
2012-11-02 20:54:45 +00:00
|
|
|
# if defined(GNU_ATOMICS)
|
2009-05-20 18:26:15 +00:00
|
|
|
__sync_synchronize();
|
|
|
|
# elif defined(_MSC_VER)
|
|
|
|
MemoryBarrier();
|
|
|
|
# else
|
|
|
|
# error No memory fence implementation for your platform!
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-06-23 20:17:22 +00:00
|
|
|
sys::cas_flag sys::CompareAndSwap(volatile sys::cas_flag* ptr,
|
|
|
|
sys::cas_flag new_value,
|
|
|
|
sys::cas_flag old_value) {
|
2011-09-19 20:43:23 +00:00
|
|
|
#if LLVM_HAS_ATOMICS == 0
|
2009-06-23 20:17:22 +00:00
|
|
|
sys::cas_flag result = *ptr;
|
2009-05-20 19:01:50 +00:00
|
|
|
if (result == old_value)
|
|
|
|
*ptr = new_value;
|
2009-05-20 18:26:15 +00:00
|
|
|
return result;
|
2012-11-02 20:54:45 +00:00
|
|
|
#elif defined(GNU_ATOMICS)
|
2009-05-20 18:26:15 +00:00
|
|
|
return __sync_val_compare_and_swap(ptr, old_value, new_value);
|
|
|
|
#elif defined(_MSC_VER)
|
2009-05-20 19:06:49 +00:00
|
|
|
return InterlockedCompareExchange(ptr, new_value, old_value);
|
2009-05-20 18:26:15 +00:00
|
|
|
#else
|
|
|
|
# error No compare-and-swap implementation for your platform!
|
|
|
|
#endif
|
2009-06-03 11:54:28 +00:00
|
|
|
}
|