implemented lock_release in thread_manager.rs, for this, I derived the clone function for lock in synch.rs and list in list.rs
This commit is contained in:
parent
ec2f50f7d3
commit
35b2949243
@ -34,6 +34,7 @@ impl Semaphore {
|
|||||||
/// counter of 1
|
/// counter of 1
|
||||||
/// It's used for critical parts
|
/// It's used for critical parts
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct Lock {
|
pub struct Lock {
|
||||||
|
|
||||||
/// Thread owning the lock
|
/// Thread owning the lock
|
||||||
|
@ -282,8 +282,47 @@ impl ThreadManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Wake up a waiter if necessary, or release it if no thread is waiting.
|
||||||
pub fn lock_release(&mut self, id: i32, machine: &mut Machine) -> Result<MachineOk, MachineError> {
|
pub fn lock_release(&mut self, id: i32, machine: &mut Machine) -> Result<MachineOk, MachineError> {
|
||||||
todo!()
|
let current_thread = self.get_g_current_thread();
|
||||||
|
match current_thread {
|
||||||
|
Some(thread) => {
|
||||||
|
let current_thread = Rc::clone(&thread);
|
||||||
|
|
||||||
|
let mut lock_option = self.get_obj_addrs().search_lock(id).cloned();
|
||||||
|
match lock_option {
|
||||||
|
Some(mut lock) => {
|
||||||
|
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
|
||||||
|
|
||||||
|
match &lock.owner {
|
||||||
|
Some(lock_owner) => {
|
||||||
|
if Rc::ptr_eq(¤t_thread, lock_owner) {
|
||||||
|
match lock.waiting_queue.pop() {
|
||||||
|
Some(thread) => {
|
||||||
|
lock.owner = Some(thread);
|
||||||
|
match &lock.owner {
|
||||||
|
Some(x) => self.ready_to_run(Rc::clone(x)),
|
||||||
|
None => ()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
lock.free = true;
|
||||||
|
lock.owner = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => ()
|
||||||
|
}
|
||||||
|
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
|
/// Currently running thread
|
||||||
|
@ -10,6 +10,7 @@ use std::ptr;
|
|||||||
/// but everything has been tested with miri to assure there's no Undefined Behaviour (use-after-free, double free, etc.)
|
/// but everything has been tested with miri to assure there's no Undefined Behaviour (use-after-free, double free, etc.)
|
||||||
/// or memory leak
|
/// or memory leak
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct List<T: PartialEq> {
|
pub struct List<T: PartialEq> {
|
||||||
head: Link<T>,
|
head: Link<T>,
|
||||||
tail: *mut Node<T>,
|
tail: *mut Node<T>,
|
||||||
|
Loading…
Reference in New Issue
Block a user