aboutsummaryrefslogtreecommitdiffstats
path: root/include/atomic.h
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2014-07-21 23:14:48 -0700
committerChris Robinson <[email protected]>2014-07-22 00:20:28 -0700
commit5a339a2a5b12545c105a2a3dcfb1d8e466b0381f (patch)
tree95dab694ebe14d41e3e775dc651d07ee26cbe44a /include/atomic.h
parent7b41ed7ec4bfb7e8ac3daef3eebc6831b96a8fa4 (diff)
Add macros for generic atomic functionality
Diffstat (limited to 'include/atomic.h')
-rw-r--r--include/atomic.h181
1 files changed, 145 insertions, 36 deletions
diff --git a/include/atomic.h b/include/atomic.h
index 4adb7a94..174aebe9 100644
--- a/include/atomic.h
+++ b/include/atomic.h
@@ -40,6 +40,33 @@ inline int CompExchangeInt(volatile int *ptr, int oldval, int newval)
inline void *CompExchangePtr(XchgPtr *ptr, void *oldval, void *newval)
{ return __sync_val_compare_and_swap(ptr, oldval, newval); }
+
+#define ATOMIC(T) struct { T volatile value; }
+
+#define ATOMIC_INIT_STATIC(_newval) {(_newval)}
+
+#define ATOMIC_LOAD_UNSAFE(_val) ((_val).value)
+#define ATOMIC_STORE_UNSAFE(_val, _newval) do { \
+ (_val).value = (_newval); \
+} while(0)
+
+#define ATOMIC_LOAD(_val) (__sync_synchronize(),(_val).value)
+#define ATOMIC_STORE(_val, _newval) do { \
+ (_val).value = (_newval); \
+ __sync_synchronize(); \
+} while(0)
+
+#define ATOMIC_EXCHANGE(T, _val, _newval) __extension__({ \
+ static_assert(sizeof(T)==sizeof((_val).value), "Type "#T" has incorrect size!"); \
+ T _r = __sync_lock_test_and_set(&(_val).value, (_newval)); \
+ _r; \
+})
+#define ATOMIC_COMPARE_EXCHANGE(T, _val, _oldval, _newval) __extension__({ \
+ static_assert(sizeof(T)==sizeof((_val).value), "Type "#T" has incorrect size!"); \
+ T _r = __sync_val_compare_and_swap(&(_val).value, (_oldval), (_newval)); \
+ _r; \
+})
+
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
inline uint xaddl(volatile uint *dest, int incr)
@@ -79,54 +106,91 @@ inline uint CompExchangeRef(volatile RefCount *ptr, uint oldval, uint newval)
return ret;
}
+#define EXCHANGE(S, ret, dest, newval) __asm__ __volatile__( \
+ "lock; xchg"S" %0,(%1)" \
+ : "=r" (ret) \
+ : "r" (dest), "0" (newval) \
+ : "memory" \
+)
+#define COMP_EXCHANGE(S, ret, dest, oldval, newval) __asm__ __volatile__( \
+ "lock; cmpxchg"S" %2,(%1)" \
+ : "=a" (ret) \
+ : "r" (dest), "r" (newval), "0" (oldval) \
+ : "memory" \
+)
+
+
inline int ExchangeInt(volatile int *dest, int newval)
{
int ret;
- __asm__ __volatile__("lock; xchgl %0,(%1)"
- : "=r" (ret)
- : "r" (dest), "0" (newval)
- : "memory");
+ EXCHANGE("l", ret, dest, newval);
return ret;
}
+inline int CompExchangeInt(volatile int *dest, int oldval, int newval)
+{
+ int ret;
+ COMP_EXCHANGE("l", ret, dest, oldval, newval);
+ return ret;
+}
+
+#ifdef __i386__
inline void *ExchangePtr(XchgPtr *dest, void *newval)
{
void *ret;
- __asm__ __volatile__(
-#ifdef __i386__
- "lock; xchgl %0,(%1)"
-#else
- "lock; xchgq %0,(%1)"
-#endif
- : "=r" (ret)
- : "r" (dest), "0" (newval)
- : "memory"
- );
+ EXCHANGE("l", ret, dest, newval);
return ret;
}
-inline int CompExchangeInt(volatile int *dest, int oldval, int newval)
+inline void *CompExchangePtr(XchgPtr *dest, void *oldval, void *newval)
{
- int ret;
- __asm__ __volatile__("lock; cmpxchgl %2,(%1)"
- : "=a" (ret)
- : "r" (dest), "r" (newval), "0" (oldval)
- : "memory");
+ void *ret;
+ COMP_EXCHANGE("l", ret, dest, oldval, newval);
+ return ret;
+}
+#else
+inline void *ExchangePtr(XchgPtr *dest, void *newval)
+{
+ void *ret;
+ EXCHANGE("q", ret, dest, newval);
return ret;
}
inline void *CompExchangePtr(XchgPtr *dest, void *oldval, void *newval)
{
void *ret;
- __asm__ __volatile__(
-#ifdef __i386__
- "lock; cmpxchgl %2,(%1)"
-#else
- "lock; cmpxchgq %2,(%1)"
-#endif
- : "=a" (ret)
- : "r" (dest), "r" (newval), "0" (oldval)
- : "memory"
- );
+ COMP_EXCHANGE("q", ret, dest, oldval, newval);
return ret;
}
+#endif
+
+
+#define ATOMIC(T) struct { T volatile value; }
+
+#define ATOMIC_INIT_STATIC(_newval) {(_newval)}
+
+#define ATOMIC_LOAD_UNSAFE(_val) ((_val).value)
+#define ATOMIC_STORE_UNSAFE(_val, _newval) do { \
+ (_val).value = (_newval); \
+} while(0)
+
+#define ATOMIC_LOAD(_val) (__asm__ __volatile__("" ::: "memory"),(_val).value)
+#define ATOMIC_STORE(_val, _newval) do { \
+ (_val).value = (_newval); \
+ __asm__ __volatile__("" ::: "memory"); \
+} while(0)
+
+#define ATOMIC_EXCHANGE(T, _val, _newval) __extension__({ \
+ T _r; \
+ static_assert(sizeof(T)==sizeof((_val).value), "Type "#T" has incorrect size!"); \
+ if(sizeof(T) == 4) EXCHANGE("l", _r, &(_val).value, (_newval)); \
+ else if(sizeof(T) == 8) EXCHANGE("q", _r, &(_val).value, (_newval)); \
+ _r; \
+})
+#define ATOMIC_COMPARE_EXCHANGE(T, _val, _oldval, _newval) __extension__({ \
+ T _r; \
+ static_assert(sizeof(T)==sizeof((_val).value), "Type "#T" has incorrect size!"); \
+ if(sizeof(T) == 4) COMP_EXCHANGE("l", _r, &(_val).value, (_oldval), (_newval)); \
+ else if(sizeof(T) == 8) COMP_EXCHANGE("q", _r, &(_val).value, (_oldval), (_newval)); \
+ _r; \
+})
#elif defined(_WIN32)
@@ -172,7 +236,7 @@ inline uint CompExchangeRef(volatile RefCount *ptr, uint oldval, uint newval)
return InterlockedCompareExchange(u.l, newval, oldval);
}
-inline int ExchangeInt(volatile int *ptr, int newval)
+inline int ExchangeInt32(volatile int *ptr, int newval)
{
union {
volatile int *i;
@@ -180,11 +244,7 @@ inline int ExchangeInt(volatile int *ptr, int newval)
} u = { ptr };
return InterlockedExchange(u.l, newval);
}
-inline void *ExchangePtr(XchgPtr *ptr, void *newval)
-{
- return InterlockedExchangePointer(ptr, newval);
-}
-inline int CompExchangeInt(volatile int *ptr, int oldval, int newval)
+inline int CompExchangeInt32(volatile int *ptr, int oldval, int newval)
{
union {
volatile int *i;
@@ -192,11 +252,60 @@ inline int CompExchangeInt(volatile int *ptr, int oldval, int newval)
} u = { ptr };
return InterlockedCompareExchange(u.l, newval, oldval);
}
+inline __int64 ExchangeInt64(volatile __int64 *ptr, __int64 newval)
+{
+ union {
+ volatile __int64 *i;
+ volatile LONGLONG *l;
+ } u = { ptr };
+ return InterlockedExchange64(u.l, newval);
+}
+inline __int64 CompExchangeInt64(volatile __int64 *ptr, __int64 oldval, __int64 newval)
+{
+ union {
+ volatile __int64 *i;
+ volatile LONGLONG *l;
+ } u = { ptr };
+ return InterlockedCompareExchange64(u.l, newval, oldval);
+}
+
+inline int ExchangeInt(volatile int *ptr, int newval)
+{ return ExchangeInt32(ptr, newval); }
+inline int CompExchangeInt(volatile int *ptr, int oldval, int newval)
+{ return CompExchangeInt32(ptr, oldval, newval); }
+
+inline void *ExchangePtr(XchgPtr *ptr, void *newval)
+{
+ return InterlockedExchangePointer(ptr, newval);
+}
inline void *CompExchangePtr(XchgPtr *ptr, void *oldval, void *newval)
{
return InterlockedCompareExchangePointer(ptr, newval, oldval);
}
+
+#define ATOMIC(T) struct { T volatile value; }
+
+#define ATOMIC_INIT_STATIC(_newval) {(_newval)}
+
+#define ATOMIC_LOAD_UNSAFE(_val) ((_val).value)
+#define ATOMIC_STORE_UNSAFE(_val, _newval) do { \
+ (_val).value = (_newval); \
+} while(0)
+
+#define ATOMIC_LOAD(_val) (_ReadBarrier(),(_val).value)
+#define ATOMIC_STORE(_val, _newval) do { \
+ (_val).value = (_newval); \
+ _WriteBarrier(); \
+} while(0)
+
+int _al_invalid_atomic_size(); /* not defined */
+
+#define ATOMIC_FUNC_SELECT(T, C, F32, F64) ((sizeof(T) == 4) ? (C)F32 : ((sizeof(T) == 8) ? (C)F64 : (C)_al_invalid_atomic_size))
+
+#define ATOMIC_EXCHANGE(T, _val, _newval) (ATOMIC_FUNC_SELECT(T, T(*)(volatile T*,T), ExchangeInt32, ExchangeInt64)(&(_val).value, (_newval)))
+#define ATOMIC_COMPARE_EXCHANGE(T, _val, _oldval, _newval) (ATOMIC_FUNC_SELECT(T, T(*)(volatile T*,T,T), CompExchangeInt32, CompExchangeInt64)(&(_val).value, (_oldval), (_newval)))
+
#else
#error "No atomic functions available on this platform!"
#endif