aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/refcount.h
blob: 01300752aaa1daaf51abbbde7d457b38c1fa99a5 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#pragma once

#include <util/system/guard.h>
#include <util/system/defaults.h>
#include <util/system/yassert.h>

#include <atomic>

template <class TCounterCheckPolicy>
class TSimpleCounterTemplate: public TCounterCheckPolicy {
    using TCounterCheckPolicy::Check;

public:
    inline TSimpleCounterTemplate(long initial = 0) noexcept
        : Counter_(initial)
    {
    }

    inline ~TSimpleCounterTemplate() {
        Check();
    }

    inline intptr_t Add(intptr_t d) noexcept {
        Check();
        return Counter_ += d;
    }

    inline intptr_t Inc() noexcept {
        return Add(1);
    }

    inline intptr_t Sub(intptr_t d) noexcept {
        Check();
        return Counter_ -= d;
    }

    inline intptr_t Dec() noexcept {
        return Sub(1);
    }

    inline bool TryWeakInc() noexcept {
        if (!Counter_) {
            return false;
        }

        Inc();
        Y_ASSERT(Counter_ != 0);

        return true;
    }

    inline intptr_t Val() const noexcept {
        return Counter_;
    }

private:
    intptr_t Counter_;
};

class TNoCheckPolicy {
protected:
    inline void Check() const {
    }
};

#if defined(SIMPLE_COUNTER_THREAD_CHECK)

    #include <util/system/thread.i>

class TCheckPolicy {
public:
    inline TCheckPolicy() {
        ThreadId = SystemCurrentThreadId();
    }

protected:
    inline void Check() const {
        Y_ABORT_UNLESS(ThreadId == SystemCurrentThreadId(), "incorrect usage of TSimpleCounter");
    }

private:
    size_t ThreadId;
};
#else
using TCheckPolicy = TNoCheckPolicy;
#endif

// Use this one if access from multiple threads to your pointer is an error and you want to enforce thread checks
using TSimpleCounter = TSimpleCounterTemplate<TCheckPolicy>;
// Use this one if you do want to share the pointer between threads, omit thread checks and do the synchronization yourself
using TExplicitSimpleCounter = TSimpleCounterTemplate<TNoCheckPolicy>;

template <class TCounterCheckPolicy>
struct TCommonLockOps<TSimpleCounterTemplate<TCounterCheckPolicy>> {
    static inline void Acquire(TSimpleCounterTemplate<TCounterCheckPolicy>* t) noexcept {
        t->Inc();
    }

    static inline void Release(TSimpleCounterTemplate<TCounterCheckPolicy>* t) noexcept {
        t->Dec();
    }
};

class TAtomicCounter {
public:
    inline TAtomicCounter(long initial = 0) noexcept
        : Counter_(initial)
    {
    }

    TAtomicCounter(const TAtomicCounter& other)
        : Counter_(other.Counter_.load())
    {
    }

    TAtomicCounter& operator=(const TAtomicCounter& other) {
        Counter_.store(other.Counter_.load());
        return *this;
    }

    inline ~TAtomicCounter() = default;

    inline intptr_t Add(intptr_t d) noexcept {
        return Counter_ += d;
    }

    inline intptr_t Inc() noexcept {
        return Add(1);
    }

    inline intptr_t Sub(intptr_t d) noexcept {
        return Counter_ -= d;
    }

    inline intptr_t Dec() noexcept {
        return Sub(1);
    }

    inline intptr_t Val() const noexcept {
        return Counter_.load();
    }

    inline bool TryWeakInc() noexcept {
        for (auto curValue = Counter_.load(std::memory_order_acquire);;) {
            if (!curValue) {
                return false;
            }

            if (Counter_.compare_exchange_weak(curValue, curValue + 1)) {
                return true;
            }
        }
    }

private:
    std::atomic<intptr_t> Counter_;
};

template <>
struct TCommonLockOps<TAtomicCounter> {
    static inline void Acquire(TAtomicCounter* t) noexcept {
        t->Inc();
    }

    static inline void Release(TAtomicCounter* t) noexcept {
        t->Dec();
    }
};