-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAtomic.h
53 lines (49 loc) · 2.01 KB
/
Atomic.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#pragma once
#include <atomic>
#include <iostream>
namespace juce
{
namespace AtomicHelpers
{
template <typename T> struct DiffTypeHelper { using Type = T; };
template <typename T> struct DiffTypeHelper<T*> { using Type = std::ptrdiff_t; };
}
template <typename Type>
struct Atomic final
{
using DiffType = typename AtomicHelpers::DiffTypeHelper<Type>::Type;
Atomic() noexcept : value (Type()) {}
Atomic (Type initialValue) noexcept : value (initialValue) {}
Atomic (const Atomic& other) noexcept : value (other.get()) {}
~Atomic() noexcept
{
#if __cpp_lib_atomic_is_always_lock_free
static_assert (std::atomic<Type>::is_always_lock_free,
"This class can only be used for lock-free types");
#endif
}
Type get() const noexcept { return value.load(); }
void set (Type newValue) noexcept { value = newValue; }
Type exchange (Type newValue) noexcept { return value.exchange (newValue); }
bool compareAndSetBool (Type newValue, Type valueToCompare) noexcept
{
return value.compare_exchange_strong (valueToCompare, newValue);
}
Atomic<Type>& operator= (const Atomic& other) noexcept
{
value = other.value.load();
return *this;
}
Atomic<Type>& operator= (Type newValue) noexcept
{
value = newValue;
return *this;
}
Type operator+= (DiffType amountToAdd) noexcept { return value += amountToAdd; }
Type operator-= (DiffType amountToSubtract) noexcept { return value -= amountToSubtract; }
Type operator++() noexcept { return ++value; }
Type operator--() noexcept { return --value; }
void memoryBarrier() noexcept { atomic_thread_fence (std::memory_order_seq_cst); }
std::atomic<Type> value;
};
} //end namespace juce