From 385d13f26d2b69db9423d806833d9b08f948a54a Mon Sep 17 00:00:00 2001 From: Jason Ekstrand Date: Mon, 2 Dec 2019 14:36:39 -0600 Subject: [PATCH] util/atomic: Add a _return variant of p_atomic_add Reviewed-by: Ivan Briano Reviewed-by: Lionel Landwerlin --- src/util/u_atomic.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/util/u_atomic.h b/src/util/u_atomic.h index 57c04ccb2ea..45e8e2e0188 100644 --- a/src/util/u_atomic.h +++ b/src/util/u_atomic.h @@ -48,6 +48,7 @@ #define p_atomic_add(v, i) (void) __atomic_add_fetch((v), (i), __ATOMIC_ACQ_REL) #define p_atomic_inc_return(v) __atomic_add_fetch((v), 1, __ATOMIC_ACQ_REL) #define p_atomic_dec_return(v) __atomic_sub_fetch((v), 1, __ATOMIC_ACQ_REL) +#define p_atomic_add_return(v, i) __atomic_add_fetch((v), (i), __ATOMIC_ACQ_REL) #define p_atomic_xchg(v, i) __atomic_exchange_n((v), (i), __ATOMIC_ACQ_REL) #define PIPE_NATIVE_ATOMIC_XCHG @@ -61,6 +62,7 @@ #define p_atomic_add(v, i) (void) __sync_add_and_fetch((v), (i)) #define p_atomic_inc_return(v) __sync_add_and_fetch((v), 1) #define p_atomic_dec_return(v) __sync_sub_and_fetch((v), 1) +#define p_atomic_add_return(v, i) __sync_add_and_fetch((v), (i)) #endif @@ -87,9 +89,10 @@ #define p_atomic_dec_zero(_v) (p_atomic_dec_return(_v) == 0) #define p_atomic_inc(_v) ((void) p_atomic_inc_return(_v)) #define p_atomic_dec(_v) ((void) p_atomic_dec_return(_v)) -#define p_atomic_add(_v, _i) (*(_v) = *(_v) + (_i)) +#define p_atomic_add(_v, _i) ((void) p_atomic_add_return((_v), (_i)) #define p_atomic_inc_return(_v) (++(*(_v))) #define p_atomic_dec_return(_v) (--(*(_v))) +#define p_atomic_add(_v, _i) (*(_v) = *(_v) + (_i)) #define p_atomic_cmpxchg(_v, _old, _new) (*(_v) == (_old) ? (*(_v) = (_new), (_old)) : *(_v)) #endif @@ -142,7 +145,10 @@ sizeof *(_v) == sizeof(__int64) ? InterlockedDecrement64 ((__int64 *)(_v)) : \ (assert(!"should not get here"), 0)) -#define p_atomic_add(_v, _i) (\ +#define p_atomic_add(_v, _i) \ + ((void) p_atomic_add_return((_v), (_i)) + +#define p_atomic_add_return(_v, _i) (\ sizeof *(_v) == sizeof(char) ? _InterlockedExchangeAdd8 ((char *) (_v), (_i)) : \ sizeof *(_v) == sizeof(short) ? _InterlockedExchangeAdd16((short *) (_v), (_i)) : \ sizeof *(_v) == sizeof(long) ? _InterlockedExchangeAdd ((long *) (_v), (_i)) : \ @@ -210,6 +216,13 @@ sizeof(*v) == sizeof(uint64_t) ? atomic_add_64((uint64_t *)(v), (i)) : \ (assert(!"should not get here"), 0)) +#define p_atomic_add_return(v, i) (void) ( \ + sizeof(*v) == sizeof(uint8_t) ? atomic_add_8_nv ((uint8_t *)(v), (i)) : \ + sizeof(*v) == sizeof(uint16_t) ? atomic_add_16_nv((uint16_t *)(v), (i)) : \ + sizeof(*v) == sizeof(uint32_t) ? atomic_add_32_nv((uint32_t *)(v), (i)) : \ + sizeof(*v) == sizeof(uint64_t) ? atomic_add_64_nv((uint64_t *)(v), (i)) : \ + (assert(!"should not get here"), 0)) + #define p_atomic_cmpxchg(v, old, _new) (__typeof(*v))( \ sizeof(*v) == sizeof(uint8_t) ? atomic_cas_8 ((uint8_t *)(v), (uint8_t )(old), (uint8_t )(_new)) : \ sizeof(*v) == sizeof(uint16_t) ? atomic_cas_16((uint16_t *)(v), (uint16_t)(old), (uint16_t)(_new)) : \ -- 2.30.2