Spaces:
Running
Running
Download include/sincekit.h from pgg-engineering/sincekit: direct link, hf CLI and curl.
- Browser
- Download file 4.41 kB
-
https://huggingface.co/spaces/pgg-engineering/sincekit/resolve/main/include/sincekit.h
- Command line
-
hf download hf://spaces/pgg-engineering/sincekit/include/sincekit.h
-
curl -L -o sincekit.h https://huggingface.co/spaces/pgg-engineering/sincekit/resolve/main/include/sincekit.h
4.41 kB
| 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 | |