From 3adba90ef3252cfbbecef0a959eb17977ef23a8c Mon Sep 17 00:00:00 2001 From: cnr-isti-vclab Date: Thu, 15 Dec 2011 17:38:34 +0000 Subject: [PATCH] small bugfixes in atomic_int_generic.h --- .../multithreading/atomic_int_generic.h | 164 +++++++++--------- 1 file changed, 84 insertions(+), 80 deletions(-) diff --git a/wrap/system/multithreading/atomic_int_generic.h b/wrap/system/multithreading/atomic_int_generic.h index 0dfab4ec..eec6491b 100644 --- a/wrap/system/multithreading/atomic_int_generic.h +++ b/wrap/system/multithreading/atomic_int_generic.h @@ -9,100 +9,104 @@ namespace mt{ class atomicInt { public: - atomicInt() - { - value = 0; - } - atomicInt( int value ) - { - value = value; - } + atomicInt() + { + value = 0; + } - // atomic API + atomicInt( int value ) + { + value = value; + } - /** - 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; - } +// atomic API - /** - 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; - } +/** +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 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; - } +/** +Atomically increments the value of this atomicInt. +Returns true if the new value is non-zero, false otherwise.*/ + inline bool ref() + { + mutexlocker lock(&m); + return ++value != 0; + } - inline bool testAndSetOrdered(int expectedValue, int newValue) - { - mutexlocker lock(m); - if (value == expectedValue) { - value = newValue; - return true; - } - return false; - } +/* +Atomically decrements the value of this QAtomicInt. +Returns true if the new value is non-zero, false otherwise.*/ + inline bool deref() + { + mutexlocker lock(&m); + return --value != 0; + } - // Non-atomic API - inline bool operator==(int value) const - { - return value == value; +/* +If the current value of this QAtomicInt is the expectedValue, +the test-and-set functions assign the newValue to this QAtomicInt +and return true. If the values are not the same, this function +does nothing and returns false. +*/ + inline bool testAndSetOrdered(int expectedValue, int newValue) + { + mutexlocker lock(&m); + if (value == expectedValue) { + value = newValue; + return true; } + return false; + } - inline bool operator!=(int value) const - { - return value != value; - } +// Non-atomic API + inline bool operator==(int value) const + { + return value == value; + } - inline bool operator!() const - { - return value == 0; - } + inline bool operator!=(int value) const + { + return value != value; + } - inline operator int() const - { - return value; - } - - inline atomicInt &operator=(int value) - { - value = value; - return *this; - } + inline bool operator!() const + { + return value == 0; + } - inline bool operator>(int value) const - { - return value > value; - } + inline operator int() const + { + return value; + } - inline bool operator<(int value) const - { - return value < 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; - + volatile int value; + mutex m; }; }//namespace