spin::Mutex->UPSafeCell
This commit is contained in:
parent
c82861f205
commit
3c9b6d7d14
26 changed files with 239 additions and 188 deletions
|
@ -1,17 +1,17 @@
|
|||
use crate::mm::{UserBuffer, translated_byte_buffer, translated_refmut};
|
||||
use crate::task::{current_user_token, current_task};
|
||||
use crate::fs::{make_pipe};
|
||||
use crate::fs::make_pipe;
|
||||
|
||||
pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize {
|
||||
let token = current_user_token();
|
||||
let task = current_task().unwrap();
|
||||
let inner = task.acquire_inner_lock();
|
||||
let inner = task.inner_exclusive_access();
|
||||
if fd >= inner.fd_table.len() {
|
||||
return -1;
|
||||
}
|
||||
if let Some(file) = &inner.fd_table[fd] {
|
||||
let file = file.clone();
|
||||
// release Task lock manually to avoid deadlock
|
||||
// release current task TCB manually to avoid multi-borrow
|
||||
drop(inner);
|
||||
file.write(
|
||||
UserBuffer::new(translated_byte_buffer(token, buf, len))
|
||||
|
@ -24,13 +24,13 @@ pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize {
|
|||
pub fn sys_read(fd: usize, buf: *const u8, len: usize) -> isize {
|
||||
let token = current_user_token();
|
||||
let task = current_task().unwrap();
|
||||
let inner = task.acquire_inner_lock();
|
||||
let inner = task.inner_exclusive_access();
|
||||
if fd >= inner.fd_table.len() {
|
||||
return -1;
|
||||
}
|
||||
if let Some(file) = &inner.fd_table[fd] {
|
||||
let file = file.clone();
|
||||
// release Task lock manually to avoid deadlock
|
||||
// release current task TCB manually to avoid multi-borrow
|
||||
drop(inner);
|
||||
file.read(
|
||||
UserBuffer::new(translated_byte_buffer(token, buf, len))
|
||||
|
@ -42,7 +42,7 @@ pub fn sys_read(fd: usize, buf: *const u8, len: usize) -> isize {
|
|||
|
||||
pub fn sys_close(fd: usize) -> isize {
|
||||
let task = current_task().unwrap();
|
||||
let mut inner = task.acquire_inner_lock();
|
||||
let mut inner = task.inner_exclusive_access();
|
||||
if fd >= inner.fd_table.len() {
|
||||
return -1;
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ pub fn sys_close(fd: usize) -> isize {
|
|||
pub fn sys_pipe(pipe: *mut usize) -> isize {
|
||||
let task = current_task().unwrap();
|
||||
let token = current_user_token();
|
||||
let mut inner = task.acquire_inner_lock();
|
||||
let mut inner = task.inner_exclusive_access();
|
||||
let (pipe_read, pipe_write) = make_pipe();
|
||||
let read_fd = inner.alloc_fd();
|
||||
inner.fd_table[read_fd] = Some(pipe_read);
|
||||
|
|
|
@ -36,7 +36,7 @@ pub fn sys_fork() -> isize {
|
|||
let new_task = current_task.fork();
|
||||
let new_pid = new_task.pid.0;
|
||||
// modify trap context of new_task, because it returns immediately after switching
|
||||
let trap_cx = new_task.acquire_inner_lock().get_trap_cx();
|
||||
let trap_cx = new_task.inner_exclusive_access().get_trap_cx();
|
||||
// we do not have to move to next instruction since we have done it before
|
||||
// for child process, fork returns 0
|
||||
trap_cx.x[10] = 0;
|
||||
|
@ -63,31 +63,31 @@ pub fn sys_waitpid(pid: isize, exit_code_ptr: *mut i32) -> isize {
|
|||
let task = current_task().unwrap();
|
||||
// find a child process
|
||||
|
||||
// ---- hold current PCB lock
|
||||
let mut inner = task.acquire_inner_lock();
|
||||
// ---- access current TCB exclusively
|
||||
let mut inner = task.inner_exclusive_access();
|
||||
if inner.children
|
||||
.iter()
|
||||
.find(|p| {pid == -1 || pid as usize == p.getpid()})
|
||||
.is_none() {
|
||||
return -1;
|
||||
// ---- release current PCB lock
|
||||
// ---- release current PCB
|
||||
}
|
||||
let pair = inner.children
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, p)| {
|
||||
// ++++ temporarily hold child PCB lock
|
||||
p.acquire_inner_lock().is_zombie() && (pid == -1 || pid as usize == p.getpid())
|
||||
// ++++ release child PCB lock
|
||||
// ++++ temporarily access child PCB lock exclusively
|
||||
p.inner_exclusive_access().is_zombie() && (pid == -1 || pid as usize == p.getpid())
|
||||
// ++++ release child PCB
|
||||
});
|
||||
if let Some((idx, _)) = pair {
|
||||
let child = inner.children.remove(idx);
|
||||
// confirm that child will be deallocated after removing from children list
|
||||
assert_eq!(Arc::strong_count(&child), 1);
|
||||
let found_pid = child.getpid();
|
||||
// ++++ temporarily hold child lock
|
||||
let exit_code = child.acquire_inner_lock().exit_code;
|
||||
// ++++ release child PCB lock
|
||||
// ++++ temporarily access child TCB exclusively
|
||||
let exit_code = child.inner_exclusive_access().exit_code;
|
||||
// ++++ release child PCB
|
||||
*translated_refmut(inner.memory_set.token(), exit_code_ptr) = exit_code;
|
||||
found_pid as isize
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue