atomic_shared_ptr.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 ) 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. constexpr atomic_shared_ptr() noexcept: l_ BOOST_DETAIL_SPINLOCK_INIT
  46. {
  47. }
  48. atomic_shared_ptr( shared_ptr<T> p ) noexcept
  49. : p_( std::move( p ) ), l_ BOOST_DETAIL_SPINLOCK_INIT
  50. {
  51. }
  52. atomic_shared_ptr& operator=( shared_ptr<T> r ) noexcept
  53. {
  54. boost::detail::spinlock::scoped_lock lock( l_ );
  55. p_.swap( r );
  56. return *this;
  57. }
  58. constexpr bool is_lock_free() const noexcept
  59. {
  60. return false;
  61. }
  62. shared_ptr<T> load() const noexcept
  63. {
  64. boost::detail::spinlock::scoped_lock lock( l_ );
  65. return p_;
  66. }
  67. template<class M> shared_ptr<T> load( M ) const noexcept
  68. {
  69. boost::detail::spinlock::scoped_lock lock( l_ );
  70. return p_;
  71. }
  72. operator shared_ptr<T>() const noexcept
  73. {
  74. boost::detail::spinlock::scoped_lock lock( l_ );
  75. return p_;
  76. }
  77. void store( shared_ptr<T> r ) noexcept
  78. {
  79. boost::detail::spinlock::scoped_lock lock( l_ );
  80. p_.swap( r );
  81. }
  82. template<class M> void store( shared_ptr<T> r, M ) noexcept
  83. {
  84. boost::detail::spinlock::scoped_lock lock( l_ );
  85. p_.swap( r );
  86. }
  87. shared_ptr<T> exchange( shared_ptr<T> r ) noexcept
  88. {
  89. {
  90. boost::detail::spinlock::scoped_lock lock( l_ );
  91. p_.swap( r );
  92. }
  93. return std::move( r );
  94. }
  95. template<class M> shared_ptr<T> exchange( shared_ptr<T> r, M ) noexcept
  96. {
  97. {
  98. boost::detail::spinlock::scoped_lock lock( l_ );
  99. p_.swap( r );
  100. }
  101. return std::move( r );
  102. }
  103. template<class M> bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, M, M ) noexcept
  104. {
  105. return compare_exchange( v, w );
  106. }
  107. template<class M> bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, M ) noexcept
  108. {
  109. return compare_exchange( v, w );
  110. }
  111. bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w ) noexcept
  112. {
  113. return compare_exchange( v, w );
  114. }
  115. template<class M> bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, M, M ) noexcept
  116. {
  117. return compare_exchange( v, w );
  118. }
  119. template<class M> bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, M ) noexcept
  120. {
  121. return compare_exchange( v, w );
  122. }
  123. bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w ) noexcept
  124. {
  125. return compare_exchange( v, w );
  126. }
  127. template<class M> bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, M, M ) noexcept
  128. {
  129. return compare_exchange( v, std::move( w ) );
  130. }
  131. template<class M> bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, M ) noexcept
  132. {
  133. return compare_exchange( v, std::move( w ) );
  134. }
  135. bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w ) noexcept
  136. {
  137. return compare_exchange( v, std::move( w ) );
  138. }
  139. template<class M> bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, M, M ) noexcept
  140. {
  141. return compare_exchange( v, std::move( w ) );
  142. }
  143. template<class M> bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, M ) noexcept
  144. {
  145. return compare_exchange( v, std::move( w ) );
  146. }
  147. bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w ) noexcept
  148. {
  149. return compare_exchange( v, std::move( w ) );
  150. }
  151. };
  152. } // namespace boost
  153. #endif // #ifndef BOOST_SMART_PTR_ATOMIC_SHARED_PTR_HPP_INCLUDED