From: Christian P. <cp...@us...> - 2005-04-23 10:54:56
|
Update of /cvsroot/pclasses/pclasses2/include/pclasses In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7503/include/pclasses Modified Files: Atomic.h AtomicTraits.h Log Message: Added AtomicMutex in favor of System::CriticalSection to get rid of dependency on System. Index: AtomicTraits.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/AtomicTraits.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- AtomicTraits.h 20 Jan 2005 22:42:30 -0000 1.3 +++ AtomicTraits.h 23 Apr 2005 10:54:45 -0000 1.4 @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2004 by Christian Prochnow * + * Copyright (C) 2004,2005 by Christian Prochnow, SecuLogiX GmbH * * cp...@se... * * * * This program is free software; you can redistribute it and/or modify * @@ -22,53 +22,87 @@ #define P_AtomicTraits_h #include <pclasses/Export.h> -#include <pclasses/System/CriticalSection.h> namespace P { namespace Traits { -//! Atomic type-traits +//! Atomic traits +/*! + The AtomicTraits template supplies the actual implementation for the + atomic operations that can be used via the Atomic<> template class. + The default implementation uses a mutex to provide the atomicity, + while the type-specialized implementations will use real atomic + operations. +*/ template <class Type> struct AtomicTraits { + + //! AtomicTraits Mutex + /*! + The Mutex class is used by the default implementation of + AtomicTraits<> to provide atomicity for types that dont have a + AtomicTraits specialisation. + */ + class Mutex { + public: + Mutex(); + ~Mutex(); + + void lock() throw(); + void unlock() throw(); + + class ScopedLock { + public: + ScopedLock(Mutex& mtx); + ~ScopedLock(); + + private: + Mutex& _mtx; + }; + + private: + unsigned long _handle; + }; + struct AtomicType { Type value; - System::CriticalSection lock; + Mutex lock; }; static void set(AtomicType* atomic, const Type& val) throw() { - System::CriticalSection::ScopedLock lck(atomic->lock); + Mutex::ScopedLock lck(atomic->lock); atomic->value = val; } static Type get(const AtomicType* atomic) throw() { - System::CriticalSection::ScopedLock lck(atomic->lock); + Mutex::ScopedLock lck(atomic->lock); return atomic->value; } static void inc(AtomicType* val) throw() { - System::CriticalSection::ScopedLock lck(val->lock); + Mutex::ScopedLock lck(val->lock); ++val->value; } static bool incAndTest(AtomicType* val) throw() { - System::CriticalSection::ScopedLock lck(val->lock); + Mutex::ScopedLock lck(val->lock); return !(++val->value); } static void dec(AtomicType* val) throw() { - System::CriticalSection::ScopedLock lck(val->lock); + Mutex::ScopedLock lck(val->lock); --val->value; } static bool decAndTest(AtomicType* val) throw() { - System::CriticalSection::ScopedLock lck(val->lock); + Mutex::ScopedLock lck(val->lock); return !(--val->value); } }; Index: Atomic.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/Atomic.h,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -d -r1.1.1.1 -r1.2 --- Atomic.h 22 Dec 2004 17:54:40 -0000 1.1.1.1 +++ Atomic.h 23 Apr 2005 10:54:45 -0000 1.2 @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2004 by Christian Prochnow * + * Copyright (C) 2004,2005 by Christian Prochnow, SecuLogiX GmbH * * cp...@se... * * * * This program is free software; you can redistribute it and/or modify * @@ -18,14 +18,19 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -#ifndef _P_Atomic_h_ -#define _P_Atomic_h_ +#ifndef P_Atomic_h +#define P_Atomic_h #include <pclasses/AtomicTraits.h> namespace P { //! Atomic value-type template +/*! + The Atomic value template class provides limited atomic operations for + a given value type. Atomic operations are synchronised across all + threads in the system. +*/ template <class Type> class Atomic { public: @@ -33,35 +38,49 @@ typedef Traits::AtomicTraits<Type> TraitsType; typedef typename Traits::AtomicTraits<Type>::AtomicType AtomicType; + //! Constructor Atomic(const Type& val = Type()) throw() { TraitsType::set(&_val, val); } + //! Destructor ~Atomic() throw() { } + //! Returns the value operator Type() const throw() { return TraitsType::get(&_val); } + //! Increment and test for zero + /*! + Increments the stored value and returns true if the value + has become zero, and false otherwise. + */ bool operator++() throw() { return TraitsType::incAndTest(&_val); } + //! Increment the value void operator++(int) throw() { TraitsType::inc(&_val); } + //! Decrement and test for zero + /*! + Decrements the stored value and returns true if the value + has become zero, and false otherwise. + */ bool operator--() throw() { return TraitsType::decAndTest(&_val); } + //! Decrement the value void operator--(int) throw() { TraitsType::dec(&_val); } + //! Assign new value Atomic& operator=(int val) throw() { TraitsType::set(&_val, val); return *this; } - //@todo deny all other operators - private: AtomicType _val; }; |