Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:08:50

0001 #ifndef BOOST_SMART_PTR_ATOMIC_SHARED_PTR_HPP_INCLUDED
0002 #define BOOST_SMART_PTR_ATOMIC_SHARED_PTR_HPP_INCLUDED
0003 
0004 //
0005 //  atomic_shared_ptr.hpp
0006 //
0007 //  Copyright 2017 Peter Dimov
0008 //
0009 //  Distributed under the Boost Software License, Version 1.0. (See
0010 //  accompanying file LICENSE_1_0.txt or copy at
0011 //  http://www.boost.org/LICENSE_1_0.txt)
0012 //
0013 //  See http://www.boost.org/libs/smart_ptr/ for documentation.
0014 //
0015 
0016 #include <boost/smart_ptr/shared_ptr.hpp>
0017 #include <boost/smart_ptr/detail/spinlock.hpp>
0018 #include <cstring>
0019 
0020 namespace boost
0021 {
0022 
0023 template<class T> class atomic_shared_ptr
0024 {
0025 private:
0026 
0027     boost::shared_ptr<T> p_;
0028 
0029     mutable boost::detail::spinlock l_;
0030 
0031     atomic_shared_ptr(const atomic_shared_ptr&);
0032     atomic_shared_ptr& operator=(const atomic_shared_ptr&);
0033 
0034 private:
0035 
0036     bool compare_exchange( shared_ptr<T>& v, shared_ptr<T> w ) noexcept
0037     {
0038         l_.lock();
0039 
0040         if( p_._internal_equiv( v ) )
0041         {
0042             p_.swap( w );
0043 
0044             l_.unlock();
0045             return true;
0046         }
0047         else
0048         {
0049             shared_ptr<T> tmp( p_ );
0050 
0051             l_.unlock();
0052 
0053             tmp.swap( v );
0054             return false;
0055         }
0056     }
0057 
0058 public:
0059 
0060     constexpr atomic_shared_ptr() noexcept: l_ BOOST_DETAIL_SPINLOCK_INIT
0061     {
0062     }
0063 
0064     atomic_shared_ptr( shared_ptr<T> p ) noexcept
0065         : p_( std::move( p ) ), l_ BOOST_DETAIL_SPINLOCK_INIT
0066     {
0067     }
0068 
0069     atomic_shared_ptr& operator=( shared_ptr<T> r ) noexcept
0070     {
0071         boost::detail::spinlock::scoped_lock lock( l_ );
0072         p_.swap( r );
0073 
0074         return *this;
0075     }
0076 
0077     constexpr bool is_lock_free() const noexcept
0078     {
0079         return false;
0080     }
0081 
0082     shared_ptr<T> load() const noexcept
0083     {
0084         boost::detail::spinlock::scoped_lock lock( l_ );
0085         return p_;
0086     }
0087 
0088     template<class M> shared_ptr<T> load( M ) const noexcept
0089     {
0090         boost::detail::spinlock::scoped_lock lock( l_ );
0091         return p_;
0092     }
0093 
0094     operator shared_ptr<T>() const noexcept
0095     {
0096         boost::detail::spinlock::scoped_lock lock( l_ );
0097         return p_;
0098     }
0099 
0100     void store( shared_ptr<T> r ) noexcept
0101     {
0102         boost::detail::spinlock::scoped_lock lock( l_ );
0103         p_.swap( r );
0104     }
0105 
0106     template<class M> void store( shared_ptr<T> r, M ) noexcept
0107     {
0108         boost::detail::spinlock::scoped_lock lock( l_ );
0109         p_.swap( r );
0110     }
0111 
0112     shared_ptr<T> exchange( shared_ptr<T> r ) noexcept
0113     {
0114         {
0115             boost::detail::spinlock::scoped_lock lock( l_ );
0116             p_.swap( r );
0117         }
0118 
0119         return std::move( r );
0120     }
0121 
0122     template<class M> shared_ptr<T> exchange( shared_ptr<T> r, M ) noexcept
0123     {
0124         {
0125             boost::detail::spinlock::scoped_lock lock( l_ );
0126             p_.swap( r );
0127         }
0128 
0129         return std::move( r );
0130     }
0131 
0132     template<class M> bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, M, M ) noexcept
0133     {
0134         return compare_exchange( v, w );
0135     }
0136 
0137     template<class M> bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, M ) noexcept
0138     {
0139         return compare_exchange( v, w );
0140     }
0141 
0142     bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w ) noexcept
0143     {
0144         return compare_exchange( v, w );
0145     }
0146 
0147     template<class M> bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, M, M ) noexcept
0148     {
0149         return compare_exchange( v, w );
0150     }
0151 
0152     template<class M> bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, M ) noexcept
0153     {
0154         return compare_exchange( v, w );
0155     }
0156 
0157     bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w ) noexcept
0158     {
0159         return compare_exchange( v, w );
0160     }
0161 
0162     template<class M> bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, M, M ) noexcept
0163     {
0164         return compare_exchange( v, std::move( w ) );
0165     }
0166 
0167     template<class M> bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, M ) noexcept
0168     {
0169         return compare_exchange( v, std::move( w ) );
0170     }
0171 
0172     bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w ) noexcept
0173     {
0174         return compare_exchange( v, std::move( w ) );
0175     }
0176 
0177     template<class M> bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, M, M ) noexcept
0178     {
0179         return compare_exchange( v, std::move( w ) );
0180     }
0181 
0182     template<class M> bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, M ) noexcept
0183     {
0184         return compare_exchange( v, std::move( w ) );
0185     }
0186 
0187     bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w ) noexcept
0188     {
0189         return compare_exchange( v, std::move( w ) );
0190     }
0191 };
0192 
0193 } // namespace boost
0194 
0195 #endif  // #ifndef BOOST_SMART_PTR_ATOMIC_SHARED_PTR_HPP_INCLUDED