fork_aware_spin_lock.h 983 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #pragma once
  2. #include "public.h"
  3. #include "spin_lock.h"
  4. #include <util/system/src_location.h>
  5. namespace NYT::NThreading {
  6. ////////////////////////////////////////////////////////////////////////////////
  7. //! Wraps TSpinLock and additionally acquires a global fork lock (in read mode)
  8. //! preventing concurrent forks from happening.
  9. class TForkAwareSpinLock
  10. {
  11. public:
  12. TForkAwareSpinLock(const TForkAwareSpinLock&) = delete;
  13. TForkAwareSpinLock& operator =(const TForkAwareSpinLock&) = delete;
  14. constexpr TForkAwareSpinLock() = default;
  15. // TODO(babenko): make use of location.
  16. explicit constexpr TForkAwareSpinLock(const ::TSourceLocation& /*location*/)
  17. { }
  18. void Acquire() noexcept;
  19. bool TryAcquire() noexcept;
  20. void Release() noexcept;
  21. bool IsLocked() const noexcept;
  22. private:
  23. ::NYT::NThreading::TSpinLock SpinLock_;
  24. };
  25. ////////////////////////////////////////////////////////////////////////////////
  26. } // namespace NYT::NThreading