Plugin: discovery, unload, config entry, multi-observer reloads, state API

Host-side additions for applications embedding a plugin system:

- PluginManager::load_directory(dir) loads every shared-library file in
  a directory (filename order) with per-file expected results.
- PluginManager::unload(name) drops the registry entry; the instance
  dies with its last pinned handle (RCU semantics, as on reload).
- Optional C entry void_plugin_configure(inst, config): fed from
  PluginManagerOptions::config_provider after create and after each
  reload swap, before on_load().
- on_reloaded() is now multi-observer: returns a ReloadCallbackId,
  remove_on_reloaded() unregisters. Manual reload() now notifies
  observers too (previously only watched reloads did, contradicting
  the documented contract). PluginServiceBinding deregisters its
  callback on destruction (was a dangling-callback hazard).
- PluginManager::serialize_state/deserialize_state expose the plugin's
  v2 state exports so an embedder can carry plugin state through its
  own hot reload.
- ServiceRegistry::remove<I>() withdraws a provided service (child
  removal re-exposes a parent's entry).
- EventBus::subscribe_scoped<E>() returns a move-only RAII
  Subscription that unsubscribes on destruction.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018yj92cCihyaYtf4gKCT3nv
This commit is contained in:
faumarayandClaude Fable 5 committed 2026-07-11 23:39:10 +03:00
1 parent 7ca0e1b62e
commit ee4486e1d2
15 files changed
+750 -24

No files matched your search

+64 -4
View File
@@ -5,14 +5,17 @@
#include <atomic>
#include <chrono>
#include <cstdint>
#include <expected>
#include <filesystem>
#include <functional>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>
namespace Void::Plugin
@@ -71,8 +74,17 @@ namespace Void::Plugin
// executing would crash. Pins already prevent the instance from
// being destroyed; this guards the code pages.
bool dlclose_on_swap = false;
// Per-plugin configuration source. When set, its result is passed
// to the plugin's optional void_plugin_configure entry point after
// create and after each reload swap, before on_load(). The plugin
// name argument is the registry name (IPlugin::name()).
std::function<std::string(std::string_view plugin_name)>
config_provider;
};
// Identifies an on_reloaded observer for remove_on_reloaded.
using ReloadCallbackId = std::size_t;
class PluginManager
{
public:
@@ -92,6 +104,22 @@ namespace Void::Plugin
std::expected<std::string, LoadError>
load(const std::filesystem::path& path);
/// Load every plugin file in `dir` (non-recursive), in filename
/// order for determinism. Only files with the platform's shared-
/// library extension are considered. Returns one entry per
/// candidate: its path and either the loaded plugin's name or
/// the load error. An empty result means no candidates existed
/// (or the directory does not).
std::vector<std::pair<std::filesystem::path,
std::expected<std::string, LoadError>>>
load_directory(const std::filesystem::path& dir);
/// Unload `name`: stop watching it and drop it from the
/// registry. The instance is destroyed once the last
/// outstanding PluginHandle drops (RCU semantics, same as a
/// reload swap).
std::expected<void, LoadError> unload(std::string_view name);
/// Pin the current instance for `name`. The returned handle
/// keeps it alive for as long as it lives. Empty handle if no
/// such plugin. One atomic load on the hot path.
@@ -109,9 +137,36 @@ namespace Void::Plugin
void watch(std::string_view name);
void unwatch(std::string_view name);
/// Invoked after every successful reload (manual or watched),
/// on the caller's / watcher's thread.
void on_reloaded(std::function<void(std::string_view name)> cb);
/// Register an observer invoked after every successful reload
/// (manual or watched), on the reloading thread. Multiple
/// observers coexist; the returned id removes this one.
ReloadCallbackId
on_reloaded(std::function<void(std::string_view name)> cb);
void remove_on_reloaded(ReloadCallbackId id);
/// A point-in-time state snapshot taken through the plugin's v2
/// serialize export, for embedders that need to carry plugin
/// state through their *own* lifecycle (e.g. the host process
/// hot-reloading itself). `version` is the plugin's state
/// schema version at snapshot time.
struct StateSnapshot {
std::string json;
std::uint32_t version{0};
};
/// Snapshot `name`'s current state. Empty when the plugin is
/// unknown, exports no v2 serialize, or serialization fails.
[[nodiscard]] std::optional<StateSnapshot>
serialize_state(std::string_view name) const;
/// Inject a serialized state into `name`'s current instance via
/// its v2 deserialize export. `from_version` is the schema
/// version the JSON was produced with (migrations live in the
/// plugin). Returns false when the plugin is unknown, exports
/// no deserialize, or rejects the state.
bool deserialize_state(std::string_view name,
std::string_view json,
std::uint32_t from_version);
[[nodiscard]] std::vector<std::string> names() const;
@@ -132,11 +187,16 @@ namespace Void::Plugin
Entry* find(std::string_view name) const;
std::expected<void, LoadError> reload_locked(Entry& entry,
std::string_view name);
std::vector<std::function<void(std::string_view)>>
reload_callbacks_locked() const;
Options m_options;
mutable std::mutex m_mutex; // guards the registry map + reloads
std::unordered_map<std::string, std::unique_ptr<Entry>> m_plugins;
std::function<void(std::string_view)> m_on_reloaded;
std::vector<std::pair<ReloadCallbackId,
std::function<void(std::string_view)>>>
m_on_reloaded;
ReloadCallbackId m_next_reload_id{1};
FileSystem::FileWatcher m_watcher;
};