mutex.h 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #pragma once
  2. #include "guard.h"
  3. #include "defaults.h"
  4. #include <util/generic/ptr.h>
  5. #include <util/generic/noncopyable.h>
  6. class TFakeMutex: public TNonCopyable {
  7. public:
  8. inline void Acquire() noexcept {
  9. }
  10. inline bool TryAcquire() noexcept {
  11. return true;
  12. }
  13. inline void Release() noexcept {
  14. }
  15. inline void lock() noexcept {
  16. Acquire();
  17. }
  18. inline bool try_lock() noexcept {
  19. return TryAcquire();
  20. }
  21. inline void unlock() noexcept {
  22. Release();
  23. }
  24. ~TFakeMutex() = default;
  25. };
  26. class TMutex {
  27. public:
  28. TMutex();
  29. TMutex(TMutex&&) noexcept;
  30. ~TMutex();
  31. void Acquire() noexcept;
  32. bool TryAcquire() noexcept;
  33. void Release() noexcept;
  34. inline void lock() noexcept {
  35. Acquire();
  36. }
  37. inline bool try_lock() noexcept {
  38. return TryAcquire();
  39. }
  40. inline void unlock() noexcept {
  41. Release();
  42. }
  43. // return opaque pointer to real handler
  44. void* Handle() const noexcept;
  45. private:
  46. class TImpl;
  47. THolder<TImpl> Impl_;
  48. };