mirror of
https://github.com/Astatin3/unshell.git
synced 2026-06-08 22:38:01 -06:00
Module runtimes, pass manager into modules.
This commit is contained in:
@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user