use super::id::ProcId;
use alloc::vec::Vec;
#[cfg(feature = "proc")]
pub struct ProcRel {
pub parent: ProcId,
pub children: Vec<ProcId>,
pub dead_children: Vec<(ProcId, isize)>,
}
impl ProcRel {
pub fn new(parent_pid: ProcId) -> Self {
Self {
parent: parent_pid,
children: Vec::new(),
dead_children: Vec::new(),
}
}
pub fn add_child(&mut self, child_pid: ProcId) {
self.children.push(child_pid);
}
pub fn del_child(&mut self, child_pid: ProcId, exit_code: isize) {
let pair = self
.children
.iter()
.enumerate()
.find(|&(_, &id)| id == child_pid);
if let Some((idx, _)) = pair {
let dead_child = self.children.remove(idx);
self.dead_children.push((dead_child, exit_code));
}
}
pub fn wait_any_child(&mut self) -> Option<(ProcId, isize)> {
if self.dead_children.is_empty() {
if self.children.is_empty() {
None
} else {
Some((ProcId::from_usize(-2 as _), -1))
}
} else {
self.dead_children.pop()
}
}
pub fn wait_child(&mut self, child_pid: ProcId) -> Option<(ProcId, isize)> {
let pair = self
.dead_children
.iter()
.enumerate()
.find(|&(_, &(id, _))| id == child_pid);
if let Some((idx, _)) = pair {
Some(self.dead_children.remove(idx))
} else {
let pair = self
.children
.iter()
.enumerate()
.find(|&(_, &id)| id == child_pid);
if let Some(_) = pair {
Some((ProcId::from_usize(-2 as _), -1))
} else {
None
}
}
}
}