implements PLN-0127 async io lane boundaries
This commit is contained in:
@@ -29,6 +29,36 @@ pub enum AsyncWorkJobKind {
|
||||
Test,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum AsyncWorkPriority {
|
||||
MemcardCommitWrite,
|
||||
FsWriteConfig,
|
||||
AssetLoad,
|
||||
NonCriticalReadList,
|
||||
}
|
||||
|
||||
impl AsyncWorkPriority {
|
||||
const fn rank(self) -> u8 {
|
||||
match self {
|
||||
Self::MemcardCommitWrite => 0,
|
||||
Self::FsWriteConfig => 1,
|
||||
Self::AssetLoad => 2,
|
||||
Self::NonCriticalReadList => 3,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncWorkJobKind {
|
||||
pub const fn default_priority(self) -> AsyncWorkPriority {
|
||||
match self {
|
||||
Self::Memcard => AsyncWorkPriority::MemcardCommitWrite,
|
||||
Self::Fs => AsyncWorkPriority::FsWriteConfig,
|
||||
Self::Asset => AsyncWorkPriority::AssetLoad,
|
||||
Self::Test => AsyncWorkPriority::NonCriticalReadList,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum AsyncWorkJobOutcome {
|
||||
Completed,
|
||||
@@ -59,6 +89,7 @@ impl Default for AsyncWorkLaneConfig {
|
||||
pub struct AsyncWorkActiveJob {
|
||||
pub id: AsyncWorkJobId,
|
||||
pub kind: AsyncWorkJobKind,
|
||||
pub priority: AsyncWorkPriority,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
@@ -131,6 +162,7 @@ impl AsyncWorkCancelToken {
|
||||
pub struct AsyncWorkJobContext {
|
||||
pub job_id: AsyncWorkJobId,
|
||||
pub kind: AsyncWorkJobKind,
|
||||
pub priority: AsyncWorkPriority,
|
||||
cancel_token: AsyncWorkCancelToken,
|
||||
}
|
||||
|
||||
@@ -145,6 +177,7 @@ type AsyncWorkFn = Box<dyn FnOnce(AsyncWorkJobContext) -> AsyncWorkJobOutcome +
|
||||
struct PendingAsyncWorkJob {
|
||||
id: AsyncWorkJobId,
|
||||
kind: AsyncWorkJobKind,
|
||||
priority: AsyncWorkPriority,
|
||||
cancel_token: AsyncWorkCancelToken,
|
||||
work: AsyncWorkFn,
|
||||
}
|
||||
@@ -152,6 +185,7 @@ struct PendingAsyncWorkJob {
|
||||
struct RunningAsyncWorkJob {
|
||||
id: AsyncWorkJobId,
|
||||
kind: AsyncWorkJobKind,
|
||||
priority: AsyncWorkPriority,
|
||||
cancel_token: AsyncWorkCancelToken,
|
||||
work: AsyncWorkFn,
|
||||
}
|
||||
@@ -177,6 +211,15 @@ impl AsyncWorkLane {
|
||||
&self,
|
||||
kind: AsyncWorkJobKind,
|
||||
work: impl FnOnce(AsyncWorkJobContext) -> AsyncWorkJobOutcome + Send + 'static,
|
||||
) -> Result<AsyncWorkJobId, AsyncWorkLaneError> {
|
||||
self.submit_with_priority(kind, kind.default_priority(), work)
|
||||
}
|
||||
|
||||
pub fn submit_with_priority(
|
||||
&self,
|
||||
kind: AsyncWorkJobKind,
|
||||
priority: AsyncWorkPriority,
|
||||
work: impl FnOnce(AsyncWorkJobContext) -> AsyncWorkJobOutcome + Send + 'static,
|
||||
) -> Result<AsyncWorkJobId, AsyncWorkLaneError> {
|
||||
let mut state = self.state.lock().map_err(|_| AsyncWorkLaneError::InternalFailure)?;
|
||||
if state.shutdown_requested {
|
||||
@@ -186,18 +229,52 @@ impl AsyncWorkLane {
|
||||
state.next_job_id += 1;
|
||||
let id = AsyncWorkJobId::new(state.next_job_id);
|
||||
let cancel_token = AsyncWorkCancelToken::new();
|
||||
state.pending.push_back(PendingAsyncWorkJob {
|
||||
id,
|
||||
kind,
|
||||
cancel_token,
|
||||
work: Box::new(work),
|
||||
});
|
||||
let job = PendingAsyncWorkJob { id, kind, priority, cancel_token, work: Box::new(work) };
|
||||
let insert_at = state
|
||||
.pending
|
||||
.iter()
|
||||
.position(|pending| priority.rank() < pending.priority.rank())
|
||||
.unwrap_or(state.pending.len());
|
||||
state.pending.insert(insert_at, job);
|
||||
let pending_depth = state.pending.len();
|
||||
state.telemetry.record_submitted(id, pending_depth);
|
||||
self.ready.notify_one();
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub fn submit_memcard_commit_write(
|
||||
&self,
|
||||
work: impl FnOnce(AsyncWorkJobContext) -> AsyncWorkJobOutcome + Send + 'static,
|
||||
) -> Result<AsyncWorkJobId, AsyncWorkLaneError> {
|
||||
self.submit_with_priority(
|
||||
AsyncWorkJobKind::Memcard,
|
||||
AsyncWorkPriority::MemcardCommitWrite,
|
||||
work,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn submit_fs_write_config(
|
||||
&self,
|
||||
work: impl FnOnce(AsyncWorkJobContext) -> AsyncWorkJobOutcome + Send + 'static,
|
||||
) -> Result<AsyncWorkJobId, AsyncWorkLaneError> {
|
||||
self.submit_with_priority(AsyncWorkJobKind::Fs, AsyncWorkPriority::FsWriteConfig, work)
|
||||
}
|
||||
|
||||
pub fn submit_asset_load(
|
||||
&self,
|
||||
work: impl FnOnce(AsyncWorkJobContext) -> AsyncWorkJobOutcome + Send + 'static,
|
||||
) -> Result<AsyncWorkJobId, AsyncWorkLaneError> {
|
||||
self.submit_with_priority(AsyncWorkJobKind::Asset, AsyncWorkPriority::AssetLoad, work)
|
||||
}
|
||||
|
||||
pub fn submit_non_critical_read_list(
|
||||
&self,
|
||||
kind: AsyncWorkJobKind,
|
||||
work: impl FnOnce(AsyncWorkJobContext) -> AsyncWorkJobOutcome + Send + 'static,
|
||||
) -> Result<AsyncWorkJobId, AsyncWorkLaneError> {
|
||||
self.submit_with_priority(kind, AsyncWorkPriority::NonCriticalReadList, work)
|
||||
}
|
||||
|
||||
pub fn request_cancel(&self, job_id: AsyncWorkJobId) -> bool {
|
||||
let state = self.state.lock().unwrap();
|
||||
if state.active.is_some_and(|active| active.id == job_id) {
|
||||
@@ -253,7 +330,11 @@ impl AsyncWorkLane {
|
||||
}
|
||||
|
||||
if let Some(pending) = state.pending.pop_front() {
|
||||
let active = AsyncWorkActiveJob { id: pending.id, kind: pending.kind };
|
||||
let active = AsyncWorkActiveJob {
|
||||
id: pending.id,
|
||||
kind: pending.kind,
|
||||
priority: pending.priority,
|
||||
};
|
||||
state.active = Some(active);
|
||||
state.active_cancel_token = Some(pending.cancel_token.clone());
|
||||
let pending_depth = state.pending.len();
|
||||
@@ -261,6 +342,7 @@ impl AsyncWorkLane {
|
||||
return Some(RunningAsyncWorkJob {
|
||||
id: pending.id,
|
||||
kind: pending.kind,
|
||||
priority: pending.priority,
|
||||
cancel_token: pending.cancel_token,
|
||||
work: pending.work,
|
||||
});
|
||||
@@ -332,8 +414,12 @@ impl Drop for AsyncWorkLaneController {
|
||||
|
||||
fn run_async_work_loop(lane: Arc<AsyncWorkLane>) {
|
||||
while let Some(job) = lane.wait_take() {
|
||||
let context =
|
||||
AsyncWorkJobContext { job_id: job.id, kind: job.kind, cancel_token: job.cancel_token };
|
||||
let context = AsyncWorkJobContext {
|
||||
job_id: job.id,
|
||||
kind: job.kind,
|
||||
priority: job.priority,
|
||||
cancel_token: job.cancel_token,
|
||||
};
|
||||
let result = catch_unwind(AssertUnwindSafe(|| (job.work)(context)))
|
||||
.unwrap_or(AsyncWorkJobOutcome::Failed);
|
||||
lane.record_finished(job.id, result);
|
||||
@@ -379,6 +465,60 @@ mod tests {
|
||||
assert_eq!(telemetry.completed_jobs, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn async_work_lane_runs_higher_priority_io_before_assets() {
|
||||
let lane = Arc::new(AsyncWorkLane::default());
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
lane.submit_asset_load({
|
||||
let tx = tx.clone();
|
||||
move |ctx| {
|
||||
tx.send((ctx.kind, ctx.priority)).unwrap();
|
||||
AsyncWorkJobOutcome::Completed
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
lane.submit_non_critical_read_list(AsyncWorkJobKind::Fs, {
|
||||
let tx = tx.clone();
|
||||
move |ctx| {
|
||||
tx.send((ctx.kind, ctx.priority)).unwrap();
|
||||
AsyncWorkJobOutcome::Completed
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
lane.submit_memcard_commit_write({
|
||||
let tx = tx.clone();
|
||||
move |ctx| {
|
||||
tx.send((ctx.kind, ctx.priority)).unwrap();
|
||||
AsyncWorkJobOutcome::Completed
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
lane.submit_fs_write_config({
|
||||
let tx = tx.clone();
|
||||
move |ctx| {
|
||||
tx.send((ctx.kind, ctx.priority)).unwrap();
|
||||
AsyncWorkJobOutcome::Completed
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let mut controller =
|
||||
AsyncWorkLaneController::start(AsyncWorkLaneConfig::default(), Arc::clone(&lane));
|
||||
|
||||
assert_eq!(
|
||||
rx.recv().unwrap(),
|
||||
(AsyncWorkJobKind::Memcard, AsyncWorkPriority::MemcardCommitWrite)
|
||||
);
|
||||
assert_eq!(rx.recv().unwrap(), (AsyncWorkJobKind::Fs, AsyncWorkPriority::FsWriteConfig));
|
||||
assert_eq!(rx.recv().unwrap(), (AsyncWorkJobKind::Asset, AsyncWorkPriority::AssetLoad));
|
||||
assert_eq!(
|
||||
rx.recv().unwrap(),
|
||||
(AsyncWorkJobKind::Fs, AsyncWorkPriority::NonCriticalReadList)
|
||||
);
|
||||
controller.stop().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn async_work_lane_has_one_active_job() {
|
||||
let lane = Arc::new(AsyncWorkLane::default());
|
||||
|
||||
Reference in New Issue
Block a user