forked from Rativel/BurritOS
update synch.rs
This commit is contained in:
@@ -40,17 +40,22 @@ impl Semaphore {
|
||||
/// Note that thread_manager::thread_sleep assumes that interrupts are disabled
|
||||
/// when it is called.
|
||||
///
|
||||
/// ### Parameters
|
||||
/// ### Parameters TODO Refaire
|
||||
/// - *current_thread* the current thread
|
||||
/// - *machine* the machine where the threads are executed
|
||||
pub fn p(&mut self, current_thread: Rc<RefCell<Thread>>, system: Rc<RefCell<System>>) {
|
||||
let old_status = system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(InterruptOff);
|
||||
pub fn p(&mut self, system: &mut System) {
|
||||
let old_status = system.get_machine().interrupt.set_status(InterruptOff);
|
||||
self.counter -= 1;
|
||||
if self.counter < 0 {
|
||||
self.waiting_queue.push(Rc::clone(¤t_thread));
|
||||
self.thread_manager.borrow_mut().thread_sleep(current_thread);
|
||||
match system.get_thread_manager().get_g_current_thread() {
|
||||
Some(thread) => {
|
||||
self.waiting_queue.push(Rc::clone(thread));
|
||||
system.get_thread_manager().thread_sleep(system, Rc::clone(thread));
|
||||
},
|
||||
None => unreachable!("Current thread should not be None")
|
||||
}
|
||||
}
|
||||
system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(old_status);
|
||||
system.get_machine().interrupt.set_status(old_status);
|
||||
}
|
||||
|
||||
/// Increment semaphore value, waking up a waiting thread if any.
|
||||
@@ -63,13 +68,13 @@ impl Semaphore {
|
||||
/// ### Parameters
|
||||
/// - **machine** the machine where the threads are executed
|
||||
/// - **scheduler** the scheduler which determine which thread to execute
|
||||
pub fn v(&mut self, system: Rc<RefCell<System>>){
|
||||
let old_status = system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(InterruptOff);
|
||||
pub fn v(&mut self, system: &mut System){
|
||||
let old_status = system.get_machine().interrupt.set_status(InterruptOff);
|
||||
self.counter += 1;
|
||||
if self.waiting_queue.peek() != None {
|
||||
system.borrow_mut().get_thread_manager().borrow_mut().g_scheduler.ready_to_run(self.waiting_queue.pop().unwrap());
|
||||
system.get_thread_manager().g_scheduler.ready_to_run(self.waiting_queue.pop().unwrap());
|
||||
}
|
||||
system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(old_status);
|
||||
system.get_machine().interrupt.set_status(old_status);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,23 +116,30 @@ impl Lock {
|
||||
/// ### Parameters
|
||||
/// - **current_thread** the current thread
|
||||
/// - **machine** the machine where the threads are executed
|
||||
pub fn acquire(&mut self, current_thread: Option<Rc<RefCell<Thread>>>, system: Rc<RefCell<System>>) {
|
||||
let old_status = system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(InterruptOff);
|
||||
pub fn acquire(&mut self, system: &mut System) {
|
||||
let old_status = system.get_machine().interrupt.set_status(InterruptOff);
|
||||
|
||||
if self.free {
|
||||
self.free = false;
|
||||
self.owner = current_thread;
|
||||
} else {
|
||||
match current_thread {
|
||||
Some(x) => {
|
||||
self.waiting_queue.push(Rc::clone(&x));
|
||||
self.thread_manager.borrow_mut().thread_sleep(x)
|
||||
self.owner = Option::Some(match system.get_thread_manager().get_g_current_thread() {
|
||||
Some(th) => {
|
||||
Rc::clone(th)
|
||||
},
|
||||
None => ()
|
||||
None => unreachable!()
|
||||
});
|
||||
} else {
|
||||
let t = system.get_thread_manager().get_g_current_thread();
|
||||
match t {
|
||||
Some(x) => {
|
||||
let x = Rc::clone(x);
|
||||
self.waiting_queue.push(Rc::clone(&x));
|
||||
system.thread_sleep(Rc::clone(&x));
|
||||
},
|
||||
None => unreachable!("Current thread should not be None")
|
||||
}
|
||||
}
|
||||
|
||||
system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(old_status);
|
||||
system.get_machine().interrupt.set_status(old_status);
|
||||
}
|
||||
|
||||
/// Wake up a waiter if necessary, or release it if no thread is waiting.
|
||||
@@ -139,31 +151,37 @@ impl Lock {
|
||||
/// ### Parameters
|
||||
/// - **machine** the machine where the code is executed
|
||||
/// - **scheduler** the scheduler which determine which thread to execute
|
||||
pub fn release(&mut self, system: Rc<RefCell<System>>, current_thread: Rc<RefCell<Thread>>) {
|
||||
let old_status = system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(InterruptOff);
|
||||
pub fn release(&mut self, system: &mut System) {
|
||||
let old_status = system.get_machine().interrupt.set_status(InterruptOff);
|
||||
|
||||
if self.held_by_current_thread(current_thread) {
|
||||
if self.waiting_queue.peek() != None {
|
||||
self.owner = Some(self.waiting_queue.pop().unwrap());
|
||||
let sys = system.borrow_mut();
|
||||
let tm = sys.get_thread_manager();
|
||||
let scheduler = &mut tm.borrow_mut().g_scheduler;
|
||||
match &self.owner {
|
||||
Some(x) => scheduler.ready_to_run(Rc::clone(&x)),
|
||||
None => ()
|
||||
match system.get_thread_manager().get_g_current_thread() {
|
||||
Some(thread) => {
|
||||
if self.held_by_current_thread(system) {
|
||||
if self.waiting_queue.peek() != None {
|
||||
self.owner = Some(self.waiting_queue.pop().unwrap());
|
||||
match &self.owner {
|
||||
Some(x) => system.get_thread_manager().g_scheduler.ready_to_run(Rc::clone(&x)),
|
||||
None => ()
|
||||
}
|
||||
} else {
|
||||
self.free = true;
|
||||
self.owner = None;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.free = true;
|
||||
self.owner = None;
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
|
||||
system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(old_status);
|
||||
system.get_machine().interrupt.set_status(old_status);
|
||||
}
|
||||
|
||||
pub fn held_by_current_thread(&mut self, current_thread: Rc<RefCell<Thread>>) -> bool {
|
||||
pub fn held_by_current_thread(&mut self, system: &mut System) -> bool {
|
||||
match &self.owner {
|
||||
Some(x) => Rc::ptr_eq(&x, ¤t_thread),
|
||||
Some(x) =>
|
||||
match system.get_thread_manager().get_g_current_thread() {
|
||||
Some(thread) => Rc::ptr_eq(&x, thread),
|
||||
None => false
|
||||
}
|
||||
None => false
|
||||
}
|
||||
}
|
||||
@@ -195,11 +213,11 @@ impl Condition {
|
||||
/// ### Parameters
|
||||
/// - **current_thread** the current thread
|
||||
/// - **machine** the machine where threads are executed
|
||||
pub fn wait(&mut self, current_thread: Rc<RefCell<Thread>>, machine: &mut Machine) {
|
||||
pub fn wait(&mut self, current_thread: Rc<RefCell<Thread>>, machine: &mut Machine, system: &mut System) {
|
||||
let old_status = machine.interrupt.set_status(InterruptOff);
|
||||
|
||||
self.waiting_queue.push(Rc::clone(¤t_thread));
|
||||
self.thread_manager.borrow_mut().thread_sleep(current_thread);
|
||||
self.thread_manager.borrow_mut().thread_sleep(system, current_thread);
|
||||
|
||||
machine.interrupt.set_status(old_status);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user