Merge branch 'thread_scheduler' of gitlab.istic.univ-rennes1.fr:simpleos/burritos into thread_scheduler
This commit is contained in:
commit
ed5b760758
@ -3,4 +3,5 @@ pub mod thread;
|
|||||||
pub mod scheduler;
|
pub mod scheduler;
|
||||||
pub mod mgerror;
|
pub mod mgerror;
|
||||||
pub mod system;
|
pub mod system;
|
||||||
mod ucontext;
|
mod ucontext;
|
||||||
|
mod synch;
|
43
src/kernel/synch.rs
Normal file
43
src/kernel/synch.rs
Normal file
@ -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<Rc<Thread>>
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Semaphore{
|
||||||
|
|
||||||
|
pub fn p(&mut self, current_thread:Rc<Thread>, 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<Thread>, 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{
|
||||||
|
|
||||||
|
}
|
@ -70,6 +70,33 @@ impl<T: PartialEq> List<T> {
|
|||||||
false
|
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<T> = 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<T> {
|
pub fn into_iter(self) -> IntoIter<T> {
|
||||||
IntoIter(self)
|
IntoIter(self)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user