aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/rain_check/core/sleep.cpp
blob: f5d0b4cac9c6798fcdca9faf7f3537f1b15c7c47 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "rain_check.h"

#include <util/system/yassert.h>

using namespace NRainCheck;
using namespace NRainCheck::NPrivate;
using namespace NBus;
using namespace NBus::NPrivate;

TSleepService::TSleepService(::NBus::NPrivate::TScheduler* scheduler)
    : Scheduler(scheduler)
{
}

NRainCheck::TSleepService::TSleepService()
    : SchedulerHolder(new TScheduler)
    , Scheduler(SchedulerHolder.Get())
{
}

NRainCheck::TSleepService::~TSleepService() {
    if (!!SchedulerHolder) {
        Scheduler->Stop();
    }
}

namespace {
    struct TSleepServiceScheduleItem: public IScheduleItem {
        ISubtaskListener* const Parent;

        TSleepServiceScheduleItem(ISubtaskListener* parent, TInstant time)
            : IScheduleItem(time)
            , Parent(parent)
        {
        }

        void Do() override {
            Parent->SetDone();
        }
    };
}

void TSleepService::Sleep(TSubtaskCompletion* r, TDuration duration) {
    TTaskRunnerBase* current = TTaskRunnerBase::CurrentTask();
    r->SetRunning(current);
    Scheduler->Schedule(new TSleepServiceScheduleItem(r, TInstant::Now() + duration));
}