Fsm/PluginBehavior.hpp: IFsmBehaviorPack::register_into(Builder&) lets a plugin contribute states/events/guards/actions; plugins export it via extern "C" void_plugin_fsm_behaviors(). build_machine_def(pack) runs the pack through a Builder and validates. PluginManager::raw_symbol(name, symbol) resolves an optional extra entry point from a plugin's current .so (returns nullptr if absent), so the host can fetch the behaviour factory without widening the frozen IPlugin vtable. Integration flow (tests/internal/FsmPluginHotReload.cpp): build a machine def from the plugin pack, run it; on reload, rebuild from the reloaded pack and swap_definition between RTC steps. Same state names across versions ⇒ the live configuration is adopted seamlessly (machine keeps running, queue intact); v2's new "skip" transition goes live. Safety: plugin actions are std::functions whose code lives in the .so, so a PluginHandle is pinned for the machine's lifetime and dlclose_on_swap= false keeps the code pages mapped under in-flight frames. Tests: FsmPluginV1/V2 (same idle/busy/done states; v2 adds skip). Definition-swap test proves configuration survival + new behaviour; the stress test runs 4 producer threads posting while 5 v1<->v2 rebuild+swaps happen concurrently, then asserts exactly one active leaf. 967 tests green, stable across runs, ASan+UBSan clean. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
72 lines
1.8 KiB
C++
72 lines
1.8 KiB
C++
#include <Void/Fsm/Definition.hpp>
|
|
#include <Void/Fsm/PluginBehavior.hpp>
|
|
#include <Void/Plugin/Plugin.hpp>
|
|
|
|
#include <atomic>
|
|
#include <string_view>
|
|
|
|
// FSM behaviour plugin, v2. Same state/event names as v1 (so the host
|
|
// adopts the active configuration seamlessly on swap_definition) plus a
|
|
// new "skip" transition idle -> done that proves the reloaded behaviour
|
|
// is live.
|
|
|
|
namespace
|
|
{
|
|
std::atomic<int> g_action_runs{0};
|
|
|
|
class FsmPlugin final : public Void::Plugin::IPlugin,
|
|
public Void::Fsm::IFsmBehaviorPack
|
|
{
|
|
public:
|
|
|
|
[[nodiscard]] std::string_view name() const noexcept override
|
|
{
|
|
return "FsmPlugin";
|
|
}
|
|
|
|
void register_into(Void::Fsm::Builder& b) override
|
|
{
|
|
using namespace Void::Fsm;
|
|
auto idle = b.state("idle");
|
|
auto busy = b.state("busy");
|
|
auto done = b.state("done");
|
|
b.initial(idle);
|
|
const auto start = b.event("start");
|
|
const auto finish = b.event("finish");
|
|
const auto reset = b.event("reset");
|
|
const auto skip = b.event("skip"); // new in v2
|
|
b.transition(idle, start, busy)
|
|
.action(SyncAction{[](Machine&, const Event&) {
|
|
g_action_runs.fetch_add(1, std::memory_order_relaxed);
|
|
}});
|
|
b.transition(busy, finish, done);
|
|
b.transition(done, reset, idle);
|
|
b.transition(idle, skip, done); // new behaviour
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
extern "C" {
|
|
|
|
void* void_plugin_create() { return new FsmPlugin; }
|
|
|
|
void void_plugin_destroy(void* p) { delete static_cast<FsmPlugin*>(p); }
|
|
|
|
Void::Plugin::PluginInfo void_plugin_info()
|
|
{
|
|
return {
|
|
.abi_version = Void::Plugin::ABI_VERSION,
|
|
.name = "FsmPlugin",
|
|
.plugin_version = "2.0.0",
|
|
.description = "FSM behaviour plugin (v2)",
|
|
};
|
|
}
|
|
|
|
Void::Fsm::IFsmBehaviorPack* void_plugin_fsm_behaviors()
|
|
{
|
|
static FsmPlugin pack;
|
|
return &pack;
|
|
}
|
|
|
|
} // extern "C"
|