From 6e5b3e2515a2238e4fbb98bda4442a584db78dd6 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Fri, 10 May 2019 12:28:37 +0800 Subject: [PATCH] fix broken pipe status. rustc works now! --- kernel/src/fs/pipe.rs | 54 ++++++++++++++------------------------ kernel/src/syscall/fs.rs | 4 +++ kernel/src/syscall/misc.rs | 9 +++---- 3 files changed, 27 insertions(+), 40 deletions(-) diff --git a/kernel/src/fs/pipe.rs b/kernel/src/fs/pipe.rs index 6059e1a..59fa2fa 100644 --- a/kernel/src/fs/pipe.rs +++ b/kernel/src/fs/pipe.rs @@ -45,13 +45,25 @@ impl Pipe { ) } - pub fn can_read(&self) -> bool { + fn can_read(&self) -> bool { if let PipeEnd::Read = self.direction { - self.data.lock().buf.len() > 0 + self.data.lock().buf.len() > 0 || self.is_broken() } else { false } } + + fn can_write(&self) -> bool { + if let PipeEnd::Write = self.direction { + !self.is_broken() + } else { + false + } + } + + fn is_broken(&self) -> bool { + Arc::strong_count(&self.data) < 2 + } } // TODO: better way to provide default impl? @@ -105,39 +117,11 @@ impl INode for Pipe { } fn poll(&self) -> Result { - let data = self.data.lock(); - match self.direction { - PipeEnd::Read => { - if data.buf.len() > 0 { - Ok(PollStatus { - read: true, - write: false, - error: false, - }) - } else { - Ok(PollStatus { - read: false, - write: false, - error: false, - }) - } - } - PipeEnd::Write => { - if data.buf.len() > 0 { - Ok(PollStatus { - read: false, - write: true, - error: false, - }) - } else { - Ok(PollStatus { - read: false, - write: false, - error: false, - }) - } - } - } + Ok(PollStatus { + read: self.can_read(), + write: self.can_write(), + error: false, + }) } impl_inode!(); } diff --git a/kernel/src/syscall/fs.rs b/kernel/src/syscall/fs.rs index 5cbe1e2..35f0a4f 100644 --- a/kernel/src/syscall/fs.rs +++ b/kernel/src/syscall/fs.rs @@ -141,6 +141,9 @@ impl Syscall<'_> { return Ok(0); } + // NOTE: To run rustc, uncomment yield_now and comment Condvar. + // Waking up from pipe is unimplemented now. + // thread::yield_now(); Condvar::wait_any(&[&STDIN.pushed, &(*SOCKET_ACTIVITY)]); } } @@ -1416,6 +1419,7 @@ impl IoVecs { } #[repr(C)] +#[derive(Debug)] pub struct PollFd { fd: u32, events: PollEvents, diff --git a/kernel/src/syscall/misc.rs b/kernel/src/syscall/misc.rs index 119cf8b..df0dec0 100644 --- a/kernel/src/syscall/misc.rs +++ b/kernel/src/syscall/misc.rs @@ -69,10 +69,9 @@ impl Syscall<'_> { val, timeout ); - // if op & OP_PRIVATE == 0 { - // unimplemented!("futex only support process-private"); - // return Err(SysError::ENOSYS); - // } + if op & OP_PRIVATE == 0 { + warn!("process-shared futex is unimplemented"); + } if uaddr % size_of::() != 0 { return Err(SysError::EINVAL); } @@ -80,7 +79,7 @@ impl Syscall<'_> { const OP_WAIT: u32 = 0; const OP_WAKE: u32 = 1; - const OP_PRIVATE: u32 = 128; + const OP_PRIVATE: u32 = 0x80; let mut proc = self.process(); let queue = proc.get_futex(uaddr);