add process and task managers
This commit is contained in:
@@ -1,2 +1,4 @@
|
||||
pub mod fs;
|
||||
pub mod memcard;
|
||||
pub mod process;
|
||||
pub mod task;
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
mod model;
|
||||
mod process_kind;
|
||||
pub mod process_manager;
|
||||
mod process_state;
|
||||
|
||||
pub use model::{Process, ProcessId};
|
||||
pub use process_kind::ProcessKind;
|
||||
pub use process_manager::ProcessManager;
|
||||
pub use process_state::ProcessState;
|
||||
@@ -0,0 +1,42 @@
|
||||
use crate::process::{ProcessKind, ProcessState};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct ProcessId(pub u32);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Process {
|
||||
pub id: ProcessId,
|
||||
pub app_id: u32,
|
||||
pub title: String,
|
||||
pub kind: ProcessKind,
|
||||
pub state: ProcessState,
|
||||
}
|
||||
|
||||
impl Process {
|
||||
pub fn new(id: ProcessId, app_id: u32, title: impl Into<String>, kind: ProcessKind) -> Self {
|
||||
Self { id, app_id, title: title.into(), kind, state: ProcessState::Created }
|
||||
}
|
||||
|
||||
pub fn mark_running(&mut self) {
|
||||
self.state = ProcessState::Running;
|
||||
}
|
||||
|
||||
pub fn mark_suspended(&mut self) {
|
||||
self.state = ProcessState::Suspended;
|
||||
}
|
||||
|
||||
pub fn mark_stopped(&mut self) {
|
||||
self.state = ProcessState::Stopped;
|
||||
}
|
||||
|
||||
pub fn mark_crashed(&mut self) {
|
||||
self.state = ProcessState::Crashed;
|
||||
}
|
||||
|
||||
pub fn is_alive(&self) -> bool {
|
||||
matches!(
|
||||
self.state,
|
||||
ProcessState::Created | ProcessState::Running | ProcessState::Suspended
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ProcessKind {
|
||||
/// VM-backed fullscreen game cartridge.
|
||||
VmGame,
|
||||
|
||||
/// VM-backed app hosted by the Prometeu Shell.
|
||||
VmShell,
|
||||
|
||||
/// Native Rust app/component hosted by the Prometeu Shell.
|
||||
NativeShell,
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
use crate::process::{Process, ProcessId, ProcessKind, ProcessState};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ProcessManager {
|
||||
next_id: u32,
|
||||
processes: HashMap<ProcessId, Process>,
|
||||
}
|
||||
|
||||
impl ProcessManager {
|
||||
pub fn new() -> Self {
|
||||
Self { next_id: 1, processes: HashMap::new() }
|
||||
}
|
||||
|
||||
pub fn spawn_vm_game(&mut self, app_id: u32, title: impl Into<String>) -> ProcessId {
|
||||
self.spawn(app_id, title, ProcessKind::VmGame)
|
||||
}
|
||||
|
||||
pub fn spawn_vm_shell(&mut self, app_id: u32, title: impl Into<String>) -> ProcessId {
|
||||
self.spawn(app_id, title, ProcessKind::VmShell)
|
||||
}
|
||||
|
||||
pub fn spawn_native_shell(&mut self, app_id: u32, title: impl Into<String>) -> ProcessId {
|
||||
self.spawn(app_id, title, ProcessKind::NativeShell)
|
||||
}
|
||||
|
||||
fn spawn(&mut self, app_id: u32, title: impl Into<String>, kind: ProcessKind) -> ProcessId {
|
||||
let id = ProcessId(self.next_id);
|
||||
self.next_id += 1;
|
||||
|
||||
let mut process = Process::new(id, app_id, title, kind);
|
||||
process.mark_running();
|
||||
|
||||
self.processes.insert(id, process);
|
||||
|
||||
id
|
||||
}
|
||||
|
||||
pub fn get(&self, id: ProcessId) -> Option<&Process> {
|
||||
self.processes.get(&id)
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, id: ProcessId) -> Option<&mut Process> {
|
||||
self.processes.get_mut(&id)
|
||||
}
|
||||
|
||||
pub fn contains(&self, id: ProcessId) -> bool {
|
||||
self.processes.contains_key(&id)
|
||||
}
|
||||
|
||||
pub fn all(&self) -> impl Iterator<Item = &Process> {
|
||||
self.processes.values()
|
||||
}
|
||||
|
||||
pub fn mark_running(&mut self, id: ProcessId) -> bool {
|
||||
self.transition(id, ProcessState::Running)
|
||||
}
|
||||
|
||||
pub fn mark_suspended(&mut self, id: ProcessId) -> bool {
|
||||
self.transition(id, ProcessState::Suspended)
|
||||
}
|
||||
|
||||
pub fn mark_stopped(&mut self, id: ProcessId) -> bool {
|
||||
self.transition(id, ProcessState::Stopped)
|
||||
}
|
||||
|
||||
pub fn mark_crashed(&mut self, id: ProcessId) -> bool {
|
||||
self.transition(id, ProcessState::Crashed)
|
||||
}
|
||||
|
||||
pub fn remove_stopped(&mut self) {
|
||||
self.processes.retain(|_, process| process.state != ProcessState::Stopped);
|
||||
}
|
||||
|
||||
fn transition(&mut self, id: ProcessId, state: ProcessState) -> bool {
|
||||
let Some(process) = self.get_mut(id) else {
|
||||
return false;
|
||||
};
|
||||
|
||||
process.state = state;
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn spawn_vm_game_creates_running_process() {
|
||||
let mut manager = ProcessManager::new();
|
||||
|
||||
let id = manager.spawn_vm_game(42, "Amazing Game");
|
||||
|
||||
let process = manager.get(id).expect("process should exist");
|
||||
|
||||
assert_eq!(process.id, id);
|
||||
assert_eq!(process.app_id, 42);
|
||||
assert_eq!(process.title, "Amazing Game");
|
||||
assert_eq!(process.kind, ProcessKind::VmGame);
|
||||
assert_eq!(process.state, ProcessState::Running);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spawn_vm_shell_creates_running_process() {
|
||||
let mut manager = ProcessManager::new();
|
||||
|
||||
let id = manager.spawn_vm_shell(42, "Amazing Shell");
|
||||
|
||||
let process = manager.get(id).expect("process should exist");
|
||||
|
||||
assert_eq!(process.id, id);
|
||||
assert_eq!(process.app_id, 42);
|
||||
assert_eq!(process.title, "Amazing Shell");
|
||||
assert_eq!(process.kind, ProcessKind::VmShell);
|
||||
assert_eq!(process.state, ProcessState::Running);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spawn_native_shell_creates_running_process() {
|
||||
let mut manager = ProcessManager::new();
|
||||
|
||||
let id = manager.spawn_native_shell(42, "Amazing Shell");
|
||||
|
||||
let process = manager.get(id).expect("process should exist");
|
||||
|
||||
assert_eq!(process.id, id);
|
||||
assert_eq!(process.app_id, 42);
|
||||
assert_eq!(process.title, "Amazing Shell");
|
||||
assert_eq!(process.kind, ProcessKind::NativeShell);
|
||||
assert_eq!(process.state, ProcessState::Running);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mark_suspended_updates_process_state() {
|
||||
let mut manager = ProcessManager::new();
|
||||
|
||||
let id = manager.spawn_vm_game(42, "Amazing Game");
|
||||
|
||||
assert!(manager.mark_suspended(id));
|
||||
|
||||
let process = manager.get(id).expect("process should exist");
|
||||
|
||||
assert_eq!(process.state, ProcessState::Suspended);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mark_stopped_updates_process_state() {
|
||||
let mut manager = ProcessManager::new();
|
||||
|
||||
let id = manager.spawn_vm_game(42, "Amazing Game");
|
||||
|
||||
assert!(manager.mark_stopped(id));
|
||||
|
||||
let process = manager.get(id).expect("process should exist");
|
||||
|
||||
assert_eq!(process.state, ProcessState::Stopped);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_process_transition_returns_false() {
|
||||
let mut manager = ProcessManager::new();
|
||||
|
||||
assert!(!manager.mark_stopped(ProcessId(999)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_stopped_keeps_running_processes() {
|
||||
let mut manager = ProcessManager::new();
|
||||
|
||||
let running = manager.spawn_vm_game(1, "Running");
|
||||
let stopped = manager.spawn_vm_game(2, "Stopped");
|
||||
|
||||
manager.mark_stopped(stopped);
|
||||
manager.remove_stopped();
|
||||
|
||||
assert!(manager.get(running).is_some());
|
||||
assert!(manager.get(stopped).is_none());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ProcessState {
|
||||
Created,
|
||||
Running,
|
||||
Stopped,
|
||||
Suspended,
|
||||
Crashed,
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
mod model;
|
||||
mod task_kind;
|
||||
mod task_manager;
|
||||
mod task_state;
|
||||
|
||||
pub use model::Task;
|
||||
pub use model::TaskId;
|
||||
pub use task_kind::TaskKind;
|
||||
pub use task_manager::TaskManager;
|
||||
pub use task_state::TaskState;
|
||||
@@ -0,0 +1,51 @@
|
||||
use crate::process::ProcessId;
|
||||
use crate::task::{TaskKind, TaskState};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct TaskId(pub u32);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Task {
|
||||
pub id: TaskId,
|
||||
pub process_id: ProcessId,
|
||||
pub app_id: u32,
|
||||
pub title: String,
|
||||
pub kind: TaskKind,
|
||||
pub state: TaskState,
|
||||
}
|
||||
|
||||
impl Task {
|
||||
pub fn new(
|
||||
id: TaskId,
|
||||
process_id: ProcessId,
|
||||
app_id: u32,
|
||||
title: impl Into<String>,
|
||||
kind: TaskKind,
|
||||
) -> Self {
|
||||
Self { id, process_id, app_id, title: title.into(), kind, state: TaskState::Background }
|
||||
}
|
||||
|
||||
pub fn mark_foreground(&mut self) {
|
||||
self.state = TaskState::Foreground;
|
||||
}
|
||||
|
||||
pub fn mark_background(&mut self) {
|
||||
self.state = TaskState::Background;
|
||||
}
|
||||
|
||||
pub fn mark_suspended(&mut self) {
|
||||
self.state = TaskState::Suspended;
|
||||
}
|
||||
|
||||
pub fn mark_closed(&mut self) {
|
||||
self.state = TaskState::Closed;
|
||||
}
|
||||
|
||||
pub fn mark_crashed(&mut self) {
|
||||
self.state = TaskState::Crashed;
|
||||
}
|
||||
|
||||
pub fn is_active(&self) -> bool {
|
||||
matches!(self.state, TaskState::Foreground | TaskState::Background | TaskState::Suspended)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum TaskKind {
|
||||
Game,
|
||||
Shell,
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
use super::{Task, TaskId, TaskKind, TaskState};
|
||||
use crate::services::process::ProcessId;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct TaskManager {
|
||||
next_id: u32,
|
||||
tasks: HashMap<TaskId, Task>,
|
||||
foreground_task: Option<TaskId>,
|
||||
}
|
||||
|
||||
impl TaskManager {
|
||||
pub fn new() -> Self {
|
||||
Self { next_id: 1, tasks: HashMap::new(), foreground_task: None }
|
||||
}
|
||||
|
||||
pub fn create_game_task(
|
||||
&mut self,
|
||||
process_id: ProcessId,
|
||||
app_id: u32,
|
||||
title: impl Into<String>,
|
||||
) -> TaskId {
|
||||
self.create(process_id, app_id, title, TaskKind::Game)
|
||||
}
|
||||
|
||||
pub fn create_shell_task(
|
||||
&mut self,
|
||||
process_id: ProcessId,
|
||||
app_id: u32,
|
||||
title: impl Into<String>,
|
||||
) -> TaskId {
|
||||
self.create(process_id, app_id, title, TaskKind::Shell)
|
||||
}
|
||||
|
||||
fn create(
|
||||
&mut self,
|
||||
process_id: ProcessId,
|
||||
app_id: u32,
|
||||
title: impl Into<String>,
|
||||
kind: TaskKind,
|
||||
) -> TaskId {
|
||||
let id = TaskId(self.next_id);
|
||||
self.next_id += 1;
|
||||
|
||||
let task = Task::new(id, process_id, app_id, title, kind);
|
||||
self.tasks.insert(id, task);
|
||||
|
||||
id
|
||||
}
|
||||
|
||||
pub fn get(&self, id: TaskId) -> Option<&Task> {
|
||||
self.tasks.get(&id)
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, id: TaskId) -> Option<&mut Task> {
|
||||
self.tasks.get_mut(&id)
|
||||
}
|
||||
|
||||
pub fn contains(&self, id: TaskId) -> bool {
|
||||
self.tasks.contains_key(&id)
|
||||
}
|
||||
|
||||
pub fn all(&self) -> impl Iterator<Item = &Task> {
|
||||
self.tasks.values()
|
||||
}
|
||||
|
||||
pub fn foreground_task(&self) -> Option<TaskId> {
|
||||
self.foreground_task
|
||||
}
|
||||
|
||||
pub fn foreground(&self) -> Option<&Task> {
|
||||
self.foreground_task.and_then(|id| self.get(id))
|
||||
}
|
||||
|
||||
pub fn set_foreground(&mut self, id: TaskId) -> bool {
|
||||
if !self.tasks.contains_key(&id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if let Some(current_id) = self.foreground_task
|
||||
&& current_id != id
|
||||
&& let Some(current) = self.tasks.get_mut(¤t_id)
|
||||
&& current.state == TaskState::Foreground
|
||||
{
|
||||
current.mark_background();
|
||||
}
|
||||
|
||||
let Some(task) = self.tasks.get_mut(&id) else {
|
||||
return false;
|
||||
};
|
||||
|
||||
task.mark_foreground();
|
||||
self.foreground_task = Some(id);
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
pub fn clear_foreground(&mut self) {
|
||||
if let Some(id) = self.foreground_task.take()
|
||||
&& let Some(task) = self.tasks.get_mut(&id)
|
||||
&& task.state == TaskState::Foreground
|
||||
{
|
||||
task.mark_background();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mark_background(&mut self, id: TaskId) -> bool {
|
||||
let Some(task) = self.tasks.get_mut(&id) else {
|
||||
return false;
|
||||
};
|
||||
|
||||
task.mark_background();
|
||||
|
||||
if self.foreground_task == Some(id) {
|
||||
self.foreground_task = None;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
pub fn mark_suspended(&mut self, id: TaskId) -> bool {
|
||||
let Some(task) = self.tasks.get_mut(&id) else {
|
||||
return false;
|
||||
};
|
||||
|
||||
task.mark_suspended();
|
||||
|
||||
if self.foreground_task == Some(id) {
|
||||
self.foreground_task = None;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
pub fn close_task(&mut self, id: TaskId) -> bool {
|
||||
let Some(task) = self.tasks.get_mut(&id) else {
|
||||
return false;
|
||||
};
|
||||
|
||||
task.mark_closed();
|
||||
|
||||
if self.foreground_task == Some(id) {
|
||||
self.foreground_task = None;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
pub fn mark_crashed(&mut self, id: TaskId) -> bool {
|
||||
let Some(task) = self.tasks.get_mut(&id) else {
|
||||
return false;
|
||||
};
|
||||
|
||||
task.mark_crashed();
|
||||
|
||||
if self.foreground_task == Some(id) {
|
||||
self.foreground_task = None;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
pub fn remove_closed(&mut self) {
|
||||
self.tasks.retain(|id, task| {
|
||||
let should_keep = task.state != TaskState::Closed;
|
||||
if !should_keep && self.foreground_task == Some(*id) {
|
||||
self.foreground_task = None;
|
||||
}
|
||||
should_keep
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::services::process::ProcessId;
|
||||
|
||||
#[test]
|
||||
fn create_game_task_registers_task() {
|
||||
let mut manager = TaskManager::new();
|
||||
let process_id = ProcessId(7);
|
||||
|
||||
let task_id = manager.create_game_task(process_id, 42, "Sector Crawl");
|
||||
|
||||
let task = manager.get(task_id).expect("task should exist");
|
||||
|
||||
assert_eq!(task.id, task_id);
|
||||
assert_eq!(task.process_id, process_id);
|
||||
assert_eq!(task.app_id, 42);
|
||||
assert_eq!(task.title, "Sector Crawl");
|
||||
assert_eq!(task.kind, TaskKind::Game);
|
||||
assert_eq!(task.state, TaskState::Background);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_foreground_marks_task_as_foreground() {
|
||||
let mut manager = TaskManager::new();
|
||||
let task_id = manager.create_game_task(ProcessId(1), 42, "Sector Crawl");
|
||||
|
||||
assert!(manager.set_foreground(task_id));
|
||||
|
||||
let task = manager.get(task_id).expect("task should exist");
|
||||
|
||||
assert_eq!(manager.foreground_task(), Some(task_id));
|
||||
assert_eq!(task.state, TaskState::Foreground);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_foreground_moves_previous_task_to_background() {
|
||||
let mut manager = TaskManager::new();
|
||||
|
||||
let first = manager.create_game_task(ProcessId(1), 1, "First");
|
||||
let second = manager.create_game_task(ProcessId(2), 2, "Second");
|
||||
|
||||
assert!(manager.set_foreground(first));
|
||||
assert!(manager.set_foreground(second));
|
||||
|
||||
assert_eq!(manager.foreground_task(), Some(second));
|
||||
assert_eq!(
|
||||
manager.get(first).expect("first task should exist").state,
|
||||
TaskState::Background
|
||||
);
|
||||
assert_eq!(
|
||||
manager.get(second).expect("second task should exist").state,
|
||||
TaskState::Foreground
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_foreground_unknown_task_returns_false() {
|
||||
let mut manager = TaskManager::new();
|
||||
|
||||
assert!(!manager.set_foreground(TaskId(999)));
|
||||
assert_eq!(manager.foreground_task(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn close_foreground_task_clears_foreground() {
|
||||
let mut manager = TaskManager::new();
|
||||
|
||||
let task_id = manager.create_game_task(ProcessId(1), 42, "Sector Crawl");
|
||||
|
||||
assert!(manager.set_foreground(task_id));
|
||||
assert!(manager.close_task(task_id));
|
||||
|
||||
let task = manager.get(task_id).expect("task should exist");
|
||||
|
||||
assert_eq!(task.state, TaskState::Closed);
|
||||
assert_eq!(manager.foreground_task(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn suspend_foreground_task_clears_foreground() {
|
||||
let mut manager = TaskManager::new();
|
||||
|
||||
let task_id = manager.create_game_task(ProcessId(1), 42, "Sector Crawl");
|
||||
|
||||
assert!(manager.set_foreground(task_id));
|
||||
assert!(manager.mark_suspended(task_id));
|
||||
|
||||
let task = manager.get(task_id).expect("task should exist");
|
||||
|
||||
assert_eq!(task.state, TaskState::Suspended);
|
||||
assert_eq!(manager.foreground_task(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn crashed_foreground_task_clears_foreground() {
|
||||
let mut manager = TaskManager::new();
|
||||
|
||||
let task_id = manager.create_game_task(ProcessId(1), 42, "Sector Crawl");
|
||||
|
||||
assert!(manager.set_foreground(task_id));
|
||||
assert!(manager.mark_crashed(task_id));
|
||||
|
||||
let task = manager.get(task_id).expect("task should exist");
|
||||
|
||||
assert_eq!(task.state, TaskState::Crashed);
|
||||
assert_eq!(manager.foreground_task(), None);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum TaskState {
|
||||
Foreground,
|
||||
Background,
|
||||
Suspended,
|
||||
Closed,
|
||||
Crashed,
|
||||
}
|
||||
Reference in New Issue
Block a user