[Cppunit-cvs] cppunit2/include/cpptl atomiccounter.h,1.2,1.3 config.h,1.14,1.15 thread.h,1.1,1.2
Brought to you by:
blep
From: Baptiste L. <bl...@us...> - 2005-03-06 20:12:41
|
Update of /cvsroot/cppunit/cppunit2/include/cpptl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4453/include/cpptl Modified Files: atomiccounter.h config.h thread.h Log Message: * added automatic detection of pthread support * added atomic counter implementation for linux Index: config.h =================================================================== RCS file: /cvsroot/cppunit/cppunit2/include/cpptl/config.h,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** config.h 6 Mar 2005 18:35:14 -0000 1.14 --- config.h 6 Mar 2005 20:12:31 -0000 1.15 *************** *** 66,69 **** --- 66,78 ---- # if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) # define CPPTL_USE_WIN32_THREAD 1 + # elif defined(linux) || defined(__linux) || defined(__linux__) + # define CPPTL_USE_PTHREAD_THREAD 1 + # define CPPTL_USE_LINUX_ATOMIC 1 + # elif defined(sun) || defined(__sun) + # define CPPTL_USE_PTHREAD_THREAD 1 + # elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) + # define CPPTL_USE_PTHREAD_THREAD 1 + # elif defined(__IBMCPP__) + # define CPPTL_USE_PTHREAD_THREAD 1 # endif Index: thread.h =================================================================== RCS file: /cvsroot/cppunit/cppunit2/include/cpptl/thread.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** thread.h 6 Mar 2005 18:36:03 -0000 1.1 --- thread.h 6 Mar 2005 20:12:31 -0000 1.2 *************** *** 18,21 **** --- 18,22 ---- + // Non-recursive mutex class CPPTL_API Mutex : public NonCopyable { Index: atomiccounter.h =================================================================== RCS file: /cvsroot/cppunit/cppunit2/include/cpptl/atomiccounter.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** atomiccounter.h 26 Feb 2005 11:53:08 -0000 1.2 --- atomiccounter.h 6 Mar 2005 20:12:31 -0000 1.3 *************** *** 3,6 **** --- 3,14 ---- # include <cpptl/config.h> + # 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 # ifdef WIN32 // Forwards declaration for WIN32 (avoid including windows.h) *************** *** 15,36 **** namespace CppTL { class CPPTL_API AtomicCounter : public NonCopyable { public: ! AtomicCounter( long count = 0 ); ! long increment(); /// Decrements the count ! /// @returns 0 if the count is null, non zero otherwise. ! long 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. ! long count() const; private: ! volatile long count_; }; --- 23,55 ---- namespace CppTL { + # if CPPTL_HAS_VACPP_CHECKLOCK + typedef int AtomicCounterIntegerType; + typedef AtomicCounterIntegerType AtomicCounterStorageType; + # elif CPPTL_USE_LINUX_ATOMIC + typedef long AtomicCounterIntegerType; + typedef atomic_t AtomicCounterStorageType; + # else + typedef long AtomicCounterIntegerType; + typedef 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: ! volatile AtomicCounterStorageType count_; }; *************** *** 45,49 **** inline ! AtomicCounter::AtomicCounter( long count ) : count_( count ) { --- 64,68 ---- inline ! AtomicCounter::AtomicCounter( AtomicCounterIntegerType count ) : count_( count ) { *************** *** 51,69 **** ! inline long AtomicCounter::increment() { ! return InterlockedIncrement( const_cast< long * >( &count_ ) ); } ! inline long AtomicCounter::decrement() { ! return InterlockedDecrement( const_cast< long * >( &count_ ) ); } ! inline long AtomicCounter::count() const { --- 70,88 ---- ! 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 { *************** *** 71,74 **** --- 90,176 ---- } + // ////////////////////////////////////////////////////////////////// + // ////////////////////////////////////////////////////////////////// + // 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; + } + + // ////////////////////////////////////////////////////////////////// + // ////////////////////////////////////////////////////////////////// + // AtomicCounter implementation for Linux + // ////////////////////////////////////////////////////////////////// + // ////////////////////////////////////////////////////////////////// + #elif CPPTL_USE_LINUX_ATOMIC + + inline + AtomicCounter::AtomicCounter( AtomicCounterIntegerType count ) + : count_( ATOMIC_INIT(count) ) + { + } + + + 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_ ); + } + // ////////////////////////////////////////////////////////////////// *************** *** 80,84 **** inline ! AtomicCounter::AtomicCounter( long count ) : count_( count ) { --- 182,186 ---- inline ! AtomicCounter::AtomicCounter( AtomicCounterIntegerType count ) : count_( count ) { *************** *** 86,90 **** ! inline long AtomicCounter::increment() { --- 188,192 ---- ! inline AtomicCounterIntegerType AtomicCounter::increment() { *************** *** 93,97 **** ! inline long AtomicCounter::decrement() { --- 195,199 ---- ! inline AtomicCounterIntegerType AtomicCounter::decrement() { *************** *** 100,104 **** ! inline long AtomicCounter::count() const { --- 202,206 ---- ! inline AtomicCounterIntegerType AtomicCounter::count() const { |