From 35b29492435a25e04da6ac96c9627899035ebc4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Rativel?= Date: Tue, 18 Apr 2023 12:13:56 +0200 Subject: [PATCH] implemented lock_release in thread_manager.rs, for this, I derived the clone function for lock in synch.rs and list in list.rs --- src/kernel/synch.rs | 1 + src/kernel/thread_manager.rs | 43 ++++++++++++++++++++++++++++++++++-- src/utility/list.rs | 1 + 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/kernel/synch.rs b/src/kernel/synch.rs index 0baa892..807793f 100644 --- a/src/kernel/synch.rs +++ b/src/kernel/synch.rs @@ -34,6 +34,7 @@ impl Semaphore { /// counter of 1 /// It's used for critical parts #[derive(PartialEq)] +#[derive(Clone)] pub struct Lock { /// Thread owning the lock diff --git a/src/kernel/thread_manager.rs b/src/kernel/thread_manager.rs index 26911b8..0f98417 100644 --- a/src/kernel/thread_manager.rs +++ b/src/kernel/thread_manager.rs @@ -282,8 +282,47 @@ impl ThreadManager { } } - pub fn lock_release(&mut self, id: i32, machine: &mut Machine) -> Result{ - todo!() + /// 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 current_thread = self.get_g_current_thread(); + match current_thread { + Some(thread) => { + let current_thread = Rc::clone(&thread); + + let mut lock_option = self.get_obj_addrs().search_lock(id).cloned(); + match lock_option { + Some(mut lock) => { + let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff); + + match &lock.owner { + Some(lock_owner) => { + if Rc::ptr_eq(¤t_thread, lock_owner) { + match lock.waiting_queue.pop() { + Some(thread) => { + lock.owner = Some(thread); + match &lock.owner { + Some(x) => self.ready_to_run(Rc::clone(x)), + None => () + } + }, + None => { + lock.free = true; + lock.owner = None; + } + } + } + }, + None => () + } + machine.interrupt.set_status(old_status); + Ok(MachineOk::Ok) + }, + None => Err("Cannot find lock")? + } + }, + None => unreachable!("Current thread should not be None") + } + } /// Currently running thread diff --git a/src/utility/list.rs b/src/utility/list.rs index adc08fa..7a90d0a 100644 --- a/src/utility/list.rs +++ b/src/utility/list.rs @@ -10,6 +10,7 @@ use std::ptr; /// but everything has been tested with miri to assure there's no Undefined Behaviour (use-after-free, double free, etc.) /// or memory leak #[derive(PartialEq)] +#[derive(Clone)] pub struct List { head: Link, tail: *mut Node,