mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-11-19 17:00:23 +01:00
Atomic access added.
[SVN r44636]
This commit is contained in:
35
include/boost/memory_order.hpp
Normal file
35
include/boost/memory_order.hpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef BOOST_MEMORY_ORDER_HPP_INCLUDED
|
||||
#define BOOST_MEMORY_ORDER_HPP_INCLUDED
|
||||
|
||||
// MS compatible compilers support #pragma once
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
||||
# pragma once
|
||||
#endif
|
||||
|
||||
// boost/memory_order.hpp
|
||||
//
|
||||
// Defines enum boost::memory_order per the C++0x working draft
|
||||
//
|
||||
// Copyright (c) 2008 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)
|
||||
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
enum memory_order
|
||||
{
|
||||
memory_order_relaxed = 0,
|
||||
memory_order_acquire = 1,
|
||||
memory_order_release = 2,
|
||||
memory_order_acq_rel = 3, // acquire | release
|
||||
memory_order_seq_cst = 7, // acq_rel | 4
|
||||
};
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // #ifndef BOOST_MEMORY_ORDER_HPP_INCLUDED
|
||||
@@ -32,6 +32,11 @@
|
||||
#include <boost/detail/shared_count.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
|
||||
#if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
|
||||
#include <boost/detail/spinlock_pool.hpp>
|
||||
#include <boost/memory_order.hpp>
|
||||
#endif
|
||||
|
||||
#include <algorithm> // for std::swap
|
||||
#include <functional> // for std::less
|
||||
#include <typeinfo> // for std::bad_cast
|
||||
@@ -472,6 +477,65 @@ public:
|
||||
return pn.get_deleter( ti );
|
||||
}
|
||||
|
||||
// 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 )
|
||||
{
|
||||
boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( this );
|
||||
|
||||
sp.lock();
|
||||
swap( r );
|
||||
sp.unlock();
|
||||
|
||||
return r; // return std::move(r)
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user