aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/core/scheduler_cookie.cpp
blob: 0fa6f543a7abd60f1ecaa8972a9d8637219a4342 (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_FAIL();
        }

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

    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_FAIL();
        }

        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_FAIL();
        }
    };

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