Skip to content

Commit a5c4f32

Browse files
committed
Merge branch 'dev' into main
2 parents ea4222b + 1b6f2c4 commit a5c4f32

File tree

3 files changed

+24
-30
lines changed

3 files changed

+24
-30
lines changed

os/src/task/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ pub fn suspend_current_and_run_next() {
2828
// There must be an application running.
2929
let task = take_current_task().unwrap();
3030

31-
// ---- temporarily hold current PCB lock
32-
let task_cx_ptr2 = task.acquire_inner_lock().get_task_cx_ptr2();
33-
// ---- release current PCB lock
34-
35-
// ++++ temporarily hold current PCB lock
31+
// ---- hold current PCB lock
32+
let mut task_inner = task.acquire_inner_lock();
33+
let task_cx_ptr2 = task_inner.get_task_cx_ptr2();
3634
// Change status to Ready
37-
task.acquire_inner_lock().task_status = TaskStatus::Ready;
38-
// ++++ release current PCB lock
35+
task_inner.task_status = TaskStatus::Ready;
36+
drop(task_inner);
37+
// ---- release current PCB lock
3938

4039
// push back to ready queue.
4140
add_task(task);
@@ -58,6 +57,7 @@ pub fn exit_current_and_run_next(exit_code: i32) {
5857
{
5958
let mut initproc_inner = INITPROC.acquire_inner_lock();
6059
for child in inner.children.iter() {
60+
child.acquire_inner_lock().parent = Some(Arc::downgrade(&INITPROC));
6161
initproc_inner.children.push(child.clone());
6262
}
6363
}

os/src/task/processor.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use super::TaskControlBlock;
22
use alloc::sync::Arc;
3-
use spin::Mutex;
3+
use core::cell::RefCell;
44
use lazy_static::*;
55
use super::{fetch_task, TaskStatus};
66
use super::__switch;
77
use crate::trap::TrapContext;
88

99
pub struct Processor {
10-
inner: Mutex<ProcessorInner>,
10+
inner: RefCell<ProcessorInner>,
1111
}
1212

1313
unsafe impl Sync for Processor {}
@@ -20,25 +20,27 @@ struct ProcessorInner {
2020
impl Processor {
2121
pub fn new() -> Self {
2222
Self {
23-
inner: Mutex::new(ProcessorInner {
23+
inner: RefCell::new(ProcessorInner {
2424
current: None,
2525
idle_task_cx_ptr: 0,
2626
}),
2727
}
2828
}
2929
fn get_idle_task_cx_ptr2(&self) -> *const usize {
30-
let inner = self.inner.lock();
30+
let inner = self.inner.borrow();
3131
&inner.idle_task_cx_ptr as *const usize
3232
}
3333
pub fn run(&self) {
3434
loop {
3535
if let Some(task) = fetch_task() {
3636
let idle_task_cx_ptr2 = self.get_idle_task_cx_ptr2();
3737
// acquire
38-
let next_task_cx_ptr2 = task.acquire_inner_lock().get_task_cx_ptr2();
39-
task.acquire_inner_lock().task_status = TaskStatus::Running;
38+
let mut task_inner = task.acquire_inner_lock();
39+
let next_task_cx_ptr2 = task_inner.get_task_cx_ptr2();
40+
task_inner.task_status = TaskStatus::Running;
41+
drop(task_inner);
4042
// release
41-
self.inner.lock().current = Some(task);
43+
self.inner.borrow_mut().current = Some(task);
4244
unsafe {
4345
__switch(
4446
idle_task_cx_ptr2,
@@ -49,10 +51,10 @@ impl Processor {
4951
}
5052
}
5153
pub fn take_current(&self) -> Option<Arc<TaskControlBlock>> {
52-
self.inner.lock().current.take()
54+
self.inner.borrow_mut().current.take()
5355
}
5456
pub fn current(&self) -> Option<Arc<TaskControlBlock>> {
55-
self.inner.lock().current.as_ref().map(|task| task.clone())
57+
self.inner.borrow().current.as_ref().map(|task| Arc::clone(task))
5658
}
5759
}
5860

os/src/task/task.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ impl TaskControlBlock {
6767
.translate(VirtAddr::from(TRAP_CONTEXT).into())
6868
.unwrap()
6969
.ppn();
70-
let task_status = TaskStatus::Ready;
7170
// alloc a pid and a kernel stack in kernel space
7271
let pid_handle = pid_alloc();
7372
let kernel_stack = KernelStack::new(&pid_handle);
@@ -81,7 +80,7 @@ impl TaskControlBlock {
8180
trap_cx_ppn,
8281
base_size: user_sp,
8382
task_cx_ptr: task_cx_ptr as usize,
84-
task_status,
83+
task_status: TaskStatus::Ready,
8584
memory_set,
8685
parent: None,
8786
children: Vec::new(),
@@ -97,9 +96,7 @@ impl TaskControlBlock {
9796
}),
9897
};
9998
// prepare TrapContext in user space
100-
// ---- acquire child PCB lock
10199
let trap_cx = task_control_block.acquire_inner_lock().get_trap_cx();
102-
// ---- release child PCB lock
103100
*trap_cx = TrapContext::app_init_context(
104101
entry_point,
105102
user_sp,
@@ -118,29 +115,25 @@ impl TaskControlBlock {
118115
.ppn();
119116

120117
// **** hold current PCB lock
121-
let mut inner = self.inner.lock();
118+
let mut inner = self.acquire_inner_lock();
122119
// substitute memory_set
123120
inner.memory_set = memory_set;
124121
// update trap_cx ppn
125122
inner.trap_cx_ppn = trap_cx_ppn;
126-
drop(inner);
127-
// **** release current PCB lock manually
128-
129123
// initialize trap_cx
130-
// **** acquire current PCB lock
131-
let trap_cx = self.acquire_inner_lock().get_trap_cx();
132-
// **** release current PCB lock
124+
let trap_cx = inner.get_trap_cx();
133125
*trap_cx = TrapContext::app_init_context(
134126
entry_point,
135127
user_sp,
136128
KERNEL_SPACE.lock().token(),
137129
self.kernel_stack.get_top(),
138130
trap_handler as usize,
139131
);
132+
// **** release current PCB lock
140133
}
141134
pub fn fork(self: &Arc<TaskControlBlock>) -> Arc<TaskControlBlock> {
142135
// ---- hold parent PCB lock
143-
let mut parent_inner = self.inner.lock();
136+
let mut parent_inner = self.acquire_inner_lock();
144137
// copy user space(include trap context)
145138
let memory_set = MemorySet::from_existed_user(
146139
&parent_inner.memory_set
@@ -149,7 +142,6 @@ impl TaskControlBlock {
149142
.translate(VirtAddr::from(TRAP_CONTEXT).into())
150143
.unwrap()
151144
.ppn();
152-
let task_status = TaskStatus::Ready;
153145
// alloc a pid and a kernel stack in kernel space
154146
let pid_handle = pid_alloc();
155147
let kernel_stack = KernelStack::new(&pid_handle);
@@ -172,7 +164,7 @@ impl TaskControlBlock {
172164
trap_cx_ppn,
173165
base_size: parent_inner.base_size,
174166
task_cx_ptr: task_cx_ptr as usize,
175-
task_status,
167+
task_status: TaskStatus::Ready,
176168
memory_set,
177169
parent: Some(Arc::downgrade(self)),
178170
children: Vec::new(),

0 commit comments

Comments
 (0)