forked from MightyPork/crsn
make stdin async
This commit is contained in:
@@ -86,6 +86,9 @@ pub enum Fault {
|
||||
|
||||
#[error("Root routine returned or yielded a value")]
|
||||
RootReturned,
|
||||
|
||||
#[error("System IO error: {0}")]
|
||||
IOError(#[from] std::io::Error),
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
|
||||
@@ -153,7 +153,9 @@ impl RunThread {
|
||||
}
|
||||
}
|
||||
Err(Fault::Blocked) => {
|
||||
// Do not advance, the last instruction will be re-tried
|
||||
trace!("Thread reports being blocked!");
|
||||
self.state.cr.frame.pc = orig_pc; // Retry
|
||||
self.state.cr.cr_state = CoroutineState::Ready;
|
||||
want_switch = true;
|
||||
}
|
||||
Err(e) => {
|
||||
@@ -191,18 +193,20 @@ impl RunThread {
|
||||
|
||||
let mut candidate = None;
|
||||
let mut closest_due = None;
|
||||
for _ in 0..self.state.parked.len() {
|
||||
'findbest: for _ in 0..self.state.parked.len() {
|
||||
if let Some(mut rt) = self.state.parked.pop_front() {
|
||||
match rt.cr_state {
|
||||
CoroutineState::Ready => {
|
||||
trace!("Found READY thread to run next");
|
||||
candidate = Some(rt);
|
||||
break;
|
||||
break 'findbest;
|
||||
}
|
||||
CoroutineState::Sleep { due } => {
|
||||
if due <= now {
|
||||
trace!("Found DUE sleeping thread to run next");
|
||||
rt.cr_state = CoroutineState::Ready;
|
||||
candidate = Some(rt);
|
||||
break;
|
||||
break 'findbest;
|
||||
} else {
|
||||
match closest_due {
|
||||
Some(d) => {
|
||||
@@ -234,10 +238,15 @@ impl RunThread {
|
||||
self.state.start_task_switching();
|
||||
|
||||
} else if let Some(due) = closest_due {
|
||||
if self.state.cr.cr_state == CoroutineState::Ready {
|
||||
trace!("No candidate found to run, retry same thread.");
|
||||
// Let it run another quantum, maybe it was just blocked
|
||||
continue 'run;
|
||||
}
|
||||
|
||||
let time = due.saturating_duration_since(now);
|
||||
trace!("No thread to switch to, sleep {:?}", time);
|
||||
thread::sleep(time);
|
||||
|
||||
continue 'next;
|
||||
} else {
|
||||
// Nothing to run?
|
||||
|
||||
Reference in New Issue
Block a user