extra_ref.h 611 B

123456789101112131415161718192021222324252627282930313233343536
  1. #pragma once
  2. #include <util/system/yassert.h>
  3. class TExtraRef {
  4. TAtomic Holds;
  5. public:
  6. TExtraRef()
  7. : Holds(false)
  8. {
  9. }
  10. ~TExtraRef() {
  11. Y_VERIFY(!Holds);
  12. }
  13. template <typename TThis>
  14. void Retain(TThis* thiz) {
  15. if (AtomicGet(Holds)) {
  16. return;
  17. }
  18. if (AtomicCas(&Holds, 1, 0)) {
  19. thiz->Ref();
  20. }
  21. }
  22. template <typename TThis>
  23. void Release(TThis* thiz) {
  24. if (!AtomicGet(Holds)) {
  25. return;
  26. }
  27. if (AtomicCas(&Holds, 0, 1)) {
  28. thiz->UnRef();
  29. }
  30. }
  31. };