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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This header file implements atomic operations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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)
|
|
|
|
#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
|
|
|
}
|
2009-06-17 02:13:00 +02:00
|
|
|
|
2009-06-23 22:17:22 +02:00
|
|
|
sys::cas_flag sys::AtomicIncrement(volatile sys::cas_flag* ptr) {
|
2011-09-19 22:43:23 +02:00
|
|
|
#if LLVM_HAS_ATOMICS == 0
|
2009-06-17 02:13:00 +02:00
|
|
|
++(*ptr);
|
|
|
|
return *ptr;
|
2012-11-02 21:54:45 +01:00
|
|
|
#elif defined(GNU_ATOMICS)
|
2009-06-17 02:13:00 +02:00
|
|
|
return __sync_add_and_fetch(ptr, 1);
|
|
|
|
#elif defined(_MSC_VER)
|
2009-06-19 20:37:50 +02:00
|
|
|
return InterlockedIncrement(ptr);
|
2009-06-17 02:13:00 +02:00
|
|
|
#else
|
|
|
|
# error No atomic increment implementation for your platform!
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-06-23 22:17:22 +02:00
|
|
|
sys::cas_flag sys::AtomicDecrement(volatile sys::cas_flag* ptr) {
|
2011-09-19 22:43:23 +02:00
|
|
|
#if LLVM_HAS_ATOMICS == 0
|
2009-06-17 02:13:00 +02:00
|
|
|
--(*ptr);
|
|
|
|
return *ptr;
|
2012-11-02 21:54:45 +01:00
|
|
|
#elif defined(GNU_ATOMICS)
|
2009-06-17 02:13:00 +02:00
|
|
|
return __sync_sub_and_fetch(ptr, 1);
|
|
|
|
#elif defined(_MSC_VER)
|
2009-06-19 20:37:50 +02:00
|
|
|
return InterlockedDecrement(ptr);
|
2009-06-17 02:13:00 +02:00
|
|
|
#else
|
|
|
|
# error No atomic decrement implementation for your platform!
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-06-23 22:17:22 +02:00
|
|
|
sys::cas_flag sys::AtomicAdd(volatile sys::cas_flag* ptr, sys::cas_flag val) {
|
2011-09-19 22:43:23 +02:00
|
|
|
#if LLVM_HAS_ATOMICS == 0
|
2009-06-23 19:39:31 +02:00
|
|
|
*ptr += val;
|
|
|
|
return *ptr;
|
2012-11-02 21:54:45 +01:00
|
|
|
#elif defined(GNU_ATOMICS)
|
2009-06-23 19:39:31 +02:00
|
|
|
return __sync_add_and_fetch(ptr, val);
|
|
|
|
#elif defined(_MSC_VER)
|
2009-12-07 06:29:59 +01:00
|
|
|
return InterlockedExchangeAdd(ptr, val) + val;
|
2009-06-23 19:39:31 +02:00
|
|
|
#else
|
|
|
|
# error No atomic add implementation for your platform!
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-06-23 23:19:04 +02:00
|
|
|
sys::cas_flag sys::AtomicMul(volatile sys::cas_flag* ptr, sys::cas_flag val) {
|
|
|
|
sys::cas_flag original, result;
|
|
|
|
do {
|
|
|
|
original = *ptr;
|
|
|
|
result = original * val;
|
|
|
|
} while (sys::CompareAndSwap(ptr, result, original) != original);
|
2009-06-17 02:13:00 +02:00
|
|
|
|
2009-06-23 23:19:04 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
sys::cas_flag sys::AtomicDiv(volatile sys::cas_flag* ptr, sys::cas_flag val) {
|
|
|
|
sys::cas_flag original, result;
|
|
|
|
do {
|
|
|
|
original = *ptr;
|
|
|
|
result = original / val;
|
|
|
|
} while (sys::CompareAndSwap(ptr, result, original) != original);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|