sleep.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "rain_check.h"
  2. #include <util/system/yassert.h>
  3. using namespace NRainCheck;
  4. using namespace NRainCheck::NPrivate;
  5. using namespace NBus;
  6. using namespace NBus::NPrivate;
  7. TSleepService::TSleepService(::NBus::NPrivate::TScheduler* scheduler)
  8. : Scheduler(scheduler)
  9. {
  10. }
  11. NRainCheck::TSleepService::TSleepService()
  12. : SchedulerHolder(new TScheduler)
  13. , Scheduler(SchedulerHolder.Get())
  14. {
  15. }
  16. NRainCheck::TSleepService::~TSleepService() {
  17. if (!!SchedulerHolder) {
  18. Scheduler->Stop();
  19. }
  20. }
  21. namespace {
  22. struct TSleepServiceScheduleItem: public IScheduleItem {
  23. ISubtaskListener* const Parent;
  24. TSleepServiceScheduleItem(ISubtaskListener* parent, TInstant time)
  25. : IScheduleItem(time)
  26. , Parent(parent)
  27. {
  28. }
  29. void Do() override {
  30. Parent->SetDone();
  31. }
  32. };
  33. }
  34. void TSleepService::Sleep(TSubtaskCompletion* r, TDuration duration) {
  35. TTaskRunnerBase* current = TTaskRunnerBase::CurrentTask();
  36. r->SetRunning(current);
  37. Scheduler->Schedule(new TSleepServiceScheduleItem(r, TInstant::Now() + duration));
  38. }