1
0
mirror of https://github.com/rcore-os/rCore.git synced 2025-01-18 08:57:05 +04:00

remove exit thread from THREADS

This commit is contained in:
刘丰源 2020-07-28 18:37:22 +08:00
parent fbf3177b5d
commit de003ed64c
3 changed files with 29 additions and 4 deletions

View File

@ -106,6 +106,10 @@ pub struct Process {
/// threads in the same process
pub threads: Vec<Tid>,
/// Threads
/// threads in the same process, witch has exit, wait release
pub exit_threads: Vec<Tid>,
/// Events like exiting
pub eventbus: Arc<Mutex<EventBus>>,
@ -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);
}

View File

@ -93,15 +93,29 @@ lazy_static! {
RwLock::new(BTreeMap::new());
}
/// next thread's id
pub static ALLOC_ID: Mutex<usize> = 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<Self> {
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<Thread>) {
_ 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

View File

@ -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 {