From feae733bb90406fddc9d98d59f07881471954ed8 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Thu, 2 May 2019 15:52:56 +0800 Subject: [PATCH] Implement poll for pipes --- kernel/src/fs/pipe.rs | 37 ++++++++++++++++++++++++++++++++++++- kernel/src/syscall/fs.rs | 4 ++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/kernel/src/fs/pipe.rs b/kernel/src/fs/pipe.rs index 8807380..6059e1a 100644 --- a/kernel/src/fs/pipe.rs +++ b/kernel/src/fs/pipe.rs @@ -57,7 +57,6 @@ impl Pipe { // TODO: better way to provide default impl? macro_rules! impl_inode { () => { - fn poll(&self) -> Result { Err(FsError::NotSupported) } fn metadata(&self) -> Result { Err(FsError::NotSupported) } fn set_metadata(&self, _metadata: &Metadata) -> Result<()> { Ok(()) } fn sync_all(&self) -> Result<()> { Ok(()) } @@ -104,5 +103,41 @@ impl INode for Pipe { Ok(0) } } + + 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, + }) + } + } + } + } impl_inode!(); } diff --git a/kernel/src/syscall/fs.rs b/kernel/src/syscall/fs.rs index 56d14ae..49aebe5 100644 --- a/kernel/src/syscall/fs.rs +++ b/kernel/src/syscall/fs.rs @@ -652,7 +652,7 @@ pub fn sys_pipe(fds: *mut u32) -> SysResult { write: false, append: false, }, - String::from(":pipe_r:"), + String::from("pipe_r:[]"), ))); let write_fd = proc.add_file(FileLike::File(FileHandle::new( @@ -662,7 +662,7 @@ pub fn sys_pipe(fds: *mut u32) -> SysResult { write: true, append: false, }, - String::from(":pipe_w:"), + String::from("pipe_w:[]"), ))); fds[0] = read_fd as u32;