Async (Void::Async):
- MpscQueue<T>: unbounded Vyukov multi-producer/single-consumer queue.
Wait-free push (XCHG + store, no CAS loop); consumer-owned reclamation so
no hazard pointers and no ABA. The transient mid-link empty window is
documented; try_pop/empty are re-poll hints.
- Executor/ManualExecutor: minimal scheduling bridge for Coro::Task with
lock-free post() from any thread and single-thread run_one()/drain();
schedule_on() awaitable and spawn() for detached task launch with a
self-destroying wrapper frame.
Data (Void::Json):
- Reflect.hpp: P2996 to_json/from_json for aggregate structs (field-name
keys; arithmetic/bool/string/enum-by-name/vector/optional/string-map/
nested aggregates), tolerant deserialization (unknown fields ignored,
missing fields keep defaults, mismatches report dotted field paths).
- Versioned envelopes {"__version", "data"} with StateTraits<T> and
MigrationChain for stepwise schema upgrades - groundwork for plugin
hot-reload state transfer.
- Member to_json/from_json customization wins over reflection and is the
only path on compilers without <meta> (Reflectable is false there).
Verified with GCC 16.1 (-freflection): 25 new tests including an
8-producer x 100k MPSC stress with per-producer FIFO assertion, coroutine
frame lifetime checks, and a 3-step v1->v3 migration; suite green under
ASan+UBSan.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
121 lines
3.5 KiB
C++
121 lines
3.5 KiB
C++
#include <Void/Async/MpscQueue.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <atomic>
|
|
#include <cstddef>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
using Void::Async::MpscQueue;
|
|
|
|
// ── single-threaded basics ────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test mpsc_starts_empty()
|
|
{
|
|
MpscQueue<int> q;
|
|
Void::Testing::expect(q.empty());
|
|
Void::Testing::expect(!q.try_pop().has_value());
|
|
}
|
|
|
|
Void::Testing::Test mpsc_push_pop_roundtrip()
|
|
{
|
|
MpscQueue<int> q;
|
|
q.push(1);
|
|
q.push(2);
|
|
q.push(3);
|
|
Void::Testing::expect(!q.empty());
|
|
Void::Testing::expect_eq(*q.try_pop(), 1);
|
|
Void::Testing::expect_eq(*q.try_pop(), 2);
|
|
Void::Testing::expect_eq(*q.try_pop(), 3);
|
|
Void::Testing::expect(!q.try_pop().has_value());
|
|
Void::Testing::expect(q.empty());
|
|
}
|
|
|
|
Void::Testing::Test mpsc_interleaved_push_pop()
|
|
{
|
|
MpscQueue<int> q;
|
|
q.push(1);
|
|
Void::Testing::expect_eq(*q.try_pop(), 1);
|
|
q.push(2);
|
|
q.push(3);
|
|
Void::Testing::expect_eq(*q.try_pop(), 2);
|
|
q.push(4);
|
|
Void::Testing::expect_eq(*q.try_pop(), 3);
|
|
Void::Testing::expect_eq(*q.try_pop(), 4);
|
|
Void::Testing::expect(!q.try_pop().has_value());
|
|
}
|
|
|
|
Void::Testing::Test mpsc_move_only_payload()
|
|
{
|
|
MpscQueue<std::unique_ptr<int>> q;
|
|
q.push(std::make_unique<int>(42));
|
|
auto v = q.try_pop();
|
|
Void::Testing::expect(v.has_value());
|
|
Void::Testing::expect_eq(**v, 42);
|
|
}
|
|
|
|
Void::Testing::Test mpsc_destructor_releases_pending_items()
|
|
{
|
|
// Leaks (pending nodes not freed) would be caught under ASan/valgrind.
|
|
MpscQueue<std::string> q;
|
|
q.push(std::string(256, 'a'));
|
|
q.push(std::string(256, 'b'));
|
|
}
|
|
|
|
// ── multi-producer stress ─────────────────────────────────────────────────────
|
|
|
|
// @tag stress, concurrency
|
|
Void::Testing::Test mpsc_multi_producer_stress()
|
|
{
|
|
constexpr int producers = 8;
|
|
constexpr int per_producer = 100'000;
|
|
|
|
MpscQueue<std::pair<int, int>> q; // {producer id, sequence}
|
|
std::atomic<bool> go{false};
|
|
|
|
std::vector<std::thread> threads;
|
|
threads.reserve(producers);
|
|
for (int p = 0; p < producers; ++p) {
|
|
threads.emplace_back([&q, &go, p] {
|
|
go.wait(false);
|
|
for (int i = 0; i < per_producer; ++i) {
|
|
q.push({p, i});
|
|
}
|
|
});
|
|
}
|
|
|
|
go = true;
|
|
go.notify_all();
|
|
|
|
// Consume on this thread while producers run. nullopt is only a hint
|
|
// (Vyukov mid-link window), so completion is tracked by count.
|
|
std::vector<int> next_seq(producers, 0);
|
|
long long received = 0;
|
|
constexpr long long total =
|
|
static_cast<long long>(producers) * per_producer;
|
|
while (received < total) {
|
|
auto item = q.try_pop();
|
|
if (!item) {
|
|
std::this_thread::yield();
|
|
continue;
|
|
}
|
|
const auto [p, seq] = *item;
|
|
// per-producer FIFO must hold
|
|
Void::Testing::expect_eq(seq, next_seq[static_cast<std::size_t>(p)]);
|
|
++next_seq[static_cast<std::size_t>(p)];
|
|
++received;
|
|
}
|
|
|
|
for (auto& t : threads) {
|
|
t.join();
|
|
}
|
|
Void::Testing::expect(!q.try_pop().has_value());
|
|
for (int p = 0; p < producers; ++p) {
|
|
Void::Testing::expect_eq(next_seq[static_cast<std::size_t>(p)],
|
|
per_producer);
|
|
}
|
|
}
|