atomic_shared_ptr.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #ifndef BOOST_SMART_PTR_ATOMIC_SHARED_PTR_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_ATOMIC_SHARED_PTR_HPP_INCLUDED
  3. //
  4. // atomic_shared_ptr.hpp
  5. //
  6. // Copyright 2017 Peter Dimov
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See
  9. // accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. //
  12. // See http://www.boost.org/libs/smart_ptr/ for documentation.
  13. //
  14. #include <boost/smart_ptr/shared_ptr.hpp>
  15. #include <boost/smart_ptr/detail/spinlock.hpp>
  16. #include <cstring>
  17. namespace boost
  18. {
  19. template<class T> class atomic_shared_ptr
  20. {
  21. private:
  22. boost::shared_ptr<T> p_;
  23. mutable boost::detail::spinlock l_;
  24. atomic_shared_ptr(const atomic_shared_ptr&);
  25. atomic_shared_ptr& operator=(const atomic_shared_ptr&);
  26. private:
  27. bool compare_exchange( shared_ptr<T>& v, shared_ptr<T> w ) BOOST_SP_NOEXCEPT
  28. {
  29. l_.lock();
  30. if( p_._internal_equiv( v ) )
  31. {
  32. p_.swap( w );
  33. l_.unlock();
  34. return true;
  35. }
  36. else
  37. {
  38. shared_ptr<T> tmp( p_ );
  39. l_.unlock();
  40. tmp.swap( v );
  41. return false;
  42. }
  43. }
  44. public:
  45. #if !defined( BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX ) && !defined( BOOST_NO_CXX11_CONSTEXPR )
  46. constexpr atomic_shared_ptr() BOOST_SP_NOEXCEPT: l_ BOOST_DETAIL_SPINLOCK_INIT
  47. {
  48. }
  49. #else
  50. atomic_shared_ptr() BOOST_SP_NOEXCEPT
  51. {
  52. boost::detail::spinlock init = BOOST_DETAIL_SPINLOCK_INIT;
  53. std::memcpy( &l_, &init, sizeof( init ) );
  54. }
  55. #endif
  56. atomic_shared_ptr( shared_ptr<T> p ) BOOST_SP_NOEXCEPT
  57. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  58. : p_( std::move( p ) )
  59. #else
  60. : p_( p )
  61. #endif
  62. {
  63. boost::detail::spinlock init = BOOST_DETAIL_SPINLOCK_INIT;
  64. std::memcpy( &l_, &init, sizeof( init ) );
  65. }
  66. atomic_shared_ptr& operator=( shared_ptr<T> r ) BOOST_SP_NOEXCEPT
  67. {
  68. boost::detail::spinlock::scoped_lock lock( l_ );
  69. p_.swap( r );
  70. return *this;
  71. }
  72. BOOST_CONSTEXPR bool is_lock_free() const BOOST_SP_NOEXCEPT
  73. {
  74. return false;
  75. }
  76. shared_ptr<T> load() const BOOST_SP_NOEXCEPT
  77. {
  78. boost::detail::spinlock::scoped_lock lock( l_ );
  79. return p_;
  80. }
  81. template<class M> shared_ptr<T> load( M ) const BOOST_SP_NOEXCEPT
  82. {
  83. boost::detail::spinlock::scoped_lock lock( l_ );
  84. return p_;
  85. }
  86. operator shared_ptr<T>() const BOOST_SP_NOEXCEPT
  87. {
  88. boost::detail::spinlock::scoped_lock lock( l_ );
  89. return p_;
  90. }
  91. void store( shared_ptr<T> r ) BOOST_SP_NOEXCEPT
  92. {
  93. boost::detail::spinlock::scoped_lock lock( l_ );
  94. p_.swap( r );
  95. }
  96. template<class M> void store( shared_ptr<T> r, M ) BOOST_SP_NOEXCEPT
  97. {
  98. boost::detail::spinlock::scoped_lock lock( l_ );
  99. p_.swap( r );
  100. }
  101. shared_ptr<T> exchange( shared_ptr<T> r ) BOOST_SP_NOEXCEPT
  102. {
  103. {
  104. boost::detail::spinlock::scoped_lock lock( l_ );
  105. p_.swap( r );
  106. }
  107. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  108. return std::move( r );
  109. #else
  110. return r;
  111. #endif
  112. }
  113. template<class M> shared_ptr<T> exchange( shared_ptr<T> r, M ) BOOST_SP_NOEXCEPT
  114. {
  115. {
  116. boost::detail::spinlock::scoped_lock lock( l_ );
  117. p_.swap( r );
  118. }
  119. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  120. return std::move( r );
  121. #else
  122. return r;
  123. #endif
  124. }
  125. template<class M> bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, M, M ) BOOST_SP_NOEXCEPT
  126. {
  127. return compare_exchange( v, w );
  128. }
  129. template<class M> bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, M ) BOOST_SP_NOEXCEPT
  130. {
  131. return compare_exchange( v, w );
  132. }
  133. bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w ) BOOST_SP_NOEXCEPT
  134. {
  135. return compare_exchange( v, w );
  136. }
  137. template<class M> bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, M, M ) BOOST_SP_NOEXCEPT
  138. {
  139. return compare_exchange( v, w );
  140. }
  141. template<class M> bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, M ) BOOST_SP_NOEXCEPT
  142. {
  143. return compare_exchange( v, w );
  144. }
  145. bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w ) BOOST_SP_NOEXCEPT
  146. {
  147. return compare_exchange( v, w );
  148. }
  149. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  150. template<class M> bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, M, M ) BOOST_SP_NOEXCEPT
  151. {
  152. return compare_exchange( v, std::move( w ) );
  153. }
  154. template<class M> bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, M ) BOOST_SP_NOEXCEPT
  155. {
  156. return compare_exchange( v, std::move( w ) );
  157. }
  158. bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w ) BOOST_SP_NOEXCEPT
  159. {
  160. return compare_exchange( v, std::move( w ) );
  161. }
  162. template<class M> bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, M, M ) BOOST_SP_NOEXCEPT
  163. {
  164. return compare_exchange( v, std::move( w ) );
  165. }
  166. template<class M> bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, M ) BOOST_SP_NOEXCEPT
  167. {
  168. return compare_exchange( v, std::move( w ) );
  169. }
  170. bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w ) BOOST_SP_NOEXCEPT
  171. {
  172. return compare_exchange( v, std::move( w ) );
  173. }
  174. #endif
  175. };
  176. } // namespace boost
  177. #endif // #ifndef BOOST_SMART_PTR_ATOMIC_SHARED_PTR_HPP_INCLUDED