Make fd_write unbuffered; fixes CraneStation/wasmtime#255

This commit is contained in:
Jakub Konka
2019-08-06 22:49:48 +02:00
committed by Dan Gohman
parent 6797db66a2
commit 8ea7a983d8

View File

@@ -376,16 +376,30 @@ pub(crate) fn fd_write(
.map(|vec| unsafe { host::iovec_to_host(vec) }) .map(|vec| unsafe { host::iovec_to_host(vec) })
.collect(); .collect();
let maybe_host_nwritten = match &mut *fe.fd_object.descriptor { // perform unbuffered writes
Descriptor::File(f) => f.write_vectored(&iovs), let host_nwritten = match &mut *fe.fd_object.descriptor {
Descriptor::File(f) => f
.write_vectored(&iovs)
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?,
Descriptor::Stdin => return Err(host::__WASI_EBADF), Descriptor::Stdin => return Err(host::__WASI_EBADF),
Descriptor::Stdout => io::stdout().lock().write_vectored(&iovs), Descriptor::Stdout => {
Descriptor::Stderr => io::stderr().lock().write_vectored(&iovs), // lock for the duration of the scope
let stdout = io::stdout();
let mut stdout = stdout.lock();
let nwritten = stdout
.write_vectored(&iovs)
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?;
stdout
.flush()
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?;
nwritten
}
Descriptor::Stderr => io::stderr()
.lock()
.write_vectored(&iovs)
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?,
}; };
let host_nwritten = maybe_host_nwritten
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?;
trace!(" | *nwritten={:?}", host_nwritten); trace!(" | *nwritten={:?}", host_nwritten);
enc_usize_byref(memory, nwritten, host_nwritten) enc_usize_byref(memory, nwritten, host_nwritten)