aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/core/scheduler_cookie.cpp
blob: b975a80c072035d357c2520cf2c82588a49a513f (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
79
80
81
82
83
84
#include "scheduler_cookie.h"

namespace NActors {
    class TSchedulerCookie2Way: public ISchedulerCookie {
        TAtomic Value;

    public:
        TSchedulerCookie2Way()
            : Value(2)
        {
        }

        bool IsArmed() noexcept override {
            return (AtomicGet(Value) == 2);
        }

        bool Detach() noexcept override {
            const ui64 x = AtomicDecrement(Value);
            if (x == 1)
                return true;

            if (x == 0) {
                delete this;
                return false;
            }

            Y_ABORT();
        }

        bool DetachEvent() noexcept override {
            Y_ABORT();
        }
    };

    ISchedulerCookie* ISchedulerCookie::Make2Way() {
        return new TSchedulerCookie2Way();
    }

    class TSchedulerCookie3Way: public ISchedulerCookie {
        TAtomic Value;

    public:
        TSchedulerCookie3Way()
            : Value(3)
        {
        }

        bool IsArmed() noexcept override {
            return (AtomicGet(Value) == 3);
        }

        bool Detach() noexcept override {
            const ui64 x = AtomicDecrement(Value);
            if (x == 2)
                return true;
            if (x == 1)
                return false;
            if (x == 0) {
                delete this;
                return false;
            }

            Y_ABORT();
        }

        bool DetachEvent() noexcept override {
            const ui64 x = AtomicDecrement(Value);
            if (x == 2)
                return false;
            if (x == 1)
                return true;
            if (x == 0) {
                delete this;
                return false;
            }

            Y_ABORT();
        }
    };

    ISchedulerCookie* ISchedulerCookie::Make3Way() {
        return new TSchedulerCookie3Way();
    }
}