aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/generationhandler.cpp
blob: dda298bf4a233c6ecda33b9d8a07a0d343eabef9 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "generationhandler.h"
#include <cassert>

namespace vespalib {

GenerationHandler::GenerationHold::GenerationHold() noexcept
    : _refCount(1),
      _generation(0),
      _next(0)
{ }

GenerationHandler::GenerationHold::~GenerationHold() {
    assert(getRefCount() == 0);
}

void
GenerationHandler::GenerationHold::setValid() noexcept {
    auto old = _refCount.fetch_sub(1, std::memory_order_release);
    assert(!valid(old));
}

bool
GenerationHandler::GenerationHold::setInvalid() noexcept {
    uint32_t refs = 0;
    if (_refCount.compare_exchange_strong(refs, 1,
                                          std::memory_order_acq_rel,
                                          std::memory_order_relaxed))
    {
        return true;
    } else {
        assert(valid(refs));
        return false;
    }
}

GenerationHandler::GenerationHold *
GenerationHandler::GenerationHold::acquire() noexcept {
    if (valid(_refCount.fetch_add(2, std::memory_order_acq_rel))) {
        return this;
    } else {
        release();
        return nullptr;
    }
}

GenerationHandler::GenerationHold *
GenerationHandler::GenerationHold::copy(GenerationHold *self) noexcept {
    if (self == nullptr) {
        return nullptr;
    } else {
        uint32_t oldRefCount = self->_refCount.fetch_add(2, std::memory_order_relaxed);
        (void) oldRefCount;
        assert(valid(oldRefCount));
        return self;
    }
}

GenerationHandler::Guard &
GenerationHandler::Guard::operator=(const Guard & rhs) noexcept
{
    if (&rhs != this) {
        cleanup();
        _hold = GenerationHold::copy(rhs._hold);
    }
    return *this;
}

GenerationHandler::Guard &
GenerationHandler::Guard::operator=(Guard &&rhs) noexcept
{
    if (&rhs != this) {
        cleanup();
        _hold = rhs._hold;
        rhs._hold = nullptr;
    }
    return *this;
}

void
GenerationHandler::update_oldest_used_generation()
{
    for (;;) {
        if (_first == _last.load(std::memory_order_relaxed))
            break;			// No elements can be freed
        if (!_first->setInvalid()) {
            break;			// First element still in use
        }
        GenerationHold *toFree = _first;
        assert(toFree->_next != nullptr);
        _first = toFree->_next;
        toFree->_next = _free;
        _free = toFree;
    }
    _oldest_used_generation.store(_first->_generation, std::memory_order_relaxed);
}

GenerationHandler::GenerationHandler()
    : _generation(0),
      _oldest_used_generation(0),
      _last(nullptr),
      _first(nullptr),
      _free(nullptr),
      _numHolds(0u)
{
    _last = _first = new GenerationHold;
    ++_numHolds;
    _first->_generation.store(getCurrentGeneration(), std::memory_order_relaxed);
    _first->setValid();
}

GenerationHandler::~GenerationHandler(void)
{
    update_oldest_used_generation();
    assert(_first == _last.load(std::memory_order_relaxed));
    while (_free != nullptr) {
        GenerationHold *toFree = _free;
        _free = toFree->_next;
        --_numHolds;
        delete toFree;
    }
    assert(_numHolds == 1);
    delete _first;
}

GenerationHandler::Guard
GenerationHandler::takeGuard() const
{
    Guard guard(_last.load(std::memory_order_acquire));
    for (;;) {
        // Must check valid() after increasing refcount
        if (guard.valid())
            break;		// Might still be marked invalid, that's OK
        /*
         * Clashed with writer freeing entry.  Must abandon current
         * guard and try again.
         */
        guard = Guard(_last.load(std::memory_order_acquire));
    }
    // Guard has been valid after bumping refCount
    return guard;
}

void
GenerationHandler::incGeneration()
{
    generation_t ngen = getNextGeneration();

    auto last = _last.load(std::memory_order_relaxed);
    if (last->getRefCountAcqRel() == 0) {
        // Last generation is unused, morph it to new generation.  This is
        // the typical case when no readers are present.
        set_generation(ngen);
        last->_generation.store(ngen, std::memory_order_relaxed);
        update_oldest_used_generation();
        return;
    }
    GenerationHold *nhold = nullptr;
    if (_free == nullptr) {
        nhold = new GenerationHold;
        ++_numHolds;
    } else {
        nhold = _free;
        _free = nhold->_next;
    }
    nhold->_generation.store(ngen, std::memory_order_relaxed);
    nhold->_next = nullptr;
    nhold->setValid();
    last->_next = nhold;
    set_generation(ngen);
    _last.store(nhold, std::memory_order_release);
    update_oldest_used_generation();
}

uint32_t
GenerationHandler::getGenerationRefCount(generation_t gen) const
{
    if (static_cast<sgeneration_t>(gen - getCurrentGeneration()) > 0)
        return 0u;
    if (static_cast<sgeneration_t>(get_oldest_used_generation() - gen) > 0)
        return 0u;
    for (GenerationHold *hold = _first; hold != nullptr; hold = hold->_next) {
        if (hold->_generation.load(std::memory_order_relaxed) == gen)
            return hold->getRefCount();
    }
    return 0u;
}

uint64_t
GenerationHandler::getGenerationRefCount(void) const
{
    uint64_t ret = 0;
    for (GenerationHold *hold = _first; hold != nullptr; hold = hold->_next) {
        ret += hold->getRefCount();
    }
    return ret;
}

}