asterinas/kernel/aster-nix/src/process/process_table.rs

112 lines
3.2 KiB
Rust
Raw Normal View History

2024-01-03 03:22:36 +00:00
// SPDX-License-Identifier: MPL-2.0
2022-10-26 09:47:38 +00:00
//! A global table stores the pid to process mapping.
//! This table can be used to get process with pid.
//! TODO: progress group, thread all need similar mapping
use alloc::collections::btree_map::Values;
use super::{Pgid, Pid, Process, ProcessGroup, Session, Sid};
use crate::{
events::{Events, Observer, Subject},
prelude::*,
};
2022-10-26 09:47:38 +00:00
static PROCESS_TABLE: Mutex<BTreeMap<Pid, Arc<Process>>> = Mutex::new(BTreeMap::new());
static PROCESS_GROUP_TABLE: Mutex<BTreeMap<Pgid, Arc<ProcessGroup>>> = Mutex::new(BTreeMap::new());
static PROCESS_TABLE_SUBJECT: Subject<PidEvent> = Subject::new();
static SESSION_TABLE: Mutex<BTreeMap<Sid, Arc<Session>>> = Mutex::new(BTreeMap::new());
2022-10-26 09:47:38 +00:00
// ************ Process *************
2023-03-27 05:55:13 +00:00
/// Gets a process with pid
pub fn get_process(pid: &Pid) -> Option<Arc<Process>> {
PROCESS_TABLE.lock().get(pid).cloned()
2022-10-26 09:47:38 +00:00
}
pub(super) fn process_table_mut() -> MutexGuard<'static, BTreeMap<Pid, Arc<Process>>> {
PROCESS_TABLE.lock()
2022-10-26 09:47:38 +00:00
}
/// Acquires a lock on the process table and returns a `ProcessTable`.
pub fn process_table() -> ProcessTable<'static> {
ProcessTable {
inner: PROCESS_TABLE.lock(),
}
}
/// A wrapper for the mutex-protected process table.
///
/// It provides the `iter` method to iterator over the processes in the table.
pub struct ProcessTable<'a> {
inner: MutexGuard<'a, BTreeMap<Pid, Arc<Process>>>,
}
impl<'a> ProcessTable<'a> {
/// Returns an iterator over the processes in the table.
pub fn iter(&self) -> ProcessTableIter {
ProcessTableIter {
inner: self.inner.values(),
}
}
}
/// An iterator over the processes of the process table.
pub struct ProcessTableIter<'a> {
inner: Values<'a, Pid, Arc<Process>>,
}
impl<'a> Iterator for ProcessTableIter<'a> {
type Item = &'a Arc<Process>;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
2022-10-26 09:47:38 +00:00
}
2022-10-31 08:14:41 +00:00
// ************ Process Group *************
/// Gets a process group with `pgid`
pub fn get_process_group(pgid: &Pgid) -> Option<Arc<ProcessGroup>> {
PROCESS_GROUP_TABLE.lock().get(pgid).cloned()
}
/// Returns whether process table contains process group with pgid
pub fn contain_process_group(pgid: &Pgid) -> bool {
PROCESS_GROUP_TABLE.lock().contains_key(pgid)
2022-10-31 08:14:41 +00:00
}
pub(super) fn group_table_mut() -> MutexGuard<'static, BTreeMap<Pgid, Arc<ProcessGroup>>> {
PROCESS_GROUP_TABLE.lock()
2022-10-31 08:14:41 +00:00
}
// ************ Session *************
/// Gets a session with `sid`.
pub fn get_session(sid: &Sid) -> Option<Arc<Session>> {
SESSION_TABLE.lock().get(sid).map(Arc::clone)
2022-10-31 08:14:41 +00:00
}
2023-03-27 05:55:13 +00:00
pub(super) fn session_table_mut() -> MutexGuard<'static, BTreeMap<Sid, Arc<Session>>> {
SESSION_TABLE.lock()
}
// ************ Observer *************
/// Registers an observer which watches `PidEvent`.
2023-03-27 05:55:13 +00:00
pub fn register_observer(observer: Weak<dyn Observer<PidEvent>>) {
PROCESS_TABLE_SUBJECT.register_observer(observer, ());
2023-03-27 05:55:13 +00:00
}
/// Unregisters an observer which watches `PidEvent`.
2023-04-18 08:13:52 +00:00
pub fn unregister_observer(observer: &Weak<dyn Observer<PidEvent>>) {
2023-03-27 05:55:13 +00:00
PROCESS_TABLE_SUBJECT.unregister_observer(observer);
}
#[derive(Copy, Clone)]
pub enum PidEvent {
Exit(Pid),
}
impl Events for PidEvent {}