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

@ -9,100 +9,104 @@ namespace mt{
class atomicInt class atomicInt
{ {
public: public:
atomicInt() atomicInt()
{ {
value = 0; value = 0;
} }
atomicInt( int value )
{
value = value;
}
// atomic API 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. Reads the current value of this QAtomicInt and then adds valueToAdd
Returns true if the new value is non-zero, false otherwise.*/ to the current value, returning the original value.
inline bool ref() */
{ inline int fetchAndAddAcquire( int valueToAdd )
mutexlocker lock(m); {
value++; mutexlocker lock(&m);
return value == 0; int originalValue = value;
} value += valueToAdd;
return originalValue;
}
/* /**
Atomically decrements the value of this QAtomicInt. Atomically increments the value of this atomicInt.
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 ref()
{ {
mutexlocker lock(m); mutexlocker lock(&m);
value--; return ++value != 0;
return value == 0; }
}
inline bool testAndSetOrdered(int expectedValue, int newValue) /*
{ Atomically decrements the value of this QAtomicInt.
mutexlocker lock(m); Returns true if the new value is non-zero, false otherwise.*/
if (value == expectedValue) { inline bool deref()
value = newValue; {
return true; mutexlocker lock(&m);
} return --value != 0;
return false; }
}
// Non-atomic API /*
inline bool operator==(int value) const If the current value of this QAtomicInt is the expectedValue,
{ the test-and-set functions assign the newValue to this QAtomicInt
return value == value; 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 // Non-atomic API
{ inline bool operator==(int value) const
return value != value; {
} return value == value;
}
inline bool operator!() const inline bool operator!=(int value) const
{ {
return value == 0; return value != value;
} }
inline operator int() const inline bool operator!() const
{ {
return value; return value == 0;
} }
inline atomicInt &operator=(int value)
{
value = value;
return *this;
}
inline bool operator>(int value) const inline operator int() const
{ {
return value > value; return value;
} }
inline bool operator<(int value) const inline atomicInt &operator=(int value)
{ {
return value < value; value = value;
} return *this;
}
inline bool operator>(int value) const
{
return value > value;
}
inline bool operator<(int value) const
{
return value < value;
}
private: private:
volatile int value; volatile int value;
mutex m; mutex m;
}; };
}//namespace }//namespace