From 7b7d48c775d71f22ade6dbc8e9d8fed626288f84 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Thu, 4 May 2023 22:58:13 +0200 Subject: [PATCH] Try to fix double free --- src/kernel/thread_manager.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/kernel/thread_manager.rs b/src/kernel/thread_manager.rs index 82faf87..f7858fa 100644 --- a/src/kernel/thread_manager.rs +++ b/src/kernel/thread_manager.rs @@ -367,29 +367,30 @@ impl ThreadManager { /// Wake up a waiter if necessary, or release it if no thread is waiting. pub fn lock_release(&mut self, id: i32, machine: &mut Machine) -> Result { + let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff); let current_thread = match self.get_g_current_thread() { Some(thread) => Rc::clone(thread), None => Err(String::from("lock_release error: current_thread should not be None."))? }; - let mut lock = match self.get_obj_addrs().search_lock(id).cloned() { + let mut lock = match self.get_obj_addrs().search_lock(id) { Some(lock) => lock, None => Err(String::from("lock_release error: cannot find lock."))? }; - let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff); if let Some(lock_owner) = &lock.owner { - if Rc::ptr_eq(¤t_thread, lock_owner) { - if let Some(thread) = lock.waiting_queue.pop() { - if !lock.waiting_queue.is_empty() { - let clone = Rc::clone(&thread); - lock.owner = Some(thread); - self.ready_to_run(clone); - } else { - lock.free = true; - lock.owner = None; - }} + if current_thread.eq(lock_owner) { // is_held_by_current_thread + match lock.waiting_queue.pop() { + Some(th) => { + lock.owner = Some(Rc::clone(&th)); + self.ready_to_run(Rc::clone(&th)); + }, + None => { + lock.free = true; + lock.owner = None; + } + } } }; - self.get_obj_addrs().update_lock(id, lock); + // self.get_obj_addrs().update_lock(id, lock); machine.interrupt.set_status(old_status); Ok(MachineOk::Ok) }