Added several more atomic functions.

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@902 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
rich_sposato 2008-11-10 05:47:06 +00:00
parent 5a243771f5
commit c661dfc348

View file

@ -92,19 +92,97 @@
#define LOKI_THREADS_MUTEX_CTOR(x) #define LOKI_THREADS_MUTEX_CTOR(x)
#define LOKI_THREADS_ATOMIC_FUNCTIONS \ #define LOKI_THREADS_ATOMIC_FUNCTIONS \
static IntType AtomicMultiply(volatile IntType& lval, const IntType val) \
{ \
::EnterCriticalSection( &atomic_mutex_ ); \
lval *= val; \
::LeaveCriticalSection( &atomic_mutex_ ); \
return lval; \
} \
\
static IntType AtomicDivide(volatile IntType& lval, const IntType val) \
{ \
::EnterCriticalSection( &atomic_mutex_ ); \
lval /= val; \
::LeaveCriticalSection( &atomic_mutex_ ); \
return lval; \
} \
\
static IntType AtomicIncrement(volatile IntType& lval) \ static IntType AtomicIncrement(volatile IntType& lval) \
{ return InterlockedIncrement(&const_cast<IntType&>(lval)); } \ { \
::EnterCriticalSection( &atomic_mutex_ ); \
++lval; \
::LeaveCriticalSection( &atomic_mutex_ ); \
return lval; \
} \
\ \
static IntType AtomicDecrement(volatile IntType& lval) \ static IntType AtomicDecrement(volatile IntType& lval) \
{ return InterlockedDecrement(&const_cast<IntType&>(lval)); } \ { \
::EnterCriticalSection( &atomic_mutex_ ); \
--lval; \
::LeaveCriticalSection( &atomic_mutex_ ); \
return lval; \
} \
\ \
static void AtomicAssign(volatile IntType& lval, IntType val) \ static void AtomicAssign(volatile IntType& lval, const IntType val) \
{ InterlockedExchange(&const_cast<IntType&>(lval), val); } \ { InterlockedExchange(&const_cast<IntType&>(lval), val); } \
\ \
static void AtomicAssign(IntType& lval, volatile IntType& val) \ static void AtomicAssign(IntType& lval, volatile const IntType& val) \
{ InterlockedExchange(&lval, val); } { InterlockedExchange(&lval, val); } \
\
static IntType AtomicIncrement(volatile IntType& lval, const IntType compare, bool & matches ) \
{ \
::EnterCriticalSection( &atomic_mutex_ ); \
++lval; \
matches = ( lval == compare ); \
::LeaveCriticalSection( &atomic_mutex_ ); \
return lval; \
} \
\
static IntType AtomicDecrement(volatile IntType& lval, const IntType compare, bool & matches ) \
{ \
::EnterCriticalSection( &atomic_mutex_ ); \
--lval; \
matches = ( lval == compare ); \
::LeaveCriticalSection( &atomic_mutex_ ); \
return lval; \
} \
\
static IntType AtomicAdd(volatile IntType& lval, const IntType val, const IntType compare, bool & matches ) \
{ \
::EnterCriticalSection( &atomic_mutex_ ); \
lval += val; \
matches = ( lval == compare ); \
::LeaveCriticalSection( &atomic_mutex_ ); \
return lval; \
} \
\
static IntType AtomicSubtract(volatile IntType& lval, const IntType val, const IntType compare, bool & matches ) \
{ \
::EnterCriticalSection( &atomic_mutex_ ); \
lval -= val; \
matches = ( lval == compare ); \
::LeaveCriticalSection( &atomic_mutex_ ); \
return lval; \
} \
\
static IntType AtomicMultiply(volatile IntType& lval, const IntType val, const IntType compare, bool & matches ) \
{ \
::EnterCriticalSection( &atomic_mutex_ ); \
lval *= val; \
matches = ( lval == compare ); \
::LeaveCriticalSection( &atomic_mutex_ ); \
return lval; \
} \
\
static IntType AtomicDivide(volatile IntType& lval, const IntType val, const IntType compare, bool & matches ) \
{ \
::EnterCriticalSection( &atomic_mutex_ ); \
lval /= val; \
matches = ( lval == compare ); \
::LeaveCriticalSection( &atomic_mutex_ ); \
return lval; \
}
#elif defined(LOKI_PTHREAD_H) #elif defined(LOKI_PTHREAD_H)
@ -136,17 +214,88 @@
private: \ private: \
static pthread_mutex_t atomic_mutex_; \ static pthread_mutex_t atomic_mutex_; \
public: \ public: \
static IntType AtomicMultiply(volatile IntType& lval, const IntType val) \
{ \
::pthread_mutex_lock( &atomic_mutex_ ); \
lval *= val; \
::pthread_mutex_unlock( &atomic_mutex_ ); \
return lval; \
} \
\
static IntType AtomicDivide(volatile IntType& lval, const IntType val) \
{ \
::pthread_mutex_lock( &atomic_mutex_ ); \
lval /= val; \
::pthread_mutex_unlock( &atomic_mutex_ ); \
return lval; \
} \
\
static IntType AtomicIncrement(volatile IntType& lval) \ static IntType AtomicIncrement(volatile IntType& lval) \
{ LOKI_THREADS_ATOMIC( lval++ ); return lval; } \ { \
::pthread_mutex_lock( &atomic_mutex_ ); \
++lval; \
::pthread_mutex_unlock( &atomic_mutex_ ); \
return lval; \
} \
\ \
static IntType AtomicDecrement(volatile IntType& lval) \ static IntType AtomicDecrement(volatile IntType& lval) \
{ LOKI_THREADS_ATOMIC(lval-- ); return lval; } \ { \
::pthread_mutex_lock( &atomic_mutex_ ); \
--lval; \
::pthread_mutex_unlock( &atomic_mutex_ ); \
return lval; \
} \
\ \
static void AtomicAssign(volatile IntType& lval, IntType val) \ static void AtomicAssign(volatile IntType& lval, const IntType val) \
{ LOKI_THREADS_ATOMIC( lval = val ); } \ { \
::pthread_mutex_lock( &atomic_mutex_ ); \
lval = val; \
::pthread_mutex_unlock( &atomic_mutex_ ); \
return lval; \
} \
\ \
static void AtomicAssign(IntType& lval, volatile IntType& val) \ static void AtomicAssign(IntType& lval, volatile const IntType& val) \
{ LOKI_THREADS_ATOMIC( lval = val ); } { \
::pthread_mutex_lock( &atomic_mutex_ ); \
lval = val; \
::pthread_mutex_unlock( &atomic_mutex_ ); \
return lval; \
} \
\
static IntType AtomicIncrement(volatile IntType& lval, const IntType compare, bool & matches ) \
{ \
::pthread_mutex_lock( &atomic_mutex_ ); \
++lval; \
matches = ( compare == lval ); \
::pthread_mutex_unlock( &atomic_mutex_ ); \
return lval; \
} \
\
static IntType AtomicDecrement(volatile IntType& lval, const IntType compare, bool & matches ) \
{ \
::pthread_mutex_lock( &atomic_mutex_ ); \
--lval; \
matches = ( compare == lval ); \
::pthread_mutex_unlock( &atomic_mutex_ ); \
return lval; \
} \
static IntType AtomicMultiply(volatile IntType& lval, const IntType val, const IntType compare, bool & matches ) \
{ \
::pthread_mutex_lock( &atomic_mutex_ ); \
lval *= val; \
matches = ( lval == compare ); \
::pthread_mutex_unlock( &atomic_mutex_ ); \
return lval; \
} \
\
static IntType AtomicDivide(volatile IntType& lval, const IntType val, const IntType compare, bool & matches ) \
{ \
::pthread_mutex_lock( &atomic_mutex_ ); \
lval /= val; \
matches = ( lval == compare ); \
::pthread_mutex_unlock( &atomic_mutex_ ); \
return lval; \
}
#else // single threaded #else // single threaded
@ -224,16 +373,16 @@ namespace Loki
typedef int IntType; typedef int IntType;
static IntType AtomicAdd(volatile IntType& lval, IntType val) static IntType AtomicAdd(volatile IntType& lval, const IntType val)
{ return lval += val; } { return lval += val; }
static IntType AtomicSubtract(volatile IntType& lval, IntType val) static IntType AtomicSubtract(volatile IntType& lval, const IntType val)
{ return lval -= val; } { return lval -= val; }
static IntType AtomicMultiply(volatile IntType& lval, IntType val) static IntType AtomicMultiply(volatile IntType& lval, const IntType val)
{ return lval *= val; } { return lval *= val; }
static IntType AtomicDivide(volatile IntType& lval, IntType val) static IntType AtomicDivide(volatile IntType& lval, const IntType val)
{ return lval /= val; } { return lval /= val; }
static IntType AtomicIncrement(volatile IntType& lval) static IntType AtomicIncrement(volatile IntType& lval)
@ -242,11 +391,54 @@ namespace Loki
static IntType AtomicDecrement(volatile IntType& lval) static IntType AtomicDecrement(volatile IntType& lval)
{ return --lval; } { return --lval; }
static void AtomicAssign(volatile IntType & lval, IntType val) static void AtomicAssign(volatile IntType & lval, const IntType val)
{ lval = val; } { lval = val; }
static void AtomicAssign(IntType & lval, volatile IntType & val) static void AtomicAssign(IntType & lval, volatile IntType & val)
{ lval = val; } { lval = val; }
static IntType AtomicAdd(volatile IntType& lval, const IntType val, const IntType compare, bool & matches )
{
lval += val;
matches = ( lval == compare );
return lval;
}
static IntType AtomicSubtract(volatile IntType& lval, const IntType val, const IntType compare, bool & matches )
{
lval -= val;
matches = ( lval == compare );
return lval;
}
static IntType AtomicMultiply(volatile IntType& lval, const IntType val, const IntType compare, bool & matches )
{
lval *= val;
matches = ( lval == compare );
return lval;
}
static IntType AtomicDivide(volatile IntType& lval, const IntType val, const IntType compare, bool & matches )
{
lval /= val;
matches = ( lval == compare );
return lval;
}
static IntType AtomicIncrement(volatile IntType& lval, const IntType compare, bool & matches )
{
++lval;
matches = ( lval == compare );
return lval;
}
static IntType AtomicDecrement(volatile IntType& lval, const IntType compare, bool & matches )
{
--lval;
matches = ( lval == compare );
return lval;
}
}; };