aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/refcount.h
blob: 558e6c3cbe9484fd118349970973140847f43e02 (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
#pragma once

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

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

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

    inline ~TSimpleCounterTemplate() {
        Check();
    }

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

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

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

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

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

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

        return true;
    }

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

private:
    TAtomicBase 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_VERIFY(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)
    {
    }

    inline ~TAtomicCounter() = default;

    inline TAtomicBase Add(TAtomicBase d) noexcept {
        return AtomicAdd(Counter_, d);
    }

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

    inline TAtomicBase Sub(TAtomicBase d) noexcept {
        return AtomicSub(Counter_, d);
    }

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

    inline TAtomicBase Val() const noexcept {
        return AtomicGet(Counter_);
    }

    inline bool TryWeakInc() noexcept {
        while (true) {
            intptr_t curValue = Counter_;

            if (!curValue) {
                return false;
            }

            intptr_t newValue = curValue + 1;
            Y_ASSERT(newValue != 0);

            if (AtomicCas(&Counter_, newValue, curValue)) {
                return true;
            }
        }
    }

private:
    TAtomic Counter_;
};

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

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