atomic_intrusive_ptr-inl.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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<T>::operator=(TIntrusivePtr<T> other)
  15. {
  16. Store(std::move(other));
  17. return *this;
  18. }
  19. template <class T>
  20. TAtomicIntrusivePtr<T>& TAtomicIntrusivePtr<T>::operator=(std::nullptr_t)
  21. {
  22. Reset();
  23. return *this;
  24. }
  25. template <class T>
  26. TAtomicIntrusivePtr<T>::operator bool() const
  27. {
  28. return Get();
  29. }
  30. #ifdef _lsan_enabled_
  31. template <class T>
  32. TAtomicIntrusivePtr<T>::TAtomicIntrusivePtr(TIntrusivePtr<T> other)
  33. : Ptr_(std::move(other))
  34. { }
  35. template <class T>
  36. TAtomicIntrusivePtr<T>::TAtomicIntrusivePtr(TAtomicIntrusivePtr&& other)
  37. : Ptr_(std::move(other))
  38. { }
  39. template <class T>
  40. TAtomicIntrusivePtr<T>::~TAtomicIntrusivePtr() = default;
  41. template <class T>
  42. TIntrusivePtr<T> TAtomicIntrusivePtr<T>::Acquire() const
  43. {
  44. auto guard = Guard(Lock_);
  45. return Ptr_;
  46. }
  47. template <class T>
  48. TIntrusivePtr<T> TAtomicIntrusivePtr<T>::Exchange(TIntrusivePtr<T> other)
  49. {
  50. auto guard = Guard(Lock_);
  51. Ptr_.Swap(other);
  52. return other;
  53. }
  54. template <class T>
  55. void TAtomicIntrusivePtr<T>::Store(TIntrusivePtr<T> other)
  56. {
  57. Exchange(std::move(other));
  58. }
  59. template <class T>
  60. void TAtomicIntrusivePtr<T>::Reset()
  61. {
  62. Exchange(nullptr);
  63. }
  64. template <class T>
  65. bool TAtomicIntrusivePtr<T>::CompareAndSwap(TRawPtr& comparePtr, T* target)
  66. {
  67. auto guard = Guard(Lock_);
  68. if (Ptr_.Get() != comparePtr) {
  69. comparePtr = Ptr_.Get();
  70. return false;
  71. }
  72. auto targetPtr = TIntrusivePtr<T>(target, /*addReference*/ false);
  73. Ptr_.Swap(targetPtr);
  74. guard.Release();
  75. // targetPtr will die here.
  76. return true;
  77. }
  78. template <class T>
  79. bool TAtomicIntrusivePtr<T>::CompareAndSwap(TRawPtr& comparePtr, TIntrusivePtr<T> target)
  80. {
  81. return CompareAndSwap(comparePtr, target.Release());
  82. }
  83. template <class T>
  84. typename TAtomicIntrusivePtr<T>::TRawPtr TAtomicIntrusivePtr<T>::Get() const
  85. {
  86. auto guard = Guard(Lock_);
  87. return Ptr_.Get();
  88. }
  89. #else
  90. template <class T>
  91. TAtomicIntrusivePtr<T>::TAtomicIntrusivePtr(TIntrusivePtr<T> other)
  92. : Ptr_(AcquireObject(other.Release(), /*consumeRef*/ true))
  93. { }
  94. template <class T>
  95. TAtomicIntrusivePtr<T>::TAtomicIntrusivePtr(TAtomicIntrusivePtr&& other)
  96. : Ptr_(other.Ptr_.load(std::memory_order::relaxed))
  97. {
  98. other.Ptr_.store(nullptr, std::memory_order::relaxed);
  99. }
  100. template <class T>
  101. TAtomicIntrusivePtr<T>::~TAtomicIntrusivePtr()
  102. {
  103. ReleaseObject(Ptr_.load());
  104. }
  105. template <class T>
  106. TIntrusivePtr<T> TAtomicIntrusivePtr<T>::Acquire() const
  107. {
  108. auto ptr = Ptr_.load();
  109. while (true) {
  110. auto [obj, localRefs] = TTaggedPtr<T>::Unpack(ptr);
  111. if (!obj) {
  112. return {};
  113. }
  114. YT_VERIFY(localRefs < ReservedRefCount);
  115. auto newLocalRefs = localRefs + 1;
  116. if (newLocalRefs == ReservedRefCount) {
  117. SpinLockPause();
  118. ptr = Ptr_.load();
  119. continue;
  120. }
  121. // Can not Ref(obj) here because it can be destroyed.
  122. auto newPtr = TTaggedPtr(obj, newLocalRefs).Pack();
  123. if (Ptr_.compare_exchange_weak(ptr, newPtr)) {
  124. ptr = newPtr;
  125. if (Y_UNLIKELY(newLocalRefs > ReservedRefCount / 2)) {
  126. Ref(obj, ReservedRefCount / 2);
  127. // Decrease local ref count.
  128. while (true) {
  129. auto [currentObj, localRefs] = TTaggedPtr<T>::Unpack(ptr);
  130. if (currentObj != obj || localRefs <= ReservedRefCount / 2) {
  131. Unref(obj, ReservedRefCount / 2);
  132. break;
  133. }
  134. if (Ptr_.compare_exchange_weak(ptr, TTaggedPtr(obj, localRefs - ReservedRefCount / 2).Pack())) {
  135. break;
  136. }
  137. }
  138. }
  139. return TIntrusivePtr<T>(obj, false);
  140. }
  141. }
  142. }
  143. template <class T>
  144. TIntrusivePtr<T> TAtomicIntrusivePtr<T>::Exchange(TIntrusivePtr<T> other)
  145. {
  146. auto [obj, localRefs] = TTaggedPtr<T>::Unpack(Ptr_.exchange(AcquireObject(other.Release(), /*consumeRef*/ true)));
  147. DoRelease(obj, localRefs + 1);
  148. return TIntrusivePtr<T>(obj, false);
  149. }
  150. template <class T>
  151. void TAtomicIntrusivePtr<T>::Store(TIntrusivePtr<T> other)
  152. {
  153. ReleaseObject(Ptr_.exchange(AcquireObject(other.Release(), /*consumeRef*/ true)));
  154. }
  155. template <class T>
  156. void TAtomicIntrusivePtr<T>::Reset()
  157. {
  158. ReleaseObject(Ptr_.exchange(0));
  159. }
  160. template <class T>
  161. bool TAtomicIntrusivePtr<T>::CompareAndSwap(TRawPtr& comparePtr, T* target)
  162. {
  163. auto* targetPtr = AcquireObject(target, /*consumeRef*/ false);
  164. auto currentPtr = Ptr_.load();
  165. if (UnpackPointer<T>(currentPtr).Ptr == comparePtr && Ptr_.compare_exchange_strong(currentPtr, targetPtr)) {
  166. ReleaseObject(currentPtr);
  167. return true;
  168. }
  169. comparePtr = UnpackPointer<T>(currentPtr).Ptr;
  170. ReleaseObject(targetPtr);
  171. return false;
  172. }
  173. template <class T>
  174. bool TAtomicIntrusivePtr<T>::CompareAndSwap(TRawPtr& comparePtr, TIntrusivePtr<T> target)
  175. {
  176. // TODO(lukyan): Make helper for packed owning ptr?
  177. auto targetPtr = AcquireObject(target.Release(), /*consumeRef*/ true);
  178. auto currentPtr = Ptr_.load();
  179. if (TTaggedPtr<T>::Unpack(currentPtr).Ptr == comparePtr && Ptr_.compare_exchange_strong(currentPtr, targetPtr)) {
  180. ReleaseObject(currentPtr);
  181. return true;
  182. }
  183. comparePtr = TTaggedPtr<T>::Unpack(currentPtr).Ptr;
  184. ReleaseObject(targetPtr);
  185. return false;
  186. }
  187. template <class T>
  188. typename TAtomicIntrusivePtr<T>::TRawPtr TAtomicIntrusivePtr<T>::Get() const
  189. {
  190. return TTaggedPtr<void>::Unpack(Ptr_.load()).Ptr;
  191. }
  192. template <class T>
  193. TPackedPtr TAtomicIntrusivePtr<T>::AcquireObject(T* obj, bool consumeRef)
  194. {
  195. if (obj) {
  196. Ref(obj, ReservedRefCount - static_cast<int>(consumeRef));
  197. }
  198. return TTaggedPtr(obj).Pack();
  199. }
  200. template <class T>
  201. void TAtomicIntrusivePtr<T>::ReleaseObject(TPackedPtr packedPtr)
  202. {
  203. auto [obj, localRefs] = TTaggedPtr<T>::Unpack(packedPtr);
  204. DoRelease(obj, localRefs);
  205. }
  206. template <class T>
  207. void TAtomicIntrusivePtr<T>::DoRelease(T* obj, int refs)
  208. {
  209. if (obj) {
  210. Unref(obj, ReservedRefCount - refs);
  211. }
  212. }
  213. #endif
  214. ////////////////////////////////////////////////////////////////////////////////
  215. template <class T>
  216. bool operator==(const TAtomicIntrusivePtr<T>& lhs, const TIntrusivePtr<T>& rhs)
  217. {
  218. return lhs.Get() == rhs.Get();
  219. }
  220. template <class T>
  221. bool operator==(const TIntrusivePtr<T>& lhs, const TAtomicIntrusivePtr<T>& rhs)
  222. {
  223. return lhs.Get() == rhs.Get();
  224. }
  225. template <class T>
  226. bool operator!=(const TAtomicIntrusivePtr<T>& lhs, const TIntrusivePtr<T>& rhs)
  227. {
  228. return lhs.Get() != rhs.Get();
  229. }
  230. template <class T>
  231. bool operator!=(const TIntrusivePtr<T>& lhs, const TAtomicIntrusivePtr<T>& rhs)
  232. {
  233. return lhs.Get() != rhs.Get();
  234. }
  235. ////////////////////////////////////////////////////////////////////////////////
  236. } // namespace NYT