file_lock.h 573 B

12345678910111213141516171819202122232425262728293031323334
  1. #pragma once
  2. #include <util/generic/fwd.h>
  3. #include <util/generic/noncopyable.h>
  4. #include <util/system/file.h>
  5. enum class EFileLockType {
  6. Exclusive,
  7. Shared
  8. };
  9. class TFileLock: public TFile {
  10. public:
  11. TFileLock(const TString& filename, const EFileLockType type = EFileLockType::Exclusive);
  12. void Acquire();
  13. bool TryAcquire();
  14. void Release();
  15. inline void lock() {
  16. Acquire();
  17. }
  18. inline bool try_lock() {
  19. return TryAcquire();
  20. }
  21. inline void unlock() {
  22. Release();
  23. }
  24. private:
  25. EFileLockType Type;
  26. };