atomic_intrusive_ptr-inl.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. if (Ptr_.compare_exchange_weak(ptr, TTaggedPtr(obj, newLocalRefs).Pack())) {
  58. if (Y_UNLIKELY(newLocalRefs > ReservedRefCount / 2)) {
  59. Ref(obj, ReservedRefCount / 2);
  60. // Decrease local ref count.
  61. while (true) {
  62. auto [currentObj, localRefs] = TTaggedPtr<T>::Unpack(ptr);
  63. if (currentObj != obj || localRefs <= ReservedRefCount / 2) {
  64. Unref(obj, ReservedRefCount / 2);
  65. break;
  66. }
  67. if (Ptr_.compare_exchange_weak(ptr, TTaggedPtr(obj, localRefs - ReservedRefCount / 2).Pack())) {
  68. break;
  69. }
  70. }
  71. }
  72. return TIntrusivePtr<T>(obj, false);
  73. }
  74. }
  75. }
  76. template <class T>
  77. TIntrusivePtr<T> TAtomicIntrusivePtr<T>::Exchange(TIntrusivePtr<T> other)
  78. {
  79. auto [obj, localRefs] = TTaggedPtr<T>::Unpack(Ptr_.exchange(AcquireObject(other.Release(), true)));
  80. DoRelease(obj, localRefs + 1);
  81. return TIntrusivePtr<T>(obj, false);
  82. }
  83. template <class T>
  84. void TAtomicIntrusivePtr<T>::Store(TIntrusivePtr<T> other)
  85. {
  86. ReleaseObject(Ptr_.exchange(AcquireObject(other.Release(), true)));
  87. }
  88. template <class T>
  89. void TAtomicIntrusivePtr<T>::Reset()
  90. {
  91. ReleaseObject(Ptr_.exchange(0));
  92. }
  93. template <class T>
  94. bool TAtomicIntrusivePtr<T>::CompareAndSwap(TRawPtr& comparePtr, T* target)
  95. {
  96. auto* targetPtr = AcquireObject(target, false);
  97. auto currentPtr = Ptr_.load();
  98. if (UnpackPointer<T>(currentPtr).Ptr == comparePtr && Ptr_.compare_exchange_strong(currentPtr, targetPtr)) {
  99. ReleaseObject(currentPtr);
  100. return true;
  101. }
  102. comparePtr = UnpackPointer<T>(currentPtr).Ptr;
  103. ReleaseObject(targetPtr);
  104. return false;
  105. }
  106. template <class T>
  107. bool TAtomicIntrusivePtr<T>::CompareAndSwap(TRawPtr& comparePtr, TIntrusivePtr<T> target)
  108. {
  109. // TODO(lukyan): Make helper for packed owning ptr?
  110. auto targetPtr = AcquireObject(target.Release(), true);
  111. auto currentPtr = Ptr_.load();
  112. if (TTaggedPtr<T>::Unpack(currentPtr).Ptr == comparePtr && Ptr_.compare_exchange_strong(currentPtr, targetPtr)) {
  113. ReleaseObject(currentPtr);
  114. return true;
  115. }
  116. comparePtr = TTaggedPtr<T>::Unpack(currentPtr).Ptr;
  117. ReleaseObject(targetPtr);
  118. return false;
  119. }
  120. template <class T>
  121. typename TAtomicIntrusivePtr<T>::TRawPtr TAtomicIntrusivePtr<T>::Get() const
  122. {
  123. return TTaggedPtr<void>::Unpack(Ptr_.load()).Ptr;
  124. }
  125. template <class T>
  126. TAtomicIntrusivePtr<T>::operator bool() const
  127. {
  128. return Get();
  129. }
  130. template <class T>
  131. TPackedPtr TAtomicIntrusivePtr<T>::AcquireObject(T* obj, bool consumeRef)
  132. {
  133. if (obj) {
  134. Ref(obj, static_cast<int>(ReservedRefCount - consumeRef));
  135. }
  136. return TTaggedPtr(obj).Pack();
  137. }
  138. template <class T>
  139. void TAtomicIntrusivePtr<T>::ReleaseObject(TPackedPtr packedPtr)
  140. {
  141. auto [obj, localRefs] = TTaggedPtr<T>::Unpack(packedPtr);
  142. DoRelease(obj, localRefs);
  143. }
  144. template <class T>
  145. void TAtomicIntrusivePtr<T>::DoRelease(T* obj, int refs)
  146. {
  147. if (obj) {
  148. Unref(obj, static_cast<int>(ReservedRefCount - refs));
  149. }
  150. }
  151. ////////////////////////////////////////////////////////////////////////////////
  152. template <class T>
  153. bool operator==(const TAtomicIntrusivePtr<T>& lhs, const TIntrusivePtr<T>& rhs)
  154. {
  155. return lhs.Get() == rhs.Get();
  156. }
  157. template <class T>
  158. bool operator==(const TIntrusivePtr<T>& lhs, const TAtomicIntrusivePtr<T>& rhs)
  159. {
  160. return lhs.Get() == rhs.Get();
  161. }
  162. template <class T>
  163. bool operator!=(const TAtomicIntrusivePtr<T>& lhs, const TIntrusivePtr<T>& rhs)
  164. {
  165. return lhs.Get() != rhs.Get();
  166. }
  167. template <class T>
  168. bool operator!=(const TIntrusivePtr<T>& lhs, const TAtomicIntrusivePtr<T>& rhs)
  169. {
  170. return lhs.Get() != rhs.Get();
  171. }
  172. ////////////////////////////////////////////////////////////////////////////////
  173. } // namespace NYT