From 6b77710d2da18e313874ad4a5448c2006d6d2d33 Mon Sep 17 00:00:00 2001 From: Jesse Natalie Date: Tue, 30 Aug 2022 07:16:47 -0700 Subject: [PATCH] u_atomic: Add a helper for pointer compare-exchange Notably this helps with MSVC, which complains about compiling the not-taken branches of the ternary when called with pointer args. Using a version that doesn't have "runtime" sizeof checks eliminates the warnings. Reviewed-by: Jonathan Strobl (cherry picked from commit 6718bff75b4d3823df5f9c1d66eace07d37e9c92) Part-of: --- src/util/u_atomic.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/util/u_atomic.h b/src/util/u_atomic.h index 4f06979798a..9c236818767 100644 --- a/src/util/u_atomic.h +++ b/src/util/u_atomic.h @@ -76,6 +76,7 @@ */ #define p_atomic_cmpxchg(v, old, _new) \ __sync_val_compare_and_swap((v), (old), (_new)) +#define p_atomic_cmpxchg_ptr(v, old, _new) p_atomic_cmpxchg(v, old, _new) #endif @@ -100,6 +101,7 @@ #define p_atomic_add_return(_v, _i) (*(_v) = *(_v) + (_i)) #define p_atomic_fetch_add(_v, _i) (*(_v) = *(_v) + (_i), *(_v) - (_i)) #define p_atomic_cmpxchg(_v, _old, _new) (*(_v) == (_old) ? (*(_v) = (_new), (_old)) : *(_v)) +#define p_atomic_cmpxchg_ptr(_v, _old, _new) p_atomic_cmpxchg(_v, _old, _new) #endif @@ -174,6 +176,12 @@ sizeof *(_v) == sizeof(__int64) ? InterlockedCompareExchange64 ((__int64 *)(_v), (__int64)(_new), (__int64)(_old)) : \ (assert(!"should not get here"), 0)) +#if defined(_WIN64) +#define p_atomic_cmpxchg_ptr(_v, _old, _new) (void *)InterlockedCompareExchange64((__int64 *)(_v), (__int64)(_new), (__int64)(_old)) +#else +#define p_atomic_cmpxchg_ptr(_v, _old, _new) (void *)InterlockedCompareExchange((long *)(_v), (long)(_new), (long)(_old)) +#endif + #define PIPE_NATIVE_ATOMIC_XCHG #define p_atomic_xchg(_v, _new) (\ sizeof *(_v) == sizeof(long) ? InterlockedExchange ((long *) (_v), (long) (_new)) : \ @@ -255,6 +263,12 @@ sizeof(*v) == sizeof(uint64_t) ? atomic_cas_64((uint64_t *)(v), (uint64_t)(old), (uint64_t)(_new)) : \ (assert(!"should not get here"), 0)) +#if INTPTR_MAX == INT32_MAX +#define p_atomic_cmpxchg_ptr(v, old, _new) (__typeof(*v))(atomic_cas_32((uint32_t *)(v), (uint32_t)(old), (uint32_t)(_new))) +#else +#define p_atomic_cmpxchg_ptr(v, old, _new) (__typeof(*v))(atomic_cas_64((uint64_t *)(v), (uint64_t)(old), (uint64_t)(_new))) +#endif + #endif #ifndef PIPE_ATOMIC