1 #ifndef BOOST_SMART_PTR_ATOMIC_SHARED_PTR_HPP_INCLUDED
2 #define BOOST_SMART_PTR_ATOMIC_SHARED_PTR_HPP_INCLUDED
5 // atomic_shared_ptr.hpp
7 // Copyright 2017 Peter Dimov
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
13 // See http://www.boost.org/libs/smart_ptr/ for documentation.
16 #include <boost/smart_ptr/shared_ptr.hpp>
17 #include <boost/smart_ptr/detail/spinlock.hpp>
23 template<class T> class atomic_shared_ptr
27 boost::shared_ptr<T> p_;
29 mutable boost::detail::spinlock l_;
31 atomic_shared_ptr(const atomic_shared_ptr&);
32 atomic_shared_ptr& operator=(const atomic_shared_ptr&);
36 bool compare_exchange( shared_ptr<T>& v, shared_ptr<T> w ) BOOST_SP_NOEXCEPT
40 if( p_._internal_equiv( v ) )
49 shared_ptr<T> tmp( p_ );
60 #if !defined( BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX ) && !defined( BOOST_NO_CXX11_CONSTEXPR )
62 constexpr atomic_shared_ptr() BOOST_SP_NOEXCEPT: l_ BOOST_DETAIL_SPINLOCK_INIT
68 atomic_shared_ptr() BOOST_SP_NOEXCEPT
70 boost::detail::spinlock init = BOOST_DETAIL_SPINLOCK_INIT;
71 std::memcpy( &l_, &init, sizeof( init ) );
76 atomic_shared_ptr( shared_ptr<T> p ) BOOST_SP_NOEXCEPT
77 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
78 : p_( std::move( p ) )
83 boost::detail::spinlock init = BOOST_DETAIL_SPINLOCK_INIT;
84 std::memcpy( &l_, &init, sizeof( init ) );
87 atomic_shared_ptr& operator=( shared_ptr<T> r ) BOOST_SP_NOEXCEPT
89 boost::detail::spinlock::scoped_lock lock( l_ );
95 BOOST_CONSTEXPR bool is_lock_free() const BOOST_SP_NOEXCEPT
100 shared_ptr<T> load() const BOOST_SP_NOEXCEPT
102 boost::detail::spinlock::scoped_lock lock( l_ );
106 template<class M> shared_ptr<T> load( M ) const BOOST_SP_NOEXCEPT
108 boost::detail::spinlock::scoped_lock lock( l_ );
112 operator shared_ptr<T>() const BOOST_SP_NOEXCEPT
114 boost::detail::spinlock::scoped_lock lock( l_ );
118 void store( shared_ptr<T> r ) BOOST_SP_NOEXCEPT
120 boost::detail::spinlock::scoped_lock lock( l_ );
124 template<class M> void store( shared_ptr<T> r, M ) BOOST_SP_NOEXCEPT
126 boost::detail::spinlock::scoped_lock lock( l_ );
130 shared_ptr<T> exchange( shared_ptr<T> r ) BOOST_SP_NOEXCEPT
133 boost::detail::spinlock::scoped_lock lock( l_ );
137 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
139 return std::move( r );
148 template<class M> shared_ptr<T> exchange( shared_ptr<T> r, M ) BOOST_SP_NOEXCEPT
151 boost::detail::spinlock::scoped_lock lock( l_ );
155 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
157 return std::move( r );
166 template<class M> bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, M, M ) BOOST_SP_NOEXCEPT
168 return compare_exchange( v, w );
171 template<class M> bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, M ) BOOST_SP_NOEXCEPT
173 return compare_exchange( v, w );
176 bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w ) BOOST_SP_NOEXCEPT
178 return compare_exchange( v, w );
181 template<class M> bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, M, M ) BOOST_SP_NOEXCEPT
183 return compare_exchange( v, w );
186 template<class M> bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, M ) BOOST_SP_NOEXCEPT
188 return compare_exchange( v, w );
191 bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w ) BOOST_SP_NOEXCEPT
193 return compare_exchange( v, w );
196 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
198 template<class M> bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, M, M ) BOOST_SP_NOEXCEPT
200 return compare_exchange( v, std::move( w ) );
203 template<class M> bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, M ) BOOST_SP_NOEXCEPT
205 return compare_exchange( v, std::move( w ) );
208 bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w ) BOOST_SP_NOEXCEPT
210 return compare_exchange( v, std::move( w ) );
213 template<class M> bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, M, M ) BOOST_SP_NOEXCEPT
215 return compare_exchange( v, std::move( w ) );
218 template<class M> bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, M ) BOOST_SP_NOEXCEPT
220 return compare_exchange( v, std::move( w ) );
223 bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w ) BOOST_SP_NOEXCEPT
225 return compare_exchange( v, std::move( w ) );
233 #endif // #ifndef BOOST_SMART_PTR_ATOMIC_SHARED_PTR_HPP_INCLUDED