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);
|
||||
}
|
||||
Reference in new issue
Block a user