2019-12-18 15:25:31 +08:00
|
|
|
|
#ifndef HGL_THREAD_ATOMIC_OSX_INCLUDE
|
2019-08-19 19:19:58 +08:00
|
|
|
|
#define HGL_THREAD_ATOMIC_OSX_INCLUDE
|
|
|
|
|
|
|
|
|
|
#include<libkern/OSAtomic.h>
|
|
|
|
|
|
|
|
|
|
namespace hgl
|
|
|
|
|
{
|
|
|
|
|
typedef int aint;
|
|
|
|
|
|
|
|
|
|
typedef volatile aint avint;
|
|
|
|
|
typedef const avint cavint;
|
|
|
|
|
|
|
|
|
|
template<typename T> class atom ///原子数据
|
|
|
|
|
{
|
|
|
|
|
avint value;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
atom(){value=0;}
|
|
|
|
|
atom(cavint new_value){operator=(new_value);}
|
|
|
|
|
|
|
|
|
|
inline aint operator *= (cavint v) {return operator=(value *=v);}
|
|
|
|
|
inline aint operator /= (cavint v) {return operator=(value /=v);}
|
|
|
|
|
inline aint operator %= (cavint v) {return operator=(value %=v);}
|
|
|
|
|
inline aint operator >>= (cavint v) {return operator=(value>>=v);}
|
|
|
|
|
inline aint operator <<= (cavint v) {return operator=(value<<=v);}
|
|
|
|
|
|
|
|
|
|
inline aint operator ! ()const {return !value;}
|
|
|
|
|
inline aint operator ~ ()const {return ~value;}
|
|
|
|
|
|
|
|
|
|
inline operator const aint ()const {return value;}
|
|
|
|
|
inline bool operator ! () {return !value;}
|
|
|
|
|
|
|
|
|
|
inline bool operator == (cavint v)const {return value==v;}
|
|
|
|
|
inline bool operator != (cavint v)const {return value!=v;}
|
|
|
|
|
inline aint operator = (cavint new_value) {value=new_value;return value;}
|
|
|
|
|
inline aint operator ++ () {return OSAtomicIncrement32(&value);}
|
|
|
|
|
inline aint operator -- () {return OSAtomicDecrement32(&value);}
|
2019-12-18 15:25:31 +08:00
|
|
|
|
inline aint operator += (cavint add_value) {return OSAtomicAdd32(value,&add_value);}
|
|
|
|
|
inline aint operator -= (cavint sub_value) {cavint sv=-sub_value;return OSAtomicAdd32(value,&sv);}
|
2019-08-19 19:19:58 +08:00
|
|
|
|
|
2019-12-18 15:25:31 +08:00
|
|
|
|
inline aint operator &= (cavint v) {return OSAtomicAnd32(value,&v);}
|
|
|
|
|
inline aint operator |= (cavint v) {return OSAtomicOr32Orig(value,&v);}
|
|
|
|
|
inline aint operator ^= (cavint v) {return OSAtomicXor32Orig(value,&v);}
|
2019-08-19 19:19:58 +08:00
|
|
|
|
|
|
|
|
|
inline aint operator ++ (int) {aint ret=value;operator++();return ret;}//后置++
|
|
|
|
|
inline aint operator -- (int) {volatile T ret=value;operator--();return ret;}//后置--
|
|
|
|
|
};//class atom
|
|
|
|
|
}//namespace hgl
|
|
|
|
|
#endif//HGL_THREAD_ATOMIC_OSX_INCLUDE
|