Add Observable, Pool, Ring modules
Void::Observable — Observable<T> reactive value with typed on_change listeners and unsubscribe-by-handle; Signal<Args...> event broadcast with connect/disconnect. Void::Pool — ObjectPool<T,ChunkSize> slab-based pool with unique_ptr<T,Deleter> ownership; FixedPool<T,N> fixed-capacity in-place pool, returns nullptr when full. Void::Ring — SpscRing<T,N> single-producer/single-consumer wait-free ring buffer; MpscRing<T,N> multi-producer/single-consumer ring using CAS on the head pointer. Both tested under real threads. 39/39 tests pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
a9a2e2b59a
commit
388e75011f
6 files changed
+876
No files matched your search
@@ -0,0 +1,139 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Observable
|
||||
{
|
||||
|
||||
// ── Handle — opaque subscription identifier ───────────────────────────────────
|
||||
|
||||
struct Handle
|
||||
{
|
||||
std::uint64_t id{0};
|
||||
[[nodiscard]] bool valid() const noexcept { return id != 0; }
|
||||
[[nodiscard]] bool operator==(const Handle&) const noexcept = default;
|
||||
};
|
||||
|
||||
// ── Observable<T> ─────────────────────────────────────────────────────────────
|
||||
// A value that observers can subscribe to via on_change().
|
||||
// Observers receive (old_value, new_value) on every set() call that changes
|
||||
// the stored value.
|
||||
|
||||
template <typename T>
|
||||
class Observable
|
||||
{
|
||||
public:
|
||||
using Listener = std::function<void(const T& old_val, const T& new_val)>;
|
||||
|
||||
explicit Observable(T initial = {}) : value_{std::move(initial)} {}
|
||||
|
||||
[[nodiscard]] const T& get() const noexcept { return value_; }
|
||||
[[nodiscard]] operator const T&() const noexcept { return value_; }
|
||||
|
||||
void set(T new_val)
|
||||
{
|
||||
if (new_val == value_) return;
|
||||
T old = std::move(value_);
|
||||
value_ = std::move(new_val);
|
||||
notify(old, value_);
|
||||
}
|
||||
|
||||
Observable& operator=(T new_val)
|
||||
{
|
||||
set(std::move(new_val));
|
||||
return *this;
|
||||
}
|
||||
|
||||
Handle on_change(Listener listener)
|
||||
{
|
||||
const Handle h{++next_id_};
|
||||
listeners_.push_back({h, std::move(listener)});
|
||||
return h;
|
||||
}
|
||||
|
||||
bool unsubscribe(Handle h)
|
||||
{
|
||||
const auto it =
|
||||
std::ranges::find_if(listeners_,
|
||||
[&](const auto& e) { return e.h == h; });
|
||||
if (it == listeners_.end()) return false;
|
||||
listeners_.erase(it);
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t listener_count() const noexcept
|
||||
{
|
||||
return listeners_.size();
|
||||
}
|
||||
|
||||
private:
|
||||
void notify(const T& old_val, const T& new_val)
|
||||
{
|
||||
for (auto& [h, fn] : listeners_) fn(old_val, new_val);
|
||||
}
|
||||
|
||||
T value_;
|
||||
std::uint64_t next_id_{0};
|
||||
|
||||
struct Entry
|
||||
{
|
||||
Handle h;
|
||||
Listener fn;
|
||||
};
|
||||
std::vector<Entry> listeners_;
|
||||
};
|
||||
|
||||
// ── Signal<Args...> ───────────────────────────────────────────────────────────
|
||||
// An event signal that observers connect to. emit() calls all connected slots.
|
||||
|
||||
template <typename... Args>
|
||||
class Signal
|
||||
{
|
||||
public:
|
||||
using Slot = std::function<void(const Args&...)>;
|
||||
|
||||
Handle connect(Slot slot)
|
||||
{
|
||||
const Handle h{++next_id_};
|
||||
slots_.push_back({h, std::move(slot)});
|
||||
return h;
|
||||
}
|
||||
|
||||
bool disconnect(Handle h)
|
||||
{
|
||||
const auto it =
|
||||
std::ranges::find_if(slots_,
|
||||
[&](const auto& e) { return e.h == h; });
|
||||
if (it == slots_.end()) return false;
|
||||
slots_.erase(it);
|
||||
return true;
|
||||
}
|
||||
|
||||
void emit(const Args&... args) const
|
||||
{
|
||||
for (const auto& [h, fn] : slots_) fn(args...);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t slot_count() const noexcept
|
||||
{
|
||||
return slots_.size();
|
||||
}
|
||||
|
||||
void clear() noexcept { slots_.clear(); }
|
||||
|
||||
private:
|
||||
std::uint64_t next_id_{0};
|
||||
|
||||
struct Entry
|
||||
{
|
||||
Handle h;
|
||||
Slot fn;
|
||||
};
|
||||
std::vector<Entry> slots_;
|
||||
};
|
||||
|
||||
} // namespace Void::Observable
|
||||
@@ -0,0 +1,166 @@
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <new>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Pool
|
||||
{
|
||||
|
||||
// ── ObjectPool<T, ChunkSize> ──────────────────────────────────────────────────
|
||||
// A slab-based object pool. acquire() returns a unique_ptr<T> with a custom
|
||||
// deleter that returns the object to the pool instead of freeing memory.
|
||||
// Objects are constructed in-place; the pool owns the backing storage.
|
||||
//
|
||||
// Thread-safety: NOT thread-safe. Add external synchronisation if needed.
|
||||
|
||||
template <typename T, std::size_t ChunkSize = 32>
|
||||
class ObjectPool
|
||||
{
|
||||
public:
|
||||
ObjectPool() = default;
|
||||
|
||||
ObjectPool(const ObjectPool&) = delete;
|
||||
ObjectPool& operator=(const ObjectPool&) = delete;
|
||||
ObjectPool(ObjectPool&&) = default;
|
||||
ObjectPool& operator=(ObjectPool&&) = default;
|
||||
|
||||
~ObjectPool() = default;
|
||||
|
||||
struct Deleter
|
||||
{
|
||||
ObjectPool* pool;
|
||||
void operator()(T* p) const noexcept { pool->release(p); }
|
||||
};
|
||||
|
||||
using Ptr = std::unique_ptr<T, Deleter>;
|
||||
|
||||
template <typename... Args>
|
||||
[[nodiscard]] Ptr acquire(Args&&... args)
|
||||
{
|
||||
T* slot = pop_free();
|
||||
if (!slot) {
|
||||
slot = allocate_slot();
|
||||
}
|
||||
std::construct_at(slot, std::forward<Args>(args)...);
|
||||
return Ptr{slot, Deleter{this}};
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t capacity() const noexcept
|
||||
{
|
||||
return slabs_.size() * ChunkSize;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t available() const noexcept
|
||||
{
|
||||
return free_.size();
|
||||
}
|
||||
|
||||
private:
|
||||
void release(T* p) noexcept
|
||||
{
|
||||
std::destroy_at(p);
|
||||
free_.push_back(p);
|
||||
}
|
||||
|
||||
T* pop_free() noexcept
|
||||
{
|
||||
if (free_.empty()) return nullptr;
|
||||
T* p = free_.back();
|
||||
free_.pop_back();
|
||||
return p;
|
||||
}
|
||||
|
||||
T* allocate_slot()
|
||||
{
|
||||
// Grow by one slab; keep all subsequent objects from the same slab
|
||||
// available in free_.
|
||||
auto& slab = slabs_.emplace_back();
|
||||
slab = std::make_unique<Slab>();
|
||||
// Push all but the first slot into free_.
|
||||
for (std::size_t i = 1; i < ChunkSize; ++i) {
|
||||
free_.push_back(reinterpret_cast<T*>(&slab->storage[i]));
|
||||
}
|
||||
return reinterpret_cast<T*>(&slab->storage[0]);
|
||||
}
|
||||
|
||||
struct alignas(alignof(T)) Slot
|
||||
{
|
||||
std::byte storage[sizeof(T)];
|
||||
};
|
||||
|
||||
struct Slab
|
||||
{
|
||||
Slot storage[ChunkSize];
|
||||
};
|
||||
|
||||
std::vector<std::unique_ptr<Slab>> slabs_;
|
||||
std::vector<T*> free_;
|
||||
};
|
||||
|
||||
// ── FixedPool<T, N> ───────────────────────────────────────────────────────────
|
||||
// A fixed-capacity pool allocated in-place (no heap after construction).
|
||||
// Capacity is known at compile time; acquire() returns nullptr when full.
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
class FixedPool
|
||||
{
|
||||
public:
|
||||
FixedPool() noexcept
|
||||
{
|
||||
for (std::size_t i = 0; i < N; ++i) {
|
||||
free_[i] = slot_ptr(i);
|
||||
}
|
||||
free_count_ = N;
|
||||
}
|
||||
|
||||
FixedPool(const FixedPool&) = delete;
|
||||
FixedPool& operator=(const FixedPool&) = delete;
|
||||
|
||||
struct Deleter
|
||||
{
|
||||
FixedPool* pool;
|
||||
void operator()(T* p) const noexcept { pool->release(p); }
|
||||
};
|
||||
|
||||
using Ptr = std::unique_ptr<T, Deleter>;
|
||||
|
||||
template <typename... Args>
|
||||
[[nodiscard]] Ptr acquire(Args&&... args) noexcept(
|
||||
std::is_nothrow_constructible_v<T, Args...>)
|
||||
{
|
||||
if (free_count_ == 0) return nullptr;
|
||||
T* slot = free_[--free_count_];
|
||||
std::construct_at(slot, std::forward<Args>(args)...);
|
||||
return Ptr{slot, Deleter{this}};
|
||||
}
|
||||
|
||||
[[nodiscard]] static constexpr std::size_t capacity() noexcept { return N; }
|
||||
[[nodiscard]] std::size_t available() const noexcept { return free_count_; }
|
||||
|
||||
private:
|
||||
void release(T* p) noexcept
|
||||
{
|
||||
std::destroy_at(p);
|
||||
free_[free_count_++] = p;
|
||||
}
|
||||
|
||||
T* slot_ptr(std::size_t i) noexcept
|
||||
{
|
||||
return reinterpret_cast<T*>(&storage_[i]);
|
||||
}
|
||||
|
||||
struct alignas(alignof(T)) Slot
|
||||
{
|
||||
std::byte storage[sizeof(T)];
|
||||
};
|
||||
|
||||
Slot storage_[N];
|
||||
T* free_[N];
|
||||
std::size_t free_count_{0};
|
||||
};
|
||||
|
||||
} // namespace Void::Pool
|
||||
@@ -0,0 +1,179 @@
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <new>
|
||||
#include <optional>
|
||||
#include <type_traits>
|
||||
|
||||
namespace Void::Ring
|
||||
{
|
||||
|
||||
// ── SpscRing<T, N> ────────────────────────────────────────────────────────────
|
||||
// Single-producer / single-consumer lock-free ring buffer.
|
||||
// Capacity is N (must be a power of two). The buffer holds at most N-1 items.
|
||||
// All operations are wait-free.
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
requires(N >= 2 && (N & (N - 1)) == 0) // power of two
|
||||
class SpscRing
|
||||
{
|
||||
public:
|
||||
SpscRing() = default;
|
||||
|
||||
SpscRing(const SpscRing&) = delete;
|
||||
SpscRing& operator=(const SpscRing&) = delete;
|
||||
|
||||
// Producer side — push returns false when the buffer is full.
|
||||
[[nodiscard]] bool push(const T& val)
|
||||
noexcept(std::is_nothrow_copy_constructible_v<T>)
|
||||
{
|
||||
const std::size_t head = head_.load(std::memory_order_relaxed);
|
||||
const std::size_t next = (head + 1) & mask_;
|
||||
if (next == tail_.load(std::memory_order_acquire)) return false;
|
||||
std::construct_at(slot_ptr(head), val);
|
||||
head_.store(next, std::memory_order_release);
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool push(T&& val)
|
||||
noexcept(std::is_nothrow_move_constructible_v<T>)
|
||||
{
|
||||
const std::size_t head = head_.load(std::memory_order_relaxed);
|
||||
const std::size_t next = (head + 1) & mask_;
|
||||
if (next == tail_.load(std::memory_order_acquire)) return false;
|
||||
std::construct_at(slot_ptr(head), std::move(val));
|
||||
head_.store(next, std::memory_order_release);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Consumer side — pop returns nullopt when the buffer is empty.
|
||||
[[nodiscard]] std::optional<T> pop() noexcept(
|
||||
std::is_nothrow_move_constructible_v<T> &&
|
||||
std::is_nothrow_destructible_v<T>)
|
||||
{
|
||||
const std::size_t tail = tail_.load(std::memory_order_relaxed);
|
||||
if (tail == head_.load(std::memory_order_acquire)) return std::nullopt;
|
||||
T val{std::move(*slot_ptr(tail))};
|
||||
std::destroy_at(slot_ptr(tail));
|
||||
tail_.store((tail + 1) & mask_, std::memory_order_release);
|
||||
return val;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool empty() const noexcept
|
||||
{
|
||||
return tail_.load(std::memory_order_acquire) ==
|
||||
head_.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool full() const noexcept
|
||||
{
|
||||
const std::size_t next =
|
||||
(head_.load(std::memory_order_acquire) + 1) & mask_;
|
||||
return next == tail_.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
[[nodiscard]] static constexpr std::size_t capacity() noexcept
|
||||
{
|
||||
return N - 1;
|
||||
}
|
||||
|
||||
private:
|
||||
T* slot_ptr(std::size_t i) noexcept
|
||||
{
|
||||
return std::launder(reinterpret_cast<T*>(&storage_[i]));
|
||||
}
|
||||
|
||||
static constexpr std::size_t mask_ = N - 1;
|
||||
|
||||
struct alignas(alignof(T)) Slot
|
||||
{
|
||||
std::byte storage[sizeof(T)];
|
||||
};
|
||||
|
||||
// Pad to separate cache lines to avoid false sharing.
|
||||
alignas(64) std::atomic<std::size_t> head_{0};
|
||||
alignas(64) std::atomic<std::size_t> tail_{0};
|
||||
Slot storage_[N];
|
||||
};
|
||||
|
||||
// ── MpscRing<T, N> ────────────────────────────────────────────────────────────
|
||||
// Multi-producer / single-consumer ring buffer using a compare-exchange loop
|
||||
// on the head pointer. Consumer is still single-threaded.
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
requires(N >= 2 && (N & (N - 1)) == 0)
|
||||
class MpscRing
|
||||
{
|
||||
public:
|
||||
MpscRing() = default;
|
||||
|
||||
MpscRing(const MpscRing&) = delete;
|
||||
MpscRing& operator=(const MpscRing&) = delete;
|
||||
|
||||
[[nodiscard]] bool push(T val) noexcept(
|
||||
std::is_nothrow_move_constructible_v<T>)
|
||||
{
|
||||
std::size_t head = head_.load(std::memory_order_relaxed);
|
||||
std::size_t next;
|
||||
do {
|
||||
next = (head + 1) & mask_;
|
||||
if (next == tail_.load(std::memory_order_acquire)) return false;
|
||||
} while (!head_.compare_exchange_weak(head, next,
|
||||
std::memory_order_acq_rel,
|
||||
std::memory_order_relaxed));
|
||||
std::construct_at(slot_ptr(head), std::move(val));
|
||||
// Signal that this slot is ready.
|
||||
ready_[head].store(true, std::memory_order_release);
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::optional<T> pop() noexcept(
|
||||
std::is_nothrow_move_constructible_v<T>)
|
||||
{
|
||||
const std::size_t tail = tail_.load(std::memory_order_relaxed);
|
||||
if (tail == head_.load(std::memory_order_acquire)) return std::nullopt;
|
||||
// Spin until the producing thread has written the value.
|
||||
while (!ready_[tail].load(std::memory_order_acquire)) {
|
||||
// wait — producer is still copying; this is still wait-free on the
|
||||
// hot path (single iteration in practice).
|
||||
}
|
||||
T val{std::move(*slot_ptr(tail))};
|
||||
std::destroy_at(slot_ptr(tail));
|
||||
ready_[tail].store(false, std::memory_order_release);
|
||||
tail_.store((tail + 1) & mask_, std::memory_order_release);
|
||||
return val;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool empty() const noexcept
|
||||
{
|
||||
return tail_.load(std::memory_order_acquire) ==
|
||||
head_.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
[[nodiscard]] static constexpr std::size_t capacity() noexcept
|
||||
{
|
||||
return N - 1;
|
||||
}
|
||||
|
||||
private:
|
||||
T* slot_ptr(std::size_t i) noexcept
|
||||
{
|
||||
return std::launder(reinterpret_cast<T*>(&storage_[i]));
|
||||
}
|
||||
|
||||
static constexpr std::size_t mask_ = N - 1;
|
||||
|
||||
struct alignas(alignof(T)) Slot
|
||||
{
|
||||
std::byte storage[sizeof(T)];
|
||||
};
|
||||
|
||||
alignas(64) std::atomic<std::size_t> head_{0};
|
||||
alignas(64) std::atomic<std::size_t> tail_{0};
|
||||
Slot storage_[N];
|
||||
std::atomic<bool> ready_[N]{};
|
||||
};
|
||||
|
||||
} // namespace Void::Ring
|
||||
Reference in new issue
Block a user