[Cppunit-cvs] cppunit2/include/cpptl atomiccounter.h,1.7,1.8 config.h,1.16,1.17
Brought to you by:
blep
From: Baptiste L. <bl...@us...> - 2005-03-07 21:34:57
|
Update of /cvsroot/cppunit/cppunit2/include/cpptl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24464/include/cpptl Modified Files: atomiccounter.h config.h Log Message: * added global define to indicate if atomic counter is thread-safe * removed vacpp specific atomic counter implementation (couldn't get it to compile) * added pthread mutex based atomic counter implementation Index: config.h =================================================================== RCS file: /cvsroot/cppunit/cppunit2/include/cpptl/config.h,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** config.h 6 Mar 2005 21:12:39 -0000 1.16 --- config.h 7 Mar 2005 21:34:45 -0000 1.17 *************** *** 135,138 **** --- 135,147 ---- # endif + # if !CPPTL_HAS_THREAD_SAFE_ATOMIC_COUNTER + # if CPPTL_USE_PTHREAD_THREAD && !CPPTL_USE_WIN32_ATOMIC && !CPPTL_USE_LINUX_ATOMIC + # define CPPTL_USE_PTHREAD_ATOMIC 1 + # endif + # if CPPTL_USE_PTHREAD_ATOMIC || CPPTL_USE_WIN32_ATOMIC || CPPTL_USE_LINUX_ATOMIC + # define CPPTL_HAS_THREAD_SAFE_ATOMIC_COUNTER 1 + # endif + # endif + // auto-link specification Index: atomiccounter.h =================================================================== RCS file: /cvsroot/cppunit/cppunit2/include/cpptl/atomiccounter.h,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** atomiccounter.h 6 Mar 2005 21:12:38 -0000 1.7 --- atomiccounter.h 7 Mar 2005 21:34:44 -0000 1.8 *************** *** 5,15 **** # if CPPTL_USE_LINUX_ATOMIC # include <asm/atomic.h> # endif // Notes: linux implementation only guaranty a 24 bits range for the atomic integer. - // For AIX implementation, see bulit-in function __check_lock_mp, __check_lock_up: - // http://publib.boulder.ibm.com/infocenter/pseries/index.jsp?topic=/com.ibm.vacpp7a.doc/compiler/ref/rubifunc.htm - # if CPPTL_USE_WIN32_ATOMIC // Forwards declaration for WIN32 (avoid including windows.h) --- 5,14 ---- # if CPPTL_USE_LINUX_ATOMIC # include <asm/atomic.h> + # elif CPPTL_USE_PTHREAD_ATOMIC + # include <pthread.h> # endif // Notes: linux implementation only guaranty a 24 bits range for the atomic integer. # if CPPTL_USE_WIN32_ATOMIC // Forwards declaration for WIN32 (avoid including windows.h) *************** *** 23,59 **** namespace CppTL { - # if CPPTL_HAS_VACPP_CHECKLOCK - typedef int AtomicCounterIntegerType; - typedef volatile AtomicCounterIntegerType AtomicCounterStorageType; - # elif CPPTL_USE_LINUX_ATOMIC - typedef long AtomicCounterIntegerType; - typedef atomic_t AtomicCounterStorageType; - # else - typedef long AtomicCounterIntegerType; - typedef volatile AtomicCounterIntegerType AtomicCounterStorageType; - # endif - - class CPPTL_API AtomicCounter : public NonCopyable - { - public: - AtomicCounter( AtomicCounterIntegerType count = 0 ); - - void increment(); - - /// Decrements the count - /// @returns true if count is non null. - bool decrement(); - - /// Should returns the current value of the count. - /// \warning this method is only defined for debugging and testing - /// purpose, you should not rely on it in production code path. - AtomicCounterIntegerType count() const; - - private: - AtomicCounterStorageType count_; - }; - - - // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// --- 22,25 ---- *************** *** 63,141 **** # if CPPTL_USE_WIN32_ATOMIC ! inline ! AtomicCounter::AtomicCounter( AtomicCounterIntegerType count ) ! : count_( count ) { ! } ! ! inline void ! AtomicCounter::increment() ! { ! InterlockedIncrement( const_cast< AtomicCounterIntegerType * >( &count_ ) ); ! } ! inline bool ! AtomicCounter::decrement() ! { ! return InterlockedDecrement( const_cast< AtomicCounterIntegerType * >( &count_ ) ) != 0; ! } - inline AtomicCounterIntegerType - AtomicCounter::count() const - { - return count_; - } // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// ! // AtomicCounter implementation for Visual Age C++ // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// ! #elif CPPTL_HAS_VACPP_CHECKLOCK ! ! // __check_lock( void *p, old_value, new_value ) ! // it is a compare and swap built-in. The implementation below spins ! // until the old_value matches the value we just read. ! inline ! AtomicCounter::AtomicCounter( AtomicCounterIntegerType count ) ! : count_( count ) { ! } ! ! inline void ! AtomicCounter::increment() ! { ! AtomicCounterIntegerType value; ! do ! value = count_; ! while ( __check_lock( &count_, value, value+1 ) ); ! } ! inline bool ! AtomicCounter::decrement() ! { ! AtomicCounterIntegerType value; ! do ! value = count_; ! while ( __check_lock( &count_, value, value-1 ) ); ! return value == 1; ! } ! inline AtomicCounterIntegerType ! AtomicCounter::count() const ! { ! AtomicCounterIntegerType value; ! do ! value = count_; ! while ( __check_lock( &count_, value, value ) ); ! return value; ! } // ////////////////////////////////////////////////////////////////// --- 29,118 ---- # if CPPTL_USE_WIN32_ATOMIC ! class CPPTL_API AtomicCounter : public NonCopyable { ! public: ! AtomicCounter( long count = 0 ) ! : count_( count ) ! { ! } ! void increment() ! { ! InterlockedIncrement( const_cast< long * >( &count_ ) ); ! } + /// Decrements the count + /// @returns true if count is non null. + bool decrement() + { + return InterlockedDecrement( const_cast< long * >( &count_ ) ) != 0; + } ! /// Should returns the current value of the count. ! /// \warning this method is only defined for debugging and testing ! /// purpose, you should not rely on it in production code path. ! long count() const ! { ! return count_; ! } + private: + volatile long count_; + }; // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// ! // AtomicCounter implementation using pthread mutex // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// ! #elif CPPTL_USE_PTHREAD_ATOMIC ! class CPPTL_API AtomicCounter : public NonCopyable { ! public: ! AtomicCounter( long count = 0 ) ! : count_( count ) ! { ! pthread_mutex_init( &lock_, 0 ); ! } ! ~AtomicCounter() ! { ! pthread_mutex_destroy( &lock_ ); ! } + void increment() + { + pthread_mutex_lock( &lock_ ); + ++count_; + pthread_mutex_unlock( &lock_ ); + } ! /// Decrements the count ! /// @returns true if count is non null. ! bool decrement() ! { ! pthread_mutex_lock( &lock_ ); ! bool isNotNull = --count_ != 0; ! pthread_mutex_unlock( &lock_ ); ! return isNotNull; ! } + /// Should returns the current value of the count. + /// \warning this method is only defined for debugging and testing + /// purpose, you should not rely on it in production code path. + long count() const + { + pthread_mutex_lock( &lock_ ); + long value = count_; + pthread_mutex_unlock( &lock_ ); + return value; + } ! private: ! mutable pthread_mutex_t lock_; ! volatile long count_; ! }; // ////////////////////////////////////////////////////////////////// *************** *** 146,176 **** #elif CPPTL_USE_LINUX_ATOMIC ! inline ! AtomicCounter::AtomicCounter( AtomicCounterIntegerType count ) { ! atomic_t value = ATOMIC_INIT(count); ! count_ = value; ! } ! inline void ! AtomicCounter::increment() ! { ! atomic_inc( &count_ ); ! } ! inline bool ! AtomicCounter::decrement() ! { ! return !atomic_dec_and_test( &count_ ); ! } ! inline AtomicCounterIntegerType ! AtomicCounter::count() const ! { ! return atomic_read( &count_ ); ! } --- 123,157 ---- #elif CPPTL_USE_LINUX_ATOMIC ! ! class CPPTL_API AtomicCounter : public NonCopyable { ! public: ! AtomicCounter( long count = 0 ) ! { ! atomic_t value = ATOMIC_INIT(count); ! count_ = value; ! } ! void increment() ! { ! atomic_inc( &count_ ); ! } ! bool decrement() ! { ! return !atomic_dec_and_test( &count_ ); ! } ! long count() const ! { ! return atomic_read( &count_ ); ! } ! ! private: ! atomic_t count_; ! }; *************** *** 182,211 **** #else ! inline ! AtomicCounter::AtomicCounter( AtomicCounterIntegerType count ) ! : count_( count ) ! { ! } ! ! ! inline AtomicCounterIntegerType ! AtomicCounter::increment() { ! return ++count_; ! } ! inline AtomicCounterIntegerType ! AtomicCounter::decrement() ! { ! return --count_; ! } ! inline AtomicCounterIntegerType ! AtomicCounter::count() const ! { ! return count_; ! } # endif // ifdef CPPTL_USE_WIN32_ATOMIC --- 163,192 ---- #else ! class CPPTL_API AtomicCounter : public NonCopyable { ! public: ! AtomicCounter( long count = 0 ); ! : count_( count ) ! { ! } + void increment() + { + return ++count_; + } ! bool decrement() ! { ! return --count_ != 0; ! } + long count() const + { + return count_; + } ! private: ! long count_; ! }; # endif // ifdef CPPTL_USE_WIN32_ATOMIC |