From f525a05be8be446f93b2ed386ab34c2e1efe5d61 Mon Sep 17 00:00:00 2001 From: Tim Moore Date: Fri, 17 Jul 2009 00:29:48 +0200 Subject: [PATCH] Use SGAtomic's compareAndExchange instead of a new SGSwappable class Also, eliminate the __declspec(32) of that class which is causing problems in osg::buffered_object. --- simgear/scene/material/Technique.hxx | 2 +- simgear/structure/SGAtomic.hxx | 79 +++++----------------------- 2 files changed, 14 insertions(+), 67 deletions(-) diff --git a/simgear/scene/material/Technique.hxx b/simgear/scene/material/Technique.hxx index 596d4dd9..26aed909 100644 --- a/simgear/scene/material/Technique.hxx +++ b/simgear/scene/material/Technique.hxx @@ -106,7 +106,7 @@ protected: ContextInfo(const ContextInfo& rhs) : valid(rhs.valid()) {} ContextInfo& operator=(const ContextInfo& rhs) { - valid = rhs.valid(); + valid = rhs.valid; } Swappable valid; }; diff --git a/simgear/structure/SGAtomic.hxx b/simgear/structure/SGAtomic.hxx index a6202a86..a7977fc3 100644 --- a/simgear/structure/SGAtomic.hxx +++ b/simgear/structure/SGAtomic.hxx @@ -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)); } }; } -- 2.39.5