atomic_intrusive_ptr-inl.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #ifndef ATOMIC_INTRUSIVE_PTR_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include atomic_intrusive_ptr.h"
  3. // For the sake of sane code completion.
  4. #include "atomic_intrusive_ptr.h"
  5. #endif
  6. #undef ATOMIC_INTRUSIVE_PTR_INL_H_
  7. #include <util/system/spinlock.h>
  8. namespace NYT {
  9. ////////////////////////////////////////////////////////////////////////////////
  10. template <class T>
  11. TAtomicIntrusivePtr<T>::TAtomicIntrusivePtr(std::nullptr_t)
  12. { }
  13. template <class T>
  14. TAtomicIntrusivePtr<T>::TAtomicIntrusivePtr(TIntrusivePtr<T> other)
  15. : Ptr_(AcquireObject(other.Release(), true))
  16. { }
  17. template <class T>
  18. TAtomicIntrusivePtr<T>::TAtomicIntrusivePtr(TAtomicIntrusivePtr&& other)
  19. : Ptr_(other.Ptr_.load(std::memory_order::relaxed))
  20. {
  21. other.Ptr_.store(nullptr, std::memory_order::relaxed);
  22. }
  23. template <class T>
  24. TAtomicIntrusivePtr<T>::~TAtomicIntrusivePtr()
  25. {
  26. ReleaseObject(Ptr_.load());
  27. }
  28. template <class T>
  29. TAtomicIntrusivePtr<T>& TAtomicIntrusivePtr<T>::operator=(TIntrusivePtr<T> other)
  30. {
  31. Store(std::move(other));
  32. return *this;
  33. }
  34. template <class T>
  35. TAtomicIntrusivePtr<T>& TAtomicIntrusivePtr<T>::operator=(std::nullptr_t)
  36. {
  37. Reset();
  38. return *this;
  39. }
  40. template <class T>
  41. TIntrusivePtr<T> TAtomicIntrusivePtr<T>::Acquire() const
  42. {
  43. auto ptr = Ptr_.load();
  44. while (true) {
  45. auto [obj, localRefs] = TTaggedPtr<T>::Unpack(ptr);
  46. if (!obj) {
  47. return {};
  48. }
  49. YT_VERIFY(localRefs < ReservedRefCount);
  50. auto newLocalRefs = localRefs + 1;
  51. if (newLocalRefs == ReservedRefCount) {
  52. SpinLockPause();
  53. ptr = Ptr_.load();
  54. continue;
  55. }
  56. // Can not Ref(obj) here because it can be destroyed.
  57. auto newPtr = TTaggedPtr(obj, newLocalRefs).Pack();
  58. if (Ptr_.compare_exchange_weak(ptr, newPtr)) {
  59. ptr = newPtr;
  60. if (Y_UNLIKELY(newLocalRefs > ReservedRefCount / 2)) {
  61. Ref(obj, ReservedRefCount / 2);
  62. // Decrease local ref count.
  63. while (true) {
  64. auto [currentObj, localRefs] = TTaggedPtr<T>::Unpack(ptr);
  65. if (currentObj != obj || localRefs <= ReservedRefCount / 2) {
  66. Unref(obj, ReservedRefCount / 2);
  67. break;
  68. }
  69. if (Ptr_.compare_exchange_weak(ptr, TTaggedPtr(obj, localRefs - ReservedRefCount / 2).Pack())) {
  70. break;
  71. }
  72. }
  73. }
  74. return TIntrusivePtr<T>(obj, false);
  75. }
  76. }
  77. }
  78. template <class T>
  79. TIntrusivePtr<T> TAtomicIntrusivePtr<T>::Exchange(TIntrusivePtr<T> other)
  80. {
  81. auto [obj, localRefs] = TTaggedPtr<T>::Unpack(Ptr_.exchange(AcquireObject(other.Release(), true)));
  82. DoRelease(obj, localRefs + 1);
  83. return TIntrusivePtr<T>(obj, false);
  84. }
  85. template <class T>
  86. void TAtomicIntrusivePtr<T>::Store(TIntrusivePtr<T> other)
  87. {
  88. ReleaseObject(Ptr_.exchange(AcquireObject(other.Release(), true)));
  89. }
  90. template <class T>
  91. void TAtomicIntrusivePtr<T>::Reset()
  92. {
  93. ReleaseObject(Ptr_.exchange(0));
  94. }
  95. template <class T>
  96. bool TAtomicIntrusivePtr<T>::CompareAndSwap(TRawPtr& comparePtr, T* target)
  97. {
  98. auto* targetPtr = AcquireObject(target, false);
  99. auto currentPtr = Ptr_.load();
  100. if (UnpackPointer<T>(currentPtr).Ptr == comparePtr && Ptr_.compare_exchange_strong(currentPtr, targetPtr)) {
  101. ReleaseObject(currentPtr);
  102. return true;
  103. }
  104. comparePtr = UnpackPointer<T>(currentPtr).Ptr;
  105. ReleaseObject(targetPtr);
  106. return false;
  107. }
  108. template <class T>
  109. bool TAtomicIntrusivePtr<T>::CompareAndSwap(TRawPtr& comparePtr, TIntrusivePtr<T> target)
  110. {
  111. // TODO(lukyan): Make helper for packed owning ptr?
  112. auto targetPtr = AcquireObject(target.Release(), true);
  113. auto currentPtr = Ptr_.load();
  114. if (TTaggedPtr<T>::Unpack(currentPtr).Ptr == comparePtr && Ptr_.compare_exchange_strong(currentPtr, targetPtr)) {
  115. ReleaseObject(currentPtr);
  116. return true;
  117. }
  118. comparePtr = TTaggedPtr<T>::Unpack(currentPtr).Ptr;
  119. ReleaseObject(targetPtr);
  120. return false;
  121. }
  122. template <class T>
  123. typename TAtomicIntrusivePtr<T>::TRawPtr TAtomicIntrusivePtr<T>::Get() const
  124. {
  125. return TTaggedPtr<void>::Unpack(Ptr_.load()).Ptr;
  126. }
  127. template <class T>
  128. TAtomicIntrusivePtr<T>::operator bool() const
  129. {
  130. return Get();
  131. }
  132. template <class T>
  133. TPackedPtr TAtomicIntrusivePtr<T>::AcquireObject(T* obj, bool consumeRef)
  134. {
  135. if (obj) {
  136. Ref(obj, static_cast<int>(ReservedRefCount - consumeRef));
  137. }
  138. return TTaggedPtr(obj).Pack();
  139. }
  140. template <class T>
  141. void TAtomicIntrusivePtr<T>::ReleaseObject(TPackedPtr packedPtr)
  142. {
  143. auto [obj, localRefs] = TTaggedPtr<T>::Unpack(packedPtr);
  144. DoRelease(obj, localRefs);
  145. }
  146. template <class T>
  147. void TAtomicIntrusivePtr<T>::DoRelease(T* obj, int refs)
  148. {
  149. if (obj) {
  150. Unref(obj, static_cast<int>(ReservedRefCount - refs));
  151. }
  152. }
  153. ////////////////////////////////////////////////////////////////////////////////
  154. template <class T>
  155. bool operator==(const TAtomicIntrusivePtr<T>& lhs, const TIntrusivePtr<T>& rhs)
  156. {
  157. return lhs.Get() == rhs.Get();
  158. }
  159. template <class T>
  160. bool operator==(const TIntrusivePtr<T>& lhs, const TAtomicIntrusivePtr<T>& rhs)
  161. {
  162. return lhs.Get() == rhs.Get();
  163. }
  164. template <class T>
  165. bool operator!=(const TAtomicIntrusivePtr<T>& lhs, const TIntrusivePtr<T>& rhs)
  166. {
  167. return lhs.Get() != rhs.Get();
  168. }
  169. template <class T>
  170. bool operator!=(const TIntrusivePtr<T>& lhs, const TAtomicIntrusivePtr<T>& rhs)
  171. {
  172. return lhs.Get() != rhs.Get();
  173. }
  174. ////////////////////////////////////////////////////////////////////////////////
  175. } // namespace NYT