aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/core/scheduler_cookie.h
blob: 2c20ca67f3f377cb8b1378918a4565f1c1105f3d (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
69
70
71
72
73
74
75
76
77
78
#pragma once

#include "defs.h"
#include <util/generic/noncopyable.h>

namespace NActors {
    class ISchedulerCookie : TNonCopyable {
    protected:
        virtual ~ISchedulerCookie() {
        }

    public:
        virtual bool Detach() noexcept = 0;
        virtual bool DetachEvent() noexcept = 0;
        virtual bool IsArmed() noexcept = 0;

        static ISchedulerCookie* Make2Way();
        static ISchedulerCookie* Make3Way();
    };

    class TSchedulerCookieHolder : TNonCopyable {
        ISchedulerCookie* Cookie;

    public:
        TSchedulerCookieHolder()
            : Cookie(nullptr)
        {
        }

        TSchedulerCookieHolder(ISchedulerCookie* x)
            : Cookie(x)
        {
        }

        ~TSchedulerCookieHolder() {
            Detach();
        }

        bool operator==(const TSchedulerCookieHolder& x) const noexcept {
            return (Cookie == x.Cookie);
        }

        ISchedulerCookie* Get() const {
            return Cookie;
        }

        ISchedulerCookie* Release() {
            ISchedulerCookie* result = Cookie;
            Cookie = nullptr;
            return result;
        }

        void Reset(ISchedulerCookie* cookie) {
            Detach();
            Cookie = cookie;
        }

        bool Detach() noexcept {
            if (Cookie) {
                const bool res = Cookie->Detach();
                Cookie = nullptr;
                return res;
            } else {
                return false;
            }
        }

        bool DetachEvent() noexcept {
            if (Cookie) {
                const bool res = Cookie->DetachEvent();
                Cookie = nullptr;
                return res;
            } else {
                return false;
            }
        }
    };
}