deadline_timer_impl.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #pragma once
  2. #include "io_service_impl.h"
  3. namespace NAsio {
  4. class TTimerOperation: public TOperation {
  5. public:
  6. TTimerOperation(TIOService::TImpl::TTimer* t, TInstant deadline)
  7. : TOperation(deadline)
  8. , T_(t)
  9. {
  10. }
  11. void AddOp(TIOService::TImpl&) override {
  12. Y_ASSERT(0);
  13. }
  14. void Finalize() override {
  15. DBGOUT("TTimerDeadlineOperation::Finalize()");
  16. T_->DelOp(this);
  17. }
  18. protected:
  19. TIOService::TImpl::TTimer* T_;
  20. };
  21. class TRegisterTimerOperation: public TTimerOperation {
  22. public:
  23. TRegisterTimerOperation(TIOService::TImpl::TTimer* t, TInstant deadline = TInstant::Max())
  24. : TTimerOperation(t, deadline)
  25. {
  26. Speculative_ = true;
  27. }
  28. bool Execute(int errorCode) override {
  29. Y_UNUSED(errorCode);
  30. T_->GetIOServiceImpl().SyncRegisterTimer(T_);
  31. return true;
  32. }
  33. };
  34. class TTimerDeadlineOperation: public TTimerOperation {
  35. public:
  36. TTimerDeadlineOperation(TIOService::TImpl::TTimer* t, TDeadlineTimer::THandler h, TInstant deadline)
  37. : TTimerOperation(t, deadline)
  38. , H_(h)
  39. {
  40. }
  41. void AddOp(TIOService::TImpl&) override {
  42. T_->AddOp(this);
  43. }
  44. bool Execute(int errorCode) override {
  45. DBGOUT("TTimerDeadlineOperation::Execute(" << errorCode << ")");
  46. H_(errorCode == ETIMEDOUT ? 0 : errorCode, *this);
  47. return true;
  48. }
  49. private:
  50. TDeadlineTimer::THandler H_;
  51. };
  52. class TCancelTimerOperation: public TTimerOperation {
  53. public:
  54. TCancelTimerOperation(TIOService::TImpl::TTimer* t)
  55. : TTimerOperation(t, TInstant::Max())
  56. {
  57. Speculative_ = true;
  58. }
  59. bool Execute(int errorCode) override {
  60. Y_UNUSED(errorCode);
  61. T_->FailOperations(ECANCELED);
  62. return true;
  63. }
  64. };
  65. class TUnregisterTimerOperation: public TTimerOperation {
  66. public:
  67. TUnregisterTimerOperation(TIOService::TImpl::TTimer* t, TInstant deadline = TInstant::Max())
  68. : TTimerOperation(t, deadline)
  69. {
  70. Speculative_ = true;
  71. }
  72. bool Execute(int errorCode) override {
  73. Y_UNUSED(errorCode);
  74. DBGOUT("TUnregisterTimerOperation::Execute(" << errorCode << ")");
  75. T_->GetIOServiceImpl().SyncUnregisterAndDestroyTimer(T_);
  76. return true;
  77. }
  78. };
  79. class TDeadlineTimer::TImpl: public TIOService::TImpl::TTimer {
  80. public:
  81. TImpl(TIOService::TImpl& srv)
  82. : TIOService::TImpl::TTimer(srv)
  83. {
  84. }
  85. void AsyncWaitExpireAt(TDeadline d, TDeadlineTimer::THandler h) {
  86. Srv_.ScheduleOp(new TTimerDeadlineOperation(this, h, d));
  87. }
  88. void Cancel() {
  89. Srv_.ScheduleOp(new TCancelTimerOperation(this));
  90. }
  91. };
  92. }