mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-21 16:33:24 +00:00
Refactor process table
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
91171c38ec
commit
6ece48c095
@ -130,7 +130,7 @@ impl DirOps for RootDirOps {
|
|||||||
cached_children
|
cached_children
|
||||||
.put_entry_if_not_found("meminfo", || MemInfoFileOps::new_inode(this_ptr.clone()));
|
.put_entry_if_not_found("meminfo", || MemInfoFileOps::new_inode(this_ptr.clone()));
|
||||||
|
|
||||||
for process in process_table::process_table().iter() {
|
for process in process_table::process_table_mut().iter() {
|
||||||
let pid = process.pid().to_string();
|
let pid = process.pid().to_string();
|
||||||
cached_children.put_entry_if_not_found(&pid, || {
|
cached_children.put_entry_if_not_found(&pid, || {
|
||||||
PidDirOps::new_inode(process.clone(), this_ptr.clone())
|
PidDirOps::new_inode(process.clone(), this_ptr.clone())
|
||||||
|
@ -105,7 +105,7 @@ pub fn tgkill(tid: Tid, tgid: Pid, signal: Option<UserSignal>, ctx: &Context) ->
|
|||||||
/// if it is authorized to send the signal to the target group.
|
/// if it is authorized to send the signal to the target group.
|
||||||
pub fn kill_all(signal: Option<UserSignal>, ctx: &Context) -> Result<()> {
|
pub fn kill_all(signal: Option<UserSignal>, ctx: &Context) -> Result<()> {
|
||||||
let current = current!();
|
let current = current!();
|
||||||
for process in process_table::process_table().iter() {
|
for process in process_table::process_table_mut().iter() {
|
||||||
if Arc::ptr_eq(¤t, process) || process.is_init_process() {
|
if Arc::ptr_eq(¤t, process) || process.is_init_process() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -14,43 +14,63 @@ use crate::{
|
|||||||
prelude::*,
|
prelude::*,
|
||||||
};
|
};
|
||||||
|
|
||||||
static PROCESS_TABLE: Mutex<BTreeMap<Pid, Arc<Process>>> = Mutex::new(BTreeMap::new());
|
static PROCESS_TABLE: Mutex<ProcessTable> = Mutex::new(ProcessTable::new());
|
||||||
static PROCESS_GROUP_TABLE: Mutex<BTreeMap<Pgid, Arc<ProcessGroup>>> = 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());
|
static SESSION_TABLE: Mutex<BTreeMap<Sid, Arc<Session>>> = Mutex::new(BTreeMap::new());
|
||||||
|
|
||||||
// ************ Process *************
|
// ************ Process *************
|
||||||
|
|
||||||
/// Gets a process with pid
|
/// Gets a process with pid
|
||||||
pub fn get_process(pid: Pid) -> Option<Arc<Process>> {
|
pub fn get_process(pid: Pid) -> Option<Arc<Process>> {
|
||||||
PROCESS_TABLE.lock().get(&pid).cloned()
|
PROCESS_TABLE.lock().get(pid).cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn process_table_mut() -> MutexGuard<'static, BTreeMap<Pid, Arc<Process>>> {
|
pub fn process_table_mut() -> MutexGuard<'static, ProcessTable> {
|
||||||
PROCESS_TABLE.lock()
|
PROCESS_TABLE.lock()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Acquires a lock on the process table and returns a `ProcessTable`.
|
/// Process Table.
|
||||||
pub fn process_table() -> ProcessTable<'static> {
|
pub struct ProcessTable {
|
||||||
ProcessTable {
|
inner: BTreeMap<Pid, Arc<Process>>,
|
||||||
inner: PROCESS_TABLE.lock(),
|
subject: Subject<PidEvent>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ProcessTable {
|
||||||
|
pub const fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
inner: BTreeMap::new(),
|
||||||
|
subject: Subject::new(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// A wrapper for the mutex-protected process table.
|
pub fn get(&self, pid: Pid) -> Option<&Arc<Process>> {
|
||||||
///
|
self.inner.get(&pid)
|
||||||
/// It provides the `iter` method to iterator over the processes in the table.
|
}
|
||||||
pub struct ProcessTable<'a> {
|
|
||||||
inner: MutexGuard<'a, BTreeMap<Pid, Arc<Process>>>,
|
pub fn insert(&mut self, pid: Pid, process: Arc<Process>) {
|
||||||
}
|
self.inner.insert(pid, process);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove(&mut self, pid: Pid) {
|
||||||
|
self.inner.remove(&pid);
|
||||||
|
self.subject.notify_observers(&PidEvent::Exit(pid));
|
||||||
|
}
|
||||||
|
|
||||||
impl ProcessTable<'_> {
|
|
||||||
/// Returns an iterator over the processes in the table.
|
/// Returns an iterator over the processes in the table.
|
||||||
pub fn iter(&self) -> ProcessTableIter {
|
pub fn iter(&self) -> ProcessTableIter {
|
||||||
ProcessTableIter {
|
ProcessTableIter {
|
||||||
inner: self.inner.values(),
|
inner: self.inner.values(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Registers an observer which watches `PidEvent`.
|
||||||
|
pub fn register_observer(&self, observer: Weak<dyn Observer<PidEvent>>) {
|
||||||
|
self.subject.register_observer(observer, ());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Unregisters an observer which watches `PidEvent`.
|
||||||
|
pub fn unregister_observer(&self, observer: &Weak<dyn Observer<PidEvent>>) {
|
||||||
|
self.subject.unregister_observer(observer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An iterator over the processes of the process table.
|
/// An iterator over the processes of the process table.
|
||||||
@ -97,12 +117,12 @@ pub(super) fn session_table_mut() -> MutexGuard<'static, BTreeMap<Sid, Arc<Sessi
|
|||||||
|
|
||||||
/// Registers an observer which watches `PidEvent`.
|
/// Registers an observer which watches `PidEvent`.
|
||||||
pub fn register_observer(observer: Weak<dyn Observer<PidEvent>>) {
|
pub fn register_observer(observer: Weak<dyn Observer<PidEvent>>) {
|
||||||
PROCESS_TABLE_SUBJECT.register_observer(observer, ());
|
PROCESS_TABLE.lock().register_observer(observer);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Unregisters an observer which watches `PidEvent`.
|
/// Unregisters an observer which watches `PidEvent`.
|
||||||
pub fn unregister_observer(observer: &Weak<dyn Observer<PidEvent>>) {
|
pub fn unregister_observer(observer: &Weak<dyn Observer<PidEvent>>) {
|
||||||
PROCESS_TABLE_SUBJECT.unregister_observer(observer);
|
PROCESS_TABLE.lock().unregister_observer(observer);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
|
@ -118,6 +118,6 @@ fn reap_zombie_child(process: &Process, pid: Pid) -> ExitCode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
process_table_mut.remove(&child_process.pid());
|
process_table_mut.remove(child_process.pid());
|
||||||
child_process.exit_code()
|
child_process.exit_code()
|
||||||
}
|
}
|
||||||
|
@ -69,7 +69,7 @@ fn get_processes(prio_target: PriorityTarget) -> Result<Vec<Arc<Process>>> {
|
|||||||
}
|
}
|
||||||
PriorityTarget::User(uid) => {
|
PriorityTarget::User(uid) => {
|
||||||
// Get the processes that are running under the specified user
|
// Get the processes that are running under the specified user
|
||||||
let processes: Vec<Arc<Process>> = process_table::process_table()
|
let processes: Vec<Arc<Process>> = process_table::process_table_mut()
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|process| {
|
.filter(|process| {
|
||||||
let Some(main_thread) = process.main_thread() else {
|
let Some(main_thread) = process.main_thread() else {
|
||||||
|
Reference in New Issue
Block a user