1 parent
9a54f15e7e
commit
f94366104e
7 files changed
+100
-13
No files matched your search
+9
-7
@@ -23,13 +23,15 @@ function(void_add_module MODULE_NAME)
|
||||
add_library(${TGT} INTERFACE)
|
||||
target_compile_features(${TGT} INTERFACE cxx_std_26)
|
||||
else()
|
||||
set(LIB_TYPE "SHARED")
|
||||
set(SCOPE "PUBLIC")
|
||||
if (MODULE_NAME STREQUAL "Testing")
|
||||
add_library(${TGT} STATIC)
|
||||
else()
|
||||
add_library(${TGT} SHARED)
|
||||
endif()
|
||||
set(SCOPE "PUBLIC")
|
||||
if (MODULE_NAME STREQUAL "Testing")
|
||||
add_library(${TGT} STATIC)
|
||||
else()
|
||||
# Honour the standard BUILD_SHARED_LIBS policy. Consumers that ship a
|
||||
# single validated executable must be able to embed Void modules,
|
||||
# while Void's standalone default remains shared.
|
||||
add_library(${TGT})
|
||||
endif()
|
||||
target_sources(${TGT} PRIVATE ${SRC})
|
||||
target_compile_features(${TGT} PUBLIC cxx_std_26)
|
||||
endif()
|
||||
|
||||
@@ -17,6 +17,7 @@ enum class LoadError {
|
||||
CreateFailed,
|
||||
NotSupported, // platform does not support dlopen
|
||||
StateRejected, // new instance refused the serialized state
|
||||
ActivationFailed, // on_load threw or readiness validation failed
|
||||
UnknownPlugin, // PluginManager: no plugin registered under that name
|
||||
};
|
||||
|
||||
@@ -72,6 +73,7 @@ private:
|
||||
IPlugin* plugin_ {};
|
||||
destroy_fn destroy_ {};
|
||||
transfer_fn transfer_ {};
|
||||
ready_fn ready_ {};
|
||||
state_version_fn state_version_{};
|
||||
serialize_fn serialize_ {};
|
||||
free_string_fn free_string_ {};
|
||||
|
||||
@@ -55,6 +55,12 @@ public:
|
||||
// JSON) after create and after each hot-reload swap, before
|
||||
// on_load(). The string is only valid for the duration of the call.
|
||||
//
|
||||
// Optional (transactional activation check):
|
||||
// int void_plugin_ready(void* instance)
|
||||
// — called after on_load() and state restoration, immediately before
|
||||
// the instance becomes visible. Return 0 when the plugin is ready.
|
||||
// Any other value aborts load/reload and keeps the old instance.
|
||||
//
|
||||
// Optional v2 state protocol (preferred over transfer_state when both
|
||||
// sides provide it; survives layout changes between plugin versions):
|
||||
//
|
||||
@@ -74,6 +80,7 @@ using destroy_fn = void (*)(void*);
|
||||
using info_fn = PluginInfo (*)(void);
|
||||
using transfer_fn = void (*)(void* old_instance, void* new_instance);
|
||||
using configure_fn = void (*)(void* instance, const char* config);
|
||||
using ready_fn = int (*)(void* instance);
|
||||
using state_version_fn = std::uint32_t (*)(void);
|
||||
using serialize_fn = const char* (*)(void* instance);
|
||||
using free_string_fn = void (*)(const char*);
|
||||
@@ -85,6 +92,7 @@ inline constexpr const char* kDestroySymbol = "void_plugin_destroy";
|
||||
inline constexpr const char* kInfoSymbol = "void_plugin_info";
|
||||
inline constexpr const char* kTransferSymbol = "void_plugin_transfer_state";
|
||||
inline constexpr const char* kConfigureSymbol = "void_plugin_configure";
|
||||
inline constexpr const char* kReadySymbol = "void_plugin_ready";
|
||||
inline constexpr const char* kStateVersionSymbol = "void_plugin_state_version";
|
||||
inline constexpr const char* kSerializeSymbol = "void_plugin_serialize_state";
|
||||
inline constexpr const char* kFreeStringSymbol = "void_plugin_free_string";
|
||||
|
||||
+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);
|
||||
|
||||
@@ -44,6 +44,11 @@ namespace
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] bool ready() const noexcept
|
||||
{
|
||||
return config_ != "reject";
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::string config_;
|
||||
@@ -75,4 +80,9 @@ void void_plugin_configure(void* instance, const char* config)
|
||||
{
|
||||
static_cast<ConfigurablePlugin*>(instance)->apply_config(config);
|
||||
}
|
||||
|
||||
int void_plugin_ready(void* instance)
|
||||
{
|
||||
return static_cast<ConfigurablePlugin*>(instance)->ready() ? 0 : -1;
|
||||
}
|
||||
}
|
||||
@@ -67,4 +67,29 @@ Void::Testing::Test configure_skipped_without_provider()
|
||||
Void::Testing::expect_eq(configurable(h)->configure_calls(), 0);
|
||||
}
|
||||
|
||||
Void::Testing::Test activation_failure_keeps_current_instance()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp{"void_cfg_ready"};
|
||||
const auto so = tmp.path() / "plugin.so";
|
||||
std::filesystem::copy_file(VOID_CONFIGURABLE_PATH, so);
|
||||
|
||||
std::string payload{"accepted"};
|
||||
PluginManager mgr{{.config_provider = [&payload](std::string_view) {
|
||||
return payload;
|
||||
}}};
|
||||
Void::Testing::expect(mgr.load(so).has_value());
|
||||
auto before = mgr.acquire("ConfigurablePlugin");
|
||||
Void::Testing::expect_eq(configurable(before)->last_config(),
|
||||
std::string_view{"accepted"});
|
||||
|
||||
payload = "reject";
|
||||
const auto result = mgr.reload("ConfigurablePlugin");
|
||||
Void::Testing::expect(!result.has_value());
|
||||
Void::Testing::expect(result.error() == LoadError::ActivationFailed);
|
||||
auto after = mgr.acquire("ConfigurablePlugin");
|
||||
Void::Testing::expect(after.get() == before.get());
|
||||
Void::Testing::expect_eq(configurable(after)->last_config(),
|
||||
std::string_view{"accepted"});
|
||||
}
|
||||
|
||||
#endif // configurable plugin available
|
||||
Reference in new issue
Block a user