|
|
@ -1,5 +1,6 @@ |
|
|
|
use std::sync::Arc; |
|
|
|
use std::sync::Arc; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use std::thread::JoinHandle; |
|
|
|
use std::time::{Duration, Instant}; |
|
|
|
use std::time::{Duration, Instant}; |
|
|
|
|
|
|
|
|
|
|
|
pub use info::ThreadInfo; |
|
|
|
pub use info::ThreadInfo; |
|
|
@ -13,8 +14,6 @@ use crate::runtime::program::Program; |
|
|
|
use crate::runtime::run_thread::state::{CoroutineContext, CoroutineState}; |
|
|
|
use crate::runtime::run_thread::state::{CoroutineContext, CoroutineState}; |
|
|
|
use std::{mem, thread}; |
|
|
|
use std::{mem, thread}; |
|
|
|
use crate::asm::instr::cond::Flag; |
|
|
|
use crate::asm::instr::cond::Flag; |
|
|
|
use parking_lot::RwLock; |
|
|
|
|
|
|
|
use crate::asm::instr::Flatten; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Eq, PartialEq, Debug, Ord, PartialOrd)] |
|
|
|
#[derive(Clone, Copy, Eq, PartialEq, Debug, Ord, PartialOrd)] |
|
|
|
pub struct ThreadToken(pub u32); |
|
|
|
pub struct ThreadToken(pub u32); |
|
|
@ -41,14 +40,12 @@ impl RunThread { |
|
|
|
pub fn new(params: ThreadParams) -> Self { |
|
|
|
pub fn new(params: ThreadParams) -> Self { |
|
|
|
let extensions = params.program.extensions.clone(); |
|
|
|
let extensions = params.program.extensions.clone(); |
|
|
|
|
|
|
|
|
|
|
|
// TODO investigate if this division to 2 structs is still needed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let ti = Arc::new(ThreadInfo { |
|
|
|
let ti = Arc::new(ThreadInfo { |
|
|
|
id: params.id, |
|
|
|
id: params.id, |
|
|
|
uniq: params.uniq, |
|
|
|
uniq: params.uniq, |
|
|
|
program: params.program, |
|
|
|
program: params.program, |
|
|
|
cycle_time: params.cycle_time, |
|
|
|
cycle_time: params.cycle_time, |
|
|
|
scheduler_interval: RwLock::new(params.scheduler_interval), |
|
|
|
scheduler_interval: params.scheduler_interval, |
|
|
|
extensions, |
|
|
|
extensions, |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
@ -63,8 +60,7 @@ impl RunThread { |
|
|
|
parked: Default::default(), |
|
|
|
parked: Default::default(), |
|
|
|
global_regs: [0; REG_COUNT], |
|
|
|
global_regs: [0; REG_COUNT], |
|
|
|
ext_data: Default::default(), |
|
|
|
ext_data: Default::default(), |
|
|
|
cr_deadline: None, |
|
|
|
cr_deadline: None |
|
|
|
critical_section: 0, |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Self { |
|
|
|
Self { |
|
|
@ -73,17 +69,21 @@ impl RunThread { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Spawn as a thread
|
|
|
|
|
|
|
|
pub fn spawn(self) -> JoinHandle<()> { |
|
|
|
|
|
|
|
std::thread::spawn(move || { |
|
|
|
|
|
|
|
self.run(); |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Start synchronously
|
|
|
|
/// Start synchronously
|
|
|
|
pub fn run(mut self) { |
|
|
|
pub fn run(mut self) { |
|
|
|
let mut loop_helper = spin_sleep::LoopHelper::builder() |
|
|
|
let mut loop_helper = spin_sleep::LoopHelper::builder() |
|
|
|
.build_with_target_rate(1.0/self.info.cycle_time.as_secs_f64()); |
|
|
|
.build_with_target_rate(1.0/self.info.cycle_time.as_secs_f64()); |
|
|
|
|
|
|
|
|
|
|
|
loop_helper.loop_start(); |
|
|
|
loop_helper.loop_start(); |
|
|
|
let mut orig_pc; |
|
|
|
'run: loop { |
|
|
|
|
|
|
|
|
|
|
|
let fault = 'run: loop { |
|
|
|
|
|
|
|
let mut want_switch = false; |
|
|
|
let mut want_switch = false; |
|
|
|
orig_pc = self.state.cr.frame.pc; |
|
|
|
|
|
|
|
match self.eval_op() { |
|
|
|
match self.eval_op() { |
|
|
|
Ok(EvalRes { cycles, advance, sched }) => { |
|
|
|
Ok(EvalRes { cycles, advance, sched }) => { |
|
|
|
for _ in 0..cycles { |
|
|
|
for _ in 0..cycles { |
|
|
@ -91,6 +91,7 @@ impl RunThread { |
|
|
|
loop_helper.loop_start(); |
|
|
|
loop_helper.loop_start(); |
|
|
|
} |
|
|
|
} |
|
|
|
trace!("Step {}; Status = {}", advance, self.state.cr.frame.status); |
|
|
|
trace!("Step {}; Status = {}", advance, self.state.cr.frame.status); |
|
|
|
|
|
|
|
let orig_pc = self.state.cr.frame.pc; |
|
|
|
self.state.cr.frame.pc.advance(advance); |
|
|
|
self.state.cr.frame.pc.advance(advance); |
|
|
|
|
|
|
|
|
|
|
|
if let Some(dl) = self.state.cr_deadline { |
|
|
|
if let Some(dl) = self.state.cr_deadline { |
|
|
@ -156,127 +157,80 @@ impl RunThread { |
|
|
|
// Do not advance, the last instruction will be re-tried
|
|
|
|
// Do not advance, the last instruction will be re-tried
|
|
|
|
want_switch = true; |
|
|
|
want_switch = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Err(Fault::Halt) => { |
|
|
|
|
|
|
|
// TODO implement coordinated shutdown when more threads are running!
|
|
|
|
|
|
|
|
break 'run; |
|
|
|
|
|
|
|
} |
|
|
|
Err(e) => { |
|
|
|
Err(e) => { |
|
|
|
break 'run e; |
|
|
|
error!("Fault: {:?}", e); |
|
|
|
|
|
|
|
error!("Core dump: {:?}", self.state); |
|
|
|
|
|
|
|
break 'run; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Resolve the next coroutine to run, or wait a bit...
|
|
|
|
if want_switch { |
|
|
|
'next: loop { |
|
|
|
trace!("Switch requested"); |
|
|
|
if want_switch { |
|
|
|
|
|
|
|
let now = Instant::now(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.state.critical_section > 0 { |
|
|
|
let now = Instant::now(); |
|
|
|
match self.state.cr.cr_state { |
|
|
|
|
|
|
|
CoroutineState::Ready => {} |
|
|
|
|
|
|
|
CoroutineState::Sleep { due } => { |
|
|
|
|
|
|
|
let time = due.saturating_duration_since(now); |
|
|
|
|
|
|
|
trace!("Sleep in critical: {:?}", time); |
|
|
|
|
|
|
|
thread::sleep(time); |
|
|
|
|
|
|
|
continue 'run; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
CoroutineState::Finished(_) | CoroutineState::YieldedValue(_) => { |
|
|
|
|
|
|
|
// This is not good
|
|
|
|
|
|
|
|
break 'run Fault::Deadlock; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This is either a bug, or waiting for IO.
|
|
|
|
let mut candidate = None; |
|
|
|
// If it is the latter, try to give the OS a bit of breathing room..
|
|
|
|
let mut closest_due = None; |
|
|
|
std::thread::yield_now(); |
|
|
|
for _ in 0..self.state.parked.len() { |
|
|
|
continue 'run; |
|
|
|
if let Some(mut rt) = self.state.parked.pop_front() { |
|
|
|
} |
|
|
|
match rt.cr_state { |
|
|
|
|
|
|
|
CoroutineState::Ready => { |
|
|
|
trace!("Switch requested"); |
|
|
|
candidate = Some(rt); |
|
|
|
|
|
|
|
break; |
|
|
|
let mut candidate = None; |
|
|
|
} |
|
|
|
let mut closest_due = None; |
|
|
|
CoroutineState::Sleep { due } => { |
|
|
|
for _ in 0..self.state.parked.len() { |
|
|
|
if due <= now { |
|
|
|
if let Some(mut rt) = self.state.parked.pop_front() { |
|
|
|
rt.cr_state = CoroutineState::Ready; |
|
|
|
match rt.cr_state { |
|
|
|
|
|
|
|
CoroutineState::Ready => { |
|
|
|
|
|
|
|
candidate = Some(rt); |
|
|
|
candidate = Some(rt); |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} else { |
|
|
|
CoroutineState::Sleep { due } => { |
|
|
|
match closest_due { |
|
|
|
if due <= now { |
|
|
|
Some(d) => { |
|
|
|
rt.cr_state = CoroutineState::Ready; |
|
|
|
if d > due { |
|
|
|
candidate = Some(rt); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
match closest_due { |
|
|
|
|
|
|
|
Some(d) => { |
|
|
|
|
|
|
|
if d > due { |
|
|
|
|
|
|
|
closest_due = Some(due); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
None => { |
|
|
|
|
|
|
|
closest_due = Some(due); |
|
|
|
closest_due = Some(due); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
None => { |
|
|
|
|
|
|
|
closest_due = Some(due); |
|
|
|
} |
|
|
|
} |
|
|
|
self.state.parked.push_back(rt); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
_ => { |
|
|
|
|
|
|
|
self.state.parked.push_back(rt); |
|
|
|
self.state.parked.push_back(rt); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
_ => { |
|
|
|
|
|
|
|
self.state.parked.push_back(rt); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if let Some(cr) = candidate { |
|
|
|
if let Some(cr) = candidate { |
|
|
|
trace!("Context switch to {:?}", cr); |
|
|
|
trace!("Context switch to {:?}", cr); |
|
|
|
|
|
|
|
|
|
|
|
// Do switch
|
|
|
|
// Do switch
|
|
|
|
let old = mem::replace(&mut self.state.cr, cr); |
|
|
|
let old = mem::replace(&mut self.state.cr, cr); |
|
|
|
self.state.parked.push_back(old); |
|
|
|
self.state.parked.push_back(old); |
|
|
|
|
|
|
|
self.state.cr_deadline = Some(now + self.info.scheduler_interval); |
|
|
|
self.state.start_task_switching(); |
|
|
|
} else if let Some(due) = closest_due { |
|
|
|
|
|
|
|
let time = due.saturating_duration_since(now); |
|
|
|
} else if let Some(due) = closest_due { |
|
|
|
trace!("No thread to switch to, sleep {:?}", time); |
|
|
|
let time = due.saturating_duration_since(now); |
|
|
|
thread::sleep(time); |
|
|
|
trace!("No thread to switch to, sleep {:?}", time); |
|
|
|
|
|
|
|
thread::sleep(time); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
continue 'next; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// Nothing to run?
|
|
|
|
|
|
|
|
thread::yield_now(); |
|
|
|
|
|
|
|
trace!("No thread to switch to!"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let n_alive = self.state.parked.iter() |
|
|
|
|
|
|
|
.filter(|p| p.cr_state.is_alive()).count(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if n_alive == 0 { |
|
|
|
|
|
|
|
trace!("Stop task switching, no parked threads are alive"); |
|
|
|
|
|
|
|
self.state.stop_task_switching(); // This should improve performance in single-threaded mode
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
match self.state.cr.cr_state { |
|
|
|
|
|
|
|
CoroutineState::Finished(_) | CoroutineState::YieldedValue(_) => { |
|
|
|
|
|
|
|
break 'run Fault::RootReturned; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
_ => {} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
break 'next; |
|
|
|
let n_alive = self.state.parked.iter() |
|
|
|
} |
|
|
|
.filter(|p| p.cr_state.is_alive()).count(); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
match fault { |
|
|
|
if n_alive == 0 { |
|
|
|
Fault::Halt => { |
|
|
|
trace!("Stop task switching, no parked threads are alive"); |
|
|
|
debug!("Thread ended."); |
|
|
|
self.state.cr_deadline = None; |
|
|
|
} |
|
|
|
|
|
|
|
e => { |
|
|
|
|
|
|
|
if let Some(instr) = self.info.program.ops.get(orig_pc.0 as usize) { |
|
|
|
|
|
|
|
error!("Fault at {}: {}", instr.pos(), e); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
error!("Fault at PC {}: {}", orig_pc, e); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
warn!("Core dump: {:?}", self.state); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
debug!("Thread ended."); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|