From 55ff9a57bec5b5d8be364280bc99f37c063bf0a7 Mon Sep 17 00:00:00 2001 From: jules Date: Mon, 18 Nov 2013 14:57:38 +0000 Subject: [PATCH] Changed the Atomic class to use compiler intrinsics directly when using Clang, rather than the OSX functions. --- modules/juce_core/memory/juce_Atomic.h | 28 ++++++++++++++------------ 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/modules/juce_core/memory/juce_Atomic.h b/modules/juce_core/memory/juce_Atomic.h index 14a62018d7..d0dd6be5bd 100644 --- a/modules/juce_core/memory/juce_Atomic.h +++ b/modules/juce_core/memory/juce_Atomic.h @@ -187,8 +187,8 @@ private: /* 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))) - #define JUCE_ATOMICS_MAC 1 // Older OSX builds using gcc4.1 or earlier +#if JUCE_MAC && (JUCE_PPC || __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 2)) + #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 #define JUCE_MAC_ATOMICS_VOLATILE @@ -207,7 +207,7 @@ private: #endif //============================================================================== -#elif JUCE_GCC +#elif JUCE_GCC || JUCE_CLANG #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) @@ -268,7 +268,7 @@ private: template inline Type Atomic::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)) : castFrom64Bit ((int64) OSAtomicAdd64Barrier ((int64_t) 0, (JUCE_MAC_ATOMICS_VOLATILE int64_t*) &value)); #elif JUCE_ATOMICS_WINDOWS @@ -283,7 +283,7 @@ inline Type Atomic::get() const noexcept template inline Type Atomic::exchange (const Type newValue) noexcept { - #if JUCE_ATOMICS_MAC || JUCE_ATOMICS_GCC + #if JUCE_ATOMICS_MAC_LEGACY || JUCE_ATOMICS_GCC Type currentVal = value; while (! compareAndSetBool (newValue, currentVal)) { currentVal = value; } return currentVal; @@ -296,7 +296,7 @@ inline Type Atomic::exchange (const Type newValue) noexcept template inline Type Atomic::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) : (Type) OSAtomicAdd64Barrier ((int64_t) amountToAdd, (JUCE_MAC_ATOMICS_VOLATILE int64_t*) &value); #elif JUCE_ATOMICS_WINDOWS @@ -316,35 +316,37 @@ inline Type Atomic::operator-= (const Type amountToSubtract) noexcept template inline Type Atomic::operator++() noexcept { - #if JUCE_ATOMICS_MAC + #if JUCE_ATOMICS_MAC_LEGACY return sizeof (Type) == 4 ? (Type) OSAtomicIncrement32Barrier ((JUCE_MAC_ATOMICS_VOLATILE int32_t*) &value) : (Type) OSAtomicIncrement64Barrier ((JUCE_MAC_ATOMICS_VOLATILE int64_t*) &value); #elif JUCE_ATOMICS_WINDOWS return sizeof (Type) == 4 ? (Type) juce_InterlockedIncrement ((volatile long*) &value) : (Type) juce_InterlockedIncrement64 ((volatile __int64*) &value); #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 } template inline Type Atomic::operator--() noexcept { - #if JUCE_ATOMICS_MAC + #if JUCE_ATOMICS_MAC_LEGACY return sizeof (Type) == 4 ? (Type) OSAtomicDecrement32Barrier ((JUCE_MAC_ATOMICS_VOLATILE int32_t*) &value) : (Type) OSAtomicDecrement64Barrier ((JUCE_MAC_ATOMICS_VOLATILE int64_t*) &value); #elif JUCE_ATOMICS_WINDOWS return sizeof (Type) == 4 ? (Type) juce_InterlockedDecrement ((volatile long*) &value) : (Type) juce_InterlockedDecrement64 ((volatile __int64*) &value); #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 } template inline bool Atomic::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) : OSAtomicCompareAndSwap64Barrier ((int64_t) castTo64Bit (valueToCompare), (int64_t) castTo64Bit (newValue), (JUCE_MAC_ATOMICS_VOLATILE int64_t*) &value); #elif JUCE_ATOMICS_WINDOWS @@ -358,7 +360,7 @@ inline bool Atomic::compareAndSetBool (const Type newValue, const Type val template inline Type Atomic::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.. { if (compareAndSetBool (newValue, valueToCompare)) @@ -381,7 +383,7 @@ inline Type Atomic::compareAndSetValue (const Type newValue, const Type va template inline void Atomic::memoryBarrier() noexcept { - #if JUCE_ATOMICS_MAC + #if JUCE_ATOMICS_MAC_LEGACY OSMemoryBarrier(); #elif JUCE_ATOMICS_GCC __sync_synchronize();