6 #ifndef TURI_ATOMIC_HPP 7 #define TURI_ATOMIC_HPP 10 #include <type_traits> 12 #include <core/storage/serialization/serialization_includes.hpp> 13 #include <core/parallel/atomic_ops.hpp> 17 template<
typename T,
bool IsIntegral>
34 T
inc() {
return __sync_add_and_fetch(&value, 1); }
37 T
dec() {
return __sync_sub_and_fetch(&value, 1); }
40 operator T()
const {
return value; }
49 T
inc(
const T val) {
return __sync_add_and_fetch(&value, val); }
52 T
dec(
const T val) {
return __sync_sub_and_fetch(&value, val); }
73 T
inc_ret_last(
const T val) {
return __sync_fetch_and_add(&value, val); }
76 T
dec_ret_last(
const T val) {
return __sync_fetch_and_sub(&value, val); }
79 T
exchange(
const T val) {
return __sync_lock_test_and_set(&value, val); }
90 atomic_impl(
const T& value = T()) : value(value) { }
93 T inc() {
return inc(1); }
96 T dec() {
return dec(1); }
99 operator T()
const {
return value; }
102 T operator++() {
return inc(); }
105 T operator--() {
return dec(); }
113 new_value = prev_value + val;
124 new_value = prev_value - val;
130 T operator+=(
const T val) {
return inc(val); }
133 T operator-=(
const T val) {
return dec(val); }
136 T inc_ret_last() {
return inc_ret_last(1); }
139 T dec_ret_last() {
return dec_ret_last(1); }
142 T operator++(
int) {
return inc_ret_last(); }
145 T operator--(
int) {
return dec_ret_last(); }
148 T inc_ret_last(
const T val) {
153 new_value = prev_value + val;
159 T dec_ret_last(
const T val) {
164 new_value = prev_value - val;
170 T exchange(
const T val) {
return __sync_lock_test_and_set(&value, val); }
174 template <
typename T>
175 class atomic:
public turi_impl::atomic_impl<T, std::is_integral<T>::value> {
178 atomic(
const T& value = T()):
179 turi_impl::atomic_impl<T, std::is_integral<T>::value>(value) { }
T exchange(const T val)
Performs an atomic exchange with 'val', returning the previous value.
atomic_impl(const T &value=T())
Creates an atomic number with value "value".
volatile T value
The current value of the atomic number.
T operator++()
Performs an atomic increment by 1, returning the new value.
T operator++(int)
Performs an atomic increment by 1, returning the old value.
T operator--(int)
Performs an atomic decrement by 1, returning the old value.
T operator+=(const T val)
Performs an atomic increment by 'val', returning the new value.
T dec()
Performs an atomic decrement by 1, returning the new value.
Inheriting from this type will force the serializer to treat the derived type as a POD type...
bool atomic_compare_and_swap(T &a, T oldval, T newval)
T inc()
Performs an atomic increment by 1, returning the new value.
T inc(const T val)
Performs an atomic increment by 'val', returning the new value.
T dec_ret_last()
Performs an atomic decrement by 1, returning the old value.
T dec(const T val)
Performs an atomic decrement by 'val', returning the new value.
T inc_ret_last(const T val)
Performs an atomic increment by 'val', returning the old value.
T operator--()
Performs an atomic decrement by 1, returning the new value.
T operator-=(const T val)
Performs an atomic decrement by 'val', returning the new value.
T dec_ret_last(const T val)
Performs an atomic decrement by 'val', returning the new value.
T inc_ret_last()
Performs an atomic increment by 1, returning the old value.