From de003ed64cdab6f9b37db954a81a189896c77c62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E4=B8=B0=E6=BA=90?= Date: Tue, 28 Jul 2020 18:37:22 +0800 Subject: [PATCH] remove exit thread from THREADS --- kernel/src/process/proc.rs | 8 ++++++++ kernel/src/process/thread.rs | 24 ++++++++++++++++++++---- kernel/src/syscall/proc.rs | 1 + 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/kernel/src/process/proc.rs b/kernel/src/process/proc.rs index dc1bfce4..930c6ede 100644 --- a/kernel/src/process/proc.rs +++ b/kernel/src/process/proc.rs @@ -106,6 +106,10 @@ pub struct Process { /// threads in the same process pub threads: Vec, + /// Threads + /// threads in the same process, witch has exit, wait release + pub exit_threads: Vec, + /// Events like exiting pub eventbus: Arc>, @@ -217,7 +221,11 @@ impl Process { for tid in self.threads.iter() { thread_table.remove(tid); } + for tid in self.exit_threads.iter() { + thread_table.remove(tid); + } self.threads.clear(); + self.exit_threads.clear(); info!("process {} exit with {}", self.pid.get(), exit_code); } diff --git a/kernel/src/process/thread.rs b/kernel/src/process/thread.rs index 3891aae2..2a6c20a9 100644 --- a/kernel/src/process/thread.rs +++ b/kernel/src/process/thread.rs @@ -93,15 +93,29 @@ lazy_static! { RwLock::new(BTreeMap::new()); } +/// next thread's id +pub static ALLOC_ID: Mutex = Mutex::new(1); + +fn alloc_tid() -> Tid { + // let tid = (Pid::INIT..) + // .find(|i| thread_table.get(i).is_none()) + // .unwrap(); + let mut w = ALLOC_ID.lock(); + let tid = *w; + *w += 1; + if *w > 1000 { + *w = 1; + } + tid +} + impl Thread { /// Assign a tid and put itself to global thread table. pub fn add_to_table(mut self) -> Arc { let mut thread_table = THREADS.write(); // assign tid, do not start from 0 - let tid = (Pid::INIT..) - .find(|i| thread_table.get(i).is_none()) - .unwrap(); + let tid = alloc_tid(); self.tid = tid; // put to thread table @@ -343,6 +357,7 @@ impl Thread { parent: (Pid::new(), Weak::new()), children: Vec::new(), threads: Vec::new(), + exit_threads: Vec::new(), exit_code: 0, pending_sigset: Sigset::empty(), sig_queue: VecDeque::new(), @@ -386,6 +401,7 @@ impl Thread { parent: (proc.pid.clone(), Arc::downgrade(&self.proc)), children: Vec::new(), threads: Vec::new(), + exit_threads: Vec::new(), exit_code: 0, pending_sigset: Sigset::empty(), sig_queue: VecDeque::new(), @@ -516,7 +532,7 @@ pub fn spawn(thread: Arc) { _ if is_page_fault(trap_num) => { // page fault let addr = get_page_fault_addr(); - info!("page fault from user @ {:#x}", addr); + // info!("page fault from user @ {:#x}", addr); if !handle_user_page_fault(&thread, addr) { // TODO: SIGSEGV diff --git a/kernel/src/syscall/proc.rs b/kernel/src/syscall/proc.rs index ddb7feed..429f73ad 100644 --- a/kernel/src/syscall/proc.rs +++ b/kernel/src/syscall/proc.rs @@ -342,6 +342,7 @@ impl Syscall<'_> { let mut proc = self.process(); proc.threads.retain(|&id| id != tid); + proc.exit_threads.push(tid); // for last thread, exit the process if proc.threads.len() == 0 {