wasi-common virtfs pipe: convert to be Send and Sync

This commit is contained in:
Pat Hickey
2020-07-01 14:51:30 -07:00
parent 4f16f0dc32
commit cf303d91fc

View File

@@ -12,9 +12,8 @@
use crate::handle::{Handle, HandleRights}; use crate::handle::{Handle, HandleRights};
use crate::wasi::{types, Errno, Result}; use crate::wasi::{types, Errno, Result};
use std::any::Any; use std::any::Any;
use std::cell::{Cell, Ref, RefCell};
use std::io::{self, Read, Write}; use std::io::{self, Read, Write};
use std::rc::Rc; use std::sync::{Arc, RwLock};
/// A virtual pipe read end. /// A virtual pipe read end.
/// ///
@@ -29,8 +28,8 @@ use std::rc::Rc;
/// ``` /// ```
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct ReadPipe<R: Read + Any> { pub struct ReadPipe<R: Read + Any> {
rights: Cell<HandleRights>, rights: Arc<RwLock<HandleRights>>,
reader: Rc<RefCell<R>>, reader: Arc<RwLock<R>>,
} }
impl<R: Read + Any> ReadPipe<R> { impl<R: Read + Any> ReadPipe<R> {
@@ -38,23 +37,23 @@ impl<R: Read + Any> ReadPipe<R> {
/// ///
/// All `Handle` read operations delegate to reading from this underlying reader. /// All `Handle` read operations delegate to reading from this underlying reader.
pub fn new(r: R) -> Self { pub fn new(r: R) -> Self {
Self::from_shared(Rc::new(RefCell::new(r))) Self::from_shared(Arc::new(RwLock::new(r)))
} }
/// Create a new pipe from a shareable `Read` type. /// Create a new pipe from a shareable `Read` type.
/// ///
/// All `Handle` read operations delegate to reading from this underlying reader. /// All `Handle` read operations delegate to reading from this underlying reader.
pub fn from_shared(reader: Rc<RefCell<R>>) -> Self { pub fn from_shared(reader: Arc<RwLock<R>>) -> Self {
use types::Rights; use types::Rights;
Self { Self {
rights: Cell::new(HandleRights::from_base( rights: Arc::new(RwLock::new(HandleRights::from_base(
Rights::FD_DATASYNC Rights::FD_DATASYNC
| Rights::FD_FDSTAT_SET_FLAGS | Rights::FD_FDSTAT_SET_FLAGS
| Rights::FD_READ | Rights::FD_READ
| Rights::FD_SYNC | Rights::FD_SYNC
| Rights::FD_FILESTAT_GET | Rights::FD_FILESTAT_GET
| Rights::POLL_FD_READWRITE, | Rights::POLL_FD_READWRITE,
)), ))),
reader, reader,
} }
} }
@@ -63,8 +62,8 @@ impl<R: Read + Any> ReadPipe<R> {
/// ///
/// This will fail with `Err(self)` if multiple references to the underlying `R` exist. /// This will fail with `Err(self)` if multiple references to the underlying `R` exist.
pub fn try_into_inner(mut self) -> std::result::Result<R, Self> { pub fn try_into_inner(mut self) -> std::result::Result<R, Self> {
match Rc::try_unwrap(self.reader) { match Arc::try_unwrap(self.reader) {
Ok(rc) => Ok(RefCell::into_inner(rc)), Ok(rc) => Ok(RwLock::into_inner(rc).unwrap()),
Err(reader) => { Err(reader) => {
self.reader = reader; self.reader = reader;
Err(self) Err(self)
@@ -114,11 +113,11 @@ impl<R: Read + Any> Handle for ReadPipe<R> {
} }
fn get_rights(&self) -> HandleRights { fn get_rights(&self) -> HandleRights {
self.rights.get() self.rights.read().unwrap().clone()
} }
fn set_rights(&self, rights: HandleRights) { fn set_rights(&self, rights: HandleRights) {
self.rights.set(rights) *self.rights.write().unwrap() = rights;
} }
fn advise( fn advise(
@@ -161,7 +160,7 @@ impl<R: Read + Any> Handle for ReadPipe<R> {
if offset != 0 { if offset != 0 {
return Err(Errno::Spipe); return Err(Errno::Spipe);
} }
Ok(self.reader.borrow_mut().read_vectored(buf)?) Ok(self.reader.write().unwrap().read_vectored(buf)?)
} }
fn seek(&self, _offset: io::SeekFrom) -> Result<types::Filesize> { fn seek(&self, _offset: io::SeekFrom) -> Result<types::Filesize> {
@@ -169,7 +168,7 @@ impl<R: Read + Any> Handle for ReadPipe<R> {
} }
fn read_vectored(&self, iovs: &mut [io::IoSliceMut]) -> Result<usize> { fn read_vectored(&self, iovs: &mut [io::IoSliceMut]) -> Result<usize> {
Ok(self.reader.borrow_mut().read_vectored(iovs)?) Ok(self.reader.write().unwrap().read_vectored(iovs)?)
} }
fn create_directory(&self, _path: &str) -> Result<()> { fn create_directory(&self, _path: &str) -> Result<()> {
@@ -225,8 +224,8 @@ impl<R: Read + Any> Handle for ReadPipe<R> {
/// A virtual pipe write end. /// A virtual pipe write end.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct WritePipe<W: Write + Any> { pub struct WritePipe<W: Write + Any> {
rights: Cell<HandleRights>, rights: Arc<RwLock<HandleRights>>,
writer: Rc<RefCell<W>>, writer: Arc<RwLock<W>>,
} }
impl<W: Write + Any> WritePipe<W> { impl<W: Write + Any> WritePipe<W> {
@@ -234,23 +233,23 @@ impl<W: Write + Any> WritePipe<W> {
/// ///
/// All `Handle` write operations delegate to writing to this underlying writer. /// All `Handle` write operations delegate to writing to this underlying writer.
pub fn new(w: W) -> Self { pub fn new(w: W) -> Self {
Self::from_shared(Rc::new(RefCell::new(w))) Self::from_shared(Arc::new(RwLock::new(w)))
} }
/// Create a new pipe from a shareable `Write` type. /// Create a new pipe from a shareable `Write` type.
/// ///
/// All `Handle` write operations delegate to writing to this underlying writer. /// All `Handle` write operations delegate to writing to this underlying writer.
pub fn from_shared(writer: Rc<RefCell<W>>) -> Self { pub fn from_shared(writer: Arc<RwLock<W>>) -> Self {
use types::Rights; use types::Rights;
Self { Self {
rights: Cell::new(HandleRights::from_base( rights: Arc::new(RwLock::new(HandleRights::from_base(
Rights::FD_DATASYNC Rights::FD_DATASYNC
| Rights::FD_FDSTAT_SET_FLAGS | Rights::FD_FDSTAT_SET_FLAGS
| Rights::FD_SYNC | Rights::FD_SYNC
| Rights::FD_WRITE | Rights::FD_WRITE
| Rights::FD_FILESTAT_GET | Rights::FD_FILESTAT_GET
| Rights::POLL_FD_READWRITE, | Rights::POLL_FD_READWRITE,
)), ))),
writer, writer,
} }
} }
@@ -259,8 +258,8 @@ impl<W: Write + Any> WritePipe<W> {
/// ///
/// This will fail with `Err(self)` if multiple references to the underlying `W` exist. /// This will fail with `Err(self)` if multiple references to the underlying `W` exist.
pub fn try_into_inner(mut self) -> std::result::Result<W, Self> { pub fn try_into_inner(mut self) -> std::result::Result<W, Self> {
match Rc::try_unwrap(self.writer) { match Arc::try_unwrap(self.writer) {
Ok(rc) => Ok(RefCell::into_inner(rc)), Ok(rc) => Ok(RwLock::into_inner(rc).unwrap()),
Err(writer) => { Err(writer) => {
self.writer = writer; self.writer = writer;
Err(self) Err(self)
@@ -274,11 +273,6 @@ impl WritePipe<io::Cursor<Vec<u8>>> {
pub fn new_in_memory() -> Self { pub fn new_in_memory() -> Self {
Self::new(io::Cursor::new(vec![])) Self::new(io::Cursor::new(vec![]))
} }
/// Get a reference to the bytes contained in the underlying `Vec<u8>` buffer.
pub fn as_slice(&self) -> Ref<[u8]> {
Ref::map(self.writer.borrow(), |c| c.get_ref().as_slice())
}
} }
impl<W: Write + Any> Handle for WritePipe<W> { impl<W: Write + Any> Handle for WritePipe<W> {
@@ -298,11 +292,11 @@ impl<W: Write + Any> Handle for WritePipe<W> {
} }
fn get_rights(&self) -> HandleRights { fn get_rights(&self) -> HandleRights {
self.rights.get() self.rights.read().unwrap().clone()
} }
fn set_rights(&self, rights: HandleRights) { fn set_rights(&self, rights: HandleRights) {
self.rights.set(rights) *self.rights.write().unwrap() = rights;
} }
fn advise( fn advise(
@@ -345,7 +339,7 @@ impl<W: Write + Any> Handle for WritePipe<W> {
if offset != 0 { if offset != 0 {
return Err(Errno::Spipe); return Err(Errno::Spipe);
} }
Ok(self.writer.borrow_mut().write_vectored(buf)?) Ok(self.writer.write().unwrap().write_vectored(buf)?)
} }
fn seek(&self, _offset: io::SeekFrom) -> Result<types::Filesize> { fn seek(&self, _offset: io::SeekFrom) -> Result<types::Filesize> {
@@ -353,7 +347,7 @@ impl<W: Write + Any> Handle for WritePipe<W> {
} }
fn write_vectored(&self, iovs: &[io::IoSlice]) -> Result<usize> { fn write_vectored(&self, iovs: &[io::IoSlice]) -> Result<usize> {
Ok(self.writer.borrow_mut().write_vectored(iovs)?) Ok(self.writer.write().unwrap().write_vectored(iovs)?)
} }
fn create_directory(&self, _path: &str) -> Result<()> { fn create_directory(&self, _path: &str) -> Result<()> {