Boost logo

Boost-Commit :

From: pdimov_at_[hidden]
Date: 2008-07-12 12:07:21


Author: pdimov
Date: 2008-07-12 12:07:20 EDT (Sat, 12 Jul 2008)
New Revision: 47357
URL: http://svn.boost.org/trac/boost/changeset/47357

Log:
Updated atomic access syntax to match N2674 and the WD.
Text files modified:
   trunk/boost/shared_ptr.hpp | 139 +++++++++++++++++++++++----------------
   trunk/libs/smart_ptr/test/sp_atomic_mt2_test.cpp | 6
   trunk/libs/smart_ptr/test/sp_atomic_mt_test.cpp | 4
   trunk/libs/smart_ptr/test/sp_atomic_test.cpp | 22 +++---
   4 files changed, 98 insertions(+), 73 deletions(-)

Modified: trunk/boost/shared_ptr.hpp
==============================================================================
--- trunk/boost/shared_ptr.hpp (original)
+++ trunk/boost/shared_ptr.hpp 2008-07-12 12:07:20 EDT (Sat, 12 Jul 2008)
@@ -5,7 +5,7 @@
 // shared_ptr.hpp
 //
 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
-// Copyright (c) 2001-2007 Peter Dimov
+// Copyright (c) 2001-2008 Peter Dimov
 //
 // Distributed under the Boost Software License, Version 1.0. (See
 // accompanying file LICENSE_1_0.txt or copy at
@@ -516,65 +516,11 @@
         return pn < rhs.pn;
     }
 
- // atomic access
-
-#if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
-
- shared_ptr<T> atomic_load( memory_order /*mo*/ = memory_order_seq_cst ) const
- {
- boost::detail::spinlock_pool<2>::scoped_lock lock( this );
- return *this;
- }
-
- void atomic_store( shared_ptr<T> r, memory_order /*mo*/ = memory_order_seq_cst )
- {
- boost::detail::spinlock_pool<2>::scoped_lock lock( this );
- swap( r );
- }
-
- shared_ptr<T> atomic_swap( shared_ptr<T> r, memory_order /*mo*/ = memory_order_seq_cst )
+ bool _internal_equiv( shared_ptr const & r ) const
     {
- boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( this );
-
- sp.lock();
- swap( r );
- sp.unlock();
-
- return r; // return std::move(r)
+ return px == r.px && pn == r.pn;
     }
 
- bool atomic_compare_swap( shared_ptr<T> & v, shared_ptr<T> w )
- {
- boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( this );
-
- sp.lock();
-
- if( px == v.px && pn == v.pn )
- {
- swap( w );
-
- sp.unlock();
-
- return true;
- }
- else
- {
- shared_ptr tmp( *this );
-
- sp.unlock();
-
- tmp.swap( v );
- return false;
- }
- }
-
- inline bool atomic_compare_swap( shared_ptr<T> & v, shared_ptr<T> w, memory_order /*success*/, memory_order /*failure*/ )
- {
- return atomic_compare_swap( v, w ); // std::move( w )
- }
-
-#endif
-
 // Tasteless as this may seem, making all members public allows member templates
 // to work in the absence of member template friends. (Matthew Langston)
 
@@ -784,6 +730,85 @@
     return del;
 }
 
+// atomic access
+
+#if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
+
+template<class T> inline bool atomic_is_lock_free( shared_ptr<T> const * /*p*/ )
+{
+ return false;
+}
+
+template<class T> shared_ptr<T> atomic_load( shared_ptr<T> const * p )
+{
+ boost::detail::spinlock_pool<2>::scoped_lock lock( p );
+ return *p;
+}
+
+template<class T> inline shared_ptr<T> atomic_load_explicit( shared_ptr<T> const * p, memory_order /*mo*/ )
+{
+ return atomic_load( p );
+}
+
+template<class T> void atomic_store( shared_ptr<T> * p, shared_ptr<T> r )
+{
+ boost::detail::spinlock_pool<2>::scoped_lock lock( p );
+ p->swap( r );
+}
+
+template<class T> inline void atomic_store_explicit( shared_ptr<T> * p, shared_ptr<T> r, memory_order /*mo*/ )
+{
+ atomic_store( p, r ); // std::move( r )
+}
+
+template<class T> shared_ptr<T> atomic_exchange( shared_ptr<T> * p, shared_ptr<T> r )
+{
+ boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
+
+ sp.lock();
+ p->swap( r );
+ sp.unlock();
+
+ return r; // return std::move( r )
+}
+
+template<class T> shared_ptr<T> atomic_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> r, memory_order /*mo*/ )
+{
+ return atomic_exchange( p, r ); // std::move( r )
+}
+
+template<class T> bool atomic_compare_exchange( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w )
+{
+ boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
+
+ sp.lock();
+
+ if( p->_internal_equiv( *v ) )
+ {
+ p->swap( w );
+
+ sp.unlock();
+
+ return true;
+ }
+ else
+ {
+ shared_ptr<T> tmp( *p );
+
+ sp.unlock();
+
+ tmp.swap( *v );
+ return false;
+ }
+}
+
+template<class T> inline bool atomic_compare_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w, memory_order /*success*/, memory_order /*failure*/ )
+{
+ return atomic_compare_exchange( p, v, w ); // std::move( w )
+}
+
+#endif
+
 } // namespace boost
 
 #ifdef BOOST_MSVC

Modified: trunk/libs/smart_ptr/test/sp_atomic_mt2_test.cpp
==============================================================================
--- trunk/libs/smart_ptr/test/sp_atomic_mt2_test.cpp (original)
+++ trunk/libs/smart_ptr/test/sp_atomic_mt2_test.cpp 2008-07-12 12:07:20 EDT (Sat, 12 Jul 2008)
@@ -83,7 +83,7 @@
 
     case pt_atomics:
         {
- boost::shared_ptr<X> p2 = ps.atomic_load();
+ boost::shared_ptr<X> p2 = boost::atomic_load( &ps );
             return p2->get();
         }
     }
@@ -109,14 +109,14 @@
 
     case pt_atomics:
         {
- boost::shared_ptr<X> p1 = ps.atomic_load();
+ boost::shared_ptr<X> p1 = boost::atomic_load( &ps );
 
             for( ;; )
             {
                 boost::shared_ptr<X> p2( new X( *p1 ) );
                 p2->set();
 
- if( ps.atomic_compare_swap( p1, p2 ) ) break;
+ if( boost::atomic_compare_exchange( &ps, &p1, p2 ) ) break;
             }
         }
         break;

Modified: trunk/libs/smart_ptr/test/sp_atomic_mt_test.cpp
==============================================================================
--- trunk/libs/smart_ptr/test/sp_atomic_mt_test.cpp (original)
+++ trunk/libs/smart_ptr/test/sp_atomic_mt_test.cpp 2008-07-12 12:07:20 EDT (Sat, 12 Jul 2008)
@@ -93,7 +93,7 @@
 
 #else
 
- boost::shared_ptr<X> p2 = ps.atomic_load();
+ boost::shared_ptr<X> p2 = boost::atomic_load( &ps );
 
         s += p2->get();
 
@@ -134,7 +134,7 @@
         BOOST_TEST( p2->v_ == i );
         p2->set();
 
- ps.atomic_store( p2 );
+ boost::atomic_store( &ps, p2 );
 
 #endif
     }

Modified: trunk/libs/smart_ptr/test/sp_atomic_test.cpp
==============================================================================
--- trunk/libs/smart_ptr/test/sp_atomic_test.cpp (original)
+++ trunk/libs/smart_ptr/test/sp_atomic_test.cpp 2008-07-12 12:07:20 EDT (Sat, 12 Jul 2008)
@@ -25,31 +25,31 @@
     boost::shared_ptr<X> px( new X );
 
     {
- boost::shared_ptr<X> p2 = px.atomic_load();
+ boost::shared_ptr<X> p2 = boost::atomic_load( &px );
         BOOST_TEST_SP_EQ( p2, px );
 
         boost::shared_ptr<X> px2( new X );
- px.atomic_store( px2 );
+ boost::atomic_store( &px, px2 );
         BOOST_TEST_SP_EQ( px, px2 );
 
- p2 = px.atomic_load();
+ p2 = boost::atomic_load( &px );
         BOOST_TEST_SP_EQ( p2, px );
         BOOST_TEST_SP_EQ( p2, px2 );
 
         boost::shared_ptr<X> px3( new X );
- boost::shared_ptr<X> p3 = px.atomic_swap( px3 );
+ boost::shared_ptr<X> p3 = boost::atomic_exchange( &px, px3 );
         BOOST_TEST_SP_EQ( p3, px2 );
         BOOST_TEST_SP_EQ( px, px3 );
 
         boost::shared_ptr<X> px4( new X );
         boost::shared_ptr<X> cmp;
 
- bool r = px.atomic_compare_swap( cmp, px4 );
+ bool r = boost::atomic_compare_exchange( &px, &cmp, px4 );
         BOOST_TEST( !r );
         BOOST_TEST_SP_EQ( px, px3 );
         BOOST_TEST_SP_EQ( cmp, px3 );
 
- r = px.atomic_compare_swap( cmp, px4 );
+ r = boost::atomic_compare_exchange( &px, &cmp, px4 );
         BOOST_TEST( r );
         BOOST_TEST_SP_EQ( px, px4 );
     }
@@ -59,26 +59,26 @@
     px.reset();
 
     {
- boost::shared_ptr<X> p2 = px.atomic_load( boost::memory_order_acquire );
+ boost::shared_ptr<X> p2 = boost::atomic_load_explicit( &px, boost::memory_order_acquire );
         BOOST_TEST_SP_EQ( p2, px );
 
         boost::shared_ptr<X> px2( new X );
- px.atomic_store( px2, boost::memory_order_release );
+ boost::atomic_store_explicit( &px, px2, boost::memory_order_release );
         BOOST_TEST_SP_EQ( px, px2 );
 
- boost::shared_ptr<X> p3 = px.atomic_swap( boost::shared_ptr<X>(), boost::memory_order_acq_rel );
+ boost::shared_ptr<X> p3 = boost::atomic_exchange_explicit( &px, boost::shared_ptr<X>(), boost::memory_order_acq_rel );
         BOOST_TEST_SP_EQ( p3, px2 );
         BOOST_TEST_SP_EQ( px, p2 );
 
         boost::shared_ptr<X> px4( new X );
         boost::shared_ptr<X> cmp( px2 );
 
- bool r = px.atomic_compare_swap( cmp, px4, boost::memory_order_acquire, boost::memory_order_relaxed );
+ bool r = boost::atomic_compare_exchange_explicit( &px, &cmp, px4, boost::memory_order_acquire, boost::memory_order_relaxed );
         BOOST_TEST( !r );
         BOOST_TEST_SP_EQ( px, p2 );
         BOOST_TEST_SP_EQ( cmp, p2 );
 
- r = px.atomic_compare_swap( cmp, px4, boost::memory_order_release, boost::memory_order_acquire );
+ r = boost::atomic_compare_exchange_explicit( &px, &cmp, px4, boost::memory_order_release, boost::memory_order_acquire );
         BOOST_TEST( r );
         BOOST_TEST_SP_EQ( px, px4 );
     }


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