vcglib/wrap/system/multithreading/atomic_int_generic.h

116 lines
2.0 KiB
C
Raw Normal View History

#ifndef _ATOMIC_INT_GENERIC_H
#define _ATOMIC_INT_GENERIC_H
#include "mt.h"
namespace mt{
class atomicInt
{
public:
2011-12-15 18:38:34 +01:00
atomicInt()
{
2011-12-15 19:51:53 +01:00
_q_value = 0;
2011-12-15 18:38:34 +01:00
}
atomicInt( int value )
{
2011-12-15 19:51:53 +01:00
_q_value = value;
2011-12-15 18:38:34 +01:00
}
// atomic API
/**
2011-12-15 19:51:53 +01:00
Reads the current _q_value of this QAtomicInt and then adds valueToAdd
to the current _q_value, returning the original _q_value.
2011-12-15 18:38:34 +01:00
*/
inline int fetchAndAddAcquire( int valueToAdd )
{
mutexlocker lock(&m);
2011-12-15 19:51:53 +01:00
int originalValue = _q_value;
_q_value += valueToAdd;
2011-12-15 18:38:34 +01:00
return originalValue;
}
/**
2011-12-15 19:51:53 +01:00
Atomically increments the _q_value of this atomicInt.
Returns true if the new _q_value is non-zero, false otherwise.*/
2011-12-15 18:38:34 +01:00
inline bool ref()
{
mutexlocker lock(&m);
2011-12-15 19:51:53 +01:00
return ++_q_value != 0;
2011-12-15 18:38:34 +01:00
}
/*
2011-12-15 19:51:53 +01:00
Atomically decrements the _q_value of this QAtomicInt.
Returns true if the new _q_value is non-zero, false otherwise.*/
2011-12-15 18:38:34 +01:00
inline bool deref()
{
mutexlocker lock(&m);
2011-12-15 19:51:53 +01:00
return --_q_value != 0;
2011-12-15 18:38:34 +01:00
}
/*
2011-12-15 19:51:53 +01:00
If the current _q_value of this QAtomicInt is the expectedValue,
2011-12-15 18:38:34 +01:00
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);
2011-12-15 19:51:53 +01:00
if (_q_value == expectedValue) {
_q_value = newValue;
2011-12-15 18:38:34 +01:00
return true;
}
2011-12-15 18:38:34 +01:00
return false;
}
// Non-atomic API
inline bool operator==(int value) const
{
2011-12-15 19:51:53 +01:00
return _q_value == value;
2011-12-15 18:38:34 +01:00
}
inline bool operator!=(int value) const
{
2011-12-15 19:51:53 +01:00
return _q_value != value;
2011-12-15 18:38:34 +01:00
}
inline bool operator!() const
{
2011-12-15 19:51:53 +01:00
return _q_value == 0;
2011-12-15 18:38:34 +01:00
}
inline operator int() const
{
2011-12-15 19:51:53 +01:00
return _q_value;
2011-12-15 18:38:34 +01:00
}
inline atomicInt &operator=(int value)
{
2011-12-15 19:51:53 +01:00
_q_value = value;
2011-12-15 18:38:34 +01:00
return *this;
}
inline bool operator>(int value) const
{
2011-12-15 19:51:53 +01:00
return _q_value > value;
2011-12-15 18:38:34 +01:00
}
inline bool operator<(int value) const
{
2011-12-15 19:51:53 +01:00
return _q_value < value;
2011-12-15 18:38:34 +01:00
}
private:
2011-12-15 19:51:53 +01:00
volatile int _q_value;
2011-12-15 18:38:34 +01:00
mutex m;
};
}//namespace
#endif