Fix lock uses in ch5

This commit is contained in:
Yifan Wu 2021-02-16 20:50:24 +08:00
parent fd75ac027a
commit 9b65abcfa8
3 changed files with 16 additions and 22 deletions

View File

@ -28,14 +28,13 @@ pub fn suspend_current_and_run_next() {
// There must be an application running. // There must be an application running.
let task = take_current_task().unwrap(); let task = take_current_task().unwrap();
// ---- temporarily hold current PCB lock // ---- hold current PCB lock
let task_cx_ptr2 = task.acquire_inner_lock().get_task_cx_ptr2(); let mut task_inner = task.acquire_inner_lock();
// ---- release current PCB lock let task_cx_ptr2 = task_inner.get_task_cx_ptr2();
// ++++ temporarily hold current PCB lock
// Change status to Ready // Change status to Ready
task.acquire_inner_lock().task_status = TaskStatus::Ready; task_inner.task_status = TaskStatus::Ready;
// ++++ release current PCB lock drop(task_inner);
// ---- release current PCB lock
// push back to ready queue. // push back to ready queue.
add_task(task); add_task(task);
@ -58,6 +57,7 @@ pub fn exit_current_and_run_next(exit_code: i32) {
{ {
let mut initproc_inner = INITPROC.acquire_inner_lock(); let mut initproc_inner = INITPROC.acquire_inner_lock();
for child in inner.children.iter() { for child in inner.children.iter() {
child.acquire_inner_lock().parent = Some(Arc::downgrade(&INITPROC));
initproc_inner.children.push(child.clone()); initproc_inner.children.push(child.clone());
} }
} }

View File

@ -35,8 +35,10 @@ impl Processor {
if let Some(task) = fetch_task() { if let Some(task) = fetch_task() {
let idle_task_cx_ptr2 = self.get_idle_task_cx_ptr2(); let idle_task_cx_ptr2 = self.get_idle_task_cx_ptr2();
// acquire // acquire
let next_task_cx_ptr2 = task.acquire_inner_lock().get_task_cx_ptr2(); let mut task_inner = task.acquire_inner_lock();
task.acquire_inner_lock().task_status = TaskStatus::Running; let next_task_cx_ptr2 = task_inner.get_task_cx_ptr2();
task_inner.task_status = TaskStatus::Running;
drop(task_inner);
// release // release
self.inner.borrow_mut().current = Some(task); self.inner.borrow_mut().current = Some(task);
unsafe { unsafe {
@ -52,7 +54,7 @@ impl Processor {
self.inner.borrow_mut().current.take() self.inner.borrow_mut().current.take()
} }
pub fn current(&self) -> Option<Arc<TaskControlBlock>> { pub fn current(&self) -> Option<Arc<TaskControlBlock>> {
self.inner.borrow().current.as_ref().map(|task| task.clone()) self.inner.borrow().current.as_ref().map(|task| Arc::clone(task))
} }
} }

View File

@ -67,7 +67,6 @@ impl TaskControlBlock {
.translate(VirtAddr::from(TRAP_CONTEXT).into()) .translate(VirtAddr::from(TRAP_CONTEXT).into())
.unwrap() .unwrap()
.ppn(); .ppn();
let task_status = TaskStatus::Ready;
// alloc a pid and a kernel stack in kernel space // alloc a pid and a kernel stack in kernel space
let pid_handle = pid_alloc(); let pid_handle = pid_alloc();
let kernel_stack = KernelStack::new(&pid_handle); let kernel_stack = KernelStack::new(&pid_handle);
@ -81,7 +80,7 @@ impl TaskControlBlock {
trap_cx_ppn, trap_cx_ppn,
base_size: user_sp, base_size: user_sp,
task_cx_ptr: task_cx_ptr as usize, task_cx_ptr: task_cx_ptr as usize,
task_status, task_status: TaskStatus::Ready,
memory_set, memory_set,
parent: None, parent: None,
children: Vec::new(), children: Vec::new(),
@ -97,9 +96,7 @@ impl TaskControlBlock {
}), }),
}; };
// prepare TrapContext in user space // prepare TrapContext in user space
// ---- acquire child PCB lock
let trap_cx = task_control_block.acquire_inner_lock().get_trap_cx(); let trap_cx = task_control_block.acquire_inner_lock().get_trap_cx();
// ---- release child PCB lock
*trap_cx = TrapContext::app_init_context( *trap_cx = TrapContext::app_init_context(
entry_point, entry_point,
user_sp, user_sp,
@ -123,13 +120,8 @@ impl TaskControlBlock {
inner.memory_set = memory_set; inner.memory_set = memory_set;
// update trap_cx ppn // update trap_cx ppn
inner.trap_cx_ppn = trap_cx_ppn; inner.trap_cx_ppn = trap_cx_ppn;
drop(inner);
// **** release current PCB lock manually
// initialize trap_cx // initialize trap_cx
// **** acquire current PCB lock let trap_cx = inner.get_trap_cx();
let trap_cx = self.acquire_inner_lock().get_trap_cx();
// **** release current PCB lock
*trap_cx = TrapContext::app_init_context( *trap_cx = TrapContext::app_init_context(
entry_point, entry_point,
user_sp, user_sp,
@ -137,6 +129,7 @@ impl TaskControlBlock {
self.kernel_stack.get_top(), self.kernel_stack.get_top(),
trap_handler as usize, trap_handler as usize,
); );
// **** release current PCB lock
} }
pub fn fork(self: &Arc<TaskControlBlock>) -> Arc<TaskControlBlock> { pub fn fork(self: &Arc<TaskControlBlock>) -> Arc<TaskControlBlock> {
// ---- hold parent PCB lock // ---- hold parent PCB lock
@ -149,7 +142,6 @@ impl TaskControlBlock {
.translate(VirtAddr::from(TRAP_CONTEXT).into()) .translate(VirtAddr::from(TRAP_CONTEXT).into())
.unwrap() .unwrap()
.ppn(); .ppn();
let task_status = TaskStatus::Ready;
// alloc a pid and a kernel stack in kernel space // alloc a pid and a kernel stack in kernel space
let pid_handle = pid_alloc(); let pid_handle = pid_alloc();
let kernel_stack = KernelStack::new(&pid_handle); let kernel_stack = KernelStack::new(&pid_handle);
@ -172,7 +164,7 @@ impl TaskControlBlock {
trap_cx_ppn, trap_cx_ppn,
base_size: parent_inner.base_size, base_size: parent_inner.base_size,
task_cx_ptr: task_cx_ptr as usize, task_cx_ptr: task_cx_ptr as usize,
task_status, task_status: TaskStatus::Ready,
memory_set, memory_set,
parent: Some(Arc::downgrade(self)), parent: Some(Arc::downgrade(self)),
children: Vec::new(), children: Vec::new(),