From 388e75011fb304e8439b376994f6641d7c880fcb Mon Sep 17 00:00:00 2001 From: faumaray Date: Thu, 14 May 2026 22:54:35 +0300 Subject: [PATCH] Add Observable, Pool, Ring modules MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Void::Observable — Observable reactive value with typed on_change listeners and unsubscribe-by-handle; Signal event broadcast with connect/disconnect. Void::Pool — ObjectPool slab-based pool with unique_ptr ownership; FixedPool fixed-capacity in-place pool, returns nullptr when full. Void::Ring — SpscRing single-producer/single-consumer wait-free ring buffer; MpscRing 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 --- include/Void/Observable/Observable.hpp | 139 ++++++++++++++++ include/Void/Pool/Pool.hpp | 166 +++++++++++++++++++ include/Void/Ring/Ring.hpp | 179 +++++++++++++++++++++ tests/standalone/Observable/Observable.cpp | 143 ++++++++++++++++ tests/standalone/Pool/Pool.cpp | 103 ++++++++++++ tests/standalone/Ring/Ring.cpp | 146 +++++++++++++++++ 6 files changed, 876 insertions(+) create mode 100644 include/Void/Observable/Observable.hpp create mode 100644 include/Void/Pool/Pool.hpp create mode 100644 include/Void/Ring/Ring.hpp create mode 100644 tests/standalone/Observable/Observable.cpp create mode 100644 tests/standalone/Pool/Pool.cpp create mode 100644 tests/standalone/Ring/Ring.cpp diff --git a/include/Void/Observable/Observable.hpp b/include/Void/Observable/Observable.hpp new file mode 100644 index 0000000..88f5b0c --- /dev/null +++ b/include/Void/Observable/Observable.hpp @@ -0,0 +1,139 @@ +#pragma once + +#include +#include +#include +#include +#include + +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 ───────────────────────────────────────────────────────────── +// 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 +class Observable +{ +public: + using Listener = std::function; + + 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 listeners_; +}; + +// ── Signal ─────────────────────────────────────────────────────────── +// An event signal that observers connect to. emit() calls all connected slots. + +template +class Signal +{ +public: + using Slot = std::function; + + 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 slots_; +}; + +} // namespace Void::Observable diff --git a/include/Void/Pool/Pool.hpp b/include/Void/Pool/Pool.hpp new file mode 100644 index 0000000..a910664 --- /dev/null +++ b/include/Void/Pool/Pool.hpp @@ -0,0 +1,166 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace Void::Pool +{ + +// ── ObjectPool ────────────────────────────────────────────────── +// A slab-based object pool. acquire() returns a unique_ptr 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 +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; + + template + [[nodiscard]] Ptr acquire(Args&&... args) + { + T* slot = pop_free(); + if (!slot) { + slot = allocate_slot(); + } + std::construct_at(slot, std::forward(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(); + // Push all but the first slot into free_. + for (std::size_t i = 1; i < ChunkSize; ++i) { + free_.push_back(reinterpret_cast(&slab->storage[i])); + } + return reinterpret_cast(&slab->storage[0]); + } + + struct alignas(alignof(T)) Slot + { + std::byte storage[sizeof(T)]; + }; + + struct Slab + { + Slot storage[ChunkSize]; + }; + + std::vector> slabs_; + std::vector free_; +}; + +// ── FixedPool ─────────────────────────────────────────────────────────── +// A fixed-capacity pool allocated in-place (no heap after construction). +// Capacity is known at compile time; acquire() returns nullptr when full. + +template +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; + + template + [[nodiscard]] Ptr acquire(Args&&... args) noexcept( + std::is_nothrow_constructible_v) + { + if (free_count_ == 0) return nullptr; + T* slot = free_[--free_count_]; + std::construct_at(slot, std::forward(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(&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 diff --git a/include/Void/Ring/Ring.hpp b/include/Void/Ring/Ring.hpp new file mode 100644 index 0000000..a0f6b4e --- /dev/null +++ b/include/Void/Ring/Ring.hpp @@ -0,0 +1,179 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace Void::Ring +{ + +// ── SpscRing ──────────────────────────────────────────────────────────── +// 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 + 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) + { + 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) + { + 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 pop() noexcept( + std::is_nothrow_move_constructible_v && + std::is_nothrow_destructible_v) + { + 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(&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 head_{0}; + alignas(64) std::atomic tail_{0}; + Slot storage_[N]; +}; + +// ── MpscRing ──────────────────────────────────────────────────────────── +// Multi-producer / single-consumer ring buffer using a compare-exchange loop +// on the head pointer. Consumer is still single-threaded. + +template + 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) + { + 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 pop() noexcept( + std::is_nothrow_move_constructible_v) + { + 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(&storage_[i])); + } + + static constexpr std::size_t mask_ = N - 1; + + struct alignas(alignof(T)) Slot + { + std::byte storage[sizeof(T)]; + }; + + alignas(64) std::atomic head_{0}; + alignas(64) std::atomic tail_{0}; + Slot storage_[N]; + std::atomic ready_[N]{}; +}; + +} // namespace Void::Ring diff --git a/tests/standalone/Observable/Observable.cpp b/tests/standalone/Observable/Observable.cpp new file mode 100644 index 0000000..91408cc --- /dev/null +++ b/tests/standalone/Observable/Observable.cpp @@ -0,0 +1,143 @@ +#include +#include +#include + +#include +#include + +// ── Observable ───────────────────────────────────────────────────────────── + +Void::Testing::Test observable_initial_value() +{ + Void::Observable::Observable obs{42}; + Void::Testing::expect_eq(obs.get(), 42); +} + +Void::Testing::Test observable_set_changes_value() +{ + Void::Observable::Observable obs{0}; + obs.set(7); + Void::Testing::expect_eq(obs.get(), 7); +} + +Void::Testing::Test observable_listener_called_on_change() +{ + Void::Observable::Observable obs{0}; + int seen_new = -1; + obs.on_change([&](int, int n) { seen_new = n; }); + obs.set(5); + Void::Testing::expect_eq(seen_new, 5); +} + +Void::Testing::Test observable_listener_receives_old_and_new() +{ + Void::Observable::Observable obs{10}; + int old_val = -1, new_val = -1; + obs.on_change([&](int o, int n) { old_val = o; new_val = n; }); + obs.set(20); + Void::Testing::expect_eq(old_val, 10); + Void::Testing::expect_eq(new_val, 20); +} + +Void::Testing::Test observable_no_notification_on_same_value() +{ + Void::Observable::Observable obs{5}; + int call_count = 0; + obs.on_change([&](int, int) { ++call_count; }); + obs.set(5); + Void::Testing::expect_eq(call_count, 0); +} + +Void::Testing::Test observable_multiple_listeners() +{ + Void::Observable::Observable obs{0}; + int a = 0, b = 0; + obs.on_change([&](int, int n) { a = n; }); + obs.on_change([&](int, int n) { b = n * 2; }); + obs.set(3); + Void::Testing::expect_eq(a, 3); + Void::Testing::expect_eq(b, 6); +} + +Void::Testing::Test observable_unsubscribe_stops_notifications() +{ + Void::Observable::Observable obs{0}; + int count = 0; + auto h = obs.on_change([&](int, int) { ++count; }); + obs.set(1); + obs.unsubscribe(h); + obs.set(2); + Void::Testing::expect_eq(count, 1); +} + +Void::Testing::Test observable_unsubscribe_returns_false_for_unknown_handle() +{ + Void::Observable::Observable obs{0}; + Void::Testing::expect(!obs.unsubscribe(Void::Observable::Handle{999})); +} + +Void::Testing::Test observable_assign_operator() +{ + Void::Observable::Observable obs{"hello"}; + int count = 0; + obs.on_change([&](const std::string&, const std::string&) { ++count; }); + obs = std::string{"world"}; + Void::Testing::expect_eq(obs.get(), std::string{"world"}); + Void::Testing::expect_eq(count, 1); +} + +// ── Signal ─────────────────────────────────────────────────────────── + +Void::Testing::Test signal_emit_calls_slot() +{ + Void::Observable::Signal sig; + int received = -1; + sig.connect([&](const int& v) { received = v; }); + sig.emit(42); + Void::Testing::expect_eq(received, 42); +} + +Void::Testing::Test signal_multiple_slots() +{ + Void::Observable::Signal sig; + std::vector vals; + sig.connect([&](const int& v) { vals.push_back(v); }); + sig.connect([&](const int& v) { vals.push_back(v * 10); }); + sig.emit(3); + Void::Testing::expect_eq(vals.size(), std::size_t{2}); + Void::Testing::expect_eq(vals[0], 3); + Void::Testing::expect_eq(vals[1], 30); +} + +Void::Testing::Test signal_disconnect_stops_slot() +{ + Void::Observable::Signal sig; + int count = 0; + auto h = sig.connect([&](const int&) { ++count; }); + sig.emit(1); + sig.disconnect(h); + sig.emit(2); + Void::Testing::expect_eq(count, 1); +} + +Void::Testing::Test signal_clear_removes_all_slots() +{ + Void::Observable::Signal<> sig; + int count = 0; + sig.connect([&] { ++count; }); + sig.connect([&] { ++count; }); + sig.clear(); + sig.emit(); + Void::Testing::expect_eq(count, 0); +} + +Void::Testing::Test signal_multi_arg_emit() +{ + Void::Observable::Signal sig; + int n = 0; + std::string s; + sig.connect([&](const int& a, const std::string& b) { n = a; s = b; }); + sig.emit(7, std::string{"hello"}); + Void::Testing::expect_eq(n, 7); + Void::Testing::expect_eq(s, std::string{"hello"}); +} diff --git a/tests/standalone/Pool/Pool.cpp b/tests/standalone/Pool/Pool.cpp new file mode 100644 index 0000000..ffdbfae --- /dev/null +++ b/tests/standalone/Pool/Pool.cpp @@ -0,0 +1,103 @@ +#include +#include +#include + +#include + +// ── ObjectPool ──────────────────────────────────────────────────────────────── + +Void::Testing::Test object_pool_acquire_returns_valid_ptr() +{ + Void::Pool::ObjectPool pool; + auto p = pool.acquire(42); + Void::Testing::expect(p != nullptr); + Void::Testing::expect_eq(*p, 42); +} + +Void::Testing::Test object_pool_release_on_ptr_destruction() +{ + Void::Pool::ObjectPool pool; + Void::Testing::expect_eq(pool.available(), std::size_t{0}); + { + auto p = pool.acquire(1); + // capacity grew to 4, 3 slots free + Void::Testing::expect_eq(pool.available(), std::size_t{3}); + } + // Released back to pool + Void::Testing::expect_eq(pool.available(), std::size_t{4}); +} + +Void::Testing::Test object_pool_reuses_memory() +{ + Void::Pool::ObjectPool pool; + int* raw1 = nullptr; + { + auto p = pool.acquire(1); + raw1 = p.get(); + } + auto p2 = pool.acquire(2); + // After release the same slot should be reused (LIFO from free list) + Void::Testing::expect(p2.get() == raw1); +} + +Void::Testing::Test object_pool_string_construction() +{ + Void::Pool::ObjectPool pool; + auto p = pool.acquire("hello"); + Void::Testing::expect_eq(*p, std::string{"hello"}); +} + +Void::Testing::Test object_pool_capacity_grows() +{ + Void::Pool::ObjectPool pool; + Void::Testing::expect_eq(pool.capacity(), std::size_t{0}); + auto p = pool.acquire(1); + Void::Testing::expect_eq(pool.capacity(), std::size_t{4}); +} + +// ── FixedPool ───────────────────────────────────────────────────────────────── + +Void::Testing::Test fixed_pool_capacity() +{ + Void::Pool::FixedPool pool; + static_assert(pool.capacity() == 8); + Void::Testing::expect_eq(pool.available(), std::size_t{8}); +} + +Void::Testing::Test fixed_pool_acquire_decrements_available() +{ + Void::Pool::FixedPool pool; + auto p = pool.acquire(99); + Void::Testing::expect_eq(pool.available(), std::size_t{3}); + Void::Testing::expect_eq(*p, 99); +} + +Void::Testing::Test fixed_pool_returns_null_when_full() +{ + Void::Pool::FixedPool pool; + auto p1 = pool.acquire(1); + auto p2 = pool.acquire(2); + auto p3 = pool.acquire(3); // should be nullptr + Void::Testing::expect(p3 == nullptr); +} + +Void::Testing::Test fixed_pool_release_restores_available() +{ + Void::Pool::FixedPool pool; + { + auto p = pool.acquire(1); + Void::Testing::expect_eq(pool.available(), std::size_t{3}); + } + Void::Testing::expect_eq(pool.available(), std::size_t{4}); +} + +Void::Testing::Test fixed_pool_reuse_after_release() +{ + Void::Pool::FixedPool pool; + auto p1 = pool.acquire(1); + auto p2 = pool.acquire(2); + p1.reset(); + auto p3 = pool.acquire(3); + Void::Testing::expect(p3 != nullptr); + Void::Testing::expect_eq(*p3, 3); +} diff --git a/tests/standalone/Ring/Ring.cpp b/tests/standalone/Ring/Ring.cpp new file mode 100644 index 0000000..adc4de4 --- /dev/null +++ b/tests/standalone/Ring/Ring.cpp @@ -0,0 +1,146 @@ +#include +#include +#include + +#include +#include +#include + +// ── SpscRing ────────────────────────────────────────────────────────────────── + +Void::Testing::Test spsc_empty_on_construction() +{ + Void::Ring::SpscRing ring; + Void::Testing::expect(ring.empty()); +} + +Void::Testing::Test spsc_push_pop_single() +{ + Void::Ring::SpscRing 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 ring; + Void::Testing::expect(!ring.pop().has_value()); +} + +Void::Testing::Test spsc_full_push_returns_false() +{ + Void::Ring::SpscRing 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 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::capacity(), + std::size_t{15}); +} + +Void::Testing::Test spsc_full_flag() +{ + Void::Ring::SpscRing ring; + ring.push(1); ring.push(2); ring.push(3); + Void::Testing::expect(ring.full()); +} + +Void::Testing::Test spsc_string_values() +{ + Void::Ring::SpscRing 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 ring; + constexpr int N = 50; + std::vector 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 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 ring; + Void::Testing::expect(ring.empty()); +} + +// @tag threading +Void::Testing::Test mpsc_two_producers_one_consumer() +{ + Void::Ring::MpscRing ring; + constexpr int N = 40; + std::atomic 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 +}