feat(Containers): fix pool lease lifetimes/budgets and non-spinning MPSC ring (M-CONTAINERS-02, M-CONTAINERS-03)

This commit is contained in:
faumaray committed 2026-11-24 03:20:04 +03:00
1 parent 883bbdcf38
commit 9ac76bbcf9
7 files changed
+1252 -285

No files matched your search

+173
View File
@@ -2,8 +2,11 @@
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <atomic>
#include <cstdint>
#include <string>
#include <thread>
#include <utility>
#include <vector>
// ── SpscRing ──────────────────────────────────────────────────────────────────
@@ -144,3 +147,173 @@ Void::Testing::Test mpsc_two_producers_one_consumer()
p1.join(); p2.join(); consumer.join();
Void::Testing::expect_eq(total.load(), N * 3); // N×1 + N×2 = 3N
}
namespace
{
struct CountedItem
{
int value{0};
std::atomic<int>* dtors{nullptr};
CountedItem(int v, std::atomic<int>* d) noexcept : value{v}, dtors{d} {}
CountedItem(CountedItem&& o) noexcept
: value{o.value}, dtors{std::exchange(o.dtors, nullptr)}
{
}
CountedItem& operator=(CountedItem&& o) noexcept
{
if (this != &o) {
if (dtors) dtors->fetch_add(1, std::memory_order_relaxed);
value = o.value;
dtors = std::exchange(o.dtors, nullptr);
}
return *this;
}
~CountedItem()
{
if (dtors) {
dtors->fetch_add(1, std::memory_order_relaxed);
}
}
};
struct DelayedMoveItem
{
int id{0};
std::atomic<bool>* entered_move{nullptr};
std::atomic<bool>* release_move{nullptr};
DelayedMoveItem() noexcept = default;
DelayedMoveItem(int v, std::atomic<bool>* entered, std::atomic<bool>* release) noexcept
: id{v}, entered_move{entered}, release_move{release}
{
}
DelayedMoveItem(DelayedMoveItem&& other) noexcept
: id{other.id},
entered_move{nullptr},
release_move{nullptr}
{
if (other.entered_move && other.release_move) {
other.entered_move->store(true, std::memory_order_release);
while (!other.release_move->load(std::memory_order_acquire)) {
std::this_thread::yield();
}
}
}
DelayedMoveItem& operator=(DelayedMoveItem&&) noexcept = default;
~DelayedMoveItem() noexcept = default;
};
} // namespace
Void::Testing::Test spsc_and_mpsc_close_wraparound_and_destructor_cleanup()
{
using Void::Ring::PopStatus;
using Void::Ring::PushStatus;
std::atomic<int> spsc_dtors{0};
{
Void::Ring::SpscRing<CountedItem, 4> spsc;
for (int i = 0; i < 16; ++i) {
Void::Testing::expect(spsc.try_push(CountedItem{i, &spsc_dtors}) == PushStatus::ok);
auto popped = spsc.try_pop();
Void::Testing::expect(popped.status == PopStatus::ok && popped.value->value == i);
}
Void::Testing::expect(spsc.try_push(CountedItem{100, &spsc_dtors}) == PushStatus::ok);
Void::Testing::expect(spsc.try_push(CountedItem{101, &spsc_dtors}) == PushStatus::ok);
spsc.close();
Void::Testing::expect(spsc.try_push(CountedItem{102, &spsc_dtors}) == PushStatus::closed);
}
Void::Testing::expect_eq(spsc_dtors.load(), 19);
std::atomic<int> mpsc_dtors{0};
{
Void::Ring::MpscRing<CountedItem, 4> mpsc;
for (int i = 0; i < 20; ++i) {
Void::Testing::expect(mpsc.try_push(CountedItem{i, &mpsc_dtors}) == PushStatus::ok);
auto popped = mpsc.try_pop();
Void::Testing::expect(popped.status == PopStatus::ok && popped.value->value == i);
}
Void::Testing::expect(mpsc.try_push(CountedItem{200, &mpsc_dtors}) == PushStatus::ok);
Void::Testing::expect(mpsc.try_push(CountedItem{201, &mpsc_dtors}) == PushStatus::ok);
mpsc.close();
Void::Testing::expect(mpsc.try_push(CountedItem{202, &mpsc_dtors}) == PushStatus::closed);
}
Void::Testing::expect_eq(mpsc_dtors.load(), 23);
}
// @tag threading
Void::Testing::Test mpsc_delayed_producer_returns_pending_without_spinning()
{
using Void::Ring::PopStatus;
using Void::Ring::PushStatus;
Void::Ring::MpscRing<DelayedMoveItem, 8> ring;
std::atomic<bool> entered{false};
std::atomic<bool> release{false};
std::thread producer([&] {
Void::Testing::expect(ring.try_push(DelayedMoveItem{77, &entered, &release}) == PushStatus::ok);
});
while (!entered.load(std::memory_order_acquire)) {
std::this_thread::yield();
}
// Slot 0 is reserved (enqueue_pos_ == 1) but not yet published: must return pending immediately.
const auto pending = ring.try_pop();
Void::Testing::expect(pending.status == PopStatus::pending);
Void::Testing::expect(!pending.has_value());
release.store(true, std::memory_order_release);
producer.join();
const auto ready = ring.try_pop();
Void::Testing::expect(ready.status == PopStatus::ok);
Void::Testing::require(ready.has_value());
Void::Testing::expect_eq(ready.value->id, 77);
ring.close();
const auto closed = ring.try_pop();
Void::Testing::expect(closed.status == PopStatus::closed);
}
// @tag threading
Void::Testing::Test mpsc_high_contention_wraparound_stress()
{
Void::Ring::MpscRing<std::uint64_t, 16> ring;
constexpr int kProducers = 4;
constexpr std::uint64_t kPerProducer = 200;
std::vector<std::thread> producers;
for (int p = 0; p < kProducers; ++p) {
producers.emplace_back([&, p] {
for (std::uint64_t i = 0; i < kPerProducer; ++i) {
const std::uint64_t val =
(static_cast<std::uint64_t>(p) << 32) | i;
while (ring.try_push(val) != Void::Ring::PushStatus::ok) {
std::this_thread::yield();
}
}
});
}
std::uint64_t consumed = 0;
std::uint64_t xor_sum = 0;
while (consumed < kProducers * kPerProducer) {
auto r = ring.try_pop();
if (r.status == Void::Ring::PopStatus::ok) {
xor_sum ^= *r.value;
++consumed;
}
}
for (auto& t : producers) {
t.join();
}
std::uint64_t expected_xor = 0;
for (int p = 0; p < kProducers; ++p) {
for (std::uint64_t i = 0; i < kPerProducer; ++i) {
expected_xor ^= (static_cast<std::uint64_t>(p) << 32) | i;
}
}
Void::Testing::expect_eq(xor_sum, expected_xor);
}