94 lines
2.3 KiB
C++
94 lines
2.3 KiB
C++
#ifndef __ARRAY_ALLOCATOR_TATOMIC_H__
|
|
#define __ARRAY_ALLOCATOR_TATOMIC_H__
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
// include part
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include "Atomic.hpp"
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
// declaration part
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
namespace Utils{
|
|
|
|
template <typename TYPE>
|
|
class TAtomic {
|
|
public:
|
|
inline explicit TAtomic(TYPE val = 0) : _ptr(val) {}
|
|
inline ~TAtomic() {}
|
|
|
|
template <typename OTYPE> inline
|
|
TAtomic& operator=(const TAtomic<OTYPE>& other )
|
|
{
|
|
if(this != &other)
|
|
{
|
|
TYPE oldValue, newValue;
|
|
do{
|
|
oldValue = atomic_get(_ptr);
|
|
newValue = (TYPE)atomic_get<OTYPE>(other._ptr);
|
|
}while(atomic_conditionalSet(&_ptr, oldValue, newValue) == oldValue);
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
template <typename OTYPE> inline
|
|
TAtomic& operator=(OTYPE other )
|
|
{
|
|
atomic_set(&_ptr, (TYPE)other);
|
|
return *this;
|
|
}
|
|
|
|
inline TYPE operator*() const
|
|
{
|
|
return atomic_get(&_ptr);
|
|
}
|
|
|
|
template <typename OTYPE> inline
|
|
TAtomic(const TAtomic<OTYPE>& other ) : _ptr(0)
|
|
{
|
|
*this = other;
|
|
}
|
|
|
|
inline TYPE operator++()
|
|
{
|
|
return pre::atomic_inc(&_ptr);
|
|
}
|
|
|
|
inline TYPE operator++(int)
|
|
{
|
|
return post::atomic_inc(&_ptr);
|
|
}
|
|
|
|
inline TYPE operator--()
|
|
{
|
|
return pre::atomic_dec(&_ptr);
|
|
}
|
|
|
|
inline TYPE operator--(int)
|
|
{
|
|
return post::atomic_dec(&_ptr);
|
|
}
|
|
|
|
template <class OTYPE> inline
|
|
TAtomic& operator+=(OTYPE value)
|
|
{
|
|
pre::atomic_add<TYPE>(&_ptr, (TYPE)value);
|
|
return *this;
|
|
}
|
|
|
|
template <class OTYPE> inline
|
|
TAtomic& operator-=(OTYPE value)
|
|
{
|
|
pre::atomic_sub(&_ptr, (TYPE)value);
|
|
return *this;
|
|
}
|
|
|
|
private:
|
|
TYPE _ptr;
|
|
};
|
|
}
|
|
|
|
#endif
|