AuroraOpenALSoft/Alc/atomic.h

181 lines
4.9 KiB
C
Raw Normal View History

2013-10-28 19:48:13 +00:00
#ifndef AL_ATOMIC_H
#define AL_ATOMIC_H
typedef void *volatile XchgPtr;
#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) && !defined(__QNXNTO__)
typedef unsigned int RefCount;
2013-11-04 20:12:31 +00:00
inline RefCount IncrementRef(volatile RefCount *ptr)
2013-10-28 19:48:13 +00:00
{ return __sync_add_and_fetch(ptr, 1); }
2013-11-04 20:12:31 +00:00
inline RefCount DecrementRef(volatile RefCount *ptr)
2013-10-28 19:48:13 +00:00
{ return __sync_sub_and_fetch(ptr, 1); }
2013-11-04 20:12:31 +00:00
inline int ExchangeInt(volatile int *ptr, int newval)
2013-10-28 19:48:13 +00:00
{
return __sync_lock_test_and_set(ptr, newval);
}
2013-11-04 20:12:31 +00:00
inline void *ExchangePtr(XchgPtr *ptr, void *newval)
2013-10-28 19:48:13 +00:00
{
return __sync_lock_test_and_set(ptr, newval);
}
2013-11-04 20:12:31 +00:00
inline ALboolean CompExchangeInt(volatile int *ptr, int oldval, int newval)
2013-10-28 19:48:13 +00:00
{
return __sync_bool_compare_and_swap(ptr, oldval, newval);
}
2013-11-04 20:12:31 +00:00
inline ALboolean CompExchangePtr(XchgPtr *ptr, void *oldval, void *newval)
2013-10-28 19:48:13 +00:00
{
return __sync_bool_compare_and_swap(ptr, oldval, newval);
}
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
2013-11-04 20:12:31 +00:00
inline unsigned int xaddl(volatile unsigned int *dest, int incr)
2013-10-28 19:48:13 +00:00
{
unsigned int ret;
__asm__ __volatile__("lock; xaddl %0,(%1)"
: "=r" (ret)
: "r" (dest), "0" (incr)
: "memory");
return ret;
}
typedef unsigned int RefCount;
2013-11-04 20:12:31 +00:00
inline RefCount IncrementRef(volatile RefCount *ptr)
2013-10-28 19:48:13 +00:00
{ return xaddl(ptr, 1)+1; }
2013-11-04 20:12:31 +00:00
inline RefCount DecrementRef(volatile RefCount *ptr)
2013-10-28 19:48:13 +00:00
{ return xaddl(ptr, -1)-1; }
2013-11-04 20:12:31 +00:00
inline int ExchangeInt(volatile int *dest, int newval)
2013-10-28 19:48:13 +00:00
{
int ret;
__asm__ __volatile__("lock; xchgl %0,(%1)"
: "=r" (ret)
: "r" (dest), "0" (newval)
: "memory");
return ret;
}
2013-11-04 20:12:31 +00:00
inline ALboolean CompExchangeInt(volatile int *dest, int oldval, int newval)
2013-10-28 19:48:13 +00:00
{
int ret;
__asm__ __volatile__("lock; cmpxchgl %2,(%1)"
: "=a" (ret)
: "r" (dest), "r" (newval), "0" (oldval)
: "memory");
return ret == oldval;
}
2013-11-04 20:12:31 +00:00
inline void *ExchangePtr(XchgPtr *dest, void *newval)
2013-10-28 19:48:13 +00:00
{
void *ret;
__asm__ __volatile__(
#ifdef __i386__
"lock; xchgl %0,(%1)"
#else
"lock; xchgq %0,(%1)"
#endif
: "=r" (ret)
: "r" (dest), "0" (newval)
: "memory"
);
return ret;
}
2013-11-04 20:12:31 +00:00
inline ALboolean CompExchangePtr(XchgPtr *dest, void *oldval, void *newval)
2013-10-28 19:48:13 +00:00
{
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"
);
return ret == oldval;
}
#elif defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
typedef LONG RefCount;
2013-11-04 20:12:31 +00:00
inline RefCount IncrementRef(volatile RefCount *ptr)
2013-10-28 19:48:13 +00:00
{ return InterlockedIncrement(ptr); }
2013-11-04 20:12:31 +00:00
inline RefCount DecrementRef(volatile RefCount *ptr)
2013-10-28 19:48:13 +00:00
{ return InterlockedDecrement(ptr); }
extern ALbyte LONG_size_does_not_match_int[(sizeof(LONG)==sizeof(int))?1:-1];
2013-11-04 20:12:31 +00:00
inline int ExchangeInt(volatile int *ptr, int newval)
2013-10-28 19:48:13 +00:00
{
union {
volatile int *i;
volatile LONG *l;
} u = { ptr };
return InterlockedExchange(u.l, newval);
}
2013-11-04 20:12:31 +00:00
inline void *ExchangePtr(XchgPtr *ptr, void *newval)
2013-10-28 19:48:13 +00:00
{
return InterlockedExchangePointer(ptr, newval);
}
2013-11-04 20:12:31 +00:00
inline ALboolean CompExchangeInt(volatile int *ptr, int oldval, int newval)
2013-10-28 19:48:13 +00:00
{
union {
volatile int *i;
volatile LONG *l;
} u = { ptr };
return InterlockedCompareExchange(u.l, newval, oldval) == oldval;
}
2013-11-04 20:12:31 +00:00
inline ALboolean CompExchangePtr(XchgPtr *ptr, void *oldval, void *newval)
2013-10-28 19:48:13 +00:00
{
return InterlockedCompareExchangePointer(ptr, newval, oldval) == oldval;
}
#elif defined(__APPLE__)
#include <libkern/OSAtomic.h>
typedef int32_t RefCount;
2013-11-04 20:12:31 +00:00
inline RefCount IncrementRef(volatile RefCount *ptr)
2013-10-28 19:48:13 +00:00
{ return OSAtomicIncrement32Barrier(ptr); }
2013-11-04 20:12:31 +00:00
inline RefCount DecrementRef(volatile RefCount *ptr)
2013-10-28 19:48:13 +00:00
{ return OSAtomicDecrement32Barrier(ptr); }
2013-11-04 20:12:31 +00:00
inline int ExchangeInt(volatile int *ptr, int newval)
2013-10-28 19:48:13 +00:00
{
/* Really? No regular old atomic swap? */
int oldval;
do {
oldval = *ptr;
} while(!OSAtomicCompareAndSwap32Barrier(oldval, newval, ptr));
return oldval;
}
2013-11-04 20:12:31 +00:00
inline void *ExchangePtr(XchgPtr *ptr, void *newval)
2013-10-28 19:48:13 +00:00
{
void *oldval;
do {
oldval = *ptr;
} while(!OSAtomicCompareAndSwapPtrBarrier(oldval, newval, ptr));
return oldval;
}
2013-11-04 20:12:31 +00:00
inline ALboolean CompExchangeInt(volatile int *ptr, int oldval, int newval)
2013-10-28 19:48:13 +00:00
{
return OSAtomicCompareAndSwap32Barrier(oldval, newval, ptr);
}
2013-11-04 20:12:31 +00:00
inline ALboolean CompExchangePtr(XchgPtr *ptr, void *oldval, void *newval)
2013-10-28 19:48:13 +00:00
{
return OSAtomicCompareAndSwapPtrBarrier(oldval, newval, ptr);
}
#else
#error "No atomic functions available on this platform!"
typedef ALuint RefCount;
#endif
#endif /* AL_ATOMIC_H */