use std::collections::VecDeque;
use crate::sendable::SendableValue;
use crate::vm::VM;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct Pid(pub u64);
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct ScopeId(pub u64);
pub struct Scope {
pub id: ScopeId,
pub owner: Pid,
pub children: Vec<Pid>,
}
#[derive(Debug)]
pub enum BlockReason {
Receive,
Await(Pid),
Io(crate::io_backend::IoToken),
}
#[derive(Debug)]
pub enum ProcessStatus {
Runnable,
Blocked(BlockReason),
Done,
Failed(String),
}
pub struct Process {
pub pid: Pid,
pub vm: VM,
pub mailbox: VecDeque<SendableValue>,
pub status: ProcessStatus,
pub parent: Option<Pid>,
pub result: Option<SendableValue>,
pub scope_id: Option<ScopeId>,
pub cancelled: bool,
}
impl Process {
pub fn new(pid: Pid, vm: VM, parent: Option<Pid>) -> Self {
Self {
pid,
vm,
mailbox: VecDeque::new(),
status: ProcessStatus::Runnable,
parent,
result: None,
scope_id: None,
cancelled: false,
}
}
pub fn new_in_scope(pid: Pid, vm: VM, parent: Option<Pid>, scope_id: ScopeId) -> Self {
Self {
pid,
vm,
mailbox: VecDeque::new(),
status: ProcessStatus::Runnable,
parent,
result: None,
scope_id: Some(scope_id),
cancelled: false,
}
}
pub fn is_runnable(&self) -> bool {
matches!(self.status, ProcessStatus::Runnable)
}
pub fn is_done(&self) -> bool {
matches!(self.status, ProcessStatus::Done | ProcessStatus::Failed(_))
}
}