atomic_shared_ptr.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. atomic_shared_ptr( shared_ptr<T> p ) BOOST_SP_NOEXCEPT
  50. : p_( std::move( p ) ), l_ BOOST_DETAIL_SPINLOCK_INIT
  51. {
  52. }
  53. #else
  54. atomic_shared_ptr() BOOST_SP_NOEXCEPT
  55. {
  56. boost::detail::spinlock init = BOOST_DETAIL_SPINLOCK_INIT;
  57. std::memcpy( &l_, &init, sizeof( init ) );
  58. }
  59. atomic_shared_ptr( shared_ptr<T> p ) BOOST_SP_NOEXCEPT
  60. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  61. : p_( std::move( p ) )
  62. #else
  63. : p_( p )
  64. #endif
  65. {
  66. boost::detail::spinlock init = BOOST_DETAIL_SPINLOCK_INIT;
  67. std::memcpy( &l_, &init, sizeof( init ) );
  68. }
  69. #endif
  70. atomic_shared_ptr& operator=( shared_ptr<T> r ) BOOST_SP_NOEXCEPT
  71. {
  72. boost::detail::spinlock::scoped_lock lock( l_ );
  73. p_.swap( r );
  74. return *this;
  75. }
  76. BOOST_CONSTEXPR bool is_lock_free() const BOOST_SP_NOEXCEPT
  77. {
  78. return false;
  79. }
  80. shared_ptr<T> load() const BOOST_SP_NOEXCEPT
  81. {
  82. boost::detail::spinlock::scoped_lock lock( l_ );
  83. return p_;
  84. }
  85. template<class M> shared_ptr<T> load( M ) const BOOST_SP_NOEXCEPT
  86. {
  87. boost::detail::spinlock::scoped_lock lock( l_ );
  88. return p_;
  89. }
  90. operator shared_ptr<T>() const BOOST_SP_NOEXCEPT
  91. {
  92. boost::detail::spinlock::scoped_lock lock( l_ );
  93. return p_;
  94. }
  95. void store( shared_ptr<T> r ) BOOST_SP_NOEXCEPT
  96. {
  97. boost::detail::spinlock::scoped_lock lock( l_ );
  98. p_.swap( r );
  99. }
  100. template<class M> void store( shared_ptr<T> r, M ) BOOST_SP_NOEXCEPT
  101. {
  102. boost::detail::spinlock::scoped_lock lock( l_ );
  103. p_.swap( r );
  104. }
  105. shared_ptr<T> exchange( shared_ptr<T> r ) BOOST_SP_NOEXCEPT
  106. {
  107. {
  108. boost::detail::spinlock::scoped_lock lock( l_ );
  109. p_.swap( r );
  110. }
  111. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  112. return std::move( r );
  113. #else
  114. return r;
  115. #endif
  116. }
  117. template<class M> shared_ptr<T> exchange( shared_ptr<T> r, M ) BOOST_SP_NOEXCEPT
  118. {
  119. {
  120. boost::detail::spinlock::scoped_lock lock( l_ );
  121. p_.swap( r );
  122. }
  123. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  124. return std::move( r );
  125. #else
  126. return r;
  127. #endif
  128. }
  129. template<class M> bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, M, M ) BOOST_SP_NOEXCEPT
  130. {
  131. return compare_exchange( v, w );
  132. }
  133. template<class M> bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, M ) BOOST_SP_NOEXCEPT
  134. {
  135. return compare_exchange( v, w );
  136. }
  137. bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w ) 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, M ) BOOST_SP_NOEXCEPT
  142. {
  143. return compare_exchange( v, w );
  144. }
  145. template<class M> bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, M ) BOOST_SP_NOEXCEPT
  146. {
  147. return compare_exchange( v, w );
  148. }
  149. bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w ) BOOST_SP_NOEXCEPT
  150. {
  151. return compare_exchange( v, w );
  152. }
  153. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  154. template<class M> bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, M, M ) BOOST_SP_NOEXCEPT
  155. {
  156. return compare_exchange( v, std::move( w ) );
  157. }
  158. template<class M> bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, M ) BOOST_SP_NOEXCEPT
  159. {
  160. return compare_exchange( v, std::move( w ) );
  161. }
  162. bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w ) 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, M ) BOOST_SP_NOEXCEPT
  167. {
  168. return compare_exchange( v, std::move( w ) );
  169. }
  170. template<class M> bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, M ) BOOST_SP_NOEXCEPT
  171. {
  172. return compare_exchange( v, std::move( w ) );
  173. }
  174. bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w ) BOOST_SP_NOEXCEPT
  175. {
  176. return compare_exchange( v, std::move( w ) );
  177. }
  178. #endif
  179. };
  180. } // namespace boost
  181. #endif // #ifndef BOOST_SMART_PTR_ATOMIC_SHARED_PTR_HPP_INCLUDED