1 parent
9a54f15e7e
commit
f94366104e
7 files changed
+100
-13
No files matched your search
+23
-4
@@ -23,6 +23,7 @@ std::string_view to_string(LoadError e) noexcept
|
||||
case LoadError::CreateFailed: return "plugin create() returned null";
|
||||
case LoadError::NotSupported: return "platform does not support dlopen";
|
||||
case LoadError::StateRejected: return "new instance rejected state";
|
||||
case LoadError::ActivationFailed: return "plugin activation failed";
|
||||
case LoadError::UnknownPlugin: return "no plugin registered under name";
|
||||
}
|
||||
return "unknown";
|
||||
@@ -44,6 +45,7 @@ void PluginLoader::destroy_current() noexcept
|
||||
}
|
||||
destroy_ = nullptr;
|
||||
transfer_ = nullptr;
|
||||
ready_ = nullptr;
|
||||
state_version_ = nullptr;
|
||||
serialize_ = nullptr;
|
||||
free_string_ = nullptr;
|
||||
@@ -121,6 +123,7 @@ PluginLoader::load_from(const std::filesystem::path& path)
|
||||
plugin_ = static_cast<IPlugin*>(raw);
|
||||
destroy_ = sym_destroy;
|
||||
transfer_ = reinterpret_cast<transfer_fn>(dlsym(h, kTransferSymbol));
|
||||
ready_ = reinterpret_cast<ready_fn>(dlsym(h, kReadySymbol));
|
||||
state_version_ = reinterpret_cast<state_version_fn>(
|
||||
dlsym(h, kStateVersionSymbol));
|
||||
serialize_ = reinterpret_cast<serialize_fn>(dlsym(h, kSerializeSymbol));
|
||||
@@ -153,7 +156,14 @@ PluginLoader::load(std::filesystem::path path)
|
||||
return std::unexpected{r.error()};
|
||||
}
|
||||
|
||||
ldr.plugin_->on_load();
|
||||
try {
|
||||
ldr.plugin_->on_load();
|
||||
} catch (...) {
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
if (ldr.ready_ && ldr.ready_(ldr.plugin_) != 0) {
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
Void::Log::info("Plugin", "loaded '{}' ({} v{})", path.string(),
|
||||
ldr.info_.name, ldr.info_.plugin_version);
|
||||
return ldr;
|
||||
@@ -166,6 +176,7 @@ PluginLoader::PluginLoader(PluginLoader&& o) noexcept
|
||||
, plugin_ {std::exchange(o.plugin_, nullptr)}
|
||||
, destroy_ {std::exchange(o.destroy_, nullptr)}
|
||||
, transfer_ {std::exchange(o.transfer_, nullptr)}
|
||||
, ready_ {std::exchange(o.ready_, nullptr)}
|
||||
, state_version_{std::exchange(o.state_version_, nullptr)}
|
||||
, serialize_ {std::exchange(o.serialize_, nullptr)}
|
||||
, free_string_ {std::exchange(o.free_string_, nullptr)}
|
||||
@@ -182,6 +193,7 @@ PluginLoader& PluginLoader::operator=(PluginLoader&& o) noexcept
|
||||
plugin_ = std::exchange(o.plugin_, nullptr);
|
||||
destroy_ = std::exchange(o.destroy_, nullptr);
|
||||
transfer_ = std::exchange(o.transfer_, nullptr);
|
||||
ready_ = std::exchange(o.ready_, nullptr);
|
||||
state_version_ = std::exchange(o.state_version_, nullptr);
|
||||
serialize_ = std::exchange(o.serialize_, nullptr);
|
||||
free_string_ = std::exchange(o.free_string_, nullptr);
|
||||
@@ -228,11 +240,18 @@ std::expected<void, LoadError> PluginLoader::reload()
|
||||
return std::unexpected{LoadError::StateRejected};
|
||||
}
|
||||
|
||||
// Destroy old, install new.
|
||||
try {
|
||||
next.plugin_->on_load();
|
||||
} catch (...) {
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
if (next.ready_ && next.ready_(next.plugin_) != 0) {
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
|
||||
// Activation succeeded; only now destroy old and install new.
|
||||
destroy_current();
|
||||
*this = std::move(next);
|
||||
|
||||
plugin_->on_load();
|
||||
Void::Log::info("Plugin", "reloaded '{}' ({} v{})", path_.string(),
|
||||
info_.name, info_.plugin_version);
|
||||
|
||||
|
||||
+23
-2
@@ -25,6 +25,7 @@ namespace Void::Plugin
|
||||
destroy_fn destroy{nullptr};
|
||||
transfer_fn transfer{nullptr};
|
||||
configure_fn configure{nullptr};
|
||||
ready_fn ready{nullptr};
|
||||
state_version_fn state_version{nullptr};
|
||||
serialize_fn serialize{nullptr};
|
||||
free_string_fn free_string{nullptr};
|
||||
@@ -121,6 +122,8 @@ namespace Void::Plugin
|
||||
reinterpret_cast<transfer_fn>(dlsym(h, kTransferSymbol));
|
||||
inst->configure =
|
||||
reinterpret_cast<configure_fn>(dlsym(h, kConfigureSymbol));
|
||||
inst->ready =
|
||||
reinterpret_cast<ready_fn>(dlsym(h, kReadySymbol));
|
||||
inst->state_version = reinterpret_cast<state_version_fn>(
|
||||
dlsym(h, kStateVersionSymbol));
|
||||
inst->serialize =
|
||||
@@ -243,7 +246,15 @@ namespace Void::Plugin
|
||||
const std::string config = m_options.config_provider(name);
|
||||
(*inst)->configure((*inst)->plugin, config.c_str());
|
||||
}
|
||||
(*inst)->plugin->on_load();
|
||||
try {
|
||||
(*inst)->plugin->on_load();
|
||||
} catch (...) {
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
if ((*inst)->ready != nullptr &&
|
||||
(*inst)->ready((*inst)->plugin) != 0) {
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
|
||||
std::lock_guard lock(m_mutex);
|
||||
auto entry = std::make_unique<Entry>();
|
||||
@@ -372,7 +383,17 @@ namespace Void::Plugin
|
||||
}
|
||||
}
|
||||
|
||||
(*next)->plugin->on_load();
|
||||
try {
|
||||
(*next)->plugin->on_load();
|
||||
} catch (...) {
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
if ((*next)->ready != nullptr &&
|
||||
(*next)->ready((*next)->plugin) != 0) {
|
||||
Void::Log::warn("Plugin.Manager",
|
||||
"reload '{}' aborted: activation failed", name);
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
// Atomic swap. The old instance keeps living while any acquired
|
||||
// handle still pins it; it is destroyed when the last one drops.
|
||||
entry.current.store(std::move(*next), std::memory_order_release);
|
||||
|
||||
Reference in new issue
Block a user