Commit 43a532fc authored by Igor Sheludko's avatar Igor Sheludko Committed by Commit Bot

[cleanup] Templatize AsAtomicWordX and AsAtomicPointer definitions

Bug: v8:8238
Change-Id: I7fa1538909bfaf368498ebcc1c59eed258e81a16
Reviewed-on: https://chromium-review.googlesource.com/c/1352272Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Commit-Queue: Igor Sheludko <ishell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57877}
parent 535542a8
......@@ -58,122 +58,57 @@ class AtomicValue {
base::AtomicWord value_;
};
class AsAtomic32 {
// Provides atomic operations for a values stored at some address.
template <typename TAtomicStorageType>
class AsAtomicImpl {
public:
template <typename T>
static T Acquire_Load(T* addr) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::Atomic32));
return to_return_type<T>(base::Acquire_Load(to_storage_addr(addr)));
}
template <typename T>
static T Relaxed_Load(T* addr) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::Atomic32));
return to_return_type<T>(base::Relaxed_Load(to_storage_addr(addr)));
}
template <typename T>
static void Release_Store(T* addr,
typename std::remove_reference<T>::type new_value) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::Atomic32));
base::Release_Store(to_storage_addr(addr), to_storage_type(new_value));
}
template <typename T>
static void Relaxed_Store(T* addr,
typename std::remove_reference<T>::type new_value) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::Atomic32));
base::Relaxed_Store(to_storage_addr(addr), to_storage_type(new_value));
}
using AtomicStorageType = TAtomicStorageType;
template <typename T>
static T Release_CompareAndSwap(
T* addr, typename std::remove_reference<T>::type old_value,
typename std::remove_reference<T>::type new_value) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::Atomic32));
return to_return_type<T>(base::Release_CompareAndSwap(
to_storage_addr(addr), to_storage_type(old_value),
to_storage_type(new_value)));
}
// Atomically sets bits selected by the mask to the given value.
// Returns false if the bits are already set as needed.
template <typename T>
static bool SetBits(T* addr, T bits, T mask) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::Atomic32));
DCHECK_EQ(bits & ~mask, static_cast<T>(0));
T old_value;
T new_value;
do {
old_value = Relaxed_Load(addr);
if ((old_value & mask) == bits) return false;
new_value = (old_value & ~mask) | bits;
} while (Release_CompareAndSwap(addr, old_value, new_value) != old_value);
return true;
}
private:
template <typename T>
static base::Atomic32 to_storage_type(T value) {
return static_cast<base::Atomic32>(value);
}
template <typename T>
static T to_return_type(base::Atomic32 value) {
return static_cast<T>(value);
}
template <typename T>
static base::Atomic32* to_storage_addr(T* value) {
return reinterpret_cast<base::Atomic32*>(value);
}
template <typename T>
static const base::Atomic32* to_storage_addr(const T* value) {
return reinterpret_cast<const base::Atomic32*>(value);
}
};
class AsAtomicWord {
public:
template <typename T>
static T Acquire_Load(T* addr) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::AtomicWord));
return to_return_type<T>(base::Acquire_Load(to_storage_addr(addr)));
STATIC_ASSERT(sizeof(T) <= sizeof(AtomicStorageType));
return cast_helper<T>::to_return_type(
base::Acquire_Load(to_storage_addr(addr)));
}
template <typename T>
static T Relaxed_Load(T* addr) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::AtomicWord));
return to_return_type<T>(base::Relaxed_Load(to_storage_addr(addr)));
STATIC_ASSERT(sizeof(T) <= sizeof(AtomicStorageType));
return cast_helper<T>::to_return_type(
base::Relaxed_Load(to_storage_addr(addr)));
}
template <typename T>
static void Release_Store(T* addr,
typename std::remove_reference<T>::type new_value) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::AtomicWord));
base::Release_Store(to_storage_addr(addr), to_storage_type(new_value));
STATIC_ASSERT(sizeof(T) <= sizeof(AtomicStorageType));
base::Release_Store(to_storage_addr(addr),
cast_helper<T>::to_storage_type(new_value));
}
template <typename T>
static void Relaxed_Store(T* addr,
typename std::remove_reference<T>::type new_value) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::AtomicWord));
base::Relaxed_Store(to_storage_addr(addr), to_storage_type(new_value));
STATIC_ASSERT(sizeof(T) <= sizeof(AtomicStorageType));
base::Relaxed_Store(to_storage_addr(addr),
cast_helper<T>::to_storage_type(new_value));
}
template <typename T>
static T Release_CompareAndSwap(
T* addr, typename std::remove_reference<T>::type old_value,
typename std::remove_reference<T>::type new_value) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::AtomicWord));
return to_return_type<T>(base::Release_CompareAndSwap(
to_storage_addr(addr), to_storage_type(old_value),
to_storage_type(new_value)));
STATIC_ASSERT(sizeof(T) <= sizeof(AtomicStorageType));
return cast_helper<T>::to_return_type(base::Release_CompareAndSwap(
to_storage_addr(addr), cast_helper<T>::to_storage_type(old_value),
cast_helper<T>::to_storage_type(new_value)));
}
// Atomically sets bits selected by the mask to the given value.
// Returns false if the bits are already set as needed.
template <typename T>
static bool SetBits(T* addr, T bits, T mask) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::AtomicWord));
STATIC_ASSERT(sizeof(T) <= sizeof(AtomicStorageType));
DCHECK_EQ(bits & ~mask, static_cast<T>(0));
T old_value;
T new_value;
......@@ -186,138 +121,51 @@ class AsAtomicWord {
}
private:
template <typename T>
static base::AtomicWord to_storage_type(T value) {
return static_cast<base::AtomicWord>(value);
}
template <typename T>
static T to_return_type(base::AtomicWord value) {
return static_cast<T>(value);
}
template <typename T>
static base::AtomicWord* to_storage_addr(T* value) {
return reinterpret_cast<base::AtomicWord*>(value);
}
template <typename T>
static const base::AtomicWord* to_storage_addr(const T* value) {
return reinterpret_cast<const base::AtomicWord*>(value);
}
};
class AsAtomic8 {
public:
template <typename T>
static T Acquire_Load(T* addr) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::Atomic8));
return to_return_type<T>(base::Acquire_Load(to_storage_addr(addr)));
}
template <typename T>
static T Relaxed_Load(T* addr) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::Atomic8));
return to_return_type<T>(base::Relaxed_Load(to_storage_addr(addr)));
}
template <typename T>
static void Release_Store(T* addr,
typename std::remove_reference<T>::type new_value) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::Atomic8));
base::Release_Store(to_storage_addr(addr), to_storage_type(new_value));
}
template <typename T>
static void Relaxed_Store(T* addr,
typename std::remove_reference<T>::type new_value) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::Atomic8));
base::Relaxed_Store(to_storage_addr(addr), to_storage_type(new_value));
}
template <typename U>
struct cast_helper {
static AtomicStorageType to_storage_type(U value) {
return static_cast<AtomicStorageType>(value);
}
static U to_return_type(AtomicStorageType value) {
return static_cast<U>(value);
}
};
template <typename T>
static T Release_CompareAndSwap(
T* addr, typename std::remove_reference<T>::type old_value,
typename std::remove_reference<T>::type new_value) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::Atomic8));
return to_return_type<T>(base::Release_CompareAndSwap(
to_storage_addr(addr), to_storage_type(old_value),
to_storage_type(new_value)));
}
template <typename U>
struct cast_helper<U*> {
static AtomicStorageType to_storage_type(U* value) {
return reinterpret_cast<AtomicStorageType>(value);
}
static U* to_return_type(AtomicStorageType value) {
return reinterpret_cast<U*>(value);
}
};
private:
template <typename T>
static base::Atomic8 to_storage_type(T value) {
return static_cast<base::Atomic8>(value);
}
template <typename T>
static T to_return_type(base::Atomic8 value) {
return static_cast<T>(value);
static AtomicStorageType* to_storage_addr(T* value) {
return reinterpret_cast<AtomicStorageType*>(value);
}
template <typename T>
static base::Atomic8* to_storage_addr(T* value) {
return reinterpret_cast<base::Atomic8*>(value);
}
template <typename T>
static const base::Atomic8* to_storage_addr(const T* value) {
return reinterpret_cast<const base::Atomic8*>(value);
static const AtomicStorageType* to_storage_addr(const T* value) {
return reinterpret_cast<const AtomicStorageType*>(value);
}
};
class AsAtomicPointer {
public:
template <typename T>
static T Acquire_Load(T* addr) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::AtomicWord));
return to_return_type<T>(base::Acquire_Load(to_storage_addr(addr)));
}
template <typename T>
static T Relaxed_Load(T* addr) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::AtomicWord));
return to_return_type<T>(base::Relaxed_Load(to_storage_addr(addr)));
}
template <typename T>
static void Release_Store(T* addr,
typename std::remove_reference<T>::type new_value) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::AtomicWord));
base::Release_Store(to_storage_addr(addr), to_storage_type(new_value));
}
template <typename T>
static void Relaxed_Store(T* addr,
typename std::remove_reference<T>::type new_value) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::AtomicWord));
base::Relaxed_Store(to_storage_addr(addr), to_storage_type(new_value));
}
template <typename T>
static T Release_CompareAndSwap(
T* addr, typename std::remove_reference<T>::type old_value,
typename std::remove_reference<T>::type new_value) {
STATIC_ASSERT(sizeof(T) <= sizeof(base::AtomicWord));
return to_return_type<T>(base::Release_CompareAndSwap(
to_storage_addr(addr), to_storage_type(old_value),
to_storage_type(new_value)));
}
using AsAtomic8 = AsAtomicImpl<base::Atomic8>;
using AsAtomic32 = AsAtomicImpl<base::Atomic32>;
using AsAtomicWord = AsAtomicImpl<base::AtomicWord>;
private:
template <typename T>
static base::AtomicWord to_storage_type(T value) {
return reinterpret_cast<base::AtomicWord>(value);
}
template <typename T>
static T to_return_type(base::AtomicWord value) {
return reinterpret_cast<T>(value);
}
template <typename T>
static base::AtomicWord* to_storage_addr(T* value) {
return reinterpret_cast<base::AtomicWord*>(value);
}
// This is similar to AsAtomicWord but it explicitly deletes functionality
// provided atomic access to bit representation of stored values.
template <typename TAtomicStorageType>
class AsAtomicPointerImpl : public AsAtomicImpl<TAtomicStorageType> {
public:
template <typename T>
static const base::AtomicWord* to_storage_addr(const T* value) {
return reinterpret_cast<const base::AtomicWord*>(value);
}
static bool SetBits(T* addr, T bits, T mask) = delete;
};
using AsAtomicPointer = AsAtomicPointerImpl<base::AtomicWord>;
template <typename T,
typename = typename std::enable_if<std::is_unsigned<T>::value>::type>
inline void CheckedIncrement(std::atomic<T>* number, T amount) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment