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:
faumarayandClaude Sonnet 4.6 committed 2026-05-14 22:54:35 +03:00
1 parent a9a2e2b59a
commit 388e75011f
6 files changed
+876

No files matched your search

+146
View File
@@ -0,0 +1,146 @@
#include <Void/Ring/Ring.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
#include <thread>
#include <vector>
// ── SpscRing ──────────────────────────────────────────────────────────────────
Void::Testing::Test spsc_empty_on_construction()
{
Void::Ring::SpscRing<int, 8> ring;
Void::Testing::expect(ring.empty());
}
Void::Testing::Test spsc_push_pop_single()
{
Void::Ring::SpscRing<int, 8> ring;
Void::Testing::expect(ring.push(42));
const auto v = ring.pop();
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(*v, 42);
}
Void::Testing::Test spsc_pop_empty_returns_nullopt()
{
Void::Ring::SpscRing<int, 8> ring;
Void::Testing::expect(!ring.pop().has_value());
}
Void::Testing::Test spsc_full_push_returns_false()
{
Void::Ring::SpscRing<int, 4> ring; // capacity = 3
Void::Testing::expect(ring.push(1));
Void::Testing::expect(ring.push(2));
Void::Testing::expect(ring.push(3));
Void::Testing::expect(!ring.push(4)); // full
}
Void::Testing::Test spsc_fifo_order()
{
Void::Ring::SpscRing<int, 8> ring;
for (int i = 0; i < 5; ++i) ring.push(i);
for (int i = 0; i < 5; ++i) {
const auto v = ring.pop();
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(*v, i);
}
}
Void::Testing::Test spsc_capacity_is_n_minus_one()
{
Void::Testing::expect_eq(Void::Ring::SpscRing<int, 16>::capacity(),
std::size_t{15});
}
Void::Testing::Test spsc_full_flag()
{
Void::Ring::SpscRing<int, 4> ring;
ring.push(1); ring.push(2); ring.push(3);
Void::Testing::expect(ring.full());
}
Void::Testing::Test spsc_string_values()
{
Void::Ring::SpscRing<std::string, 4> ring;
ring.push(std::string{"hello"});
const auto v = ring.pop();
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(*v, std::string{"hello"});
}
// @tag threading
Void::Testing::Test spsc_producer_consumer_threads()
{
Void::Ring::SpscRing<int, 64> ring;
constexpr int N = 50;
std::vector<int> received;
received.reserve(N);
std::thread producer([&] {
for (int i = 0; i < N; ++i) {
while (!ring.push(i)) {}
}
});
std::thread consumer([&] {
int got = 0;
while (got < N) {
auto v = ring.pop();
if (v) { received.push_back(*v); ++got; }
}
});
producer.join();
consumer.join();
Void::Testing::expect_eq(received.size(), std::size_t{N});
for (int i = 0; i < N; ++i) {
Void::Testing::expect_eq(received[i], i);
}
}
// ── MpscRing ──────────────────────────────────────────────────────────────────
Void::Testing::Test mpsc_push_pop_single()
{
Void::Ring::MpscRing<int, 8> ring;
Void::Testing::expect(ring.push(99));
const auto v = ring.pop();
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(*v, 99);
}
Void::Testing::Test mpsc_empty_on_construction()
{
Void::Ring::MpscRing<int, 8> ring;
Void::Testing::expect(ring.empty());
}
// @tag threading
Void::Testing::Test mpsc_two_producers_one_consumer()
{
Void::Ring::MpscRing<int, 128> ring;
constexpr int N = 40;
std::atomic<int> total{0};
std::thread p1([&] {
for (int i = 0; i < N; ++i) while (!ring.push(1)) {}
});
std::thread p2([&] {
for (int i = 0; i < N; ++i) while (!ring.push(2)) {}
});
std::thread consumer([&] {
int got = 0;
while (got < N * 2) {
auto v = ring.pop();
if (v) { total.fetch_add(*v, std::memory_order_relaxed); ++got; }
}
});
p1.join(); p2.join(); consumer.join();
Void::Testing::expect_eq(total.load(), N * 3); // N×1 + N×2 = 3N
}