1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Changed the Atomic class to use compiler intrinsics directly when using Clang, rather than the OSX functions.

This commit is contained in:
jules 2013-11-18 14:57:38 +00:00
parent f48c158846
commit 55ff9a57be

View file

@ -187,8 +187,8 @@ private:
/* /*
The following code is in the header so that the atomics can be inlined where possible... The following code is in the header so that the atomics can be inlined where possible...
*/ */
#if JUCE_IOS || (JUCE_MAC && (JUCE_PPC || JUCE_CLANG || __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 2))) #if JUCE_MAC && (JUCE_PPC || __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 2))
#define JUCE_ATOMICS_MAC 1 // Older OSX builds using gcc4.1 or earlier #define JUCE_ATOMICS_MAC_LEGACY 1 // Older OSX builds using gcc4.1 or earlier
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
#define JUCE_MAC_ATOMICS_VOLATILE #define JUCE_MAC_ATOMICS_VOLATILE
@ -207,7 +207,7 @@ private:
#endif #endif
//============================================================================== //==============================================================================
#elif JUCE_GCC #elif JUCE_GCC || JUCE_CLANG
#define JUCE_ATOMICS_GCC 1 // GCC with intrinsics #define JUCE_ATOMICS_GCC 1 // GCC with intrinsics
#if JUCE_IOS || JUCE_ANDROID // (64-bit ops will compile but not link on these mobile OSes) #if JUCE_IOS || JUCE_ANDROID // (64-bit ops will compile but not link on these mobile OSes)
@ -268,7 +268,7 @@ private:
template <typename Type> template <typename Type>
inline Type Atomic<Type>::get() const noexcept inline Type Atomic<Type>::get() const noexcept
{ {
#if JUCE_ATOMICS_MAC #if JUCE_ATOMICS_MAC_LEGACY
return sizeof (Type) == 4 ? castFrom32Bit ((int32) OSAtomicAdd32Barrier ((int32_t) 0, (JUCE_MAC_ATOMICS_VOLATILE int32_t*) &value)) return sizeof (Type) == 4 ? castFrom32Bit ((int32) OSAtomicAdd32Barrier ((int32_t) 0, (JUCE_MAC_ATOMICS_VOLATILE int32_t*) &value))
: castFrom64Bit ((int64) OSAtomicAdd64Barrier ((int64_t) 0, (JUCE_MAC_ATOMICS_VOLATILE int64_t*) &value)); : castFrom64Bit ((int64) OSAtomicAdd64Barrier ((int64_t) 0, (JUCE_MAC_ATOMICS_VOLATILE int64_t*) &value));
#elif JUCE_ATOMICS_WINDOWS #elif JUCE_ATOMICS_WINDOWS
@ -283,7 +283,7 @@ inline Type Atomic<Type>::get() const noexcept
template <typename Type> template <typename Type>
inline Type Atomic<Type>::exchange (const Type newValue) noexcept inline Type Atomic<Type>::exchange (const Type newValue) noexcept
{ {
#if JUCE_ATOMICS_MAC || JUCE_ATOMICS_GCC #if JUCE_ATOMICS_MAC_LEGACY || JUCE_ATOMICS_GCC
Type currentVal = value; Type currentVal = value;
while (! compareAndSetBool (newValue, currentVal)) { currentVal = value; } while (! compareAndSetBool (newValue, currentVal)) { currentVal = value; }
return currentVal; return currentVal;
@ -296,7 +296,7 @@ inline Type Atomic<Type>::exchange (const Type newValue) noexcept
template <typename Type> template <typename Type>
inline Type Atomic<Type>::operator+= (const Type amountToAdd) noexcept inline Type Atomic<Type>::operator+= (const Type amountToAdd) noexcept
{ {
#if JUCE_ATOMICS_MAC #if JUCE_ATOMICS_MAC_LEGACY
return sizeof (Type) == 4 ? (Type) OSAtomicAdd32Barrier ((int32_t) castTo32Bit (amountToAdd), (JUCE_MAC_ATOMICS_VOLATILE int32_t*) &value) return sizeof (Type) == 4 ? (Type) OSAtomicAdd32Barrier ((int32_t) castTo32Bit (amountToAdd), (JUCE_MAC_ATOMICS_VOLATILE int32_t*) &value)
: (Type) OSAtomicAdd64Barrier ((int64_t) amountToAdd, (JUCE_MAC_ATOMICS_VOLATILE int64_t*) &value); : (Type) OSAtomicAdd64Barrier ((int64_t) amountToAdd, (JUCE_MAC_ATOMICS_VOLATILE int64_t*) &value);
#elif JUCE_ATOMICS_WINDOWS #elif JUCE_ATOMICS_WINDOWS
@ -316,35 +316,37 @@ inline Type Atomic<Type>::operator-= (const Type amountToSubtract) noexcept
template <typename Type> template <typename Type>
inline Type Atomic<Type>::operator++() noexcept inline Type Atomic<Type>::operator++() noexcept
{ {
#if JUCE_ATOMICS_MAC #if JUCE_ATOMICS_MAC_LEGACY
return sizeof (Type) == 4 ? (Type) OSAtomicIncrement32Barrier ((JUCE_MAC_ATOMICS_VOLATILE int32_t*) &value) return sizeof (Type) == 4 ? (Type) OSAtomicIncrement32Barrier ((JUCE_MAC_ATOMICS_VOLATILE int32_t*) &value)
: (Type) OSAtomicIncrement64Barrier ((JUCE_MAC_ATOMICS_VOLATILE int64_t*) &value); : (Type) OSAtomicIncrement64Barrier ((JUCE_MAC_ATOMICS_VOLATILE int64_t*) &value);
#elif JUCE_ATOMICS_WINDOWS #elif JUCE_ATOMICS_WINDOWS
return sizeof (Type) == 4 ? (Type) juce_InterlockedIncrement ((volatile long*) &value) return sizeof (Type) == 4 ? (Type) juce_InterlockedIncrement ((volatile long*) &value)
: (Type) juce_InterlockedIncrement64 ((volatile __int64*) &value); : (Type) juce_InterlockedIncrement64 ((volatile __int64*) &value);
#elif JUCE_ATOMICS_GCC #elif JUCE_ATOMICS_GCC
return (Type) __sync_add_and_fetch (&value, 1); return sizeof (Type) == 4 ? (Type) __sync_add_and_fetch (&value, (Type) 1)
: (Type) __sync_add_and_fetch ((int64_t*) &value, 1);
#endif #endif
} }
template <typename Type> template <typename Type>
inline Type Atomic<Type>::operator--() noexcept inline Type Atomic<Type>::operator--() noexcept
{ {
#if JUCE_ATOMICS_MAC #if JUCE_ATOMICS_MAC_LEGACY
return sizeof (Type) == 4 ? (Type) OSAtomicDecrement32Barrier ((JUCE_MAC_ATOMICS_VOLATILE int32_t*) &value) return sizeof (Type) == 4 ? (Type) OSAtomicDecrement32Barrier ((JUCE_MAC_ATOMICS_VOLATILE int32_t*) &value)
: (Type) OSAtomicDecrement64Barrier ((JUCE_MAC_ATOMICS_VOLATILE int64_t*) &value); : (Type) OSAtomicDecrement64Barrier ((JUCE_MAC_ATOMICS_VOLATILE int64_t*) &value);
#elif JUCE_ATOMICS_WINDOWS #elif JUCE_ATOMICS_WINDOWS
return sizeof (Type) == 4 ? (Type) juce_InterlockedDecrement ((volatile long*) &value) return sizeof (Type) == 4 ? (Type) juce_InterlockedDecrement ((volatile long*) &value)
: (Type) juce_InterlockedDecrement64 ((volatile __int64*) &value); : (Type) juce_InterlockedDecrement64 ((volatile __int64*) &value);
#elif JUCE_ATOMICS_GCC #elif JUCE_ATOMICS_GCC
return (Type) __sync_add_and_fetch (&value, -1); return sizeof (Type) == 4 ? (Type) __sync_add_and_fetch (&value, (Type) -1)
: (Type) __sync_add_and_fetch ((int64_t*) &value, -1);
#endif #endif
} }
template <typename Type> template <typename Type>
inline bool Atomic<Type>::compareAndSetBool (const Type newValue, const Type valueToCompare) noexcept inline bool Atomic<Type>::compareAndSetBool (const Type newValue, const Type valueToCompare) noexcept
{ {
#if JUCE_ATOMICS_MAC #if JUCE_ATOMICS_MAC_LEGACY
return sizeof (Type) == 4 ? OSAtomicCompareAndSwap32Barrier ((int32_t) castTo32Bit (valueToCompare), (int32_t) castTo32Bit (newValue), (JUCE_MAC_ATOMICS_VOLATILE int32_t*) &value) return sizeof (Type) == 4 ? OSAtomicCompareAndSwap32Barrier ((int32_t) castTo32Bit (valueToCompare), (int32_t) castTo32Bit (newValue), (JUCE_MAC_ATOMICS_VOLATILE int32_t*) &value)
: OSAtomicCompareAndSwap64Barrier ((int64_t) castTo64Bit (valueToCompare), (int64_t) castTo64Bit (newValue), (JUCE_MAC_ATOMICS_VOLATILE int64_t*) &value); : OSAtomicCompareAndSwap64Barrier ((int64_t) castTo64Bit (valueToCompare), (int64_t) castTo64Bit (newValue), (JUCE_MAC_ATOMICS_VOLATILE int64_t*) &value);
#elif JUCE_ATOMICS_WINDOWS #elif JUCE_ATOMICS_WINDOWS
@ -358,7 +360,7 @@ inline bool Atomic<Type>::compareAndSetBool (const Type newValue, const Type val
template <typename Type> template <typename Type>
inline Type Atomic<Type>::compareAndSetValue (const Type newValue, const Type valueToCompare) noexcept inline Type Atomic<Type>::compareAndSetValue (const Type newValue, const Type valueToCompare) noexcept
{ {
#if JUCE_ATOMICS_MAC #if JUCE_ATOMICS_MAC_LEGACY
for (;;) // Annoying workaround for only having a bool CAS operation.. for (;;) // Annoying workaround for only having a bool CAS operation..
{ {
if (compareAndSetBool (newValue, valueToCompare)) if (compareAndSetBool (newValue, valueToCompare))
@ -381,7 +383,7 @@ inline Type Atomic<Type>::compareAndSetValue (const Type newValue, const Type va
template <typename Type> template <typename Type>
inline void Atomic<Type>::memoryBarrier() noexcept inline void Atomic<Type>::memoryBarrier() noexcept
{ {
#if JUCE_ATOMICS_MAC #if JUCE_ATOMICS_MAC_LEGACY
OSMemoryBarrier(); OSMemoryBarrier();
#elif JUCE_ATOMICS_GCC #elif JUCE_ATOMICS_GCC
__sync_synchronize(); __sync_synchronize();