Skip to content

Commit 599dbb6

Browse files
committed
ch8
1 parent 5b460bf commit 599dbb6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2804
-992
lines changed

easy-fs-fuse/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ fn easy_fs_pack() -> std::io::Result<()> {
7373
})
7474
.collect::<std::io::Result<Vec<_>>>()?;
7575
println!("easy-fs-use >>>>");
76+
let mut size_total = 0;
7677
for app in apps {
7778
// load built app only from host file system
7879
let path = opt.target.join(&app);
@@ -88,12 +89,13 @@ fn easy_fs_pack() -> std::io::Result<()> {
8889
all_data.len(),
8990
all_data.len() / 1024
9091
);
92+
size_total += all_data.len();
9193
// create a file in easy-fs
9294
let inode = root_inode.create(&app).unwrap();
9395
// write data to easy-fs
9496
inode.write_at(0, &all_data);
9597
}
96-
println!("easy-fs-use <<<<");
98+
println!("easy-fs-use (total: {}KB) <<<<", size_total / 1024);
9799
Ok(())
98100
}
99101

os/src/fs/inode.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,11 @@ impl OSInode {
2929

3030
pub fn read_all(&self) -> Vec<u8> {
3131
let mut inner = self.inner.exclusive_access();
32-
let mut buffer = [0u8; 512];
33-
let mut v = Vec::new();
34-
loop {
35-
let len = inner.inode.read_at(inner.offset, &mut buffer);
36-
if len == 0 {
37-
break;
38-
}
39-
inner.offset += len;
40-
v.extend_from_slice(&buffer[..len]);
41-
}
32+
let size = inner.inode.get_size();
33+
let mut v = alloc::vec![0u8; size];
34+
let len = inner.inode.read_at(inner.offset, v.as_mut_slice());
35+
assert_eq!(size, len);
36+
inner.offset += len;
4237
v
4338
}
4439

@@ -76,15 +71,6 @@ lazy_static! {
7671
};
7772
}
7873

79-
/// List all files in the filesystems
80-
pub fn ls_root() {
81-
println!("/**** ls / ****");
82-
for app in ROOT_INODE.ls() {
83-
println!("{}", app);
84-
}
85-
println!("**************/");
86-
}
87-
8874
bitflags! {
8975
pub struct OpenFlags: u32 {
9076
const RDONLY = 0;

os/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ pub fn rust_main() -> ! {
9999
trap::enable_timer_interrupt();
100100
timer::set_next_trigger();
101101

102-
fs::ls_root();
103102
task::run_tasks();
104103
panic!("Unreachable in rust_main!");
105104
}

os/src/mm/memory_set.rs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use lazy_static::lazy_static;
66
use riscv::register::satp;
77

88
use crate::{
9-
config::{MEMORY_END, MMIO, PAGE_SIZE, TRAMPOLINE, TRAP_CONTEXT, USER_STACK_SIZE},
9+
config::{MEMORY_END, MMIO, PAGE_SIZE, TRAMPOLINE},
1010
mm::address::StepByOne,
1111
sync::UPSafeCell,
1212
};
@@ -176,14 +176,6 @@ impl MemorySet {
176176
memory_set.map_trampoline();
177177

178178
// map kernel sections
179-
// println!(".text [{:#x}, {:#x})", stext as usize, etext as usize);
180-
// println!(".rodata [{:#x}, {:#x})", srodata as usize, erodata as usize);
181-
// println!(".data [{:#x}, {:#x})", sdata as usize, edata as usize);
182-
// println!(
183-
// ".bss [{:#x}, {:#x})",
184-
// sbss_with_stack as usize, ebss as usize
185-
// );
186-
187179
// println!("mapping kernel .text section");
188180
memory_set.push(
189181
MapArea::new(
@@ -310,30 +302,10 @@ impl MemorySet {
310302
let mut user_stack_bottom: usize = max_end_va.into();
311303
// guard page
312304
user_stack_bottom += PAGE_SIZE;
313-
let user_stack_top = user_stack_bottom + USER_STACK_SIZE;
314-
memory_set.push(
315-
MapArea::new(
316-
user_stack_bottom.into(),
317-
user_stack_top.into(),
318-
MapType::Framed,
319-
MapPermission::R | MapPermission::W | MapPermission::U,
320-
),
321-
None,
322-
);
323-
// map TrapContext
324-
memory_set.push(
325-
MapArea::new(
326-
TRAP_CONTEXT.into(),
327-
TRAMPOLINE.into(),
328-
MapType::Framed,
329-
MapPermission::R | MapPermission::W,
330-
),
331-
None,
332-
);
333305

334306
(
335307
memory_set,
336-
user_stack_top,
308+
user_stack_bottom,
337309
elf.header.pt2.entry_point() as usize,
338310
)
339311
}
@@ -474,5 +446,4 @@ pub fn remap_test() {
474446
.executable(),
475447
false
476448
);
477-
println!("remap_test passed!");
478449
}

os/src/sync/condvar.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use alloc::{collections::vec_deque::VecDeque, sync::Arc};
2+
3+
use crate::task::{block_current_and_run_next, current_task, wakeup_task, TaskControlBlock};
4+
5+
use super::{Mutex, UPSafeCell};
6+
7+
pub struct CondVar {
8+
pub inner: UPSafeCell<CondVarInner>,
9+
}
10+
11+
pub struct CondVarInner {
12+
pub wait_queue: VecDeque<Arc<TaskControlBlock>>,
13+
}
14+
15+
impl CondVar {
16+
pub fn new() -> Self {
17+
Self {
18+
inner: unsafe {
19+
UPSafeCell::new(CondVarInner {
20+
wait_queue: VecDeque::new(),
21+
})
22+
},
23+
}
24+
}
25+
26+
pub fn signal(&self) {
27+
let mut inner = self.inner.exclusive_access();
28+
if let Some(task) = inner.wait_queue.pop_front() {
29+
wakeup_task(task);
30+
}
31+
}
32+
33+
pub fn wait(&self, mutex: Arc<dyn Mutex>) {
34+
mutex.unlock();
35+
let mut inner = self.inner.exclusive_access();
36+
let task = current_task().unwrap();
37+
inner.wait_queue.push_back(task);
38+
drop(inner);
39+
block_current_and_run_next();
40+
mutex.lock();
41+
}
42+
}

os/src/sync/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
mod up;
22
pub use up::UPSafeCell;
3+
4+
mod mutex;
5+
pub use mutex::{Mutex, MutexBlocking, MutexSpin};
6+
7+
mod semaphore;
8+
pub use semaphore::Semaphore;
9+
10+
mod condvar;
11+
pub use condvar::CondVar;

os/src/sync/mutex.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use alloc::{collections::vec_deque::VecDeque, sync::Arc};
2+
3+
use crate::task::{
4+
block_current_and_run_next, current_task, suspend_current_and_run_next, wakeup_task,
5+
TaskControlBlock,
6+
};
7+
8+
use super::UPSafeCell;
9+
10+
pub trait Mutex: Sync + Send {
11+
fn lock(&self);
12+
fn unlock(&self);
13+
}
14+
15+
/// based on yield
16+
pub struct MutexSpin {
17+
locked: UPSafeCell<bool>,
18+
}
19+
20+
impl MutexSpin {
21+
pub fn new() -> Self {
22+
Self {
23+
locked: unsafe { UPSafeCell::new(false) },
24+
}
25+
}
26+
}
27+
28+
impl Mutex for MutexSpin {
29+
fn lock(&self) {
30+
loop {
31+
let mut locked = self.locked.exclusive_access();
32+
if *locked {
33+
drop(locked);
34+
suspend_current_and_run_next();
35+
continue;
36+
} else {
37+
*locked = true;
38+
return;
39+
}
40+
}
41+
}
42+
43+
fn unlock(&self) {
44+
let mut locked = self.locked.exclusive_access();
45+
*locked = false;
46+
}
47+
}
48+
49+
/// based on thread blocking
50+
pub struct MutexBlocking {
51+
inner: UPSafeCell<MutexBlockingInner>,
52+
}
53+
54+
impl MutexBlocking {
55+
pub fn new() -> Self {
56+
Self {
57+
inner: unsafe {
58+
UPSafeCell::new(MutexBlockingInner {
59+
locked: false,
60+
wait_queue: VecDeque::new(),
61+
})
62+
},
63+
}
64+
}
65+
}
66+
67+
struct MutexBlockingInner {
68+
locked: bool,
69+
wait_queue: VecDeque<Arc<TaskControlBlock>>,
70+
}
71+
72+
impl Mutex for MutexBlocking {
73+
fn lock(&self) {
74+
let mut inner = self.inner.exclusive_access();
75+
if inner.locked {
76+
inner.wait_queue.push_back(current_task().unwrap());
77+
drop(inner);
78+
block_current_and_run_next();
79+
} else {
80+
inner.locked = true;
81+
}
82+
}
83+
84+
fn unlock(&self) {
85+
let mut inner = self.inner.exclusive_access();
86+
assert!(inner.locked);
87+
if let Some(task) = inner.wait_queue.pop_front() {
88+
wakeup_task(task);
89+
} else {
90+
inner.locked = false;
91+
}
92+
}
93+
}

os/src/sync/semaphore.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use alloc::{collections::vec_deque::VecDeque, sync::Arc};
2+
3+
use crate::task::{block_current_and_run_next, current_task, wakeup_task, TaskControlBlock};
4+
5+
use super::UPSafeCell;
6+
7+
pub struct Semaphore {
8+
pub inner: UPSafeCell<SemaphoreInner>,
9+
}
10+
11+
pub struct SemaphoreInner {
12+
pub count: isize,
13+
pub wait_queue: VecDeque<Arc<TaskControlBlock>>,
14+
}
15+
16+
impl Semaphore {
17+
pub fn new(res_count: usize) -> Self {
18+
Self {
19+
inner: unsafe {
20+
UPSafeCell::new(SemaphoreInner {
21+
count: res_count as isize,
22+
wait_queue: VecDeque::new(),
23+
})
24+
},
25+
}
26+
}
27+
28+
pub fn up(&self) {
29+
let mut inner = self.inner.exclusive_access();
30+
inner.count += 1;
31+
if let Some(task) = inner.wait_queue.pop_front() {
32+
wakeup_task(task);
33+
}
34+
}
35+
36+
pub fn down(&self) {
37+
let mut inner = self.inner.exclusive_access();
38+
inner.count -= 1;
39+
if inner.count < 0 {
40+
let task = current_task().unwrap();
41+
inner.wait_queue.push_back(task);
42+
drop(inner);
43+
block_current_and_run_next();
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)