mutex.h 850 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #pragma once
  2. #include "impl.h"
  3. #include "events.h"
  4. class TContMutex {
  5. public:
  6. TContMutex() noexcept
  7. : Token_(true)
  8. {
  9. }
  10. ~TContMutex() {
  11. Y_ASSERT(Token_);
  12. }
  13. int LockD(TCont* current, TInstant deadline) {
  14. while (!Token_) {
  15. const int ret = WaitQueue_.WaitD(current, deadline);
  16. if (ret != EWAKEDUP) {
  17. return ret;
  18. }
  19. }
  20. Token_ = false;
  21. return 0;
  22. }
  23. int LockT(TCont* current, TDuration timeout) {
  24. return LockD(current, timeout.ToDeadLine());
  25. }
  26. int LockI(TCont* current) {
  27. return LockD(current, TInstant::Max());
  28. }
  29. void UnLock() noexcept {
  30. Y_ASSERT(!Token_);
  31. Token_ = true;
  32. WaitQueue_.Signal();
  33. }
  34. private:
  35. TContWaitQueue WaitQueue_;
  36. bool Token_;
  37. };