atomic_intrusive_ptr.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. #include "intrusive_ptr.h"
  3. #include <util/system/compiler.h>
  4. #ifdef _lsan_enabled_
  5. #include <util/system/spinlock.h>
  6. #endif
  7. namespace NYT {
  8. ////////////////////////////////////////////////////////////////////////////////
  9. //! Atomic pointer with split reference counting.
  10. /*
  11. * \see https://github.com/facebook/folly/blob/main/folly/concurrency/AtomicSharedPtr.h
  12. */
  13. template <class T>
  14. class TAtomicIntrusivePtr
  15. {
  16. public:
  17. TAtomicIntrusivePtr() = default;
  18. TAtomicIntrusivePtr(std::nullptr_t);
  19. explicit TAtomicIntrusivePtr(TIntrusivePtr<T> other);
  20. TAtomicIntrusivePtr(TAtomicIntrusivePtr&& other);
  21. ~TAtomicIntrusivePtr();
  22. TAtomicIntrusivePtr& operator=(TIntrusivePtr<T> other);
  23. TAtomicIntrusivePtr& operator=(std::nullptr_t);
  24. TIntrusivePtr<T> Acquire() const;
  25. TIntrusivePtr<T> Exchange(TIntrusivePtr<T> other);
  26. void Store(TIntrusivePtr<T> other);
  27. void Reset();
  28. using TRawPtr = std::conditional_t<std::is_const_v<T>, const void*, void*>;
  29. bool CompareAndSwap(TRawPtr& comparePtr, T* target);
  30. bool CompareAndSwap(TRawPtr& comparePtr, TIntrusivePtr<T> target);
  31. //! Result is only suitable for comparison, not dereference.
  32. TRawPtr Get() const;
  33. explicit operator bool() const;
  34. private:
  35. template <class U>
  36. friend bool operator==(const TAtomicIntrusivePtr<U>& lhs, const TIntrusivePtr<U>& rhs);
  37. template <class U>
  38. friend bool operator==(const TIntrusivePtr<U>& lhs, const TAtomicIntrusivePtr<U>& rhs);
  39. template <class U>
  40. friend bool operator!=(const TAtomicIntrusivePtr<U>& lhs, const TIntrusivePtr<U>& rhs);
  41. template <class U>
  42. friend bool operator!=(const TIntrusivePtr<U>& lhs, const TAtomicIntrusivePtr<U>& rhs);
  43. #ifdef _lsan_enabled_
  44. ::TSpinLock Lock_;
  45. TIntrusivePtr<T> Ptr_;
  46. #else
  47. // Keeps packed pointer (localRefCount, objectPtr).
  48. // Atomic ptr holds N references, where N = ReservedRefCount - localRefCount.
  49. // LocalRefCount is incremented in Acquire method.
  50. // When localRefCount exceeds ReservedRefCount / 2 a new portion of refs are required globally.
  51. // This field is marked mutable in order to make Acquire const-qualified in accordance to its semantics.
  52. mutable std::atomic<TPackedPtr> Ptr_ = 0;
  53. constexpr static int CounterBits = PackedPtrTagBits;
  54. constexpr static int ReservedRefCount = (1 << CounterBits) - 1;
  55. // Consume ref if ownership is transferred.
  56. static TPackedPtr AcquireObject(T* obj, bool consumeRef = false);
  57. static void ReleaseObject(TPackedPtr packedPtr);
  58. static void DoRelease(T* obj, int refs);
  59. #endif
  60. };
  61. ////////////////////////////////////////////////////////////////////////////////
  62. } // namespace NYT
  63. #define ATOMIC_INTRUSIVE_PTR_INL_H_
  64. #include "atomic_intrusive_ptr-inl.h"
  65. #undef ATOMIC_INTRUSIVE_PTR_INL_H_