diff options
Diffstat (limited to 'core/safe_refcount.h')
-rw-r--r-- | core/safe_refcount.h | 32 |
1 files changed, 12 insertions, 20 deletions
diff --git a/core/safe_refcount.h b/core/safe_refcount.h index 953a877397..dc4e62354a 100644 --- a/core/safe_refcount.h +++ b/core/safe_refcount.h @@ -43,9 +43,9 @@ template <class T> static _ALWAYS_INLINE_ T atomic_conditional_increment(volatile T *pw) { - - if (*pw == 0) + if (*pw == 0) { return 0; + } (*pw)++; @@ -54,7 +54,6 @@ static _ALWAYS_INLINE_ T atomic_conditional_increment(volatile T *pw) { template <class T> static _ALWAYS_INLINE_ T atomic_decrement(volatile T *pw) { - (*pw)--; return *pw; @@ -62,7 +61,6 @@ static _ALWAYS_INLINE_ T atomic_decrement(volatile T *pw) { template <class T> static _ALWAYS_INLINE_ T atomic_increment(volatile T *pw) { - (*pw)++; return *pw; @@ -70,7 +68,6 @@ static _ALWAYS_INLINE_ T atomic_increment(volatile T *pw) { template <class T, class V> static _ALWAYS_INLINE_ T atomic_sub(volatile T *pw, volatile V val) { - (*pw) -= val; return *pw; @@ -78,7 +75,6 @@ static _ALWAYS_INLINE_ T atomic_sub(volatile T *pw, volatile V val) { template <class T, class V> static _ALWAYS_INLINE_ T atomic_add(volatile T *pw, volatile V val) { - (*pw) += val; return *pw; @@ -86,9 +82,9 @@ static _ALWAYS_INLINE_ T atomic_add(volatile T *pw, volatile V val) { template <class T, class V> static _ALWAYS_INLINE_ T atomic_exchange_if_greater(volatile T *pw, volatile V val) { - - if (val > *pw) + if (val > *pw) { *pw = val; + } return *pw; } @@ -102,49 +98,47 @@ static _ALWAYS_INLINE_ T atomic_exchange_if_greater(volatile T *pw, volatile V v template <class T> static _ALWAYS_INLINE_ T atomic_conditional_increment(volatile T *pw) { - while (true) { T tmp = static_cast<T const volatile &>(*pw); - if (tmp == 0) + if (tmp == 0) { return 0; // if zero, can't add to it anymore - if (__sync_val_compare_and_swap(pw, tmp, tmp + 1) == tmp) + } + if (__sync_val_compare_and_swap(pw, tmp, tmp + 1) == tmp) { return tmp + 1; + } } } template <class T> static _ALWAYS_INLINE_ T atomic_decrement(volatile T *pw) { - return __sync_sub_and_fetch(pw, 1); } template <class T> static _ALWAYS_INLINE_ T atomic_increment(volatile T *pw) { - return __sync_add_and_fetch(pw, 1); } template <class T, class V> static _ALWAYS_INLINE_ T atomic_sub(volatile T *pw, volatile V val) { - return __sync_sub_and_fetch(pw, val); } template <class T, class V> static _ALWAYS_INLINE_ T atomic_add(volatile T *pw, volatile V val) { - return __sync_add_and_fetch(pw, val); } template <class T, class V> static _ALWAYS_INLINE_ T atomic_exchange_if_greater(volatile T *pw, volatile V val) { - while (true) { T tmp = static_cast<T const volatile &>(*pw); - if (tmp >= val) + if (tmp >= val) { return tmp; // already greater, or equal - if (__sync_val_compare_and_swap(pw, tmp, val) == tmp) + } + if (__sync_val_compare_and_swap(pw, tmp, val) == tmp) { return val; + } } } @@ -171,7 +165,6 @@ uint64_t atomic_exchange_if_greater(volatile uint64_t *pw, volatile uint64_t val #endif struct SafeRefCount { - uint32_t count; public: @@ -203,7 +196,6 @@ public: } _ALWAYS_INLINE_ void init(uint32_t p_value = 1) { - count = p_value; } }; |