X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fstructure%2FSGAtomic.hxx;h=d4611d11c078c0e8c5ef3cbb6332d68047d7ffad;hb=b9a34b1b05ce9cab1b4b67816d7d24bd2bc364b7;hp=1ff9a44d05213f08c4cfd3ab7337c99e86d6ee7a;hpb=0f7b65a9216458b290113bae0ff75bd3ec4dce36;p=simgear.git diff --git a/simgear/structure/SGAtomic.hxx b/simgear/structure/SGAtomic.hxx index 1ff9a44d..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,4 +113,33 @@ private: unsigned mValue; }; +namespace simgear +{ +// Typesafe wrapper around SGSwappable +template +class Swappable : private SGAtomic +{ +public: + Swappable(const T& value) : SGAtomic(static_cast(value)) + { + } + T operator() () const + { + 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 SGAtomic::compareAndExchange(static_cast(oldVal), + static_cast(newVal)); + } +}; +} #endif