From cf65688566a91af5fb0df795432c8eb8ce591caa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Rativel?= Date: Wed, 8 Mar 2023 15:45:35 +0100 Subject: [PATCH 1/3] Sarting synch.rs implementation --- src/kernel/mod.rs | 3 ++- src/kernel/synch.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/kernel/synch.rs diff --git a/src/kernel/mod.rs b/src/kernel/mod.rs index e061a2a..f1abafa 100644 --- a/src/kernel/mod.rs +++ b/src/kernel/mod.rs @@ -3,4 +3,5 @@ pub mod thread; pub mod scheduler; pub mod mgerror; pub mod system; -mod ucontext; \ No newline at end of file +mod ucontext; +mod synch; \ No newline at end of file diff --git a/src/kernel/synch.rs b/src/kernel/synch.rs new file mode 100644 index 0000000..8a7fe98 --- /dev/null +++ b/src/kernel/synch.rs @@ -0,0 +1,43 @@ +use crate::utility::list::List; +use crate::kernel::thread::Thread; +use std::rc::Rc; +use crate::simulator::interrupt::InterruptStatus::InterruptOff; +use crate::simulator::machine::Machine; + +pub struct Semaphore{ + + counter:i32, + waiting_queue:List> + +} + +impl Semaphore{ + + pub fn p(&mut self, current_thread:Rc, machine: &mut Machine){ + let old_status = machine.interrupt.set_status(InterruptOff); + self.counter-=1; + if self.counter < 0 { + self.waiting_queue.push(Rc::clone(¤t_thread)); + current_thread.sleep(); + } + machine.interrupt.set_status(old_status); + } + + pub fn v(&mut self, current_thread:Rc, machine: &mut Machine){ + let old_status = machine.interrupt.set_status(InterruptOff); + self.counter-=1; + if self.waiting_queue.peek == None { + self.waiting_queue.push(Rc::clone(¤t_thread)); + current_thread.sleep(); + } + machine.interrupt.set_status(old_status); + } +} + +pub struct Lock{ + +} + +pub struct Condition{ + +} \ No newline at end of file From a29f410a6637c7eedb942b2a4cd1016a8fc35042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Rativel?= Date: Wed, 8 Mar 2023 15:46:27 +0100 Subject: [PATCH 2/3] small fix --- src/kernel/synch.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kernel/synch.rs b/src/kernel/synch.rs index 8a7fe98..a2038e6 100644 --- a/src/kernel/synch.rs +++ b/src/kernel/synch.rs @@ -26,7 +26,7 @@ impl Semaphore{ pub fn v(&mut self, current_thread:Rc, machine: &mut Machine){ let old_status = machine.interrupt.set_status(InterruptOff); self.counter-=1; - if self.waiting_queue.peek == None { + if self.waiting_queue.peek() == None { self.waiting_queue.push(Rc::clone(¤t_thread)); current_thread.sleep(); } From 62b60186e97d29fe201e619239bdaa66820c2f79 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Wed, 8 Mar 2023 15:46:53 +0100 Subject: [PATCH 3/3] Add list.remove(T) --- src/utility/list.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/utility/list.rs b/src/utility/list.rs index a90ec2b..e750b17 100644 --- a/src/utility/list.rs +++ b/src/utility/list.rs @@ -69,6 +69,33 @@ impl List { false } + /// Remove the item from the list + /// + /// Return true if the item has been found, otherwise return false + /// + /// Worst-case complexity is O(n) + pub fn remove(&mut self, item: T)-> bool { + let mut found = false; + let mut tmp_list: List = List::new(); + while !self.is_empty() { + let current = self.pop().unwrap(); + if current != item { + tmp_list.push(current); + } else { + found = true; + break; + } + } + while !tmp_list.is_empty() { + self.push(tmp_list.pop().unwrap()); + } + found + } + + pub fn is_empty(&self) -> bool { + self.head.is_none() + } + pub fn into_iter(self) -> IntoIter { IntoIter(self) }