aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/scheduler/scheduler.h
blob: afcc0de55d51624e55d9aef53b70a52e61c45265 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once

#include <library/cpp/threading/future/legacy_future.h>

#include <util/datetime/base.h>
#include <util/generic/object_counter.h>
#include <util/generic/ptr.h>
#include <util/generic/vector.h>
#include <util/system/atomic.h>
#include <util/system/condvar.h>
#include <util/system/mutex.h>
#include <util/system/thread.h>

namespace NBus {
    namespace NPrivate {
        class IScheduleItem {
        public:
            inline IScheduleItem(TInstant scheduleTime) noexcept;
            virtual ~IScheduleItem() {
            }

            virtual void Do() = 0;
            inline TInstant GetScheduleTime() const noexcept;

        private:
            TInstant ScheduleTime;
        };

        using IScheduleItemAutoPtr = TAutoPtr<IScheduleItem>;

        class TScheduler {
        public:
            TScheduler();
            ~TScheduler();
            void Stop();
            void Schedule(TAutoPtr<IScheduleItem> i);

            size_t Size() const;

        private:
            void SchedulerThread();

            void FillNextItem();

        private:
            TVector<IScheduleItemAutoPtr> Items;
            IScheduleItemAutoPtr NextItem;
            typedef TMutex TLock;
            TLock Lock;
            TCondVar CondVar;

            TObjectCounter<TScheduler> ObjectCounter;

            bool StopThread;
            NThreading::TLegacyFuture<> Thread;
        };

        inline IScheduleItem::IScheduleItem(TInstant scheduleTime) noexcept
            : ScheduleTime(scheduleTime)
        {
        }

        inline TInstant IScheduleItem::GetScheduleTime() const noexcept {
            return ScheduleTime;
        }

    }
}