Module runtimes, pass manager into modules.

This commit is contained in:
Michael Mikovsky
2025-11-06 15:49:47 -07:00
parent 3e0d927465
commit 0a9b86edfa
8 changed files with 438 additions and 71 deletions
+42 -5
View File
@@ -2,11 +2,15 @@
#[macro_use]
extern crate log;
use std::thread::{self, Thread};
use std::{
sync::{Arc, Mutex},
thread::{self, JoinHandle},
time::Duration,
};
// pub use unshell_logger::setup_logger;
pub use unshell_modules::setup_logger;
use unshell_modules::{ManagerInterface, module_interface};
use unshell_modules::{Manager, ModuleRuntime, module_interface};
extern "C" fn test1() {
warn!("Test1 called");
@@ -31,7 +35,40 @@ pub fn interface() -> Interface {
Interface::from_raw(test1, test2, test3)
}
#[unsafe(no_mangle)]
pub fn init(interface: ManagerInterface) {
thread::spawn(|| {});
struct RuntimeTest {
thread_handle: JoinHandle<()>,
}
impl RuntimeTest {
pub fn new(manager: Arc<Mutex<Manager>>) -> RuntimeTest {
Self {
thread_handle: thread::spawn(move || {
thread::sleep(Duration::from_secs(2));
let manager_lock = manager.lock().unwrap();
manager_lock.test1234(111.1111);
drop(manager_lock);
}),
}
}
}
impl ModuleRuntime for RuntimeTest {
// fn init(&mut self) {}
fn is_running(&self) -> bool {
!self.thread_handle.is_finished()
}
fn kill(self: Box<Self>) {
if !self.thread_handle.is_finished() {
let _ = self.thread_handle.join();
}
// drop(self);
}
}
#[unsafe(no_mangle)]
pub fn init(manager: Arc<Mutex<Manager>>) -> Box<dyn ModuleRuntime> {
Box::new(RuntimeTest::new(manager))
}