Skip to content

Commit

Permalink
refactor: Cleanup Fiber implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
leoshimo committed Dec 14, 2023
1 parent 5e2eb39 commit a3a2d21
Show file tree
Hide file tree
Showing 7 changed files with 537 additions and 576 deletions.
68 changes: 32 additions & 36 deletions libvrs/src/rt/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::rt::mailbox::{Mailbox, MailboxHandle};
use crate::rt::{Error, Result};
use crate::{Connection, Program};
use futures::future::{FutureExt, Shared};
use lyric::FiberState;
use tokio::sync::{mpsc, oneshot};
use tokio::task::JoinSet;
use tracing::{debug, error, info};
Expand Down Expand Up @@ -114,42 +113,39 @@ impl Process {
procs.spawn(async move {
let exit: Result<_> = async {
let mut io = self.io;
let mut state = fiber.resume()?;
let mut v = fiber.start()?;
loop {
match state {
FiberState::Done(v) => {
return Ok(ProcessExit {
id: self.id,
status: Ok(ProcessResult::Done(v)),
})
}
FiberState::Yield(v) => {
debug!("proc yield - {:?} {:?}", self.id, v);
tokio::select!(
Some(msg) = msg_rx.recv() => match msg {
Event::Kill => return Ok(ProcessExit {
id: self.id,
status: Ok(ProcessResult::Cancelled)
})
},
io_result = Self::handle_yield(&mut fiber, v, &mut io) => {
debug!("proc yield result - {:?} {:?}", self.id, io_result);

let io_result = match io_result {
Ok(r) => Ok(r),
Err(Error::ConnectionClosed) => {
return Ok(ProcessExit {
id: self.id,
status: Ok(ProcessResult::Disconnected)
})
}
Err(e) => Err(e),
}?;

state = fiber.resume_from_yield(io_result)?;
}
);
}
if fiber.is_done() {
return Ok(ProcessExit {
id: self.id,
status: Ok(ProcessResult::Done(v)),
});
} else {
debug!("proc yield - {:?} {:?}", self.id, v);
tokio::select!(
Some(msg) = msg_rx.recv() => match msg {
Event::Kill => return Ok(ProcessExit {
id: self.id,
status: Ok(ProcessResult::Cancelled)
})
},
io_result = Self::handle_yield(&mut fiber, v, &mut io) => {
debug!("proc yield result - {:?} {:?}", self.id, io_result);

let io_result = match io_result {
Ok(r) => Ok(r),
Err(Error::ConnectionClosed) => {
return Ok(ProcessExit {
id: self.id,
status: Ok(ProcessResult::Disconnected)
})
}
Err(e) => Err(e),
}?;

v = fiber.resume(io_result)?;
}
);
}
}
}
Expand Down
24 changes: 2 additions & 22 deletions lyric/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@ use serde::{Deserialize, Serialize};

#[derive(thiserror::Error, Debug, PartialEq, Clone, Serialize, Deserialize)]
pub enum Error {
#[error("Failed to lex - {0}")]
FailedToLex(String),

#[error("Failed to parse - {0}")]
FailedToParse(String),

#[error("Incomplete expression - {0}")]
IncompleteExpression(String),

#[error("Missing procedure")]
MissingProcedure,
#[error("Invalid expression - {0}")]
InvalidExpression(String),

#[error("Undefined symbol - {0}")]
UndefinedSymbol(SymbolId),
Expand All @@ -24,24 +18,10 @@ pub enum Error {
#[error("Unexpected type - {0}")]
UnexpectedType(String),

#[error("Invalid form to expr - {0}")]
InvalidFormToExpr(String),

/// A fiber that is already running was asked to start
#[error("Fiber is already running")]
AlreadyRunning,

/// Executed past end of fiber
#[error("Exceeded fiber instructions")]
NoMoreBytecode,

/// Unexpected state on stack
#[error("Unexpected stack state - {0}")]
UnexpectedStack(String),

#[error("Invalid expression - {0}")]
InvalidExpression(String),

#[error("Unexpected resume of fiber - {0}")]
UnexpectedResume(String),

Expand Down
Loading

0 comments on commit a3a2d21

Please sign in to comment.