first commit
This commit is contained in:
48
inc/hgl/thread/atomic/AtomicGNU.h
Normal file
48
inc/hgl/thread/atomic/AtomicGNU.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef HGL_THREAD_ATOMIC_GNU_INCLUDE
|
||||
#define HGL_THREAD_ATOMIC_GNU_INCLUDE
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
template<typename T> class atom
|
||||
{
|
||||
volatile T value;
|
||||
|
||||
public:
|
||||
|
||||
atom(){value=0;}
|
||||
atom(const volatile T new_value){operator=(new_value);}
|
||||
|
||||
inline T operator ->(){return value;}
|
||||
|
||||
inline T operator *= (const volatile T v) {return operator=(value *=v);}
|
||||
inline T operator /= (const volatile T v) {return operator=(value /=v);}
|
||||
inline T operator %= (const volatile T v) {return operator=(value %=v);}
|
||||
inline T operator >>= (const volatile T v) {return operator=(value>>=v);}
|
||||
inline T operator <<= (const volatile T v) {return operator=(value<<=v);}
|
||||
|
||||
inline T operator ! ()const {return !value;}
|
||||
inline T operator ~ ()const {return ~value;}
|
||||
|
||||
inline operator T () {return value;}
|
||||
inline operator const T ()const {return value;}
|
||||
inline bool operator ! () {return !value;}
|
||||
|
||||
inline bool operator == (const volatile T v)const {return value==v;}
|
||||
inline bool operator != (const volatile T v)const {return value!=v;}
|
||||
|
||||
inline T operator = (const volatile T new_value) {__sync_lock_test_and_set(&value,new_value); return value;}
|
||||
|
||||
inline T operator += (const volatile T add_value) {return __sync_add_and_fetch(&value,add_value);}
|
||||
inline T operator -= (const volatile T sub_value) {return __sync_sub_and_fetch(&value,sub_value);}
|
||||
|
||||
inline T operator &= (const volatile T v) {return __sync_and_and_fetch(&value,v);}
|
||||
inline T operator |= (const volatile T v) {return __sync_or_and_fetch (&value,v);}
|
||||
inline T operator ^= (const volatile T v) {return __sync_xor_and_fetch(&value,v);}
|
||||
|
||||
inline T operator ++ () {return __sync_add_and_fetch(&value,1);} //前置++
|
||||
inline T operator ++ (int) {return __sync_fetch_and_add(&value,1);} //后置++
|
||||
inline T operator -- () {return __sync_sub_and_fetch(&value,1);}
|
||||
inline T operator -- (int) {return __sync_fetch_and_sub(&value,1);}
|
||||
};//template<typename T> class atom
|
||||
}//namespace hgl
|
||||
#endif//HGL_THREAD_ATOMIC_GNU_INCLUDE
|
50
inc/hgl/thread/atomic/AtomicOSX.h
Normal file
50
inc/hgl/thread/atomic/AtomicOSX.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef HGL_THREAD_ATOMIC_OSX_INCLUDE
|
||||
#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);}
|
||||
inline aint operator += (cavint add_value) {return;}
|
||||
inline aint operator -= (cavint sub_value) {return;}
|
||||
|
||||
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 ++ (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
|
91
inc/hgl/thread/atomic/AtomicWin.h
Normal file
91
inc/hgl/thread/atomic/AtomicWin.h
Normal file
@@ -0,0 +1,91 @@
|
||||
#ifndef HGL_THREAD_ATOMIC_WINDOWS_INCLUDE
|
||||
#define HGL_THREAD_ATOMIC_WINDOWS_INCLUDE
|
||||
|
||||
#include<windows.h>
|
||||
namespace hgl
|
||||
{
|
||||
//开发日志 2013-06-19
|
||||
//1.原本是一个模板,但将32/64中不一样的部分各自特例化,但不知为何VC2012中无法认出对应的操作符,所以改成2个模板
|
||||
//2.部分原子函数没有8/16位版本
|
||||
|
||||
//32位版
|
||||
template<typename T> class atom_win32
|
||||
{
|
||||
volatile T value;
|
||||
|
||||
public:
|
||||
|
||||
atom_win32(){value=0;}
|
||||
atom_win32(const volatile T new_value){operator=(new_value);}
|
||||
|
||||
T operator *= (const volatile T v) {return operator=(value *=v);}
|
||||
T operator /= (const volatile T v) {return operator=(value /=v);}
|
||||
T operator %= (const volatile T v) {return operator=(value %=v);}
|
||||
T operator >>= (const volatile T v) {return operator=(value>>=v);}
|
||||
T operator <<= (const volatile T v) {return operator=(value<<=v);}
|
||||
|
||||
T operator ! ()const {return !value;}
|
||||
T operator ~ ()const {return ~value;}
|
||||
|
||||
operator T () {return value;}
|
||||
operator const T ()const {return value;}
|
||||
bool operator ! () {return !value;}
|
||||
|
||||
bool operator == (const volatile T v)const {return value==v;}
|
||||
bool operator != (const volatile T v)const {return value!=v;}
|
||||
|
||||
T operator = (const volatile T nv) {return InterlockedExchange((unsigned long *)&value,(unsigned long)nv);}
|
||||
T operator ++ () {return InterlockedIncrement((unsigned long *)&value);}
|
||||
T operator -- () {return InterlockedDecrement((unsigned long *)&value);}
|
||||
T operator += (const volatile T av) {return InterlockedExchangeAdd((unsigned long *)&value, av); }
|
||||
T operator -= (const volatile T av) {return InterlockedExchangeAdd((unsigned long *)&value, -av); }
|
||||
|
||||
volatile T operator &= (const volatile T v) {return InterlockedAnd((unsigned long *)&value,v);}
|
||||
volatile T operator |= (const volatile T v) {return InterlockedOr((unsigned long *)&value, v); }
|
||||
volatile T operator ^= (const volatile T v) {return InterlockedXor((unsigned long *)&value, v); }
|
||||
|
||||
volatile T operator ++ (int) {volatile T ret=value;operator++();return ret;}//后置++
|
||||
volatile T operator -- (int) {volatile T ret=value;operator--();return ret;}//后置--
|
||||
};//template<typename T> class atom_win32
|
||||
|
||||
//64位版
|
||||
template<typename T> class atom_win64
|
||||
{
|
||||
volatile T value;
|
||||
|
||||
public:
|
||||
|
||||
atom_win64(){value=0;}
|
||||
atom_win64(const volatile T new_value){operator=(new_value);}
|
||||
|
||||
T operator *= (const volatile T v) {return operator=(value *=v);}
|
||||
T operator /= (const volatile T v) {return operator=(value /=v);}
|
||||
T operator %= (const volatile T v) {return operator=(value %=v);}
|
||||
T operator >>= (const volatile T v) {return operator=(value>>=v);}
|
||||
T operator <<= (const volatile T v) {return operator=(value<<=v);}
|
||||
|
||||
T operator ! ()const {return !value;}
|
||||
T operator ~ ()const {return ~value;}
|
||||
|
||||
operator T () {return value;}
|
||||
operator const T ()const {return value;}
|
||||
bool operator ! () {return !value;}
|
||||
|
||||
bool operator == (const volatile T v)const {return value==v;}
|
||||
bool operator != (const volatile T v)const {return value!=v;}
|
||||
|
||||
T operator = (const volatile T nv) {return InterlockedExchange64((LONG64 *)&value, (LONG64)nv); }
|
||||
T operator ++ () {return InterlockedIncrement64((LONG64 *)&value); }
|
||||
T operator -- () {return InterlockedDecrement64((LONG64 *)&value); }
|
||||
T operator += (const volatile T av) {return InterlockedExchangeAdd64((LONG64 *)&value, av); }
|
||||
T operator -= (const volatile T av) {return InterlockedExchangeAdd64((LONG64 *)&value, -av); }
|
||||
|
||||
volatile T operator &= (const volatile T v) {return InterlockedAnd64((LONG64 *)&value, v); }
|
||||
volatile T operator |= (const volatile T v) {return InterlockedOr64((LONG64 *)&value, v); }
|
||||
volatile T operator ^= (const volatile T v) {return InterlockedXor64((LONG64 *)&value, v); }
|
||||
|
||||
volatile T operator ++ (int) {volatile T ret=value;operator++();return ret;}//后置++
|
||||
volatile T operator -- (int) {volatile T ret=value;operator--();return ret;}//后置--
|
||||
};//template<typename T> class atom_win64
|
||||
}//namespace hgl
|
||||
#endif//HGL_THREAD_ATOMIC_WINDOWS_INCLUDE
|
Reference in New Issue
Block a user