Auto merge of #77154 - fusion-engineering-forks:lazy-stdio, r=dtolnay
Remove std::io::lazy::Lazy in favour of SyncOnceCell The (internal) std::io::lazy::Lazy was used to lazily initialize the stdout and stdin buffers (and mutexes). It uses atexit() to register a destructor to flush the streams on exit, and mark the streams as 'closed'. Using the stream afterwards would result in a panic. Stdout uses a LineWriter which contains a BufWriter that will flush the buffer on drop. This one is important to be executed during shutdown, to make sure no buffered output is lost. It also forbids access to stdout afterwards, since the buffer is already flushed and gone. Stdin uses a BufReader, which does not implement Drop. It simply forgets any previously read data that was not read from the buffer yet. This means that in the case of stdin, the atexit() function's only effect is making stdin inaccessible to the program, such that later accesses result in a panic. This is uncessary, as it'd have been safe to access stdin during shutdown of the program. --- This change removes the entire io::lazy module in favour of SyncOnceCell. SyncOnceCell's fast path is much faster (a single atomic operation) than locking a sys_common::Mutex on every access like Lazy did. However, SyncOnceCell does not use atexit() to drop the contained object during shutdown. As noted above, this is not a problem for stdin. It simply means stdin is now usable during shutdown. The atexit() call for stdout is moved to the stdio module. Unlike the now-removed Lazy struct, SyncOnceCell does not have a 'gone and unusable' state that panics. Instead of adding this again, this simply replaces the buffer with one with zero capacity. This effectively flushes the old buffer *and* makes any writes afterwards pass through directly without touching a buffer, making print!() available during shutdown without panicking. --- In addition, because the contents of the SyncOnceCell are no longer dropped, we can now use `&'static` instead of `Arc` in `Stdout` and `Stdin`. This also saves two levels of indirection in `stdin()` and `stdout()`, since Lazy effectively stored a `Box<Arc<T>>`, and SyncOnceCell stores the `T` directly.
This commit is contained in:
commit
c9e5e6a53a
|
@ -1,63 +0,0 @@
|
||||||
use crate::cell::Cell;
|
|
||||||
use crate::ptr;
|
|
||||||
use crate::sync::Arc;
|
|
||||||
use crate::sys_common;
|
|
||||||
use crate::sys_common::mutex::Mutex;
|
|
||||||
|
|
||||||
pub struct Lazy<T> {
|
|
||||||
// We never call `lock.init()`, so it is UB to attempt to acquire this mutex reentrantly!
|
|
||||||
lock: Mutex,
|
|
||||||
ptr: Cell<*mut Arc<T>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
const fn done<T>() -> *mut Arc<T> {
|
|
||||||
1_usize as *mut _
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe impl<T> Sync for Lazy<T> {}
|
|
||||||
|
|
||||||
impl<T> Lazy<T> {
|
|
||||||
pub const fn new() -> Lazy<T> {
|
|
||||||
Lazy { lock: Mutex::new(), ptr: Cell::new(ptr::null_mut()) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: Send + Sync + 'static> Lazy<T> {
|
|
||||||
/// Safety: `init` must not call `get` on the variable that is being
|
|
||||||
/// initialized.
|
|
||||||
pub unsafe fn get(&'static self, init: fn() -> Arc<T>) -> Option<Arc<T>> {
|
|
||||||
let _guard = self.lock.lock();
|
|
||||||
let ptr = self.ptr.get();
|
|
||||||
if ptr.is_null() {
|
|
||||||
Some(self.init(init))
|
|
||||||
} else if ptr == done() {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some((*ptr).clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Must only be called with `lock` held
|
|
||||||
unsafe fn init(&'static self, init: fn() -> Arc<T>) -> Arc<T> {
|
|
||||||
// If we successfully register an at exit handler, then we cache the
|
|
||||||
// `Arc` allocation in our own internal box (it will get deallocated by
|
|
||||||
// the at exit handler). Otherwise we just return the freshly allocated
|
|
||||||
// `Arc`.
|
|
||||||
let registered = sys_common::at_exit(move || {
|
|
||||||
let ptr = {
|
|
||||||
let _guard = self.lock.lock();
|
|
||||||
self.ptr.replace(done())
|
|
||||||
};
|
|
||||||
drop(Box::from_raw(ptr))
|
|
||||||
});
|
|
||||||
// This could reentrantly call `init` again, which is a problem
|
|
||||||
// because our `lock` allows reentrancy!
|
|
||||||
// That's why `get` is unsafe and requires the caller to ensure no reentrancy happens.
|
|
||||||
let ret = init();
|
|
||||||
if registered.is_ok() {
|
|
||||||
self.ptr.set(Box::into_raw(Box::new(ret.clone())));
|
|
||||||
}
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -285,7 +285,6 @@ mod buffered;
|
||||||
mod cursor;
|
mod cursor;
|
||||||
mod error;
|
mod error;
|
||||||
mod impls;
|
mod impls;
|
||||||
mod lazy;
|
|
||||||
pub mod prelude;
|
pub mod prelude;
|
||||||
mod stdio;
|
mod stdio;
|
||||||
mod util;
|
mod util;
|
||||||
|
|
|
@ -7,10 +7,11 @@ use crate::io::prelude::*;
|
||||||
|
|
||||||
use crate::cell::RefCell;
|
use crate::cell::RefCell;
|
||||||
use crate::fmt;
|
use crate::fmt;
|
||||||
use crate::io::lazy::Lazy;
|
|
||||||
use crate::io::{self, BufReader, Initializer, IoSlice, IoSliceMut, LineWriter};
|
use crate::io::{self, BufReader, Initializer, IoSlice, IoSliceMut, LineWriter};
|
||||||
use crate::sync::{Arc, Mutex, MutexGuard, Once};
|
use crate::lazy::SyncOnceCell;
|
||||||
|
use crate::sync::{Mutex, MutexGuard};
|
||||||
use crate::sys::stdio;
|
use crate::sys::stdio;
|
||||||
|
use crate::sys_common;
|
||||||
use crate::sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
|
use crate::sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
|
||||||
use crate::thread::LocalKey;
|
use crate::thread::LocalKey;
|
||||||
|
|
||||||
|
@ -217,7 +218,7 @@ fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub struct Stdin {
|
pub struct Stdin {
|
||||||
inner: Arc<Mutex<BufReader<StdinRaw>>>,
|
inner: &'static Mutex<BufReader<StdinRaw>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A locked reference to the `Stdin` handle.
|
/// A locked reference to the `Stdin` handle.
|
||||||
|
@ -292,15 +293,11 @@ pub struct StdinLock<'a> {
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub fn stdin() -> Stdin {
|
pub fn stdin() -> Stdin {
|
||||||
static INSTANCE: Lazy<Mutex<BufReader<StdinRaw>>> = Lazy::new();
|
static INSTANCE: SyncOnceCell<Mutex<BufReader<StdinRaw>>> = SyncOnceCell::new();
|
||||||
return Stdin {
|
Stdin {
|
||||||
inner: unsafe { INSTANCE.get(stdin_init).expect("cannot access stdin during shutdown") },
|
inner: INSTANCE.get_or_init(|| {
|
||||||
};
|
Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin_raw()))
|
||||||
|
}),
|
||||||
fn stdin_init() -> Arc<Mutex<BufReader<StdinRaw>>> {
|
|
||||||
// This must not reentrantly access `INSTANCE`
|
|
||||||
let stdin = stdin_raw();
|
|
||||||
Arc::new(Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -476,7 +473,7 @@ pub struct Stdout {
|
||||||
// FIXME: this should be LineWriter or BufWriter depending on the state of
|
// FIXME: this should be LineWriter or BufWriter depending on the state of
|
||||||
// stdout (tty or not). Note that if this is not line buffered it
|
// stdout (tty or not). Note that if this is not line buffered it
|
||||||
// should also flush-on-panic or some form of flush-on-abort.
|
// should also flush-on-panic or some form of flush-on-abort.
|
||||||
inner: Arc<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>>,
|
inner: &'static ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A locked reference to the `Stdout` handle.
|
/// A locked reference to the `Stdout` handle.
|
||||||
|
@ -534,19 +531,27 @@ pub struct StdoutLock<'a> {
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub fn stdout() -> Stdout {
|
pub fn stdout() -> Stdout {
|
||||||
static INSTANCE: Lazy<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> = Lazy::new();
|
static INSTANCE: SyncOnceCell<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> =
|
||||||
return Stdout {
|
SyncOnceCell::new();
|
||||||
inner: unsafe { INSTANCE.get(stdout_init).expect("cannot access stdout during shutdown") },
|
Stdout {
|
||||||
};
|
inner: INSTANCE.get_or_init(|| unsafe {
|
||||||
|
let _ = sys_common::at_exit(|| {
|
||||||
fn stdout_init() -> Arc<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> {
|
if let Some(instance) = INSTANCE.get() {
|
||||||
// This must not reentrantly access `INSTANCE`
|
// Flush the data and disable buffering during shutdown
|
||||||
let stdout = stdout_raw();
|
// by replacing the line writer by one with zero
|
||||||
unsafe {
|
// buffering capacity.
|
||||||
let ret = Arc::new(ReentrantMutex::new(RefCell::new(LineWriter::new(stdout))));
|
// We use try_lock() instead of lock(), because someone
|
||||||
ret.init();
|
// might have leaked a StdoutLock, which would
|
||||||
ret
|
// otherwise cause a deadlock here.
|
||||||
}
|
if let Some(lock) = instance.try_lock() {
|
||||||
|
*lock.borrow_mut() = LineWriter::with_capacity(0, stdout_raw());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let r = ReentrantMutex::new(RefCell::new(LineWriter::new(stdout_raw())));
|
||||||
|
r.init();
|
||||||
|
r
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -741,16 +746,15 @@ pub fn stderr() -> Stderr {
|
||||||
//
|
//
|
||||||
// This has the added benefit of allowing `stderr` to be usable during
|
// This has the added benefit of allowing `stderr` to be usable during
|
||||||
// process shutdown as well!
|
// process shutdown as well!
|
||||||
static INSTANCE: ReentrantMutex<RefCell<StderrRaw>> =
|
static INSTANCE: SyncOnceCell<ReentrantMutex<RefCell<StderrRaw>>> = SyncOnceCell::new();
|
||||||
unsafe { ReentrantMutex::new(RefCell::new(stderr_raw())) };
|
|
||||||
|
|
||||||
// When accessing stderr we need one-time initialization of the reentrant
|
Stderr {
|
||||||
// mutex. Afterwards we can just always use the now-filled-in `INSTANCE` value.
|
inner: INSTANCE.get_or_init(|| unsafe {
|
||||||
static INIT: Once = Once::new();
|
let r = ReentrantMutex::new(RefCell::new(stderr_raw()));
|
||||||
INIT.call_once(|| unsafe {
|
r.init();
|
||||||
INSTANCE.init();
|
r
|
||||||
});
|
}),
|
||||||
Stderr { inner: &INSTANCE }
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Stderr {
|
impl Stderr {
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
// run-pass
|
||||||
|
// check-run-results
|
||||||
|
// ignore-emscripten
|
||||||
|
|
||||||
|
// Emscripten doesn't flush its own stdout buffers on exit, which would fail
|
||||||
|
// this test. So this test is disabled on this platform.
|
||||||
|
// See https://emscripten.org/docs/getting_started/FAQ.html#what-does-exiting-the-runtime-mean-why-don-t-atexit-s-run
|
||||||
|
|
||||||
|
#![feature(rustc_private)]
|
||||||
|
|
||||||
|
extern crate libc;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
extern "C" fn bye() {
|
||||||
|
print!(", world!");
|
||||||
|
}
|
||||||
|
unsafe { libc::atexit(bye) };
|
||||||
|
print!("hello");
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
hello, world!
|
Loading…
Reference in New Issue