[Mesa-dev] [PATCH 7/7] util: Make u_atomic.h typeless.
Matt Turner
mattst88 at gmail.com
Mon Nov 24 16:39:57 PST 2014
like how C11's stdatomic.h provides generic functions. GCC's __sync_*
builtins already take a variety of types, so that's simple.
MSVC and Sun Studio don't, but we can implement it with something that
looks a little crazy but is actually quite readable.
---
src/util/u_atomic.h | 202 +++++++++++++++++++++++-----------------------------
1 file changed, 91 insertions(+), 111 deletions(-)
diff --git a/src/util/u_atomic.h b/src/util/u_atomic.h
index 620191c..097f270 100644
--- a/src/util/u_atomic.h
+++ b/src/util/u_atomic.h
@@ -34,52 +34,15 @@
#define PIPE_ATOMIC "GCC Sync Intrinsics"
-#ifdef __cplusplus
-extern "C" {
-#endif
-
#define p_atomic_set(_v, _i) (*(_v) = (_i))
#define p_atomic_read(_v) (*(_v))
-
-static inline boolean
-p_atomic_dec_zero(int32_t *v)
-{
- return (__sync_sub_and_fetch(v, 1) == 0);
-}
-
-static inline void
-p_atomic_inc(int32_t *v)
-{
- (void) __sync_add_and_fetch(v, 1);
-}
-
-static inline void
-p_atomic_dec(int32_t *v)
-{
- (void) __sync_sub_and_fetch(v, 1);
-}
-
-static inline int32_t
-p_atomic_inc_return(int32_t *v)
-{
- return __sync_add_and_fetch(v, 1);
-}
-
-static inline int32_t
-p_atomic_dec_return(int32_t *v)
-{
- return __sync_sub_and_fetch(v, 1);
-}
-
-static inline int32_t
-p_atomic_cmpxchg(int32_t *v, int32_t old, int32_t _new)
-{
- return __sync_val_compare_and_swap(v, old, _new);
-}
-
-#ifdef __cplusplus
-}
-#endif
+#define p_atomic_dec_zero(v) (__sync_sub_and_fetch((v), 1) == 0)
+#define p_atomic_inc(v) (void) __sync_add_and_fetch((v), 1)
+#define p_atomic_dec(v) (void) __sync_sub_and_fetch((v), 1)
+#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_cmpxchg(v, old, _new) \
+ __sync_val_compare_and_swap((v), (old), (_new))
#endif
@@ -110,56 +73,56 @@ p_atomic_cmpxchg(int32_t *v, int32_t old, int32_t _new)
#include <intrin.h>
+#pragma intrinsic(_InterlockedIncrement16)
#pragma intrinsic(_InterlockedIncrement)
+#pragma intrinsic(_InterlockedIncrement64)
+#pragma intrinsic(_InterlockedDecrement16)
#pragma intrinsic(_InterlockedDecrement)
+#pragma intrinsic(_InterlockedDecrement64)
+#pragma intrinsic(_InterlockedCompareExchange8)
+#pragma intrinsic(_InterlockedCompareExchange16)
#pragma intrinsic(_InterlockedCompareExchange)
-
-#ifdef __cplusplus
-extern "C" {
-#endif
+#pragma intrinsic(_InterlockedCompareExchange64)
#define p_atomic_set(_v, _i) (*(_v) = (_i))
#define p_atomic_read(_v) (*(_v))
-static inline bool
-p_atomic_dec_zero(int32_t *v)
-{
- return _InterlockedDecrement((long *)v) == 0;
-}
-
-static inline void
-p_atomic_inc(int32_t *v)
-{
- _InterlockedIncrement((long *)v);
-}
-
-static inline int32_t
-p_atomic_inc_return(int32_t *v)
-{
- return _InterlockedIncrement((long *)v);
-}
-
-static inline void
-p_atomic_dec(int32_t *v)
-{
- _InterlockedDecrement((long *)v);
-}
-
-static inline int32_t
-p_atomic_dec_return(int32_t *v)
-{
- return _InterlockedDecrement((long *)v);
-}
-
-static inline int32_t
-p_atomic_cmpxchg(int32_t *v, int32_t old, int32_t _new)
-{
- return _InterlockedCompareExchange((long *)v, _new, old);
-}
-
-#ifdef __cplusplus
-}
-#endif
+#define p_atomic_dec_zero(v) \
+ sizeof(*v) == sizeof(short) ? _InterlockedDecrement16((short *) (v)) == 0 : \
+ sizeof(*v) == sizeof(long) ? _InterlockedDecrement ((long *) (v)) == 0 : \
+ sizeof(*v) == sizeof(__int64) ? _InterlockedDecrement64((__int64 *)(v)) == 0 : \
+ (assert(!"should not get here"), 0)
+
+#define p_atomic_inc(v) (void) \
+ sizeof(*v) == sizeof(short) ? _InterlockedIncrement16((short *) (v)) : \
+ sizeof(*v) == sizeof(long) ? _InterlockedIncrement ((long *) (v)) : \
+ sizeof(*v) == sizeof(__int64) ? _InterlockedIncrement64((__int64 *)(v)) : \
+ (assert(!"should not get here"), 0)
+
+#define p_atomic_inc_return(v) (decltype(*v)) \
+ sizeof(*v) == sizeof(short) ? _InterlockedIncrement16((short *) (v)) : \
+ sizeof(*v) == sizeof(long) ? _InterlockedIncrement ((long *) (v)) : \
+ sizeof(*v) == sizeof(__int64) ? _InterlockedIncrement64((__int64 *)(v)) : \
+ (assert(!"should not get here"), 0)
+
+#define p_atomic_dec(v) (void) \
+ sizeof(*v) == sizeof(short) ? _InterlockedDecrement16((short *) (v)) : \
+ sizeof(*v) == sizeof(long) ? _InterlockedDecrement ((long *) (v)) : \
+ sizeof(*v) == sizeof(__int64) ? _InterlockedDecrement64((__int64 *)(v)) : \
+ (assert(!"should not get here"), 0)
+
+#define p_atomic_dec_return(v) (decltype(*v)) \
+ sizeof(*v) == sizeof(short) ? _InterlockedDecrement16((short *) (v)) : \
+ sizeof(*v) == sizeof(long) ? _InterlockedDecrement ((long *) (v)) : \
+ sizeof(*v) == sizeof(__int64) ? _InterlockedDecrement64((__int64 *)(v)) : \
+ (assert(!"should not get here"), 0)
+
+#define p_atomic_cmpxchg(v, old, _new) (decltype(*v)) \
+ sizeof(*v) == sizeof(char) ? _InterlockedCompareExchange8 ((char *) (v), (_new), (old)) : \
+ sizeof(*v) == sizeof(short) ? _InterlockedCompareExchange16((short *) (v), (_new), (old)) : \
+ sizeof(*v) == sizeof(long) ? _InterlockedCompareExchange ((long *) (v), (_new), (old)) : \
+ sizeof(*v) == sizeof(__int64) ? _InterlockedCompareExchange64((__int64 *)(v), (_new), (old)) : \
+ (assert(!"should not get here"), 0)
#endif
@@ -169,36 +132,53 @@ p_atomic_cmpxchg(int32_t *v, int32_t old, int32_t _new)
#include <atomic.h>
-#ifdef __cplusplus
-extern "C" {
-#endif
-
#define p_atomic_set(_v, _i) (*(_v) = (_i))
#define p_atomic_read(_v) (*(_v))
-static inline bool
-p_atomic_dec_zero(int32_t *v)
-{
- uint32_t n = atomic_dec_32_nv((uint32_t *) v);
-
- return n != 0;
-}
-
-#define p_atomic_inc(_v) atomic_inc_32((uint32_t *) _v)
-#define p_atomic_dec(_v) atomic_dec_32((uint32_t *) _v)
-#define p_atomic_inc_return(_v) atomic_inc_32_nv((uint32_t *) _v)
-#define p_atomic_dec_return(_v) atomic_dec_32_nv((uint32_t *) _v)
-
-#define p_atomic_cmpxchg(_v, _old, _new) \
- atomic_cas_32( (uint32_t *) _v, (uint32_t) _old, (uint32_t) _new)
+#define p_atomic_dec_zero(v) \
+ sizeof(*v) == sizeof(uint8_t) ? atomic_dec_8_nv ((uint8_t *)(v)) == 0 : \
+ sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16_nv((uint16_t *)(v)) == 0 : \
+ sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32_nv((uint32_t *)(v)) == 0 : \
+ sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64_nv((uint64_t *)(v)) == 0 : \
+ (assert(!"should not get here"), 0)
+
+#define p_atomic_inc(v) (void) \
+ sizeof(*v) == sizeof(uint8_t) ? atomic_inc_8 ((uint8_t *)(v)) : \
+ sizeof(*v) == sizeof(uint16_t) ? atomic_inc_16((uint16_t *)(v)) : \
+ sizeof(*v) == sizeof(uint32_t) ? atomic_inc_32((uint32_t *)(v)) : \
+ sizeof(*v) == sizeof(uint64_t) ? atomic_inc_64((uint64_t *)(v)) : \
+ (assert(!"should not get here"), 0)
+
+#define p_atomic_inc_return(v) (typeof(*v)) \
+ sizeof(*v) == sizeof(uint8_t) ? atomic_inc_8_nv ((uint8_t *)(v)) : \
+ sizeof(*v) == sizeof(uint16_t) ? atomic_inc_16_nv((uint16_t *)(v)) : \
+ sizeof(*v) == sizeof(uint32_t) ? atomic_inc_32_nv((uint32_t *)(v)) : \
+ sizeof(*v) == sizeof(uint64_t) ? atomic_inc_64_nv((uint64_t *)(v)) : \
+ (assert(!"should not get here"), 0)
+
+#define p_atomic_dec(v) (void) \
+ sizeof(*v) == sizeof(uint8_t) ? atomic_dec_8 ((uint8_t *)(v)) : \
+ sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16((uint16_t *)(v)) : \
+ sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32((uint32_t *)(v)) : \
+ sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64((uint64_t *)(v)) : \
+ (assert(!"should not get here"), 0)
+
+#define p_atomic_dec_return(v) (typeof(*v)) \
+ sizeof(*v) == sizeof(uint8_t) ? atomic_dec_8_nv ((uint8_t *)(v)) : \
+ sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16_nv((uint16_t *)(v)) : \
+ sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32_nv((uint32_t *)(v)) : \
+ sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64_nv((uint64_t *)(v)) : \
+ (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), (old), (_new)) : \
+ sizeof(*v) == sizeof(uint16_t) ? atomic_cas_16((uint16_t *)(v), (old), (_new)) : \
+ sizeof(*v) == sizeof(uint32_t) ? atomic_cas_32((uint32_t *)(v), (old), (_new)) : \
+ sizeof(*v) == sizeof(uint64_t) ? atomic_cas_64((uint64_t *)(v), (old), (_new)) : \
+ (assert(!"should not get here"), 0)
-#ifdef __cplusplus
-}
#endif
-#endif
-
-
#ifndef PIPE_ATOMIC
#error "No pipe_atomic implementation selected"
#endif
--
2.0.4
More information about the mesa-dev
mailing list