diff --git a/wrap/system/multithreading/atomic_int.h b/wrap/system/multithreading/atomic_int.h index 38df8e89..a4aad13a 100755 --- a/wrap/system/multithreading/atomic_int.h +++ b/wrap/system/multithreading/atomic_int.h @@ -1,16 +1,35 @@ -#include "../platform.h" - -#ifndef GC_ATOMIC_INT_H -#define GC_ATOMIC_INT_H +#ifndef _ATOMIC_INT_H +#define _ATOMIC_INT_H #ifdef QT_CORE_LIB + #include + namespace mt{ + typedef QAtomicInt atomicInt; + } -#include -typedef QAtomicInt GAtomincInt; +#elif defined(__APPLE__) +# include "atomic_int_apple.h" -#elif defined(__GC_APPLE__) ?? -# include "GCAtomicInt_apple.h" + +//generic implementation using mutexes +#else +# include "atomic_int_generic.h" #endif +/* +#elif defined(_WIN32) +# include "atomic_int_win32.h" #endif +#elif defined(__linux__) +# include "atomic_int_linux.h" +#endif +*/ +/* +__linux__ +__unix__ +__posix__ +*/ + +#endif // _ATOMIC_INT_H + diff --git a/wrap/system/multithreading/atomic_int_apple.h b/wrap/system/multithreading/atomic_int_apple.h index 09f3c454..777dcae4 100755 --- a/wrap/system/multithreading/atomic_int_apple.h +++ b/wrap/system/multithreading/atomic_int_apple.h @@ -1,22 +1,24 @@ -#ifndef GC_ATOMIC_INT_APPLE_H +#ifndef _ATOMIC_INT_APPLE_H -#define GC_ATOMIC_INT_APPLE_H +#define _ATOMIC_INT_APPLE_H #include //http://developer.apple.com/library/mac/#documentation/Darwin/Reference/KernelIOKitFramework/OSAtomic_h/index.html -class GCAtomicInt +namespace mt{ + +class atomicInt { public: - GCAtomicInt() + atomicInt() { - _q_value = 0; + value = 0; } - GCAtomicInt( int value ) + atomicInt( int value ) { - _q_value = value; + value = value; } // atomic API @@ -39,17 +41,17 @@ public: int originalValue; do { - originalValue = _q_value; - } while (!OSAtomicCompareAndSwap32Barrier(originalValue, originalValue+valueToAdd, &_q_value)); + originalValue = value; + } while (!OSAtomicCompareAndSwap32Barrier(originalValue, originalValue+valueToAdd, &value)); return originalValue; } /** - Atomically increments the value of this GCAtomicInt. + Atomically increments the value of this atomicInt. Returns true if the new value is non-zero, false otherwise.*/ inline bool ref() { - return OSAtomicIncrement32Barrier(&_q_value) != 0; + return OSAtomicIncrement32Barrier(&value) != 0; } /* @@ -57,7 +59,7 @@ public: Returns true if the new value is non-zero, false otherwise.*/ inline bool deref() { - return OSAtomicDecrement32Barrier(&_q_value) != 0; + return OSAtomicDecrement32Barrier(&value) != 0; } inline bool testAndSetOrdered(int expectedValue, int newValue) @@ -68,51 +70,52 @@ public: // } //return false; - return OSAtomicCompareAndSwap32Barrier(expectedValue, newValue, &_q_value); + return OSAtomicCompareAndSwap32Barrier(expectedValue, newValue, &value); } // Non-atomic API inline bool operator==(int value) const { - return _q_value == value; + return value == value; } inline bool operator!=(int value) const { - return _q_value != value; + return value != value; } inline bool operator!() const { - return _q_value == 0; + return value == 0; } inline operator int() const { - return _q_value; + return value; } - inline GCAtomicInt &operator=(int value) + inline atomicInt &operator=(int value) { - _q_value = value; + value = value; return *this; } inline bool operator>(int value) const { - return _q_value > value; + return value > value; } inline bool operator<(int value) const { - return _q_value < value; + return value < value; } private: - volatile int _q_value; + volatile int value; }; +}//namespace #endif diff --git a/wrap/system/multithreading/atomic_int_generic.h b/wrap/system/multithreading/atomic_int_generic.h new file mode 100644 index 00000000..0dfab4ec --- /dev/null +++ b/wrap/system/multithreading/atomic_int_generic.h @@ -0,0 +1,111 @@ +#ifndef _ATOMIC_INT_GENERIC_H + +#define _ATOMIC_INT_GENERIC_H + +#include "mt.h" + +namespace mt{ + +class atomicInt +{ +public: + atomicInt() + { + value = 0; + } + atomicInt( int value ) + { + value = value; + } + + // atomic API + + /** + Reads the current value of this QAtomicInt and then adds valueToAdd + to the current value, returning the original value. + */ + inline int fetchAndAddAcquire( int valueToAdd ) + { + mutexlocker lock(m); + int originalValue = value; + value += valueToAdd; + return originalValue; + } + + /** + Atomically increments the value of this atomicInt. + Returns true if the new value is non-zero, false otherwise.*/ + inline bool ref() + { + mutexlocker lock(m); + value++; + return value == 0; + } + + /* + Atomically decrements the value of this QAtomicInt. + Returns true if the new value is non-zero, false otherwise.*/ + inline bool deref() + { + mutexlocker lock(m); + value--; + return value == 0; + } + + inline bool testAndSetOrdered(int expectedValue, int newValue) + { + mutexlocker lock(m); + if (value == expectedValue) { + value = newValue; + return true; + } + return false; + } + + // Non-atomic API + inline bool operator==(int value) const + { + return value == value; + } + + inline bool operator!=(int value) const + { + return value != value; + } + + inline bool operator!() const + { + return value == 0; + } + + inline operator int() const + { + return value; + } + + inline atomicInt &operator=(int value) + { + value = value; + return *this; + } + + inline bool operator>(int value) const + { + return value > value; + } + + inline bool operator<(int value) const + { + return value < value; + } + +private: + volatile int value; + mutex m; + +}; + +}//namespace + +#endif +