♻️ Refactored lock_acquire
This commit is contained in:
parent
f55189f1fe
commit
5734e02b30
@ -257,29 +257,24 @@ impl ThreadManager {
|
|||||||
/// - **id** id of the lock, stored in [`ObjAddr`], id given by user program thought exceptions
|
/// - **id** id of the lock, stored in [`ObjAddr`], id given by user program thought exceptions
|
||||||
/// - **machine** the machine where the threads are executed
|
/// - **machine** the machine where the threads are executed
|
||||||
pub fn lock_acquire(&mut self, id: i32, machine: &mut Machine) -> Result<MachineOk, MachineError> {
|
pub fn lock_acquire(&mut self, id: i32, machine: &mut Machine) -> Result<MachineOk, MachineError> {
|
||||||
match self.get_g_current_thread() {
|
let current_thread = match self.get_g_current_thread() {
|
||||||
Some(thread) => {
|
Some(thread) => Rc::clone(thread),
|
||||||
let rc_thread = Rc::clone(thread);
|
None => Err("lock_acquire error: current_thread should not be None.")?
|
||||||
let lock = self.get_obj_addrs().search_lock(id);
|
};
|
||||||
match lock {
|
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
|
||||||
Some(lock) => {
|
if let Some(lock) = self.get_obj_addrs().search_lock(id) {
|
||||||
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
|
if lock.free {
|
||||||
if lock.free {
|
lock.free = false;
|
||||||
lock.free = false;
|
lock.owner = Some(current_thread)
|
||||||
lock.owner = Some(Rc::clone(&rc_thread))
|
} else {
|
||||||
} else {
|
lock.waiting_queue.push(current_thread.clone());
|
||||||
let thread = Rc::clone(&rc_thread);
|
self.thread_sleep(machine, current_thread);
|
||||||
lock.waiting_queue.push(Rc::clone(&thread));
|
}
|
||||||
self.thread_sleep(machine, Rc::clone(&thread));
|
} else {
|
||||||
}
|
Err("lock_acquire error: cannot find Lock.")?
|
||||||
machine.interrupt.set_status(old_status);
|
|
||||||
Ok(MachineOk::Ok)
|
|
||||||
},
|
|
||||||
None => Err("Cannot find lock")?
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None => unreachable!("Current thread should not be None")
|
|
||||||
}
|
}
|
||||||
|
machine.interrupt.set_status(old_status);
|
||||||
|
Ok(MachineOk::Ok)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wake up a waiter if necessary, or release it if no thread is waiting.
|
/// Wake up a waiter if necessary, or release it if no thread is waiting.
|
||||||
|
Loading…
Reference in New Issue
Block a user