Changeset 44636


Ignore:
Timestamp:
Apr 20, 2008, 2:59:12 PM (10 years ago)
Author:
Peter Dimov
Message:

Atomic access added.

Location:
trunk
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/boost/shared_ptr.hpp

    r44595 r44636  
    3333#include <boost/detail/workaround.hpp>
    3434
     35#if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
     36#include <boost/detail/spinlock_pool.hpp>
     37#include <boost/memory_order.hpp>
     38#endif
     39
    3540#include <algorithm>            // for std::swap
    3641#include <functional>           // for std::less
     
    473478    }
    474479
     480    // atomic access
     481
     482#if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
     483
     484    shared_ptr<T> atomic_load( memory_order /*mo*/ = memory_order_seq_cst ) const
     485    {
     486        boost::detail::spinlock_pool<2>::scoped_lock lock( this );
     487        return *this;
     488    }
     489
     490    void atomic_store( shared_ptr<T> r, memory_order /*mo*/ = memory_order_seq_cst )
     491    {
     492        boost::detail::spinlock_pool<2>::scoped_lock lock( this );
     493        swap( r );
     494    }
     495
     496    shared_ptr<T> atomic_swap( shared_ptr<T> r, memory_order /*mo*/ = memory_order_seq_cst )
     497    {
     498        boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( this );
     499
     500        sp.lock();
     501        swap( r );
     502        sp.unlock();
     503
     504        return r; // return std::move(r)
     505    }
     506
     507    bool atomic_compare_swap( shared_ptr<T> & v, shared_ptr<T> w )
     508    {
     509        boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( this );
     510
     511        sp.lock();
     512
     513        if( px == v.px && pn == v.pn )
     514        {
     515            swap( w );
     516
     517            sp.unlock();
     518
     519            return true;
     520        }
     521        else
     522        {
     523            shared_ptr tmp( *this );
     524
     525            sp.unlock();
     526
     527            tmp.swap( v );
     528            return false;
     529        }
     530    }
     531
     532    inline bool atomic_compare_swap( shared_ptr<T> & v, shared_ptr<T> w, memory_order /*success*/, memory_order /*failure*/ )
     533    {
     534        return atomic_compare_swap( v, w ); // std::move( w )
     535    }
     536
     537#endif
     538
    475539// Tasteless as this may seem, making all members public allows member templates
    476540// to work in the absence of member template friends. (Matthew Langston)
  • trunk/libs/smart_ptr/test/Jamfile.v2

    r44353 r44636  
    4545          [ run spinlock_pool_test.cpp ]
    4646          [ run sp_accept_owner_test.cpp ]
     47          [ run sp_atomic_test.cpp ]
    4748        ;
    4849}
Note: See TracChangeset for help on using the changeset viewer.