File size: 4,412 Bytes
ef8334d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#ifndef SINCEKIT_H_INCLUDED
#define SINCEKIT_H_INCLUDED

#include <cstddef>
#include <cstdint>
#include <limits>
#include <stdexcept>
#include <vector>

namespace sincekit {

// Tracks the last mutation of each dense application-owned slot. Serialize
// writers against all accesses. Readers may run concurrently between writes.
class Index {
public:
    explicit Index(std::size_t capacity)
        : slots_(checked_capacity(capacity)),
          tombstones_(capacity / 64 + (capacity % 64 != 0)) {}

    Index(const Index&) = delete;
    Index& operator=(const Index&) = delete;

    std::size_t capacity() const noexcept { return slots_.size(); }
    std::uint64_t revision() const noexcept { return revision_; }

    // Every call publishes one mutation, even for repeated writes or erases.
    std::uint64_t touch(std::uint32_t id) { return publish(id, false); }
    std::uint64_t erase(std::uint32_t id) { return publish(id, true); }

    // Visits each changed slot once, newest first. Checkpoint the returned
    // revision only after successful delivery. Cursor zero begins at creation.
    // Callbacks may throw or perform nested reads. Mutating this Index during
    // its callback throws before the mutation. Cursors are instance-local.
    template<class Fn>
    std::uint64_t since(std::uint64_t cursor, Fn&& callback) const {
        if (cursor > revision_) {
            throw std::out_of_range("sincekit: cursor exceeds current revision");
        }
        if (cursor == revision_) {
            return revision_;
        }
        const ReadScope reading(this);
        for (auto id = head_; id != none; id = slots_[id].older) {
            const auto stamp = slots_[id].stamp;
            if (stamp <= cursor) {
                break;
            }
            callback(std::uint32_t{id}, std::uint64_t{stamp},
                     (tombstones_[id / 64] & mask(id)) != 0);
        }
        return revision_;
    }

private:
    static constexpr std::uint32_t none =
        (std::numeric_limits<std::uint32_t>::max)();

    struct Slot {
        std::uint64_t stamp = 0;
        std::uint32_t newer = none;
        std::uint32_t older = none;
    };

    // A thread-local stack keeps read-only calls free of shared writes and
    // detects indirect reentry (for example, index A -> index B -> write A).
    struct ReadScope {
        inline static thread_local const ReadScope* current = nullptr;
        const Index* index;
        const ReadScope* previous;

        explicit ReadScope(const Index* owner) noexcept
            : index(owner), previous(current) { current = this; }
        ~ReadScope() { current = previous; }
    };

    static std::size_t checked_capacity(std::size_t size) {
        if (size > none) {
            throw std::length_error("sincekit: capacity exceeds uint32 slot limit");
        }
        return size;
    }

    static std::uint64_t mask(std::uint32_t id) noexcept {
        return std::uint64_t{1} << (id % 64);
    }

    std::uint64_t publish(std::uint32_t id, bool deleted) {
        if (id >= slots_.size()) {
            throw std::out_of_range("sincekit: slot outside capacity");
        }
        for (auto* read = ReadScope::current; read; read = read->previous) {
            if (read->index == this) {
                throw std::logic_error("sincekit: write during traversal");
            }
        }
        if (revision_ == (std::numeric_limits<std::uint64_t>::max)()) {
            throw std::overflow_error("sincekit: revision exhausted");
        }

        auto& slot = slots_[id];
        if (head_ != id) {
            if (slot.newer != none) {
                slots_[slot.newer].older = slot.older;
            }
            if (slot.older != none) {
                slots_[slot.older].newer = slot.newer;
            }
            slot.newer = none;
            slot.older = head_;
            if (head_ != none) {
                slots_[head_].newer = id;
            }
            head_ = id;
        }
        auto& bits = tombstones_[id / 64];
        if (deleted) {
            bits |= mask(id);
        } else {
            bits &= ~mask(id);
        }
        slot.stamp = ++revision_;
        return revision_;
    }

    std::vector<Slot> slots_;
    std::vector<std::uint64_t> tombstones_;
    std::uint64_t revision_ = 0;
    std::uint32_t head_ = none;
};

}  // namespace sincekit

#endif  // SINCEKIT_H_INCLUDED