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,115 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Async/MpscQueue.hpp>
|
||||
#include <Void/Async/Task.hpp>
|
||||
|
||||
#include <coroutine>
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
|
||||
namespace Void::Async
|
||||
{
|
||||
|
||||
// ── Executor ──────────────────────────────────────────────────────────────────
|
||||
// Minimal scheduling interface bridging Coro::Task to a driving thread.
|
||||
// post() must be safe to call from any thread.
|
||||
|
||||
class Executor
|
||||
{
|
||||
public:
|
||||
virtual ~Executor() = default;
|
||||
|
||||
// Schedule a coroutine for resumption on this executor. Thread-safe.
|
||||
virtual void post(std::coroutine_handle<> handle) = 0;
|
||||
};
|
||||
|
||||
// ── ManualExecutor ────────────────────────────────────────────────────────────
|
||||
// An executor whose queue is drained explicitly by its owning thread.
|
||||
// post() is lock-free from any thread; run_one()/drain() must only be
|
||||
// called from a single consumer thread (typically the owner's loop).
|
||||
|
||||
class ManualExecutor final : public Executor
|
||||
{
|
||||
public:
|
||||
void post(std::coroutine_handle<> handle) override
|
||||
{
|
||||
queue_.push(handle);
|
||||
}
|
||||
|
||||
// Resume one pending coroutine. Returns false if none was pending.
|
||||
bool run_one()
|
||||
{
|
||||
auto h = queue_.try_pop();
|
||||
if (!h) {
|
||||
return false;
|
||||
}
|
||||
h->resume();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Resume pending coroutines until the queue drains. Returns how many
|
||||
// coroutines were resumed (including ones posted while draining).
|
||||
std::size_t drain()
|
||||
{
|
||||
std::size_t count = 0;
|
||||
while (run_one()) {
|
||||
++count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool empty() const noexcept { return queue_.empty(); }
|
||||
|
||||
private:
|
||||
MpscQueue<std::coroutine_handle<>> queue_;
|
||||
};
|
||||
|
||||
// ── schedule_on ───────────────────────────────────────────────────────────────
|
||||
// Awaitable that re-schedules the awaiting coroutine onto an executor:
|
||||
//
|
||||
// co_await schedule_on(executor); // resumes on the executor's thread
|
||||
|
||||
struct ScheduleAwaiter {
|
||||
Executor& executor;
|
||||
|
||||
[[nodiscard]] bool await_ready() const noexcept { return false; }
|
||||
void await_suspend(std::coroutine_handle<> h) const { executor.post(h); }
|
||||
void await_resume() const noexcept {}
|
||||
};
|
||||
|
||||
[[nodiscard]] inline ScheduleAwaiter schedule_on(Executor& executor)
|
||||
{
|
||||
return ScheduleAwaiter{executor};
|
||||
}
|
||||
|
||||
// ── spawn ─────────────────────────────────────────────────────────────────────
|
||||
// Launch a Task<void> detached, driven on the given executor. The wrapper
|
||||
// coroutine owns the task; its frame self-destroys on completion. An
|
||||
// exception escaping the task terminates (there is no awaiter to receive
|
||||
// it) — handle errors inside the task.
|
||||
|
||||
namespace Detail
|
||||
{
|
||||
struct DetachedTask {
|
||||
struct promise_type {
|
||||
DetachedTask get_return_object() noexcept { return {}; }
|
||||
std::suspend_never initial_suspend() noexcept { return {}; }
|
||||
std::suspend_never final_suspend() noexcept { return {}; }
|
||||
void return_void() noexcept {}
|
||||
void unhandled_exception() noexcept { std::terminate(); }
|
||||
};
|
||||
};
|
||||
|
||||
inline DetachedTask spawn_impl(Executor& executor, Coro::Task<void> task)
|
||||
{
|
||||
co_await schedule_on(executor);
|
||||
co_await std::move(task);
|
||||
}
|
||||
} // namespace Detail
|
||||
|
||||
inline void spawn(Executor& executor, Coro::Task<void> task)
|
||||
{
|
||||
Detail::spawn_impl(executor, std::move(task));
|
||||
}
|
||||
|
||||
} // namespace Void::Async
|
||||
Reference in new issue
Block a user