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

@ -2,14 +2,14 @@
// The Loki Library // The Loki Library
// Copyright (c) 2001 by Andrei Alexandrescu // Copyright (c) 2001 by Andrei Alexandrescu
// This code accompanies the book: // This code accompanies the book:
// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design // Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
// Patterns Applied". Copyright (c) 2001. Addison-Wesley. // Patterns Applied". Copyright (c) 2001. Addison-Wesley.
// Permission to use, copy, modify, distribute and sell this software for any // Permission to use, copy, modify, distribute and sell this software for any
// purpose is hereby granted without fee, provided that the above copyright // purpose is hereby granted without fee, provided that the above copyright
// notice appear in all copies and that both that copyright notice and this // notice appear in all copies and that both that copyright notice and this
// permission notice appear in supporting documentation. // permission notice appear in supporting documentation.
// The author or Addison-Wesley Longman make no representations about the // The author or Addison-Wesley Longman make no representations about the
// suitability of this software for any purpose. It is provided "as is" // suitability of this software for any purpose. It is provided "as is"
// without express or implied warranty. // without express or implied warranty.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#ifndef LOKI_THREADS_INC_ #ifndef LOKI_THREADS_INC_
@ -27,12 +27,12 @@
/// ///
/// All classes in Loki have configurable threading model. /// All classes in Loki have configurable threading model.
/// ///
/// The macro LOKI_DEFAULT_THREADING selects the default /// The macro LOKI_DEFAULT_THREADING selects the default
/// threading model for certain components of Loki /// threading model for certain components of Loki
/// (it affects only default template arguments) /// (it affects only default template arguments)
/// ///
/// \par Usage: /// \par Usage:
/// ///
/// To use a specific threading model define /// To use a specific threading model define
/// ///
/// - nothing, single-theading is default /// - nothing, single-theading is default
@ -43,9 +43,9 @@
/// ///
/// - Windows (windows.h) /// - Windows (windows.h)
/// - POSIX (pthread.h): /// - POSIX (pthread.h):
/// No recursive mutex support with pthread. /// No recursive mutex support with pthread.
/// This means: calling Lock() on a Loki::Mutex twice from the /// This means: calling Lock() on a Loki::Mutex twice from the
/// same thread before unlocking the mutex deadlocks the system. /// same thread before unlocking the mutex deadlocks the system.
/// To avoid this redesign your synchronization. See also: /// To avoid this redesign your synchronization. See also:
/// http://sourceforge.net/tracker/index.php?func=detail&aid=1516182&group_id=29557&atid=396647 /// http://sourceforge.net/tracker/index.php?func=detail&aid=1516182&group_id=29557&atid=396647
@ -55,28 +55,28 @@
#if defined(LOKI_CLASS_LEVEL_THREADING) || defined(LOKI_OBJECT_LEVEL_THREADING) #if defined(LOKI_CLASS_LEVEL_THREADING) || defined(LOKI_OBJECT_LEVEL_THREADING)
#define LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL ::Loki::ClassLevelLockable #define LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL ::Loki::ClassLevelLockable
#if defined(LOKI_CLASS_LEVEL_THREADING) && !defined(LOKI_OBJECT_LEVEL_THREADING) #if defined(LOKI_CLASS_LEVEL_THREADING) && !defined(LOKI_OBJECT_LEVEL_THREADING)
#define LOKI_DEFAULT_THREADING ::Loki::ClassLevelLockable #define LOKI_DEFAULT_THREADING ::Loki::ClassLevelLockable
#else #else
#define LOKI_DEFAULT_THREADING ::Loki::ObjectLevelLockable #define LOKI_DEFAULT_THREADING ::Loki::ObjectLevelLockable
#endif #endif
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
#include <windows.h> #include <windows.h>
#define LOKI_WINDOWS_H #define LOKI_WINDOWS_H
#else #else
#include <pthread.h> #include <pthread.h>
#define LOKI_PTHREAD_H #define LOKI_PTHREAD_H
#endif #endif
#else #else
#define LOKI_DEFAULT_THREADING ::Loki::SingleThreaded #define LOKI_DEFAULT_THREADING ::Loki::SingleThreaded
#define LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL ::Loki::SingleThreaded #define LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL ::Loki::SingleThreaded
#endif #endif
#ifndef LOKI_DEFAULT_MUTEX #ifndef LOKI_DEFAULT_MUTEX
#define LOKI_DEFAULT_MUTEX ::Loki::Mutex #define LOKI_DEFAULT_MUTEX ::Loki::Mutex
#endif #endif
@ -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)
@ -130,32 +208,103 @@
#define LOKI_THREADS_ATOMIC(x) \ #define LOKI_THREADS_ATOMIC(x) \
pthread_mutex_lock(&atomic_mutex_); \ pthread_mutex_lock(&atomic_mutex_); \
x; \ x; \
pthread_mutex_unlock(&atomic_mutex_) pthread_mutex_unlock(&atomic_mutex_)
#define LOKI_THREADS_ATOMIC_FUNCTIONS \ #define LOKI_THREADS_ATOMIC_FUNCTIONS \
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
#define LOKI_THREADS_MUTEX(x) #define LOKI_THREADS_MUTEX(x)
#define LOKI_THREADS_MUTEX_INIT(x) #define LOKI_THREADS_MUTEX_INIT(x)
#define LOKI_THREADS_MUTEX_DELETE(x) #define LOKI_THREADS_MUTEX_DELETE(x)
#define LOKI_THREADS_MUTEX_LOCK(x) #define LOKI_THREADS_MUTEX_LOCK(x)
#define LOKI_THREADS_MUTEX_UNLOCK(x) #define LOKI_THREADS_MUTEX_UNLOCK(x)
#define LOKI_THREADS_LONG #define LOKI_THREADS_LONG
#define LOKI_THREADS_MUTEX_CTOR(x) #define LOKI_THREADS_MUTEX_CTOR(x)
#endif #endif
@ -219,38 +368,81 @@ namespace Loki
explicit Lock(const SingleThreaded&) {} explicit Lock(const SingleThreaded&) {}
explicit Lock(const SingleThreaded*) {} explicit Lock(const SingleThreaded*) {}
}; };
typedef Host VolatileType; typedef Host VolatileType;
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)
{ return ++lval; } { return ++lval; }
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; }
};
#if defined(LOKI_WINDOWS_H) || defined(LOKI_PTHREAD_H) 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;
}
};
#if defined(LOKI_WINDOWS_H) || defined(LOKI_PTHREAD_H)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// \class ObjectLevelLockable /// \class ObjectLevelLockable
@ -273,13 +465,13 @@ namespace Loki
class Lock; class Lock;
friend class Lock; friend class Lock;
/// \struct Lock /// \struct Lock
/// Lock class to lock on object level /// Lock class to lock on object level
class Lock class Lock
{ {
public: public:
/// Lock object /// Lock object
explicit Lock(const ObjectLevelLockable& host) : host_(host) explicit Lock(const ObjectLevelLockable& host) : host_(host)
{ {
@ -308,17 +500,17 @@ namespace Loki
typedef volatile Host VolatileType; typedef volatile Host VolatileType;
typedef LOKI_THREADS_LONG IntType; typedef LOKI_THREADS_LONG IntType;
LOKI_THREADS_ATOMIC_FUNCTIONS LOKI_THREADS_ATOMIC_FUNCTIONS
}; };
#ifdef LOKI_PTHREAD_H #ifdef LOKI_PTHREAD_H
template <class Host, class MutexPolicy> template <class Host, class MutexPolicy>
pthread_mutex_t ObjectLevelLockable<Host, MutexPolicy>::atomic_mutex_ = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t ObjectLevelLockable<Host, MutexPolicy>::atomic_mutex_ = PTHREAD_MUTEX_INITIALIZER;
#endif #endif
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// \class ClassLevelLockable /// \class ClassLevelLockable
/// ///
@ -330,7 +522,7 @@ namespace Loki
class ClassLevelLockable class ClassLevelLockable
{ {
struct Initializer struct Initializer
{ {
bool init_; bool init_;
MutexPolicy mtx_; MutexPolicy mtx_;
@ -355,7 +547,7 @@ namespace Loki
/// \struct Lock /// \struct Lock
/// Lock class to lock on class level /// Lock class to lock on class level
class Lock class Lock
{ {
public: public:
/// Lock class /// Lock class
@ -393,23 +585,23 @@ namespace Loki
typedef volatile Host VolatileType; typedef volatile Host VolatileType;
typedef LOKI_THREADS_LONG IntType; typedef LOKI_THREADS_LONG IntType;
LOKI_THREADS_ATOMIC_FUNCTIONS LOKI_THREADS_ATOMIC_FUNCTIONS
}; };
#ifdef LOKI_PTHREAD_H #ifdef LOKI_PTHREAD_H
template <class Host, class MutexPolicy> template <class Host, class MutexPolicy>
pthread_mutex_t ClassLevelLockable<Host, MutexPolicy>::atomic_mutex_ = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t ClassLevelLockable<Host, MutexPolicy>::atomic_mutex_ = PTHREAD_MUTEX_INITIALIZER;
#endif #endif
template < class Host, class MutexPolicy > template < class Host, class MutexPolicy >
typename ClassLevelLockable< Host, MutexPolicy >::Initializer typename ClassLevelLockable< Host, MutexPolicy >::Initializer
ClassLevelLockable< Host, MutexPolicy >::initializer_; ClassLevelLockable< Host, MutexPolicy >::initializer_;
#endif // #if defined(LOKI_WINDOWS_H) || defined(LOKI_PTHREAD_H) #endif // #if defined(LOKI_WINDOWS_H) || defined(LOKI_PTHREAD_H)
} // namespace Loki } // namespace Loki