mirror of
https://github.com/rcore-os/rCore-Tutorial-v3.git
synced 2024-11-24 10:26:25 +04:00
Fix pipe impl
This commit is contained in:
parent
bcfdcf33e3
commit
95d853d8d2
@ -114,36 +114,40 @@ impl File for Pipe {
|
|||||||
}
|
}
|
||||||
fn read(&self, buf: UserBuffer) -> usize {
|
fn read(&self, buf: UserBuffer) -> usize {
|
||||||
assert!(self.readable());
|
assert!(self.readable());
|
||||||
|
let want_to_read = buf.len();
|
||||||
let mut buf_iter = buf.into_iter();
|
let mut buf_iter = buf.into_iter();
|
||||||
let mut read_size = 0usize;
|
let mut already_read = 0usize;
|
||||||
loop {
|
loop {
|
||||||
let mut ring_buffer = self.buffer.exclusive_access();
|
let mut ring_buffer = self.buffer.exclusive_access();
|
||||||
let loop_read = ring_buffer.available_read();
|
let loop_read = ring_buffer.available_read();
|
||||||
if loop_read == 0 {
|
if loop_read == 0 {
|
||||||
if ring_buffer.all_write_ends_closed() {
|
if ring_buffer.all_write_ends_closed() {
|
||||||
return read_size;
|
return already_read;
|
||||||
}
|
}
|
||||||
drop(ring_buffer);
|
drop(ring_buffer);
|
||||||
suspend_current_and_run_next();
|
suspend_current_and_run_next();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// read at most loop_read bytes
|
|
||||||
for _ in 0..loop_read {
|
for _ in 0..loop_read {
|
||||||
if let Some(byte_ref) = buf_iter.next() {
|
if let Some(byte_ref) = buf_iter.next() {
|
||||||
unsafe {
|
unsafe {
|
||||||
*byte_ref = ring_buffer.read_byte();
|
*byte_ref = ring_buffer.read_byte();
|
||||||
}
|
}
|
||||||
read_size += 1;
|
already_read += 1;
|
||||||
|
if already_read == want_to_read {
|
||||||
|
return want_to_read;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return read_size;
|
return already_read;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn write(&self, buf: UserBuffer) -> usize {
|
fn write(&self, buf: UserBuffer) -> usize {
|
||||||
assert!(self.writable());
|
assert!(self.writable());
|
||||||
|
let want_to_write = buf.len();
|
||||||
let mut buf_iter = buf.into_iter();
|
let mut buf_iter = buf.into_iter();
|
||||||
let mut write_size = 0usize;
|
let mut already_write = 0usize;
|
||||||
loop {
|
loop {
|
||||||
let mut ring_buffer = self.buffer.exclusive_access();
|
let mut ring_buffer = self.buffer.exclusive_access();
|
||||||
let loop_write = ring_buffer.available_write();
|
let loop_write = ring_buffer.available_write();
|
||||||
@ -156,9 +160,12 @@ impl File for Pipe {
|
|||||||
for _ in 0..loop_write {
|
for _ in 0..loop_write {
|
||||||
if let Some(byte_ref) = buf_iter.next() {
|
if let Some(byte_ref) = buf_iter.next() {
|
||||||
ring_buffer.write_byte(unsafe { *byte_ref });
|
ring_buffer.write_byte(unsafe { *byte_ref });
|
||||||
write_size += 1;
|
already_write += 1;
|
||||||
|
if already_write == want_to_write {
|
||||||
|
return want_to_write;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return write_size;
|
return already_write;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user