small bugfixes in atomic_int_generic.h

This commit is contained in:
Paolo Cignoni 2011-12-15 17:38:34 +00:00
parent 9af315bb18
commit 3adba90ef3
1 changed files with 84 additions and 80 deletions

View File

@ -13,6 +13,7 @@ public:
{ {
value = 0; value = 0;
} }
atomicInt( int value ) atomicInt( int value )
{ {
value = value; value = value;
@ -26,7 +27,7 @@ public:
*/ */
inline int fetchAndAddAcquire( int valueToAdd ) inline int fetchAndAddAcquire( int valueToAdd )
{ {
mutexlocker lock(m); mutexlocker lock(&m);
int originalValue = value; int originalValue = value;
value += valueToAdd; value += valueToAdd;
return originalValue; return originalValue;
@ -37,9 +38,8 @@ public:
Returns true if the new value is non-zero, false otherwise.*/ Returns true if the new value is non-zero, false otherwise.*/
inline bool ref() inline bool ref()
{ {
mutexlocker lock(m); mutexlocker lock(&m);
value++; return ++value != 0;
return value == 0;
} }
/* /*
@ -47,14 +47,19 @@ public:
Returns true if the new value is non-zero, false otherwise.*/ Returns true if the new value is non-zero, false otherwise.*/
inline bool deref() inline bool deref()
{ {
mutexlocker lock(m); mutexlocker lock(&m);
value--; return --value != 0;
return value == 0;
} }
/*
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) inline bool testAndSetOrdered(int expectedValue, int newValue)
{ {
mutexlocker lock(m); mutexlocker lock(&m);
if (value == expectedValue) { if (value == expectedValue) {
value = newValue; value = newValue;
return true; return true;
@ -102,7 +107,6 @@ public:
private: private:
volatile int value; volatile int value;
mutex m; mutex m;
}; };
}//namespace }//namespace