Integrate FSM with Plugin: states/actions from plugins, hot reload
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>
This commit is contained in:
1 parent
b343bce396
commit
c988f635e9
8 files changed
+403
No files matched your search
@@ -146,6 +146,8 @@ Other key points:
|
||||
- **Persistence**: `serialize_state()` → JSON with leaf/history *names*; `restore_state(json)` on a stopped machine re-activates silently (no entry actions).
|
||||
- History records are snapshotted at exit time in `perform_transition` (not incrementally) — keep it that way; incremental recording accumulates stale leaves.
|
||||
|
||||
**FSM behaviours from plugins** (`Fsm/PluginBehavior.hpp`): a plugin implements `IFsmBehaviorPack::register_into(Builder&)` and exports `extern "C" IFsmBehaviorPack* void_plugin_fsm_behaviors()`. The host resolves it via `PluginManager::raw_symbol(name, kFsmBehaviorsSymbol)`, calls `build_machine_def(pack)`, and on `on_reloaded` rebuilds + `swap_definition` — same state names ⇒ the running machine keeps its configuration. Plugin-provided actions are `std::function`s whose code lives in the `.so`; keep a `PluginHandle` pinned and rely on `dlclose_on_swap=false` so the code pages stay mapped under in-flight coroutine frames. See `tests/internal/FsmPluginHotReload.cpp`.
|
||||
|
||||
### Plugin module (hot reload)
|
||||
|
||||
`Void::Plugin` loads C-ABI plugins (`.so`) and hot-reloads them with state transfer:
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Fsm/Definition.hpp>
|
||||
|
||||
#include <expected>
|
||||
#include <memory>
|
||||
|
||||
namespace Void::Fsm
|
||||
{
|
||||
// ── IFsmBehaviorPack ──────────────────────────────────────────────────
|
||||
// A plugin exposes FSM states/events/guards/actions by implementing
|
||||
// this interface and returning it from the optional C entry point
|
||||
//
|
||||
// extern "C" Void::Fsm::IFsmBehaviorPack* void_plugin_fsm_behaviors();
|
||||
//
|
||||
// The host calls register_into(builder) while assembling a machine
|
||||
// definition, then build()s it. Because the actions/guards the pack
|
||||
// installs are std::functions whose code lives in the plugin .so, the
|
||||
// machine (and its MachineDef) must not outlive the plugin: keep a
|
||||
// Plugin::PluginHandle pinned for the machine's lifetime, and rely on
|
||||
// PluginManager's dlclose_on_swap=false so the code pages stay mapped
|
||||
// across reloads. See tests/internal/FsmPluginHotReload.cpp.
|
||||
//
|
||||
// The interface is a same-compiler C++ ABI (like IPlugin): the host and
|
||||
// plugin must be built with the same toolchain, which they are within
|
||||
// this project.
|
||||
class IFsmBehaviorPack
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~IFsmBehaviorPack() = default;
|
||||
|
||||
/// Declare this pack's states, events, and transitions on the
|
||||
/// builder. Called once per machine-definition build.
|
||||
virtual void register_into(Builder& builder) = 0;
|
||||
};
|
||||
|
||||
using fsm_behaviors_fn = IFsmBehaviorPack* (*)();
|
||||
|
||||
inline constexpr const char* kFsmBehaviorsSymbol =
|
||||
"void_plugin_fsm_behaviors";
|
||||
|
||||
/// Build an immutable machine definition from a behaviour pack.
|
||||
[[nodiscard]] inline std::expected<std::shared_ptr<const MachineDef>,
|
||||
DefError>
|
||||
build_machine_def(IFsmBehaviorPack& pack)
|
||||
{
|
||||
Builder builder;
|
||||
pack.register_into(builder);
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
} // namespace Void::Fsm
|
||||
@@ -115,6 +115,13 @@ namespace Void::Plugin
|
||||
|
||||
[[nodiscard]] std::vector<std::string> names() const;
|
||||
|
||||
/// Resolve an optional extra entry point exported by the
|
||||
/// plugin's current .so (e.g. an FSM behaviour factory).
|
||||
/// Returns nullptr if the plugin or symbol is absent.
|
||||
/// reinterpret_cast the result to the expected function type.
|
||||
[[nodiscard]] void* raw_symbol(std::string_view name,
|
||||
const char* symbol) const;
|
||||
|
||||
private:
|
||||
|
||||
struct Entry {
|
||||
|
||||
@@ -351,4 +351,28 @@ namespace Void::Plugin
|
||||
return out;
|
||||
}
|
||||
|
||||
void* PluginManager::raw_symbol(std::string_view name,
|
||||
const char* symbol) const
|
||||
{
|
||||
#ifdef VOID_HAS_DLOPEN
|
||||
std::shared_ptr<detail::Instance> inst;
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
auto* entry = find(name);
|
||||
if (entry == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
inst = entry->current.load(std::memory_order_acquire);
|
||||
}
|
||||
if (!inst || inst->handle == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
return dlsym(inst->handle, symbol);
|
||||
#else
|
||||
(void)name;
|
||||
(void)symbol;
|
||||
return nullptr;
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace Void::Plugin
|
||||
@@ -116,4 +116,22 @@ if(TARGET Void::Plugin)
|
||||
target_include_directories(VoidTests PRIVATE
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/plugins")
|
||||
add_dependencies(VoidTests VoidStatefulPluginV1 VoidStatefulPluginV2)
|
||||
|
||||
# FSM behaviour plugins (states/actions provided by a plugin) for the
|
||||
# FSM x Plugin hot-reload integration test. Same state names across
|
||||
# versions so swap_definition adopts the live configuration.
|
||||
if(TARGET Void::Fsm)
|
||||
add_library(VoidFsmPluginV1 SHARED
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/plugins/FsmPluginV1.cpp")
|
||||
add_library(VoidFsmPluginV2 SHARED
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/plugins/FsmPluginV2.cpp")
|
||||
foreach(_fp VoidFsmPluginV1 VoidFsmPluginV2)
|
||||
target_link_libraries(${_fp} PRIVATE Void::Plugin Void::Fsm)
|
||||
endforeach()
|
||||
target_compile_definitions(VoidTests PRIVATE
|
||||
"VOID_FSM_PLUGIN_V1_PATH=\"$<TARGET_FILE:VoidFsmPluginV1>\""
|
||||
"VOID_FSM_PLUGIN_V2_PATH=\"$<TARGET_FILE:VoidFsmPluginV2>\""
|
||||
)
|
||||
add_dependencies(VoidTests VoidFsmPluginV1 VoidFsmPluginV2)
|
||||
endif()
|
||||
endif()
|
||||
@@ -0,0 +1,158 @@
|
||||
#include <Void/Fsm/Machine.hpp>
|
||||
#include <Void/Fsm/PluginBehavior.hpp>
|
||||
#include <Void/Plugin/Manager.hpp>
|
||||
#include <Void/System/TempDir.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#if defined(VOID_FSM_PLUGIN_V1_PATH) && defined(VOID_FSM_PLUGIN_V2_PATH) && \
|
||||
__has_include(<dlfcn.h>)
|
||||
|
||||
using namespace Void::Fsm;
|
||||
using Void::Plugin::PluginManager;
|
||||
|
||||
namespace
|
||||
{
|
||||
void install(const std::filesystem::path& src,
|
||||
const std::filesystem::path& dst)
|
||||
{
|
||||
auto staging = dst;
|
||||
staging += ".incoming";
|
||||
std::filesystem::copy_file(
|
||||
src, staging, std::filesystem::copy_options::overwrite_existing);
|
||||
std::filesystem::rename(staging, dst);
|
||||
}
|
||||
|
||||
// Build a machine definition from the plugin's behaviour pack.
|
||||
std::shared_ptr<const MachineDef> def_from(const PluginManager& mgr)
|
||||
{
|
||||
auto* sym = reinterpret_cast<fsm_behaviors_fn>(
|
||||
mgr.raw_symbol("FsmPlugin", kFsmBehaviorsSymbol));
|
||||
Void::Testing::expect(sym != nullptr);
|
||||
if (sym == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
auto built = build_machine_def(*sym());
|
||||
Void::Testing::expect(built.has_value());
|
||||
return built.value_or(nullptr);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// A machine whose states and actions come entirely from a plugin keeps
|
||||
// running across a plugin reload: the new definition is built from the
|
||||
// reloaded behaviour pack and swapped in between run-to-completion steps.
|
||||
Void::Testing::Test internal_fsm_plugin_definition_swap()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp{"void_fsmplugin"};
|
||||
const auto so = tmp.path() / "fsm.so";
|
||||
std::filesystem::copy_file(VOID_FSM_PLUGIN_V1_PATH, so);
|
||||
|
||||
PluginManager mgr;
|
||||
Void::Testing::expect(mgr.load(so).has_value());
|
||||
|
||||
auto def = def_from(mgr);
|
||||
Void::Testing::expect(def != nullptr);
|
||||
|
||||
Machine machine{def, MachineOptions{.own_worker = false}};
|
||||
machine.start();
|
||||
machine.post(Event{*def->event_id("start"), {}});
|
||||
machine.run_pending();
|
||||
Void::Testing::expect(machine.is_in("busy"));
|
||||
|
||||
// Reload to v2 (adds a "skip" transition; same state names) and swap
|
||||
// the definition in. The machine keeps its active "busy" leaf.
|
||||
install(VOID_FSM_PLUGIN_V2_PATH, so);
|
||||
Void::Testing::expect(mgr.reload("FsmPlugin").has_value());
|
||||
auto def2 = def_from(mgr);
|
||||
Void::Testing::expect(def2 != nullptr);
|
||||
machine.swap_definition(def2);
|
||||
machine.run_pending();
|
||||
Void::Testing::expect(machine.is_in("busy")); // configuration survived
|
||||
|
||||
// Finish the v1 cycle, then exercise the v2-only "skip" transition.
|
||||
machine.post(Event{*def2->event_id("finish"), {}});
|
||||
machine.run_pending();
|
||||
Void::Testing::expect(machine.is_in("done"));
|
||||
machine.post(Event{*def2->event_id("reset"), {}});
|
||||
machine.run_pending();
|
||||
Void::Testing::expect(machine.is_in("idle"));
|
||||
machine.post(Event{*def2->event_id("skip"), {}}); // new in v2
|
||||
machine.run_pending();
|
||||
Void::Testing::expect(machine.is_in("done"));
|
||||
}
|
||||
|
||||
// @tag stress, concurrency
|
||||
Void::Testing::Test internal_fsm_plugin_hot_reload_under_load()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp{"void_fsmplugin"};
|
||||
const auto so = tmp.path() / "fsm.so";
|
||||
std::filesystem::copy_file(VOID_FSM_PLUGIN_V1_PATH, so);
|
||||
|
||||
PluginManager mgr;
|
||||
Void::Testing::expect(mgr.load(so).has_value());
|
||||
auto def = def_from(mgr);
|
||||
|
||||
// Worker-driven machine; the plugin's .so stays mapped (the manager's
|
||||
// dlclose_on_swap=false) so its action code remains valid for frames
|
||||
// in flight. Pin a handle for the machine's lifetime as belt-and-
|
||||
// suspenders against the .so unmapping.
|
||||
auto pin = mgr.acquire("FsmPlugin");
|
||||
Void::Testing::expect(static_cast<bool>(pin));
|
||||
|
||||
Machine machine{def};
|
||||
machine.start();
|
||||
|
||||
const auto start = *def->event_id("start");
|
||||
const auto finish = *def->event_id("finish");
|
||||
const auto reset = *def->event_id("reset");
|
||||
|
||||
std::atomic<bool> stop{false};
|
||||
std::atomic<long long> posts{0};
|
||||
std::vector<std::thread> producers;
|
||||
producers.reserve(4);
|
||||
for (int p = 0; p < 4; ++p) {
|
||||
producers.emplace_back([&] {
|
||||
while (!stop.load(std::memory_order_relaxed)) {
|
||||
machine.post(Event{start, {}});
|
||||
machine.post(Event{finish, {}});
|
||||
machine.post(Event{reset, {}});
|
||||
posts.fetch_add(3, std::memory_order_relaxed);
|
||||
std::this_thread::yield();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Rebuild + swap the definition repeatedly while events flow.
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
const char* src =
|
||||
(i % 2 == 0) ? VOID_FSM_PLUGIN_V2_PATH : VOID_FSM_PLUGIN_V1_PATH;
|
||||
install(src, so);
|
||||
Void::Testing::expect(mgr.reload("FsmPlugin").has_value());
|
||||
machine.swap_definition(def_from(mgr));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{3});
|
||||
}
|
||||
|
||||
stop.store(true);
|
||||
for (auto& t : producers) {
|
||||
t.join();
|
||||
}
|
||||
|
||||
// Drain and confirm the machine is still coherent: exactly one of the
|
||||
// three leaves is active.
|
||||
(void)machine.send_wait(Event{reset, {}});
|
||||
const int active = static_cast<int>(machine.is_in("idle")) +
|
||||
static_cast<int>(machine.is_in("busy")) +
|
||||
static_cast<int>(machine.is_in("done"));
|
||||
Void::Testing::expect_eq(active, 1);
|
||||
Void::Testing::expect(posts.load() > 0);
|
||||
machine.stop();
|
||||
}
|
||||
|
||||
#endif // FSM plugins available
|
||||
@@ -0,0 +1,70 @@
|
||||
#include <Void/Fsm/Definition.hpp>
|
||||
#include <Void/Fsm/PluginBehavior.hpp>
|
||||
#include <Void/Plugin/Plugin.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <string_view>
|
||||
|
||||
// FSM behaviour plugin, v1. Defines a 3-state cycle (idle -> busy -> done
|
||||
// -> idle) and counts how many times its action ran. v2 keeps the same
|
||||
// state/event names so the host can swap_definition seamlessly.
|
||||
|
||||
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");
|
||||
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);
|
||||
}
|
||||
};
|
||||
} // 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 = "1.0.0",
|
||||
.description = "FSM behaviour plugin (v1)",
|
||||
};
|
||||
}
|
||||
|
||||
// Stateless factory: returns a fresh pack the host uses only during
|
||||
// definition build. It does not need to be the IPlugin instance.
|
||||
Void::Fsm::IFsmBehaviorPack* void_plugin_fsm_behaviors()
|
||||
{
|
||||
static FsmPlugin pack;
|
||||
return &pack;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -0,0 +1,71 @@
|
||||
#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"
|
||||
Reference in new issue
Block a user