Several bugfixes
This commit is contained in:
parent
3adba90ef3
commit
6b2e8c50d2
|
|
@ -14,11 +14,12 @@ class atomicInt
|
||||||
public:
|
public:
|
||||||
atomicInt()
|
atomicInt()
|
||||||
{
|
{
|
||||||
value = 0;
|
_q_value = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
atomicInt( int value )
|
atomicInt( int value )
|
||||||
{
|
{
|
||||||
value = value;
|
_q_value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// atomic API
|
// atomic API
|
||||||
|
|
@ -41,8 +42,8 @@ public:
|
||||||
|
|
||||||
int originalValue;
|
int originalValue;
|
||||||
do {
|
do {
|
||||||
originalValue = value;
|
originalValue = _q_value;
|
||||||
} while (!OSAtomicCompareAndSwap32Barrier(originalValue, originalValue+valueToAdd, &value));
|
} while (!OSAtomicCompareAndSwap32Barrier(originalValue, originalValue+valueToAdd, &_q_value));
|
||||||
return originalValue;
|
return originalValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -51,7 +52,7 @@ 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()
|
||||||
{
|
{
|
||||||
return OSAtomicIncrement32Barrier(&value) != 0;
|
return OSAtomicIncrement32Barrier(&_q_value) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -59,9 +60,15 @@ 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()
|
||||||
{
|
{
|
||||||
return OSAtomicDecrement32Barrier(&value) != 0;
|
return OSAtomicDecrement32Barrier(&_q_value) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
If the current _q_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)
|
||||||
{
|
{
|
||||||
//if (currentValue == expectedValue) {
|
//if (currentValue == expectedValue) {
|
||||||
|
|
@ -69,49 +76,49 @@ public:
|
||||||
// return true;
|
// return true;
|
||||||
// }
|
// }
|
||||||
//return false;
|
//return false;
|
||||||
|
return OSAtomicCompareAndSwap32Barrier(expectedValue, newValue, &_q_value));
|
||||||
return OSAtomicCompareAndSwap32Barrier(expectedValue, newValue, &value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Non-atomic API
|
// Non-atomic API
|
||||||
|
|
||||||
inline bool operator==(int value) const
|
inline bool operator==(int value) const
|
||||||
{
|
{
|
||||||
return value == value;
|
return _q_value == value;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool operator!=(int value) const
|
inline bool operator!=(int value) const
|
||||||
{
|
{
|
||||||
return value != value;
|
return _q_value != value;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool operator!() const
|
inline bool operator!() const
|
||||||
{
|
{
|
||||||
return value == 0;
|
return _q_value == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline operator int() const
|
inline operator int() const
|
||||||
{
|
{
|
||||||
return value;
|
return _q_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline atomicInt &operator=(int value)
|
inline atomicInt &operator=(int value)
|
||||||
{
|
{
|
||||||
value = value;
|
_q_value = value;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool operator>(int value) const
|
inline bool operator>(int value) const
|
||||||
{
|
{
|
||||||
return value > value;
|
return _q_value > value;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool operator<(int value) const
|
inline bool operator<(int value) const
|
||||||
{
|
{
|
||||||
return value < value;
|
return _q_value < value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
volatile int value;
|
volatile int _q_value;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@
|
||||||
|
|
||||||
#include "mt.h"
|
#include "mt.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
namespace mt{
|
namespace mt{
|
||||||
|
|
||||||
class atomicInt
|
class atomicInt
|
||||||
|
|
@ -11,48 +13,50 @@ class atomicInt
|
||||||
public:
|
public:
|
||||||
atomicInt()
|
atomicInt()
|
||||||
{
|
{
|
||||||
value = 0;
|
_q_value = 0;
|
||||||
|
std::cout << "atomicInt init a 0\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
atomicInt( int value )
|
atomicInt( int value )
|
||||||
{
|
{
|
||||||
value = value;
|
_q_value = value;
|
||||||
|
std::cout << "atomicInt init a " << _q_value << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// atomic API
|
// atomic API
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Reads the current value of this QAtomicInt and then adds valueToAdd
|
Reads the current _q_value of this QAtomicInt and then adds valueToAdd
|
||||||
to the current value, returning the original value.
|
to the current _q_value, returning the original _q_value.
|
||||||
*/
|
*/
|
||||||
inline int fetchAndAddAcquire( int valueToAdd )
|
inline int fetchAndAddAcquire( int valueToAdd )
|
||||||
{
|
{
|
||||||
mutexlocker lock(&m);
|
mutexlocker lock(&m);
|
||||||
int originalValue = value;
|
int originalValue = _q_value;
|
||||||
value += valueToAdd;
|
_q_value += valueToAdd;
|
||||||
return originalValue;
|
return originalValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Atomically increments the value of this atomicInt.
|
Atomically increments the _q_value of this atomicInt.
|
||||||
Returns true if the new value is non-zero, false otherwise.*/
|
Returns true if the new _q_value is non-zero, false otherwise.*/
|
||||||
inline bool ref()
|
inline bool ref()
|
||||||
{
|
{
|
||||||
mutexlocker lock(&m);
|
mutexlocker lock(&m);
|
||||||
return ++value != 0;
|
return ++_q_value != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Atomically decrements the value of this QAtomicInt.
|
Atomically decrements the _q_value of this QAtomicInt.
|
||||||
Returns true if the new value is non-zero, false otherwise.*/
|
Returns true if the new _q_value is non-zero, false otherwise.*/
|
||||||
inline bool deref()
|
inline bool deref()
|
||||||
{
|
{
|
||||||
mutexlocker lock(&m);
|
mutexlocker lock(&m);
|
||||||
return --value != 0;
|
return --_q_value != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
If the current value of this QAtomicInt is the expectedValue,
|
If the current _q_value of this QAtomicInt is the expectedValue,
|
||||||
the test-and-set functions assign the newValue to this QAtomicInt
|
the test-and-set functions assign the newValue to this QAtomicInt
|
||||||
and return true. If the values are not the same, this function
|
and return true. If the values are not the same, this function
|
||||||
does nothing and returns false.
|
does nothing and returns false.
|
||||||
|
|
@ -60,8 +64,8 @@ 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 (_q_value == expectedValue) {
|
||||||
value = newValue;
|
_q_value = newValue;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -70,42 +74,42 @@ does nothing and returns false.
|
||||||
// Non-atomic API
|
// Non-atomic API
|
||||||
inline bool operator==(int value) const
|
inline bool operator==(int value) const
|
||||||
{
|
{
|
||||||
return value == value;
|
return _q_value == value;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool operator!=(int value) const
|
inline bool operator!=(int value) const
|
||||||
{
|
{
|
||||||
return value != value;
|
return _q_value != value;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool operator!() const
|
inline bool operator!() const
|
||||||
{
|
{
|
||||||
return value == 0;
|
return _q_value == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline operator int() const
|
inline operator int() const
|
||||||
{
|
{
|
||||||
return value;
|
return _q_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline atomicInt &operator=(int value)
|
inline atomicInt &operator=(int value)
|
||||||
{
|
{
|
||||||
value = value;
|
_q_value = value;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool operator>(int value) const
|
inline bool operator>(int value) const
|
||||||
{
|
{
|
||||||
return value > value;
|
return _q_value > value;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool operator<(int value) const
|
inline bool operator<(int value) const
|
||||||
{
|
{
|
||||||
return value < value;
|
return _q_value < value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
volatile int value;
|
volatile int _q_value;
|
||||||
mutex m;
|
mutex m;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue