2023-03-08 15:45:35 +01:00
|
|
|
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;
|
2023-03-08 15:46:27 +01:00
|
|
|
if self.waiting_queue.peek() == None {
|
2023-03-08 15:45:35 +01:00
|
|
|
self.waiting_queue.push(Rc::clone(¤t_thread));
|
|
|
|
current_thread.sleep();
|
|
|
|
}
|
|
|
|
machine.interrupt.set_status(old_status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Lock{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Condition{
|
|
|
|
|
|
|
|
}
|