rwlock.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. #include "guard.h"
  3. #include "defaults.h"
  4. #include <util/generic/ptr.h>
  5. class TRWMutex {
  6. public:
  7. TRWMutex();
  8. ~TRWMutex();
  9. void AcquireRead() noexcept;
  10. bool TryAcquireRead() noexcept;
  11. void ReleaseRead() noexcept;
  12. void AcquireWrite() noexcept;
  13. bool TryAcquireWrite() noexcept;
  14. void ReleaseWrite() noexcept;
  15. void Release() noexcept;
  16. private:
  17. class TImpl;
  18. THolder<TImpl> Impl_;
  19. };
  20. template <class T>
  21. struct TReadGuardOps {
  22. static inline void Acquire(T* t) noexcept {
  23. t->AcquireRead();
  24. }
  25. static inline void Release(T* t) noexcept {
  26. t->ReleaseRead();
  27. }
  28. };
  29. template <class T>
  30. struct TTryReadGuardOps: public TReadGuardOps<T> {
  31. static inline bool TryAcquire(T* t) noexcept {
  32. return t->TryAcquireRead();
  33. }
  34. };
  35. template <class T>
  36. struct TWriteGuardOps {
  37. static inline void Acquire(T* t) noexcept {
  38. t->AcquireWrite();
  39. }
  40. static inline void Release(T* t) noexcept {
  41. t->ReleaseWrite();
  42. }
  43. };
  44. template <class T>
  45. struct TTryWriteGuardOps: public TWriteGuardOps<T> {
  46. static inline bool TryAcquire(T* t) noexcept {
  47. return t->TryAcquireWrite();
  48. }
  49. };
  50. template <class T>
  51. using TReadGuardBase = TGuard<T, TReadGuardOps<T>>;
  52. template <class T>
  53. using TTryReadGuardBase = TTryGuard<T, TTryReadGuardOps<T>>;
  54. template <class T>
  55. using TWriteGuardBase = TGuard<T, TWriteGuardOps<T>>;
  56. template <class T>
  57. using TTryWriteGuardBase = TTryGuard<T, TTryWriteGuardOps<T>>;
  58. using TReadGuard = TReadGuardBase<TRWMutex>;
  59. using TTryReadGuard = TTryReadGuardBase<TRWMutex>;
  60. using TWriteGuard = TWriteGuardBase<TRWMutex>;
  61. using TTryWriteGuard = TTryWriteGuardBase<TRWMutex>;