Croissant Runtime
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
crsn/runtime/src/run_thread.rs

66 lines
1.5 KiB

use std::time::Duration;
use std::thread::JoinHandle;
use asm::data::literal::{Addr};
use crate::frame::StackFrame;
use crate::program::Program;
use crate::exec::EvalRes;
const CYCLE_TIME : Duration = Duration::from_millis(0);
//const CYCLE_TIME : Duration = Duration::from_millis(100);
#[derive(Clone, Copy, Eq, PartialEq, Debug, Ord, PartialOrd)]
pub struct ThreadToken(pub u32);
pub struct RunThread {
/// Thread ID
pub id: ThreadToken,
/// Active stack frame
pub frame: StackFrame,
/// Call stack
pub call_stack: Vec<StackFrame>,
/// Program to run
pub program: Program,
}
impl RunThread {
pub fn new(id: ThreadToken, program: Program, pc: Addr, args: &[u64]) -> Self {
let sf = StackFrame::new(pc, args);
Self {
id,
frame: sf,
call_stack: vec![],
program,
}
}
pub fn start(self) -> JoinHandle<()> {
std::thread::spawn(move || {
self.run();
})
}
fn run(mut self) {
'run: loop {
match self.eval_op() {
Ok(EvalRes {
cycles, advance
}) => {
std::thread::sleep(CYCLE_TIME * (cycles as u32));
debug!("Step {}; Status = {}", advance, self.frame.status);
self.frame.pc.advance(advance);
}
Err(e) => {
error!("Fault: {:?}", e);
break 'run;
}
}
}
}
}