mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-07-24 23:29:41 +00:00
[C++11] Replace LLVM atomics with std::atomic.
With C++11 we finally have a standardized way to specify atomic operations. Use them to replace the existing custom implemention. Sadly the translation is not entirely trivial as std::atomic allows more fine-grained control over the atomicity. I tried to preserve the old semantics as well as possible. Differential Revision: http://llvm-reviews.chandlerc.com/D2915 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@202730 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
59a4517759
commit
4721e55a0c
@ -26,8 +26,8 @@
|
|||||||
#ifndef LLVM_ADT_STATISTIC_H
|
#ifndef LLVM_ADT_STATISTIC_H
|
||||||
#define LLVM_ADT_STATISTIC_H
|
#define LLVM_ADT_STATISTIC_H
|
||||||
|
|
||||||
#include "llvm/Support/Atomic.h"
|
|
||||||
#include "llvm/Support/Valgrind.h"
|
#include "llvm/Support/Valgrind.h"
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
namespace llvm {
|
namespace llvm {
|
||||||
class raw_ostream;
|
class raw_ostream;
|
||||||
@ -36,10 +36,10 @@ class Statistic {
|
|||||||
public:
|
public:
|
||||||
const char *Name;
|
const char *Name;
|
||||||
const char *Desc;
|
const char *Desc;
|
||||||
volatile llvm::sys::cas_flag Value;
|
std::atomic<unsigned> Value;
|
||||||
bool Initialized;
|
bool Initialized;
|
||||||
|
|
||||||
llvm::sys::cas_flag getValue() const { return Value; }
|
unsigned getValue() const { return Value; }
|
||||||
const char *getName() const { return Name; }
|
const char *getName() const { return Name; }
|
||||||
const char *getDesc() const { return Desc; }
|
const char *getDesc() const { return Desc; }
|
||||||
|
|
||||||
@ -63,48 +63,54 @@ public:
|
|||||||
// atomic operation to update the value safely in the presence of
|
// atomic operation to update the value safely in the presence of
|
||||||
// concurrent accesses, but not to read the return value, so the
|
// concurrent accesses, but not to read the return value, so the
|
||||||
// return value is not thread safe.
|
// return value is not thread safe.
|
||||||
sys::AtomicIncrement(&Value);
|
++Value;
|
||||||
return init();
|
return init();
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned operator++(int) {
|
unsigned operator++(int) {
|
||||||
init();
|
init();
|
||||||
unsigned OldValue = Value;
|
unsigned OldValue = Value++;
|
||||||
sys::AtomicIncrement(&Value);
|
|
||||||
return OldValue;
|
return OldValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Statistic &operator--() {
|
const Statistic &operator--() {
|
||||||
sys::AtomicDecrement(&Value);
|
--Value;
|
||||||
return init();
|
return init();
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned operator--(int) {
|
unsigned operator--(int) {
|
||||||
init();
|
init();
|
||||||
unsigned OldValue = Value;
|
unsigned OldValue = Value--;
|
||||||
sys::AtomicDecrement(&Value);
|
|
||||||
return OldValue;
|
return OldValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Statistic &operator+=(const unsigned &V) {
|
const Statistic &operator+=(const unsigned &V) {
|
||||||
if (!V) return *this;
|
if (!V) return *this;
|
||||||
sys::AtomicAdd(&Value, V);
|
Value += V;
|
||||||
return init();
|
return init();
|
||||||
}
|
}
|
||||||
|
|
||||||
const Statistic &operator-=(const unsigned &V) {
|
const Statistic &operator-=(const unsigned &V) {
|
||||||
if (!V) return *this;
|
if (!V) return *this;
|
||||||
sys::AtomicAdd(&Value, -V);
|
Value -= V;
|
||||||
return init();
|
return init();
|
||||||
}
|
}
|
||||||
|
|
||||||
const Statistic &operator*=(const unsigned &V) {
|
const Statistic &operator*=(const unsigned &V) {
|
||||||
sys::AtomicMul(&Value, V);
|
unsigned Original, Result;
|
||||||
|
do {
|
||||||
|
Original = Value;
|
||||||
|
Result = Original * V;
|
||||||
|
} while (!Value.compare_exchange_strong(Original, Result));
|
||||||
return init();
|
return init();
|
||||||
}
|
}
|
||||||
|
|
||||||
const Statistic &operator/=(const unsigned &V) {
|
const Statistic &operator/=(const unsigned &V) {
|
||||||
sys::AtomicDiv(&Value, V);
|
unsigned Original, Result;
|
||||||
|
do {
|
||||||
|
Original = Value;
|
||||||
|
Result = Original / V;
|
||||||
|
} while (!Value.compare_exchange_strong(Original, Result));
|
||||||
return init();
|
return init();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,7 +157,7 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
Statistic &init() {
|
Statistic &init() {
|
||||||
bool tmp = Initialized;
|
bool tmp = Initialized;
|
||||||
sys::MemoryFence();
|
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||||
if (!tmp) RegisterStatistic();
|
if (!tmp) RegisterStatistic();
|
||||||
TsanHappensAfter(this);
|
TsanHappensAfter(this);
|
||||||
return *this;
|
return *this;
|
||||||
@ -162,7 +168,7 @@ protected:
|
|||||||
// STATISTIC - A macro to make definition of statistics really simple. This
|
// STATISTIC - A macro to make definition of statistics really simple. This
|
||||||
// automatically passes the DEBUG_TYPE of the file into the statistic.
|
// automatically passes the DEBUG_TYPE of the file into the statistic.
|
||||||
#define STATISTIC(VARNAME, DESC) \
|
#define STATISTIC(VARNAME, DESC) \
|
||||||
static llvm::Statistic VARNAME = { DEBUG_TYPE, DESC, 0, 0 }
|
static llvm::Statistic VARNAME = { DEBUG_TYPE, DESC, {}, 0 }
|
||||||
|
|
||||||
/// \brief Enable the collection and printing of statistics.
|
/// \brief Enable the collection and printing of statistics.
|
||||||
void EnableStatistics();
|
void EnableStatistics();
|
||||||
|
@ -24,8 +24,8 @@
|
|||||||
#include "Pass.h"
|
#include "Pass.h"
|
||||||
#include "llvm/InitializePasses.h"
|
#include "llvm/InitializePasses.h"
|
||||||
#include "llvm/PassRegistry.h"
|
#include "llvm/PassRegistry.h"
|
||||||
#include "llvm/Support/Atomic.h"
|
|
||||||
#include "llvm/Support/Valgrind.h"
|
#include "llvm/Support/Valgrind.h"
|
||||||
|
#include <atomic>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace llvm {
|
namespace llvm {
|
||||||
@ -147,21 +147,21 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
#define CALL_ONCE_INITIALIZATION(function) \
|
#define CALL_ONCE_INITIALIZATION(function) \
|
||||||
static volatile sys::cas_flag initialized = 0; \
|
static std::atomic<int> initialized; \
|
||||||
sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \
|
int old_val = 0; \
|
||||||
if (old_val == 0) { \
|
if (initialized.compare_exchange_strong(old_val, 1)) { \
|
||||||
function(Registry); \
|
function(Registry); \
|
||||||
sys::MemoryFence(); \
|
std::atomic_thread_fence(std::memory_order_seq_cst); \
|
||||||
TsanIgnoreWritesBegin(); \
|
TsanIgnoreWritesBegin(); \
|
||||||
TsanHappensBefore(&initialized); \
|
TsanHappensBefore(&initialized); \
|
||||||
initialized = 2; \
|
initialized = 2; \
|
||||||
TsanIgnoreWritesEnd(); \
|
TsanIgnoreWritesEnd(); \
|
||||||
} else { \
|
} else { \
|
||||||
sys::cas_flag tmp = initialized; \
|
int tmp = initialized.load(); \
|
||||||
sys::MemoryFence(); \
|
std::atomic_thread_fence(std::memory_order_seq_cst); \
|
||||||
while (tmp != 2) { \
|
while (tmp != 2) { \
|
||||||
tmp = initialized; \
|
tmp = initialized.load(); \
|
||||||
sys::MemoryFence(); \
|
std::atomic_thread_fence(std::memory_order_seq_cst); \
|
||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
TsanHappensAfter(&initialized);
|
TsanHappensAfter(&initialized);
|
||||||
|
@ -14,9 +14,9 @@
|
|||||||
#ifndef LLVM_SUPPORT_MANAGED_STATIC_H
|
#ifndef LLVM_SUPPORT_MANAGED_STATIC_H
|
||||||
#define LLVM_SUPPORT_MANAGED_STATIC_H
|
#define LLVM_SUPPORT_MANAGED_STATIC_H
|
||||||
|
|
||||||
#include "llvm/Support/Atomic.h"
|
|
||||||
#include "llvm/Support/Threading.h"
|
#include "llvm/Support/Threading.h"
|
||||||
#include "llvm/Support/Valgrind.h"
|
#include "llvm/Support/Valgrind.h"
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
namespace llvm {
|
namespace llvm {
|
||||||
|
|
||||||
@ -64,7 +64,8 @@ public:
|
|||||||
// Accessors.
|
// Accessors.
|
||||||
C &operator*() {
|
C &operator*() {
|
||||||
void* tmp = Ptr;
|
void* tmp = Ptr;
|
||||||
if (llvm_is_multithreaded()) sys::MemoryFence();
|
if (llvm_is_multithreaded())
|
||||||
|
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||||
if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
|
if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
|
||||||
TsanHappensAfter(this);
|
TsanHappensAfter(this);
|
||||||
|
|
||||||
@ -72,7 +73,8 @@ public:
|
|||||||
}
|
}
|
||||||
C *operator->() {
|
C *operator->() {
|
||||||
void* tmp = Ptr;
|
void* tmp = Ptr;
|
||||||
if (llvm_is_multithreaded()) sys::MemoryFence();
|
if (llvm_is_multithreaded())
|
||||||
|
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||||
if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
|
if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
|
||||||
TsanHappensAfter(this);
|
TsanHappensAfter(this);
|
||||||
|
|
||||||
@ -80,7 +82,8 @@ public:
|
|||||||
}
|
}
|
||||||
const C &operator*() const {
|
const C &operator*() const {
|
||||||
void* tmp = Ptr;
|
void* tmp = Ptr;
|
||||||
if (llvm_is_multithreaded()) sys::MemoryFence();
|
if (llvm_is_multithreaded())
|
||||||
|
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||||
if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
|
if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
|
||||||
TsanHappensAfter(this);
|
TsanHappensAfter(this);
|
||||||
|
|
||||||
@ -88,7 +91,8 @@ public:
|
|||||||
}
|
}
|
||||||
const C *operator->() const {
|
const C *operator->() const {
|
||||||
void* tmp = Ptr;
|
void* tmp = Ptr;
|
||||||
if (llvm_is_multithreaded()) sys::MemoryFence();
|
if (llvm_is_multithreaded())
|
||||||
|
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||||
if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
|
if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
|
||||||
TsanHappensAfter(this);
|
TsanHappensAfter(this);
|
||||||
|
|
||||||
|
@ -18,7 +18,6 @@
|
|||||||
#include "LLVMContextImpl.h"
|
#include "LLVMContextImpl.h"
|
||||||
#include "llvm/ADT/StringExtras.h"
|
#include "llvm/ADT/StringExtras.h"
|
||||||
#include "llvm/IR/Type.h"
|
#include "llvm/IR/Type.h"
|
||||||
#include "llvm/Support/Atomic.h"
|
|
||||||
#include "llvm/Support/Debug.h"
|
#include "llvm/Support/Debug.h"
|
||||||
#include "llvm/Support/ManagedStatic.h"
|
#include "llvm/Support/ManagedStatic.h"
|
||||||
#include "llvm/Support/Mutex.h"
|
#include "llvm/Support/Mutex.h"
|
||||||
|
@ -19,14 +19,14 @@
|
|||||||
#include "llvm/IR/Function.h"
|
#include "llvm/IR/Function.h"
|
||||||
#include "llvm/IR/Instruction.h"
|
#include "llvm/IR/Instruction.h"
|
||||||
#include "llvm/IR/Metadata.h"
|
#include "llvm/IR/Metadata.h"
|
||||||
#include "llvm/Support/Atomic.h"
|
#include <atomic>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
|
||||||
int llvm::getNextAvailablePluginDiagnosticKind() {
|
int llvm::getNextAvailablePluginDiagnosticKind() {
|
||||||
static sys::cas_flag PluginKindID = DK_FirstPluginKind;
|
static std::atomic<int> PluginKindID(DK_FirstPluginKind);
|
||||||
return (int)sys::AtomicIncrement(&PluginKindID);
|
return ++PluginKindID;
|
||||||
}
|
}
|
||||||
|
|
||||||
DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(const Instruction &I,
|
DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(const Instruction &I,
|
||||||
|
@ -13,7 +13,6 @@
|
|||||||
|
|
||||||
#include "llvm/Support/ManagedStatic.h"
|
#include "llvm/Support/ManagedStatic.h"
|
||||||
#include "llvm/Config/config.h"
|
#include "llvm/Config/config.h"
|
||||||
#include "llvm/Support/Atomic.h"
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
|
||||||
@ -28,7 +27,7 @@ void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
|
|||||||
void* tmp = Creator ? Creator() : 0;
|
void* tmp = Creator ? Creator() : 0;
|
||||||
|
|
||||||
TsanHappensBefore(this);
|
TsanHappensBefore(this);
|
||||||
sys::MemoryFence();
|
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||||
|
|
||||||
// This write is racy against the first read in the ManagedStatic
|
// This write is racy against the first read in the ManagedStatic
|
||||||
// accessors. The race is benign because it does a second read after a
|
// accessors. The race is benign because it does a second read after a
|
||||||
|
@ -76,7 +76,7 @@ void Statistic::RegisterStatistic() {
|
|||||||
StatInfo->addStatistic(this);
|
StatInfo->addStatistic(this);
|
||||||
|
|
||||||
TsanHappensBefore(this);
|
TsanHappensBefore(this);
|
||||||
sys::MemoryFence();
|
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||||
// Remember we have been registered.
|
// Remember we have been registered.
|
||||||
TsanIgnoreWritesBegin();
|
TsanIgnoreWritesBegin();
|
||||||
Initialized = true;
|
Initialized = true;
|
||||||
|
@ -13,8 +13,8 @@
|
|||||||
|
|
||||||
#include "llvm/Support/Threading.h"
|
#include "llvm/Support/Threading.h"
|
||||||
#include "llvm/Config/config.h"
|
#include "llvm/Config/config.h"
|
||||||
#include "llvm/Support/Atomic.h"
|
|
||||||
#include "llvm/Support/Mutex.h"
|
#include "llvm/Support/Mutex.h"
|
||||||
|
#include <atomic>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
@ -31,7 +31,7 @@ bool llvm::llvm_start_multithreaded() {
|
|||||||
|
|
||||||
// We fence here to ensure that all initialization is complete BEFORE we
|
// We fence here to ensure that all initialization is complete BEFORE we
|
||||||
// return from llvm_start_multithreaded().
|
// return from llvm_start_multithreaded().
|
||||||
sys::MemoryFence();
|
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||||
return true;
|
return true;
|
||||||
#else
|
#else
|
||||||
return false;
|
return false;
|
||||||
@ -44,7 +44,7 @@ void llvm::llvm_stop_multithreaded() {
|
|||||||
|
|
||||||
// We fence here to insure that all threaded operations are complete BEFORE we
|
// We fence here to insure that all threaded operations are complete BEFORE we
|
||||||
// return from llvm_stop_multithreaded().
|
// return from llvm_stop_multithreaded().
|
||||||
sys::MemoryFence();
|
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||||
|
|
||||||
multithreaded_mode = false;
|
multithreaded_mode = false;
|
||||||
delete global_lock;
|
delete global_lock;
|
||||||
|
@ -81,14 +81,14 @@ raw_ostream *llvm::CreateInfoOutputFile() {
|
|||||||
static TimerGroup *DefaultTimerGroup = 0;
|
static TimerGroup *DefaultTimerGroup = 0;
|
||||||
static TimerGroup *getDefaultTimerGroup() {
|
static TimerGroup *getDefaultTimerGroup() {
|
||||||
TimerGroup *tmp = DefaultTimerGroup;
|
TimerGroup *tmp = DefaultTimerGroup;
|
||||||
sys::MemoryFence();
|
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||||
if (tmp) return tmp;
|
if (tmp) return tmp;
|
||||||
|
|
||||||
llvm_acquire_global_lock();
|
llvm_acquire_global_lock();
|
||||||
tmp = DefaultTimerGroup;
|
tmp = DefaultTimerGroup;
|
||||||
if (!tmp) {
|
if (!tmp) {
|
||||||
tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
|
tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
|
||||||
sys::MemoryFence();
|
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||||
DefaultTimerGroup = tmp;
|
DefaultTimerGroup = tmp;
|
||||||
}
|
}
|
||||||
llvm_release_global_lock();
|
llvm_release_global_lock();
|
||||||
|
Loading…
Reference in New Issue
Block a user