forked from Rativel/BurritOS
Changed all reference to thread with an RefCell to enforce mutability
This commit is contained in:
committed by
François Autin
parent
a1713e0373
commit
45fea708fc
@@ -2,26 +2,29 @@ use crate::utility::list::List;
|
||||
use crate::kernel::thread::Thread;
|
||||
use crate::simulator::interrupt::InterruptStatus::InterruptOff;
|
||||
use crate::simulator::machine::Machine;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
|
||||
use super::scheduler::Scheduler;
|
||||
use super::thread_manager::ThreadManager;
|
||||
|
||||
pub struct Semaphore{
|
||||
pub struct Semaphore<'t> {
|
||||
|
||||
counter:i32,
|
||||
waiting_queue:List<Rc<Thread>>
|
||||
waiting_queue:List<Rc<RefCell<Thread>>>,
|
||||
thread_manager: Rc<RefCell<ThreadManager<'t>>> // On s'assure que le tm vit plus longtemps que les semaphore avec le lifetime
|
||||
|
||||
}
|
||||
|
||||
impl Semaphore{
|
||||
impl<'t> Semaphore<'_> {
|
||||
|
||||
pub fn p(&mut self, current_thread: Rc<Thread>, machine: &mut Machine){
|
||||
pub fn p(&mut self, current_thread: Rc<RefCell<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();
|
||||
self.thread_manager.borrow_mut().thread_sleep(current_thread);
|
||||
}
|
||||
machine.interrupt.set_status(old_status);
|
||||
}
|
||||
@@ -36,16 +39,17 @@ impl Semaphore{
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Lock{
|
||||
pub struct Lock<'t>{
|
||||
|
||||
owner: Rc<Thread>,
|
||||
waiting_queue:List<Rc<Thread>>,
|
||||
owner: Rc<RefCell<Thread>>,
|
||||
waiting_queue:List<Rc<RefCell<Thread>>>,
|
||||
thread_manager: Rc<RefCell<ThreadManager<'t>>>,
|
||||
free: bool
|
||||
|
||||
}
|
||||
|
||||
impl Lock {
|
||||
pub fn acquire(&mut self, machine: &mut Machine, current_thread: Rc<Thread>) {
|
||||
impl<'t> Lock<'_> {
|
||||
pub fn acquire(&mut self, machine: &mut Machine, current_thread: Rc<RefCell<Thread>>) {
|
||||
let old_status = machine.interrupt.set_status(InterruptOff);
|
||||
|
||||
if self.free {
|
||||
@@ -53,13 +57,13 @@ impl Lock {
|
||||
self.owner = current_thread;
|
||||
} else {
|
||||
self.waiting_queue.push(Rc::clone(¤t_thread));
|
||||
current_thread.sleep();
|
||||
self.thread_manager.borrow_mut().thread_sleep(current_thread);
|
||||
}
|
||||
|
||||
machine.interrupt.set_status(old_status);
|
||||
}
|
||||
|
||||
pub fn release(&mut self, machine: &mut Machine, scheduler: &mut Scheduler, current_thread: Rc<Thread>) {
|
||||
pub fn release(&mut self, machine: &mut Machine, scheduler: &mut Scheduler, current_thread: Rc<RefCell<Thread>>) {
|
||||
let old_status = machine.interrupt.set_status(InterruptOff);
|
||||
|
||||
if self.is_held_by_current_thread(current_thread) {
|
||||
@@ -74,24 +78,25 @@ impl Lock {
|
||||
machine.interrupt.set_status(old_status);
|
||||
}
|
||||
|
||||
pub fn is_held_by_current_thread(&mut self, current_thread: Rc<Thread>) -> bool {
|
||||
pub fn is_held_by_current_thread(&mut self, current_thread: Rc<RefCell<Thread>>) -> bool {
|
||||
Rc::ptr_eq(&self.owner, ¤t_thread)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Condition{
|
||||
pub struct Condition<'t>{
|
||||
|
||||
waiting_queue:List<Rc<Thread>>
|
||||
waiting_queue:List<Rc<RefCell<Thread>>>,
|
||||
thread_manager: Rc<RefCell<ThreadManager<'t>>>,
|
||||
|
||||
}
|
||||
|
||||
impl Condition {
|
||||
impl<'t> Condition<'_> {
|
||||
|
||||
pub fn wait(&mut self, machine: &mut Machine, current_thread: Rc<Thread>) {
|
||||
pub fn wait(&mut self, machine: &mut Machine, current_thread: Rc<RefCell<Thread>>) {
|
||||
let old_status = machine.interrupt.set_status(InterruptOff);
|
||||
|
||||
self.waiting_queue.push(Rc::clone(¤t_thread));
|
||||
current_thread.sleep();
|
||||
self.thread_manager.borrow_mut().thread_sleep(current_thread);
|
||||
|
||||
machine.interrupt.set_status(old_status);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user