summary refs log tree commit diff
path: root/library/std/src/io/stdio.rs
AgeCommit message (Collapse)AuthorLines
2020-12-08Pin the ReentrantMutexes in sys::io::stdio.Mara Bos-25/+29
The code before this change misused the ReentrantMutexes, by calling init() on them and moving them afterwards. Now that ReentrantMutex requires Pin for init(), this mistake is no longer easy to make.
2020-12-08Fix outdated comment about not needing to flush stderr.Mara Bos-7/+3
2020-10-22Only load LOCAL_STREAMS if they are being usedSergio Benitez-0/+5
2020-10-22Capture output from threads spawned in testsTyler Mandry-5/+28
Fixes #42474.
2020-09-27Optimize set_{panic,print}(None).Mara Bos-0/+8
2020-09-27Relax memory ordering of LOCAL_STREAMS and document it.Mara Bos-5/+17
2020-09-27Only use LOCAL_{STDOUT,STDERR} when set_{print/panic} is used.Mara Bos-23/+40
The thread local LOCAL_STDOUT and LOCAL_STDERR are only used by the test crate to capture output from tests when running them in the same process in differen threads. However, every program will check these variables on every print, even outside of testing. This involves allocating a thread local key, and registering a thread local destructor. This can be somewhat expensive. This change keeps a global flag (LOCAL_STREAMS) which will be set to true when either of these local streams is used. (So, effectively only in test and benchmark runs.) When this flag is off, these thread locals are not even looked at and therefore will not be initialized on the first output on every thread, which also means no thread local destructors will be registered.
2020-09-27Auto merge of #77154 - fusion-engineering-forks:lazy-stdio, r=dtolnaybors-36/+40
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.
2020-09-24Call ReentrantMutex::init() in stdout().Mara Bos-1/+3
2020-09-24Drop use of Arc from Stdin and Stdout.Mara Bos-27/+23
2020-09-24Remove std::io::lazy::Lazy in favour of SyncOnceCellMara Bos-34/+40
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.
2020-09-21Rollup merge of #76275 - FedericoPonzi:immutable-write-impl-73836, r=dtolnayecstatic-morse-0/+54
Implementation of Write for some immutable ref structs Fixes #73836
2020-09-21Updates stability attributes to the current nightly versionFederico Ponzi-2/+2
2020-09-11Deduplicates io::Write implementationsFederico Ponzi-14/+14
2020-09-03More implementations of Write for immutable refsFederico Ponzi-0/+54
Fixes #73836
2020-08-31std: move "mod tests/benches" to separate filesLzu Tao-51/+3
Also doing fmt inplace as requested.
2020-08-21Remove wrapper type handling absent raw standard streamsTomasz Miąsko-100/+33
Raw standard streams are always available. Remove unused wrapper type that was supposed to be responsible for handling their absence.
2020-08-21Make raw standard stream constructors constTomasz Miąsko-3/+6
2020-08-21Remove result type from raw standard streams constructorsTomasz Miąsko-18/+10
Raw standard streams constructors are infallible. Remove unnecessary result type.
2020-08-18Move to intra doc links for std::ioAlexis Bourget-24/+5
2020-07-27mv std libs to library/mark-0/+1064