X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fstructure%2FSGAtomic.hxx;h=d4611d11c078c0e8c5ef3cbb6332d68047d7ffad;hb=b9a34b1b05ce9cab1b4b67816d7d24bd2bc364b7;hp=a6202a869132c4372be5bf50dd0d658eb90e7362;hpb=d320a6facba71227454375c3dea80df330b31ca9;p=simgear.git diff --git a/simgear/structure/SGAtomic.hxx b/simgear/structure/SGAtomic.hxx index a6202a86..d4611d11 100644 --- a/simgear/structure/SGAtomic.hxx +++ b/simgear/structure/SGAtomic.hxx @@ -28,7 +28,7 @@ #elif defined(__sgi) && defined(_COMPILER_VERSION) && (_COMPILER_VERSION>=730) // No need to include something. Is a Compiler API ... # define SGATOMIC_USE_MIPSPRO_BUILTINS -#elif defined(WIN32) && !defined ( __CYGWIN__ ) +#elif defined(_WIN32) # include # define SGATOMIC_USE_WIN32_INTERLOCKED #else @@ -113,85 +113,32 @@ private: unsigned mValue; }; -// Value that can be atomically compared and swapped. -class SGSwappable -{ -public: - typedef unsigned long value_type; - SGSwappable(unsigned long value = 0) : mValue(value) {} - operator unsigned long() const - { -#if defined(SGATOMIC_USE_GCC4_BUILTINS) - __sync_synchronize(); - return mValue; -#elif defined(SGATOMIC_USE_MIPOSPRO_BUILTINS) - __synchronize(); - return mValue; -#elif defined(SGATOMIC_USE_WIN32_INTERLOCKED) - return static_cast(mValue); -#else - SGGuard lock(mMutex); - return mValue; -#endif - } - - bool compareAndSwap(unsigned long oldVal, unsigned long newVal) - { -#if defined(SGATOMIC_USE_GCC4_BUILTINS) - return __sync_bool_compare_and_swap(&mValue, oldVal, newVal); -#elif defined(SGATOMIC_USE_MIPOSPRO_BUILTINS) - return __compare_and_swap(&mValue, oldVal, newVal); -#elif defined(SGATOMIC_USE_WIN32_INTERLOCKED) - long previous - = InterlockedCompareExchange(reinterpret_cast(&mValue), - (long)newVal, - (long)oldVal); - return previous == (long)oldVal; -#else - SGGuard lock(mMutex); - if (oldVal == mValue) { - mValue = newVal; - return true; - } else { - return false; - } -#endif - } - -private: - SGSwappable(const SGAtomic&); - SGSwappable& operator=(const SGAtomic&); - -#if !defined(SGATOMIC_USE_GCC4_BUILTINS) \ - && !defined(SGATOMIC_USE_MIPOSPRO_BUILTINS) \ - && !defined(SGATOMIC_USE_WIN32_INTERLOCKED) - mutable SGMutex mMutex; -#endif -#ifdef SGATOMIC_USE_WIN32_INTERLOCKED - __declspec(align(32)) -#endif - value_type mValue; - -}; - namespace simgear { // Typesafe wrapper around SGSwappable template -class Swappable : private SGSwappable +class Swappable : private SGAtomic { public: - Swappable(const T& value) : SGSwappable(static_cast(value)) + Swappable(const T& value) : SGAtomic(static_cast(value)) { } T operator() () const { - return static_cast(SGSwappable::operator unsigned long ()); + return static_cast(SGAtomic::operator unsigned ()); + } + Swappable& operator=(const Swappable& rhs) + { + for (unsigned oldval = unsigned(*this); + !compareAndExchange(oldval, unsigned(rhs)); + oldval = unsigned(*this)) + ; + return *this; } bool compareAndSwap(const T& oldVal, const T& newVal) { - return SGSwappable::compareAndSwap(static_cast(oldVal), - static_cast(newVal)); + return SGAtomic::compareAndExchange(static_cast(oldVal), + static_cast(newVal)); } }; }