2023-07-10 06:44:11 -05:00
|
|
|
use alloc::{collections::VecDeque, vec::Vec, rc::Rc, slice};
|
|
|
|
use hbvm::validate::validate;
|
|
|
|
|
|
|
|
use {crate::host::TrapHandler, hbvm::vm::Vm};
|
2023-06-26 07:55:37 -05:00
|
|
|
|
|
|
|
pub struct Scheduler<'a> {
|
2023-07-10 06:44:11 -05:00
|
|
|
data: VecDeque<Vm<'a, TrapHandler>>,
|
2023-06-26 07:55:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Scheduler<'_> {
|
2023-07-10 06:44:11 -05:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
data: VecDeque::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn new_process(&mut self, program: Vec<u8>) {
|
|
|
|
let prog = program.clone();
|
|
|
|
let prog_arc = Rc::new(prog);
|
|
|
|
|
|
|
|
let binding = Rc::try_unwrap(prog_arc).ok().unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[allow(clippy::redundant_else)]
|
|
|
|
if let Err(e) = validate(&program.as_slice()) {
|
|
|
|
log::error!("Program validation error: {e:?}");
|
|
|
|
} else {
|
|
|
|
log::info!("valid program");
|
|
|
|
unsafe {
|
|
|
|
let slice = slice::from_raw_parts(binding.as_ptr(), binding.len());
|
|
|
|
let mut vm = Vm::new_unchecked(&*slice, TrapHandler);
|
|
|
|
vm.memory.insert_test_page();
|
|
|
|
self.data.push_front(vm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn scheduler_loop(&mut self){
|
|
|
|
loop {
|
|
|
|
let mut prog = self.data.pop_front().unwrap();
|
|
|
|
prog.run().unwrap();
|
|
|
|
self.data.push_back(prog);
|
|
|
|
}
|
|
|
|
}
|
2023-06-26 07:55:37 -05:00
|
|
|
}
|