atomic_intrusive_ptr.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #pragma once
  2. #include "intrusive_ptr.h"
  3. namespace NYT {
  4. ////////////////////////////////////////////////////////////////////////////////
  5. // Atomic ptr based on https://github.com/facebook/folly/blob/main/folly/concurrency/AtomicSharedPtr.h
  6. // Operators * and -> for TAtomicIntrusivePtr are useless because it is not safe to work with atomic ptr such way
  7. // Safe usage is to convert to TIntrusivePtr.
  8. // Max TAtomicIntrusivePtr count per object is (2**16 = 2**32 / 2**16).
  9. template <class T>
  10. class TAtomicIntrusivePtr
  11. {
  12. public:
  13. TAtomicIntrusivePtr() = default;
  14. TAtomicIntrusivePtr(std::nullptr_t);
  15. explicit TAtomicIntrusivePtr(TIntrusivePtr<T> other);
  16. TAtomicIntrusivePtr(TAtomicIntrusivePtr&& other);
  17. ~TAtomicIntrusivePtr();
  18. TAtomicIntrusivePtr& operator=(TIntrusivePtr<T> other);
  19. TAtomicIntrusivePtr& operator=(std::nullptr_t);
  20. TIntrusivePtr<T> Acquire() const;
  21. TIntrusivePtr<T> Exchange(TIntrusivePtr<T> other);
  22. void Store(TIntrusivePtr<T> other);
  23. void Reset();
  24. bool CompareAndSwap(void*& comparePtr, T* target);
  25. bool CompareAndSwap(void*& comparePtr, TIntrusivePtr<T> target);
  26. // Result is suitable only for comparison. Not dereference.
  27. void* Get() const;
  28. explicit operator bool() const;
  29. private:
  30. template <class U>
  31. friend bool operator==(const TAtomicIntrusivePtr<U>& lhs, const TIntrusivePtr<U>& rhs);
  32. template <class U>
  33. friend bool operator==(const TIntrusivePtr<U>& lhs, const TAtomicIntrusivePtr<U>& rhs);
  34. template <class U>
  35. friend bool operator!=(const TAtomicIntrusivePtr<U>& lhs, const TIntrusivePtr<U>& rhs);
  36. template <class U>
  37. friend bool operator!=(const TIntrusivePtr<U>& lhs, const TAtomicIntrusivePtr<U>& rhs);
  38. // Keeps packed pointer (localRefCount, objectPtr).
  39. // Atomic ptr holds N references, where N = ReservedRefCount - localRefCount.
  40. // LocalRefCount is incremented in Acquire method.
  41. // When localRefCount exceeds ReservedRefCount / 2 a new portion of refs are required globally.
  42. // This field is marked mutable in order to make Acquire const-qualified in accordance to its semantics.
  43. mutable std::atomic<void*> Ptr_ = nullptr;
  44. constexpr static int CounterBits = 64 - PtrBits;
  45. constexpr static int ReservedRefCount = (1 << CounterBits) - 1;
  46. // Consume ref if ownership is transferred.
  47. // AcquireObject(ptr.Release(), true)
  48. // AcquireObject(ptr.Get(), false)
  49. static void* AcquireObject(T* obj, bool consumeRef = false);
  50. static void ReleaseObject(void* packedPtr);
  51. static void DoRelease(T* obj, int refs);
  52. };
  53. ////////////////////////////////////////////////////////////////////////////////
  54. } // namespace NYT
  55. #define ATOMIC_INTRUSIVE_PTR_INL_H_
  56. #include "atomic_intrusive_ptr-inl.h"
  57. #undef ATOMIC_INTRUSIVE_PTR_INL_H_