condvar.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #pragma once
  2. #include "mutex.h"
  3. #include <util/generic/ptr.h>
  4. #include <util/generic/noncopyable.h>
  5. #include <util/datetime/base.h>
  6. #include <utility>
  7. class TCondVar {
  8. public:
  9. TCondVar();
  10. ~TCondVar();
  11. void BroadCast() noexcept;
  12. void Signal() noexcept;
  13. /*
  14. * returns false if failed by timeout
  15. */
  16. bool WaitD(TMutex& m, TInstant deadline) noexcept;
  17. template <typename P>
  18. inline bool WaitD(TMutex& m, TInstant deadline, P pred) noexcept {
  19. while (!pred()) {
  20. if (!WaitD(m, deadline)) {
  21. return pred();
  22. }
  23. }
  24. return true;
  25. }
  26. /*
  27. * returns false if failed by timeout
  28. */
  29. inline bool WaitT(TMutex& m, TDuration timeout) noexcept {
  30. return WaitD(m, timeout.ToDeadLine());
  31. }
  32. template <typename P>
  33. inline bool WaitT(TMutex& m, TDuration timeout, P pred) noexcept {
  34. return WaitD(m, timeout.ToDeadLine(), std::move(pred));
  35. }
  36. /*
  37. * infinite wait
  38. */
  39. inline void WaitI(TMutex& m) noexcept {
  40. WaitD(m, TInstant::Max());
  41. }
  42. template <typename P>
  43. inline void WaitI(TMutex& m, P pred) noexcept {
  44. WaitD(m, TInstant::Max(), std::move(pred));
  45. }
  46. // deprecated
  47. inline void Wait(TMutex& m) noexcept {
  48. WaitI(m);
  49. }
  50. template <typename P>
  51. inline void Wait(TMutex& m, P pred) noexcept {
  52. WaitI(m, std::move(pred));
  53. }
  54. private:
  55. class TImpl;
  56. THolder<TImpl> Impl_;
  57. };