Skip to content

Commit

Permalink
Spawn task on another thread
Browse files Browse the repository at this point in the history
  • Loading branch information
1whatleytay committed Mar 20, 2024
1 parent 4ada844 commit daa6086
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 81 deletions.
22 changes: 11 additions & 11 deletions src-backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ rand_chacha = "0.3.1"
tokio-util = "0.7.10"
async-trait = "0.1.77"

titan = { git = "https://github.com/1whatleytay/titan.git", branch = "main" }
titan = { git = "https://github.com/1whatleytay/titan.git", branch = "lock-test" }
13 changes: 5 additions & 8 deletions src-backend/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::display::{FlushDisplayBody, read_display};
use crate::syscall::{SyscallDelegate, SyscallResult};
use serde::Serialize;
use std::collections::HashSet;
use async_trait::async_trait;
use titan::cpu::error::Error::{CpuTrap, MemoryAlign, MemoryUnmapped};
use titan::cpu::{Memory, State};
use titan::cpu::memory::section::{ListenResponder, SectionMemory};
Expand Down Expand Up @@ -149,9 +148,8 @@ pub trait ExecutionRewindable {

pub trait RewindableDevice: ExecutionDevice + ExecutionRewindable { }

#[async_trait]
pub trait ExecutionDevice: Send + Sync {
async fn resume(
fn resume(
&self,
count: Option<usize>,
breakpoints: Option<Vec<u32>>,
Expand All @@ -173,9 +171,8 @@ pub trait ExecutionDevice: Send + Sync {
fn post_input(&self, text: String);
}

#[async_trait]
impl<Mem: Memory + Send, Track: Tracker<Mem> + Send> ExecutionDevice for ExecutionState<Mem, Track> {
async fn resume(
fn resume(
&self,
count: Option<usize>,
breakpoints: Option<Vec<u32>>,
Expand All @@ -202,16 +199,16 @@ impl<Mem: Memory + Send, Track: Tracker<Mem> + Send> ExecutionDevice for Executi
if let Some(count) = count {
// Taylor: Why did I split the last iteration out?
for _ in 0..count - 1 {
delegate.cycle(&debugger).await;
delegate.cycle(&debugger);
}

if count > 0 {
delegate.cycle(&debugger).await
delegate.cycle(&debugger)
} else {
return Err(());
}
} else {
delegate.run(&debugger).await
delegate.run(&debugger)
}
};

Expand Down
13 changes: 7 additions & 6 deletions src-backend/src/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use titan::execution::Executor;
use titan::execution::trackers::Tracker;
use tokio::select;
use tokio::sync::Notify;
use tokio::runtime::Handle;
use tokio_util::sync::CancellationToken;

pub struct MidiRequest {
Expand Down Expand Up @@ -734,7 +735,7 @@ impl SyscallDelegate {
}
}

async fn handle_frame<Mem: Memory, Track: Tracker<Mem>>(
fn handle_frame<Mem: Memory, Track: Tracker<Mem>>(
&self,
debugger: &Executor<Mem, Track>,
frame: DebugFrame,
Expand All @@ -743,7 +744,7 @@ impl SyscallDelegate {
Invalid(CpuSyscall) => {
// $v0
let code = debugger.with_state(|s| s.registers.line[V0_REG]);
let result = self.dispatch(debugger, code).await;
let result = Handle::current().block_on(self.dispatch(debugger, code));

(
match result {
Expand All @@ -761,12 +762,12 @@ impl SyscallDelegate {
}
}

pub async fn run<Mem: Memory, Track: Tracker<Mem>>(
pub fn run<Mem: Memory, Track: Tracker<Mem>>(
&self, debugger: &Executor<Mem, Track>
) -> (DebugFrame, Option<SyscallResult>) {
loop {
let frame = debugger.run();
let (frame, result) = self.handle_frame(debugger, frame).await;
let (frame, result) = self.handle_frame(debugger, frame);

if let Some(frame) = frame {
return (frame, result);
Expand All @@ -781,14 +782,14 @@ impl SyscallDelegate {
}
}

pub async fn cycle<Mem: Memory + Send, Track: Tracker<Mem> + Send>(
pub fn cycle<Mem: Memory + Send, Track: Tracker<Mem> + Send>(
&self,
debugger: &Executor<Mem, Track>,
) -> (DebugFrame, Option<SyscallResult>) {
let frame = debugger.cycle(true);

let (frame, result) = if let Some(frame) = frame {
self.handle_frame(debugger, frame).await
self.handle_frame(debugger, frame)
} else {
(None, None)
};
Expand Down
Loading

0 comments on commit daa6086

Please sign in to comment.