]> git.lizzy.rs Git - rust.git/commit
Remove std::io::lazy::Lazy in favour of SyncOnceCell
authorMara Bos <m-ou.se@m-ou.se>
Thu, 24 Sep 2020 15:45:50 +0000 (17:45 +0200)
committerMara Bos <m-ou.se@m-ou.se>
Thu, 24 Sep 2020 16:18:48 +0000 (18:18 +0200)
commitbab15f773afd5724023d9a065b5af276e2468ff5
tree16a5dc00cdd0272d64358e850d0857ebf2c9f2ce
parent4eff9b0b29a8898c839d46f3c66526710afed68a
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.
library/std/src/io/lazy.rs [deleted file]
library/std/src/io/mod.rs
library/std/src/io/stdio.rs