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:
1 parent
7ca0e1b62e
commit
ee4486e1d2
15 files changed
+750
-24
No files matched your search
+151
-11
@@ -7,6 +7,7 @@
|
||||
#define VOID_HAS_DLOPEN 1
|
||||
#endif
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <system_error>
|
||||
#include <utility>
|
||||
@@ -21,6 +22,7 @@ namespace Void::Plugin
|
||||
IPlugin* plugin{nullptr};
|
||||
destroy_fn destroy{nullptr};
|
||||
transfer_fn transfer{nullptr};
|
||||
configure_fn configure{nullptr};
|
||||
state_version_fn state_version{nullptr};
|
||||
serialize_fn serialize{nullptr};
|
||||
free_string_fn free_string{nullptr};
|
||||
@@ -115,6 +117,8 @@ namespace Void::Plugin
|
||||
inst->destroy = destroy_sym;
|
||||
inst->transfer =
|
||||
reinterpret_cast<transfer_fn>(dlsym(h, kTransferSymbol));
|
||||
inst->configure =
|
||||
reinterpret_cast<configure_fn>(dlsym(h, kConfigureSymbol));
|
||||
inst->state_version = reinterpret_cast<state_version_fn>(
|
||||
dlsym(h, kStateVersionSymbol));
|
||||
inst->serialize =
|
||||
@@ -213,8 +217,12 @@ namespace Void::Plugin
|
||||
path.string(), to_string(inst.error()));
|
||||
return std::unexpected{inst.error()};
|
||||
}
|
||||
(*inst)->plugin->on_load();
|
||||
std::string name{(*inst)->plugin->name()};
|
||||
if (m_options.config_provider && (*inst)->configure != nullptr) {
|
||||
const std::string config = m_options.config_provider(name);
|
||||
(*inst)->configure((*inst)->plugin, config.c_str());
|
||||
}
|
||||
(*inst)->plugin->on_load();
|
||||
|
||||
std::lock_guard lock(m_mutex);
|
||||
auto entry = std::make_unique<Entry>();
|
||||
@@ -228,6 +236,52 @@ namespace Void::Plugin
|
||||
return name;
|
||||
}
|
||||
|
||||
std::vector<
|
||||
std::pair<std::filesystem::path, std::expected<std::string, LoadError>>>
|
||||
PluginManager::load_directory(const std::filesystem::path& dir)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
constexpr std::string_view ext = ".dll";
|
||||
#elif defined(__APPLE__)
|
||||
constexpr std::string_view ext = ".dylib";
|
||||
#else
|
||||
constexpr std::string_view ext = ".so";
|
||||
#endif
|
||||
std::vector<std::filesystem::path> candidates;
|
||||
std::error_code ec;
|
||||
for (const auto& entry : std::filesystem::directory_iterator{dir, ec}) {
|
||||
if (entry.is_regular_file(ec) && entry.path().extension() == ext) {
|
||||
candidates.push_back(entry.path());
|
||||
}
|
||||
}
|
||||
std::sort(candidates.begin(), candidates.end());
|
||||
|
||||
std::vector<std::pair<std::filesystem::path,
|
||||
std::expected<std::string, LoadError>>>
|
||||
out;
|
||||
out.reserve(candidates.size());
|
||||
for (auto& path : candidates) {
|
||||
auto result = load(path);
|
||||
out.emplace_back(std::move(path), std::move(result));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::expected<void, LoadError> PluginManager::unload(std::string_view name)
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
auto* entry = find(name);
|
||||
if (entry == nullptr) {
|
||||
return std::unexpected{LoadError::UnknownPlugin};
|
||||
}
|
||||
m_watcher.unwatch(entry->path);
|
||||
// Erasing drops the registry's reference; outstanding handles keep
|
||||
// the instance (and its mapped .so) alive until they drop.
|
||||
m_plugins.erase(std::string{name});
|
||||
Void::Log::info("Plugin.Manager", "unloaded '{}'", name);
|
||||
return {};
|
||||
}
|
||||
|
||||
PluginHandle PluginManager::acquire(std::string_view name) const
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
@@ -241,12 +295,23 @@ namespace Void::Plugin
|
||||
|
||||
std::expected<void, LoadError> PluginManager::reload(std::string_view name)
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
auto* entry = find(name);
|
||||
if (entry == nullptr) {
|
||||
return std::unexpected{LoadError::UnknownPlugin};
|
||||
std::vector<std::function<void(std::string_view)>> callbacks;
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
auto* entry = find(name);
|
||||
if (entry == nullptr) {
|
||||
return std::unexpected{LoadError::UnknownPlugin};
|
||||
}
|
||||
auto result = reload_locked(*entry, name);
|
||||
if (!result.has_value()) {
|
||||
return result;
|
||||
}
|
||||
callbacks = reload_callbacks_locked();
|
||||
}
|
||||
return reload_locked(*entry, name);
|
||||
for (const auto& cb : callbacks) {
|
||||
cb(name);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, LoadError>
|
||||
@@ -271,6 +336,11 @@ namespace Void::Plugin
|
||||
return std::unexpected{next.error()};
|
||||
}
|
||||
|
||||
if (m_options.config_provider && (*next)->configure != nullptr) {
|
||||
const std::string config = m_options.config_provider(name);
|
||||
(*next)->configure((*next)->plugin, config.c_str());
|
||||
}
|
||||
|
||||
// Transfer state with both instances alive (transactional).
|
||||
auto old = entry.current.load(std::memory_order_acquire);
|
||||
if (old) {
|
||||
@@ -306,7 +376,7 @@ namespace Void::Plugin
|
||||
if (!e.exists) {
|
||||
return;
|
||||
}
|
||||
std::function<void(std::string_view)> cb;
|
||||
std::vector<std::function<void(std::string_view)>> callbacks;
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
auto* entry = find(name_str);
|
||||
@@ -316,9 +386,9 @@ namespace Void::Plugin
|
||||
if (!reload_locked(*entry, name_str).has_value()) {
|
||||
return;
|
||||
}
|
||||
cb = m_on_reloaded;
|
||||
callbacks = reload_callbacks_locked();
|
||||
}
|
||||
if (cb) {
|
||||
for (const auto& cb : callbacks) {
|
||||
cb(name_str);
|
||||
}
|
||||
});
|
||||
@@ -333,11 +403,81 @@ namespace Void::Plugin
|
||||
}
|
||||
}
|
||||
|
||||
void PluginManager::on_reloaded(
|
||||
ReloadCallbackId PluginManager::on_reloaded(
|
||||
std::function<void(std::string_view name)> cb)
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
m_on_reloaded = std::move(cb);
|
||||
const ReloadCallbackId id = m_next_reload_id++;
|
||||
m_on_reloaded.emplace_back(id, std::move(cb));
|
||||
return id;
|
||||
}
|
||||
|
||||
void PluginManager::remove_on_reloaded(ReloadCallbackId id)
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::erase_if(m_on_reloaded,
|
||||
[id](const auto& pair) { return pair.first == id; });
|
||||
}
|
||||
|
||||
std::vector<std::function<void(std::string_view)>>
|
||||
PluginManager::reload_callbacks_locked() const
|
||||
{
|
||||
std::vector<std::function<void(std::string_view)>> out;
|
||||
out.reserve(m_on_reloaded.size());
|
||||
for (const auto& [_, cb] : m_on_reloaded) {
|
||||
out.push_back(cb);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::optional<PluginManager::StateSnapshot>
|
||||
PluginManager::serialize_state(std::string_view name) const
|
||||
{
|
||||
std::shared_ptr<detail::Instance> inst;
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
auto* entry = find(name);
|
||||
if (entry == nullptr) {
|
||||
return std::nullopt;
|
||||
}
|
||||
inst = entry->current.load(std::memory_order_acquire);
|
||||
}
|
||||
if (!inst || inst->serialize == nullptr) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const char* json = inst->serialize(inst->plugin);
|
||||
if (json == nullptr) {
|
||||
return std::nullopt;
|
||||
}
|
||||
StateSnapshot snapshot{.json = std::string{json},
|
||||
.version = inst->state_version != nullptr
|
||||
? inst->state_version()
|
||||
: 0};
|
||||
if (inst->free_string != nullptr) {
|
||||
inst->free_string(json);
|
||||
}
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
bool PluginManager::deserialize_state(std::string_view name,
|
||||
std::string_view json,
|
||||
std::uint32_t from_version)
|
||||
{
|
||||
std::shared_ptr<detail::Instance> inst;
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
auto* entry = find(name);
|
||||
if (entry == nullptr) {
|
||||
return false;
|
||||
}
|
||||
inst = entry->current.load(std::memory_order_acquire);
|
||||
}
|
||||
if (!inst || inst->deserialize == nullptr) {
|
||||
return false;
|
||||
}
|
||||
const std::string terminated{json};
|
||||
return inst->deserialize(inst->plugin, terminated.c_str(),
|
||||
from_version) == 0;
|
||||
}
|
||||
|
||||
std::vector<std::string> PluginManager::names() const
|
||||
|
||||
Reference in new issue
Block a user