aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/common/pendinglidtracker.cpp
blob: 42472425f393be35135721dbbca894f68843339e (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

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

namespace proton {

PendingLidTrackerBase::PendingLidTrackerBase() = default;
PendingLidTrackerBase::~PendingLidTrackerBase() = default;

ILidCommitState::State
PendingLidTrackerBase::waitState(State state, uint32_t lid) const {
    MonitorGuard guard(_mutex);
    return waitFor(guard, state, lid);
}

ILidCommitState::State
PendingLidTrackerBase::waitState(State state, const LidList & lids) const {
    MonitorGuard guard(_mutex);
    State lowest = State::COMPLETED;
    for (uint32_t lid : lids) {
        State next = waitFor(guard, state, lid);
        if ((state == State::NEED_COMMIT) && next == state) {
            return next;
        }
        lowest = std::min(next, lowest);
    }
    return lowest;
}

PendingLidTracker::PendingLidTracker() = default;

PendingLidTracker::~PendingLidTracker() {
    assert(_pending.empty());
}

IPendingLidTracker::Token
PendingLidTracker::produce(uint32_t lid) {
    std::lock_guard guard(_mutex);
    _pending[lid]++;
    return Token(lid, *this);
}
void
PendingLidTracker::consume(uint32_t lid) {
    std::lock_guard guard(_mutex);
    auto found = _pending.find(lid);
    assert (found != _pending.end());
    assert (found->second > 0);
    if (found->second == 1) {
        _pending.erase(found);
        _cond.notify_all();
    } else {
        found->second--;
    }
}

ILidCommitState::State
PendingLidTracker::waitFor(MonitorGuard & guard, State state, uint32_t lid) const {
    for (auto found = _pending.find(lid); found != _pending.end(); found = _pending.find(lid)) {
        if (state == State::NEED_COMMIT) {
            return State::WAITING;
        }
        _cond.wait(guard);
    }
    return State::COMPLETED;
}

PendingLidTrackerBase::Snapshot
PendingLidTracker::produceSnapshot() {
    return Snapshot();
}

}