/* ----------------------------------------------------------------------------- GSFramework Copyright 2001-2013 Emmanuel Julien. All Rights Reserved. ----------------------------------------------------------------------------- */ #include "thread/atomic_value.h" #include "assert/nassert.h" #if __PLATFORM_WINDOWS__ #include namespace GS { namespace Threading { //------------------------------------------------------------------------------ int Atomic32::Inc() { return (int)InterlockedIncrement((LONG volatile *)v); } int Atomic32::Dec() { return (int)InterlockedDecrement((LONG volatile *)v); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ int Atomic32::Get() const { return *((int volatile *)v); } int Atomic32::Set(int _v) { return InterlockedExchange((LONG volatile *)v, _v); } int Atomic32::Cas(int cmp, int _v) { return InterlockedCompareExchange((LONG volatile *)v, _v, cmp); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ Atomic32::Atomic32(int _v) { v = _aligned_malloc(sizeof(LONG), 4); __ASSERT__(v != NULL); Set(_v); } Atomic32::~Atomic32() { _aligned_free(v); } //------------------------------------------------------------------------------ } } #elif (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 1)) // use >GCC4.1 builtins #include namespace GS { namespace Threading { //------------------------------------------------------------------------------ int Atomic32::Inc() { return __sync_fetch_and_add((long volatile *)v, 1) + 1; } int Atomic32::Dec() { return __sync_fetch_and_add((long volatile *)v, -1) - 1; } int Atomic32::Get() const { return *((int volatile *)v); } int Atomic32::Set(int _v) { return __sync_lock_test_and_set((long volatile *)v, _v); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ int Atomic32::Cas(int cmp, int _v) { return __sync_val_compare_and_swap((long volatile *)v, cmp, _v); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ Atomic32::Atomic32(int _v) { v = memalign(4, sizeof(long)); __ASSERT__(v != NULL); *(long *)v = _v; } Atomic32::~Atomic32() { free(v); } //------------------------------------------------------------------------------ } } #else namespace GS { namespace Threading { //------------------------------------------------------------------------------ int Atomic32::Get() const { __ASSERT(true); } int Atomic32::Set(int _v) { __ASSERT(true); } int Atomic32::Cas(int cmp, int _v) { __ASSERT(true); } //------------------------------------------------------------------------------ } } #endif