Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r52456 - in trunk: boost/smart_ptr/detail libs/smart_ptr/test
From: pdimov_at_[hidden]
Date: 2009-04-17 16:24:02


Author: pdimov
Date: 2009-04-17 16:24:01 EDT (Fri, 17 Apr 2009)
New Revision: 52456
URL: http://svn.boost.org/trac/boost/changeset/52456

Log:
Make ++a, where a is an atomic_count, return the new value.
Added:
   trunk/libs/smart_ptr/test/atomic_count_test2.cpp (contents, props changed)
Text files modified:
   trunk/boost/smart_ptr/detail/atomic_count.hpp | 5 ++---
   trunk/boost/smart_ptr/detail/atomic_count_gcc.hpp | 10 +++++-----
   trunk/boost/smart_ptr/detail/atomic_count_gcc_x86.hpp | 11 ++---------
   trunk/boost/smart_ptr/detail/atomic_count_pthreads.hpp | 4 ++--
   trunk/boost/smart_ptr/detail/atomic_count_sync.hpp | 4 ++--
   trunk/libs/smart_ptr/test/Jamfile.v2 | 1 +
   6 files changed, 14 insertions(+), 21 deletions(-)

Modified: trunk/boost/smart_ptr/detail/atomic_count.hpp
==============================================================================
--- trunk/boost/smart_ptr/detail/atomic_count.hpp (original)
+++ trunk/boost/smart_ptr/detail/atomic_count.hpp 2009-04-17 16:24:01 EDT (Fri, 17 Apr 2009)
@@ -31,13 +31,12 @@
 // ++a;
 //
 // Effects: Atomically increments the value of a
-// Returns: nothing
+// Returns: (long) the new value of a
 //
 // --a;
 //
 // Effects: Atomically decrements the value of a
-// Returns: (long) zero if the new value of a is zero,
-// unspecified non-zero value otherwise (usually the new value)
+// Returns: (long) the new value of a
 //
 // Important note: when --a returns zero, it must act as a
 // read memory barrier (RMB); i.e. the calling thread must

Modified: trunk/boost/smart_ptr/detail/atomic_count_gcc.hpp
==============================================================================
--- trunk/boost/smart_ptr/detail/atomic_count_gcc.hpp (original)
+++ trunk/boost/smart_ptr/detail/atomic_count_gcc.hpp 2009-04-17 16:24:01 EDT (Fri, 17 Apr 2009)
@@ -40,21 +40,21 @@
 {
 public:
 
- explicit atomic_count(long v) : value_(v) {}
+ explicit atomic_count( long v ) : value_( v ) {}
 
- void operator++()
+ long operator++()
     {
- __atomic_add(&value_, 1);
+ return __exchange_and_add( &value_, +1 ) + 1;
     }
 
     long operator--()
     {
- return __exchange_and_add(&value_, -1) - 1;
+ return __exchange_and_add( &value_, -1 ) - 1;
     }
 
     operator long() const
     {
- return __exchange_and_add(&value_, 0);
+ return __exchange_and_add( &value_, 0 );
     }
 
 private:

Modified: trunk/boost/smart_ptr/detail/atomic_count_gcc_x86.hpp
==============================================================================
--- trunk/boost/smart_ptr/detail/atomic_count_gcc_x86.hpp (original)
+++ trunk/boost/smart_ptr/detail/atomic_count_gcc_x86.hpp 2009-04-17 16:24:01 EDT (Fri, 17 Apr 2009)
@@ -25,16 +25,9 @@
 
     explicit atomic_count( long v ) : value_( static_cast< int >( v ) ) {}
 
- void operator++()
+ long operator++()
     {
- __asm__
- (
- "lock\n\t"
- "incl %0":
- "+m"( value_ ): // output (%0)
- : // inputs
- "cc" // clobbers
- );
+ return atomic_exchange_and_add( &value_, +1 ) + 1;
     }
 
     long operator--()

Modified: trunk/boost/smart_ptr/detail/atomic_count_pthreads.hpp
==============================================================================
--- trunk/boost/smart_ptr/detail/atomic_count_pthreads.hpp (original)
+++ trunk/boost/smart_ptr/detail/atomic_count_pthreads.hpp 2009-04-17 16:24:01 EDT (Fri, 17 Apr 2009)
@@ -62,10 +62,10 @@
         pthread_mutex_destroy(&mutex_);
     }
 
- void operator++()
+ long operator++()
     {
         scoped_lock lock(mutex_);
- ++value_;
+ return ++value_;
     }
 
     long operator--()

Modified: trunk/boost/smart_ptr/detail/atomic_count_sync.hpp
==============================================================================
--- trunk/boost/smart_ptr/detail/atomic_count_sync.hpp (original)
+++ trunk/boost/smart_ptr/detail/atomic_count_sync.hpp 2009-04-17 16:24:01 EDT (Fri, 17 Apr 2009)
@@ -31,9 +31,9 @@
 
     explicit atomic_count( long v ) : value_( v ) {}
 
- void operator++()
+ long operator++()
     {
- __sync_add_and_fetch( &value_, 1 );
+ return __sync_add_and_fetch( &value_, 1 );
     }
 
     long operator--()

Modified: trunk/libs/smart_ptr/test/Jamfile.v2
==============================================================================
--- trunk/libs/smart_ptr/test/Jamfile.v2 (original)
+++ trunk/libs/smart_ptr/test/Jamfile.v2 2009-04-17 16:24:01 EDT (Fri, 17 Apr 2009)
@@ -58,5 +58,6 @@
           [ run sp_recursive_assign2_rv_test.cpp ]
           [ run esft_constructor_test.cpp ]
           [ compile-fail auto_ptr_lv_fail.cpp ]
+ [ run atomic_count_test2.cpp ]
         ;
 }

Added: trunk/libs/smart_ptr/test/atomic_count_test2.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/smart_ptr/test/atomic_count_test2.cpp 2009-04-17 16:24:01 EDT (Fri, 17 Apr 2009)
@@ -0,0 +1,55 @@
+//
+// atomic_count_test2.cpp
+//
+// Copyright 2009 Peter Dimov
+//
+// Distributed under the Boost Software License, Version 1.0.
+// See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt
+//
+
+#include <boost/detail/atomic_count.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+int main()
+{
+ boost::detail::atomic_count n( 4 );
+
+ BOOST_TEST( n == 4 );
+
+ BOOST_TEST( ++n == 5 );
+ BOOST_TEST( ++n == 6 );
+
+ BOOST_TEST( n == 6 );
+
+ BOOST_TEST( --n == 5 );
+ BOOST_TEST( --n == 4 );
+
+ BOOST_TEST( n == 4 );
+
+ boost::detail::atomic_count m( 0 );
+
+ BOOST_TEST( m == 0 );
+
+ BOOST_TEST( ++m == 1 );
+ BOOST_TEST( ++m == 2 );
+
+ BOOST_TEST( m == 2 );
+
+ BOOST_TEST( --m == 1 );
+ BOOST_TEST( --m == 0 );
+
+ BOOST_TEST( m == 0 );
+
+ BOOST_TEST( --m == -1 );
+ BOOST_TEST( --m == -2 );
+
+ BOOST_TEST( m == -2 );
+
+ BOOST_TEST( ++m == -1 );
+ BOOST_TEST( ++m == 0 );
+
+ BOOST_TEST( m == 0 );
+
+ return boost::report_errors();
+}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk