unfortunately, this whole project was doomed from the start
unloading libraries is unsupported/will lead to serious runtime issues on most if not all platforms
This commit is contained in:
parent
9efcf6b526
commit
75f26afd01
3 changed files with 28 additions and 96 deletions
|
@ -13,7 +13,6 @@ pub type RegisterFn = *mut dyn Plugin;
|
|||
#[derive(Debug)]
|
||||
pub enum PluginResult {
|
||||
Ok,
|
||||
FunctionNotFound,
|
||||
Other(String),
|
||||
}
|
||||
|
||||
|
|
|
@ -16,11 +16,11 @@ impl Plugin for ExamplePlugin {
|
|||
fn register_functions(&self) -> HashMap<String, AsyncFn> {
|
||||
let mut map = HashMap::new();
|
||||
map.insert("example_function".to_string(), example_function as AsyncFn);
|
||||
dbg!(map)
|
||||
map
|
||||
}
|
||||
|
||||
fn unregister_functions(&self) -> Vec<String> {
|
||||
dbg!(vec!["example_function".to_string()])
|
||||
vec!["example_function".to_string()]
|
||||
}
|
||||
}
|
||||
|
||||
|
|
119
src/main.rs
119
src/main.rs
|
@ -1,15 +1,13 @@
|
|||
pub use host_tools::tokio;
|
||||
|
||||
use host_tools::tokio::runtime::Handle;
|
||||
use host_tools::tokio::sync::RwLock;
|
||||
use host_tools::tracing::{error, info};
|
||||
use host_tools::tracing::{error, info, warn};
|
||||
use host_tools::{AsyncFn, PluginResult, RegisterFn};
|
||||
use libloading::{Library, Symbol};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
fn get_name() -> String {
|
||||
"example_function".to_string()
|
||||
}
|
||||
|
||||
async fn load_plugin(
|
||||
lib_path: &str,
|
||||
function_map: Arc<RwLock<HashMap<String, AsyncFn>>>,
|
||||
|
@ -34,113 +32,48 @@ async fn unload_plugin(
|
|||
unsafe { lib.get(b"register_plugin") }.map_err(|e| PluginResult::Other(e.to_string()))?;
|
||||
let plugin_instance = unsafe { Box::from_raw(plugin()) };
|
||||
|
||||
info!("found plugin instance");
|
||||
|
||||
let functions = plugin_instance.unregister_functions();
|
||||
info!("runregister functions");
|
||||
{
|
||||
let mut map = function_map.write().await;
|
||||
for function in functions {
|
||||
map.remove(&function);
|
||||
}
|
||||
let mut map = function_map.write().await;
|
||||
for function in functions {
|
||||
map.remove(&function);
|
||||
}
|
||||
|
||||
info!("removed functions");
|
||||
|
||||
let _ = lib;
|
||||
|
||||
info!("unloaded lib");
|
||||
drop(lib);
|
||||
Ok(())
|
||||
}
|
||||
//#[tokio::main(flavor = "multi_thread", worker_threads = 6)]
|
||||
//async fn main() -> Result<(), PluginResult> {
|
||||
// tracing::subscriber::set_global_default(tracing_subscriber::fmt().finish()).unwrap();
|
||||
// let function_map: Arc<RwLock<HashMap<String, AsyncFn>>> = Arc::new(RwLock::new(HashMap::new()));
|
||||
|
||||
// // Load the plugin
|
||||
// info!("Loading plugin");
|
||||
// let plugin_lib = load_plugin("target/release/libtesting.so", Arc::clone(&function_map)).await?;
|
||||
// info!("Plugin loaded");
|
||||
|
||||
// let function_name = get_name();
|
||||
// let mut handles = Vec::new();
|
||||
|
||||
// // Start tasks
|
||||
// info!("Starting task execution loop");
|
||||
// for i in 0..3 {
|
||||
// if let Some(function) = function_map.read().await.get(&function_name).cloned() {
|
||||
// info!("Starting task for function '{}'", function_name);
|
||||
// let handle = function(Handle::current().clone());
|
||||
// handles.push(handle);
|
||||
// } else {
|
||||
// info!("Function '{}' not found in map", function_name);
|
||||
// }
|
||||
// }
|
||||
|
||||
// // Await all tasks
|
||||
// info!("Awaiting tasks to complete");
|
||||
// for (i, handle) in handles.into_iter().enumerate() {
|
||||
// match handle.await.unwrap() {
|
||||
// PluginResult::Ok => info!("Task {} exited with Ok", i),
|
||||
// PluginResult::FunctionNotFound => info!("Task {} found FunctionNotFound", i),
|
||||
// PluginResult::Other(err) => error!("Task {} exited with error: `{}`", i, err),
|
||||
// }
|
||||
// }
|
||||
|
||||
// // Unload the plugin
|
||||
// info!("Unloading plugin");
|
||||
// unload_plugin(plugin_lib, Arc::clone(&function_map)).await?;
|
||||
// info!("Plugin unloaded successfully");
|
||||
|
||||
// // Ensure all tasks are done
|
||||
// info!("Waiting for all tasks to complete");
|
||||
// tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; // Add a short sleep to ensure tasks finish
|
||||
|
||||
// info!("Exiting main function");
|
||||
// Ok(())
|
||||
//}
|
||||
|
||||
#[host_tools::tokio::main(
|
||||
crate = "host_tools::tokio",
|
||||
flavor = "multi_thread",
|
||||
worker_threads = 6
|
||||
)]
|
||||
#[host_tools::tokio::main]
|
||||
async fn main() -> Result<(), PluginResult> {
|
||||
host_tools::tracing::subscriber::set_global_default(host_tools::tracing_subscriber::fmt().finish())
|
||||
.unwrap();
|
||||
host_tools::tracing::subscriber::set_global_default(
|
||||
host_tools::tracing_subscriber::fmt().finish(),
|
||||
)
|
||||
.unwrap();
|
||||
let function_map: Arc<RwLock<HashMap<String, AsyncFn>>> = Arc::new(RwLock::new(HashMap::new()));
|
||||
|
||||
let plugin_lib = load_plugin("target/release/libtesting.so", Arc::clone(&function_map)).await?;
|
||||
let function_name = "example_function".to_string();
|
||||
|
||||
let function_name = get_name();
|
||||
let mut tasks = vec![];
|
||||
|
||||
//for i in 0..3 {
|
||||
// if let Some(function) = function_map.read().await.get(&function_name).cloned() {
|
||||
// match function(Handle::current().clone()).await.unwrap() {
|
||||
// PluginResult::Ok => info!("Function {i} exited with Ok"),
|
||||
// PluginResult::FunctionNotFound => todo!(),
|
||||
// PluginResult::Other(err) => error!("Function exited with error: `{err}`"),
|
||||
// };
|
||||
// } else {
|
||||
// println!("Function '{}' not found", function_name);
|
||||
// }
|
||||
//}
|
||||
|
||||
for i in 0..3 {
|
||||
for _ in 0..16 {
|
||||
let plugin_lib =
|
||||
load_plugin("target/release/libtesting.so", Arc::clone(&function_map)).await?;
|
||||
if let Some(function) = function_map.read().await.get(&function_name).cloned() {
|
||||
tasks.push(host_tools::tokio::spawn(async move {
|
||||
let join_handle = function(Handle::current().clone());
|
||||
match join_handle.await.unwrap() {
|
||||
PluginResult::Ok => info!("Function {i} exited with Ok"),
|
||||
PluginResult::FunctionNotFound => todo!(),
|
||||
PluginResult::Ok => {}
|
||||
PluginResult::Other(err) => error!("Function exited with error: `{err}`"),
|
||||
};
|
||||
}));
|
||||
} else {
|
||||
println!("Function '{}' not found", function_name);
|
||||
warn!("Function '{}' not found", function_name);
|
||||
}
|
||||
unload_plugin(plugin_lib, Arc::clone(&function_map)).await?;
|
||||
}
|
||||
|
||||
info!("unloading");
|
||||
unload_plugin(plugin_lib, Arc::clone(&function_map)).await?;
|
||||
for task in tasks {
|
||||
info!("awaiting {task:?}");
|
||||
task.await.unwrap();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue