wait_proxy.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "wait_proxy.h"
  2. #include <library/cpp/threading/future/future.h>
  3. #include <util/system/event.h>
  4. #include <util/system/condvar.h>
  5. namespace NYT {
  6. namespace NDetail {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. bool TDefaultWaitProxy::WaitFuture(const NThreading::TFuture<void>& future, TDuration timeout)
  9. {
  10. return future.Wait(timeout);
  11. }
  12. bool TDefaultWaitProxy::WaitEvent(TSystemEvent& event, TDuration timeout)
  13. {
  14. return event.WaitT(timeout);
  15. }
  16. bool TDefaultWaitProxy::WaitCondVar(TCondVar &condVar, TMutex &mutex, TDuration timeout)
  17. {
  18. return condVar.WaitT(mutex, timeout);
  19. }
  20. void TDefaultWaitProxy::Sleep(TDuration timeout)
  21. {
  22. ::Sleep(timeout);
  23. }
  24. ////////////////////////////////////////////////////////////////////////////////
  25. TWaitProxy::TWaitProxy()
  26. : Proxy_(::MakeIntrusive<TDefaultWaitProxy>())
  27. { }
  28. TWaitProxy* TWaitProxy::Get()
  29. {
  30. return Singleton<TWaitProxy>();
  31. }
  32. void TWaitProxy::SetProxy(::TIntrusivePtr<IWaitProxy> proxy)
  33. {
  34. Proxy_ = std::move(proxy);
  35. }
  36. bool TWaitProxy::WaitFuture(const NThreading::TFuture<void>& future)
  37. {
  38. return Proxy_->WaitFuture(future, TDuration::Max());
  39. }
  40. bool TWaitProxy::WaitFuture(const NThreading::TFuture<void>& future, TInstant deadLine)
  41. {
  42. return Proxy_->WaitFuture(future, deadLine - TInstant::Now());
  43. }
  44. bool TWaitProxy::WaitFuture(const NThreading::TFuture<void>& future, TDuration timeout)
  45. {
  46. return Proxy_->WaitFuture(future, timeout);
  47. }
  48. bool TWaitProxy::WaitEventD(TSystemEvent& event, TInstant deadLine)
  49. {
  50. return Proxy_->WaitEvent(event, deadLine - TInstant::Now());
  51. }
  52. bool TWaitProxy::WaitEventT(TSystemEvent& event, TDuration timeout)
  53. {
  54. return Proxy_->WaitEvent(event, timeout);
  55. }
  56. void TWaitProxy::WaitEventI(TSystemEvent& event)
  57. {
  58. Proxy_->WaitEvent(event, TDuration::Max());
  59. }
  60. bool TWaitProxy::WaitEvent(TSystemEvent& event)
  61. {
  62. return Proxy_->WaitEvent(event, TDuration::Max());
  63. }
  64. bool TWaitProxy::WaitCondVarD(TCondVar& condVar, TMutex& m, TInstant deadLine)
  65. {
  66. return Proxy_->WaitCondVar(condVar, m, deadLine - TInstant::Now());
  67. }
  68. bool TWaitProxy::WaitCondVarT(TCondVar& condVar, TMutex& m, TDuration timeOut)
  69. {
  70. return Proxy_->WaitCondVar(condVar, m, timeOut);
  71. }
  72. void TWaitProxy::WaitCondVarI(TCondVar& condVar, TMutex& m)
  73. {
  74. Proxy_->WaitCondVar(condVar, m, TDuration::Max());
  75. }
  76. void TWaitProxy::WaitCondVar(TCondVar& condVar, TMutex& m)
  77. {
  78. Proxy_->WaitCondVar(condVar, m, TDuration::Max());
  79. }
  80. void TWaitProxy::Sleep(TDuration timeout)
  81. {
  82. Proxy_->Sleep(timeout);
  83. }
  84. void TWaitProxy::SleepUntil(TInstant instant)
  85. {
  86. Proxy_->Sleep(instant - TInstant::Now());
  87. }
  88. ////////////////////////////////////////////////////////////////////////////////
  89. } // namespace NDetail
  90. } // namespace NYT