Add Async lock-free infrastructure and reflection JSON serializer
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>
This commit is contained in:
1 parent
28b94883fd
commit
8a6065539a
6 files changed
+1150
No files matched your search
@@ -0,0 +1,150 @@
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Async/Task.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
using Void::Async::ManualExecutor;
|
||||
using Void::Async::schedule_on;
|
||||
using Void::Async::spawn;
|
||||
using Void::Coro::Task;
|
||||
|
||||
// ── schedule_on ───────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test executor_schedule_on_hops_to_drain()
|
||||
{
|
||||
ManualExecutor ex;
|
||||
std::vector<std::string> order;
|
||||
|
||||
spawn(ex, [](ManualExecutor& ex2, std::vector<std::string>& log)
|
||||
-> Task<void> {
|
||||
log.push_back("before");
|
||||
co_await schedule_on(ex2);
|
||||
log.push_back("after");
|
||||
}(ex, order));
|
||||
|
||||
// spawn() posts the wrapper; nothing ran yet.
|
||||
Void::Testing::expect_eq(order.size(), std::size_t{0});
|
||||
|
||||
ex.drain();
|
||||
Void::Testing::expect_eq(order.size(), std::size_t{2});
|
||||
Void::Testing::expect_eq(order[0], std::string{"before"});
|
||||
Void::Testing::expect_eq(order[1], std::string{"after"});
|
||||
}
|
||||
|
||||
Void::Testing::Test executor_run_one_steps_individually()
|
||||
{
|
||||
ManualExecutor ex;
|
||||
int counter = 0;
|
||||
|
||||
spawn(ex, [](ManualExecutor& ex2, int& c) -> Task<void> {
|
||||
++c;
|
||||
co_await schedule_on(ex2);
|
||||
++c;
|
||||
}(ex, counter));
|
||||
|
||||
Void::Testing::expect(ex.run_one()); // runs until the re-schedule point
|
||||
Void::Testing::expect_eq(counter, 1);
|
||||
Void::Testing::expect(ex.run_one()); // resumes after the hop
|
||||
Void::Testing::expect_eq(counter, 2);
|
||||
Void::Testing::expect(!ex.run_one());
|
||||
Void::Testing::expect(ex.empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test executor_drain_returns_resume_count()
|
||||
{
|
||||
ManualExecutor ex;
|
||||
int counter = 0;
|
||||
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
spawn(ex, [](int& c) -> Task<void> {
|
||||
++c;
|
||||
co_return;
|
||||
}(counter));
|
||||
}
|
||||
|
||||
const auto resumed = ex.drain();
|
||||
Void::Testing::expect_eq(resumed, std::size_t{3});
|
||||
Void::Testing::expect_eq(counter, 3);
|
||||
}
|
||||
|
||||
// ── spawn lifetime ────────────────────────────────────────────────────────────
|
||||
|
||||
namespace
|
||||
{
|
||||
struct DtorProbe {
|
||||
int* flag;
|
||||
~DtorProbe()
|
||||
{
|
||||
if (flag) {
|
||||
++*flag;
|
||||
}
|
||||
}
|
||||
DtorProbe(int* f) : flag{f} {}
|
||||
DtorProbe(DtorProbe&& o) noexcept : flag{o.flag} { o.flag = nullptr; }
|
||||
DtorProbe(const DtorProbe&) = delete;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test executor_spawn_destroys_frame_on_completion()
|
||||
{
|
||||
ManualExecutor ex;
|
||||
int destroyed = 0;
|
||||
|
||||
spawn(ex, [](DtorProbe probe) -> Task<void> {
|
||||
(void)probe;
|
||||
co_return;
|
||||
}(DtorProbe{&destroyed}));
|
||||
|
||||
Void::Testing::expect_eq(destroyed, 0);
|
||||
ex.drain();
|
||||
// Both the task frame and the wrapper frame are gone; the probe captured
|
||||
// by value inside the coroutine frame must have been destroyed.
|
||||
Void::Testing::expect_eq(destroyed, 1);
|
||||
}
|
||||
|
||||
Void::Testing::Test executor_nested_task_awaiting()
|
||||
{
|
||||
ManualExecutor ex;
|
||||
int result = 0;
|
||||
|
||||
spawn(ex, [](int& out) -> Task<void> {
|
||||
auto inner = []() -> Task<int> { co_return 21; };
|
||||
out = co_await inner() + co_await inner();
|
||||
}(result));
|
||||
|
||||
ex.drain();
|
||||
Void::Testing::expect_eq(result, 42);
|
||||
}
|
||||
|
||||
// ── cross-thread posting ──────────────────────────────────────────────────────
|
||||
|
||||
// @tag concurrency
|
||||
Void::Testing::Test executor_post_from_other_threads()
|
||||
{
|
||||
ManualExecutor ex;
|
||||
int counter = 0;
|
||||
constexpr int tasks = 100;
|
||||
|
||||
std::vector<std::thread> threads;
|
||||
threads.reserve(4);
|
||||
for (int t = 0; t < 4; ++t) {
|
||||
threads.emplace_back([&ex, &counter] {
|
||||
for (int i = 0; i < tasks / 4; ++i) {
|
||||
spawn(ex, [](int& c) -> Task<void> {
|
||||
++c;
|
||||
co_return;
|
||||
}(counter));
|
||||
}
|
||||
});
|
||||
}
|
||||
for (auto& t : threads) {
|
||||
t.join();
|
||||
}
|
||||
|
||||
ex.drain();
|
||||
Void::Testing::expect_eq(counter, tasks);
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
#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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
#include <Void/Data/Reflect.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifdef VOID_HAS_JSON_REFLECTION
|
||||
|
||||
namespace
|
||||
{
|
||||
enum class Color { Red, Green, Blue };
|
||||
|
||||
struct Point {
|
||||
double x = 0;
|
||||
double y = 0;
|
||||
};
|
||||
|
||||
struct Shape {
|
||||
std::string name;
|
||||
Color color = Color::Red;
|
||||
std::vector<Point> points;
|
||||
std::optional<double> opacity;
|
||||
std::map<std::string, int> tags;
|
||||
bool visible = true;
|
||||
int layer = 0;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// ── round trips ───────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test reflect_flat_round_trip()
|
||||
{
|
||||
const Point p{1.5, -2.0};
|
||||
const auto j = Void::Json::to_json(p);
|
||||
Void::Testing::expect(j.is_object());
|
||||
Void::Testing::expect_eq(j["x"].as_number(), 1.5);
|
||||
|
||||
const auto back = Void::Json::from_json<Point>(j);
|
||||
Void::Testing::expect(back.has_value());
|
||||
Void::Testing::expect_eq(back->x, 1.5);
|
||||
Void::Testing::expect_eq(back->y, -2.0);
|
||||
}
|
||||
|
||||
Void::Testing::Test reflect_nested_round_trip()
|
||||
{
|
||||
Shape s;
|
||||
s.name = "tri";
|
||||
s.color = Color::Blue;
|
||||
s.points = {{0, 0}, {1, 0}, {0, 1}};
|
||||
s.opacity = 0.5;
|
||||
s.tags = {{"kind", 3}, {"z", 9}};
|
||||
s.visible = false;
|
||||
s.layer = 7;
|
||||
|
||||
const auto j = Void::Json::to_json(s);
|
||||
const auto back = Void::Json::from_json<Shape>(j);
|
||||
Void::Testing::expect(back.has_value());
|
||||
Void::Testing::expect_eq(back->name, std::string{"tri"});
|
||||
Void::Testing::expect(back->color == Color::Blue);
|
||||
Void::Testing::expect_eq(back->points.size(), std::size_t{3});
|
||||
Void::Testing::expect_eq(back->points[2].y, 1.0);
|
||||
Void::Testing::expect(back->opacity.has_value());
|
||||
Void::Testing::expect_eq(*back->opacity, 0.5);
|
||||
Void::Testing::expect_eq(back->tags.at("kind"), 3);
|
||||
Void::Testing::expect(!back->visible);
|
||||
Void::Testing::expect_eq(back->layer, 7);
|
||||
}
|
||||
|
||||
Void::Testing::Test reflect_enum_serializes_by_name()
|
||||
{
|
||||
const Shape s{.name = "x", .color = Color::Green};
|
||||
const auto j = Void::Json::to_json(s);
|
||||
Void::Testing::expect_eq(j["color"].as_string(), std::string{"Green"});
|
||||
}
|
||||
|
||||
Void::Testing::Test reflect_optional_null_round_trip()
|
||||
{
|
||||
const Shape s{.name = "noop"};
|
||||
const auto j = Void::Json::to_json(s);
|
||||
Void::Testing::expect(j["opacity"].is_null());
|
||||
|
||||
const auto back = Void::Json::from_json<Shape>(j);
|
||||
Void::Testing::expect(back.has_value());
|
||||
Void::Testing::expect(!back->opacity.has_value());
|
||||
}
|
||||
|
||||
// ── tolerance ─────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test reflect_unknown_fields_ignored()
|
||||
{
|
||||
const auto j = Void::Json::parse(
|
||||
R"({"x": 1.0, "y": 2.0, "future_field": "whatever"})");
|
||||
Void::Testing::expect(j.has_value());
|
||||
const auto p = Void::Json::from_json<Point>(*j);
|
||||
Void::Testing::expect(p.has_value());
|
||||
Void::Testing::expect_eq(p->x, 1.0);
|
||||
}
|
||||
|
||||
Void::Testing::Test reflect_missing_fields_keep_defaults()
|
||||
{
|
||||
const auto j = Void::Json::parse(R"({"name": "partial"})");
|
||||
Void::Testing::expect(j.has_value());
|
||||
const auto s = Void::Json::from_json<Shape>(*j);
|
||||
Void::Testing::expect(s.has_value());
|
||||
Void::Testing::expect_eq(s->name, std::string{"partial"});
|
||||
Void::Testing::expect(s->visible); // default kept
|
||||
Void::Testing::expect_eq(s->layer, 0);
|
||||
}
|
||||
|
||||
// ── errors ────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test reflect_type_mismatch_reports_field_path()
|
||||
{
|
||||
const auto j = Void::Json::parse(
|
||||
R"({"name": "s", "points": [{"x": 0, "y": 0}, {"x": "oops"}]})");
|
||||
Void::Testing::expect(j.has_value());
|
||||
const auto s = Void::Json::from_json<Shape>(*j);
|
||||
Void::Testing::expect(!s.has_value());
|
||||
Void::Testing::expect_eq(s.error().field, std::string{"points.[1].x"});
|
||||
}
|
||||
|
||||
Void::Testing::Test reflect_non_object_rejected()
|
||||
{
|
||||
const auto s = Void::Json::from_json<Shape>(Void::Json::Value{42});
|
||||
Void::Testing::expect(!s.has_value());
|
||||
}
|
||||
|
||||
// ── versioned envelope + migrations ───────────────────────────────────────────
|
||||
|
||||
namespace
|
||||
{
|
||||
struct StateV3 {
|
||||
std::string title; // v1: "name", v2: renamed to "title"
|
||||
int count = 0; // v1/v2
|
||||
bool enabled = true; // added in v3
|
||||
};
|
||||
} // namespace
|
||||
|
||||
template <>
|
||||
struct Void::Json::StateTraits<StateV3> {
|
||||
static constexpr std::uint32_t version = 3;
|
||||
};
|
||||
|
||||
namespace
|
||||
{
|
||||
Void::Json::MigrationChain state_migrations()
|
||||
{
|
||||
Void::Json::MigrationChain chain;
|
||||
// v1 -> v2: rename "name" to "title"
|
||||
chain.add(1, [](Void::Json::Value data) {
|
||||
if (auto* name = data.find("name")) {
|
||||
data.as_object().emplace_back("title", std::move(*name));
|
||||
}
|
||||
return data;
|
||||
});
|
||||
// v2 -> v3: nothing structural; "enabled" appears with its default
|
||||
chain.add(2, [](Void::Json::Value data) { return data; });
|
||||
return chain;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test reflect_versioned_envelope_round_trip()
|
||||
{
|
||||
const StateV3 s{.title = "t", .count = 5, .enabled = false};
|
||||
const auto j = Void::Json::to_versioned_json(s);
|
||||
Void::Testing::expect_eq(j["__version"].as_int(), std::int64_t{3});
|
||||
|
||||
const auto back = Void::Json::from_versioned_json<StateV3>(j);
|
||||
Void::Testing::expect(back.has_value());
|
||||
Void::Testing::expect_eq(back->count, 5);
|
||||
Void::Testing::expect(!back->enabled);
|
||||
}
|
||||
|
||||
Void::Testing::Test reflect_migration_v1_to_v3()
|
||||
{
|
||||
const auto v1 = Void::Json::parse(
|
||||
R"({"__version": 1, "data": {"name": "legacy", "count": 9}})");
|
||||
Void::Testing::expect(v1.has_value());
|
||||
|
||||
const auto s =
|
||||
Void::Json::from_versioned_json<StateV3>(*v1, state_migrations());
|
||||
Void::Testing::expect(s.has_value());
|
||||
Void::Testing::expect_eq(s->title, std::string{"legacy"});
|
||||
Void::Testing::expect_eq(s->count, 9);
|
||||
Void::Testing::expect(s->enabled); // default for the field added in v3
|
||||
}
|
||||
|
||||
Void::Testing::Test reflect_migration_missing_step_fails()
|
||||
{
|
||||
const auto v1 = Void::Json::parse(
|
||||
R"({"__version": 1, "data": {"name": "legacy"}})");
|
||||
Void::Testing::expect(v1.has_value());
|
||||
|
||||
const auto s = Void::Json::from_versioned_json<StateV3>(*v1); // no chain
|
||||
Void::Testing::expect(!s.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test reflect_newer_version_rejected()
|
||||
{
|
||||
const auto v9 = Void::Json::parse(R"({"__version": 9, "data": {}})");
|
||||
Void::Testing::expect(v9.has_value());
|
||||
const auto s = Void::Json::from_versioned_json<StateV3>(*v9);
|
||||
Void::Testing::expect(!s.has_value());
|
||||
}
|
||||
|
||||
// ── customization point ───────────────────────────────────────────────────────
|
||||
|
||||
namespace
|
||||
{
|
||||
struct Custom {
|
||||
int doubled = 0;
|
||||
|
||||
[[nodiscard]] Void::Json::Value to_json() const
|
||||
{
|
||||
return Void::Json::Value{doubled / 2};
|
||||
}
|
||||
|
||||
static std::expected<Custom, Void::Json::ReflectError>
|
||||
from_json(const Void::Json::Value& v)
|
||||
{
|
||||
if (!v.is_number()) {
|
||||
return std::unexpected{
|
||||
Void::Json::ReflectError{"", "expected number"}};
|
||||
}
|
||||
return Custom{static_cast<int>(v.as_number()) * 2};
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test reflect_member_customization_wins()
|
||||
{
|
||||
const Custom c{10};
|
||||
const auto j = Void::Json::to_json(c);
|
||||
Void::Testing::expect(j.is_number());
|
||||
Void::Testing::expect_eq(j.as_int(), std::int64_t{5});
|
||||
|
||||
const auto back = Void::Json::from_json<Custom>(j);
|
||||
Void::Testing::expect(back.has_value());
|
||||
Void::Testing::expect_eq(back->doubled, 10);
|
||||
}
|
||||
|
||||
#endif // VOID_HAS_JSON_REFLECTION
|
||||
Reference in new issue
Block a user