added lock acquire system call

This commit is contained in:
Samy Solhi
2023-04-13 00:17:34 +02:00
parent 6e6d3424f5
commit 05f72af035
3 changed files with 48 additions and 5 deletions

View File

@ -236,6 +236,44 @@ impl ThreadManager {
}
/// Wait until the lock become free. Checking the
/// state of the lock (free or busy) and modify it must be done
/// atomically, so we need to disable interrupts before checking
/// the value of free.
///
/// Note that thread_manager::thread_seep assumes that interrupts are disabled
/// when it is called.
///
/// ### Parameters
/// - **id** id of the lock, stored in [`ObjAddr`], id given by user program thought exceptions
/// - **machine** the machine where the threads are executed
pub fn lock_acquire(&mut self, id: i32, machine: &mut Machine) -> Result<MachineOk, MachineError> {
match self.get_g_current_thread() {
Some(thread) => {
let rc_thread = Rc::clone(thread);
let lock = self.get_obj_addrs().search_lock(id);
match lock {
Some(lock) => {
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
if lock.free {
lock.free = false;
lock.owner = Some(Rc::clone(&rc_thread))
} else {
let thread = Rc::clone(&rc_thread);
lock.waiting_queue.push(Rc::clone(&thread));
self.thread_sleep(machine, Rc::clone(&thread));
}
machine.interrupt.set_status(old_status);
Ok(MachineOk::Ok)
},
None => Err("Cannot find lock")?
}
},
None => unreachable!("Current thread should not be None")
}
}
/// Currently running thread
pub fn get_g_current_thread(&mut self) -> &Option<Rc<RefCell<Thread>>> {
&self.g_current_thread