Skip to content

Commit 45700a9

Browse files
committed
Drop use of Arc from Stdin and Stdout.
1 parent e9b25f5 commit 45700a9

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

library/std/src/io/stdio.rs

+23-27
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::cell::RefCell;
99
use crate::fmt;
1010
use crate::io::{self, BufReader, Initializer, IoSlice, IoSliceMut, LineWriter};
1111
use crate::lazy::SyncOnceCell;
12-
use crate::sync::{Arc, Mutex, MutexGuard};
12+
use crate::sync::{Mutex, MutexGuard};
1313
use crate::sys::stdio;
1414
use crate::sys_common;
1515
use crate::sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
@@ -218,7 +218,7 @@ fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
218218
/// ```
219219
#[stable(feature = "rust1", since = "1.0.0")]
220220
pub struct Stdin {
221-
inner: Arc<Mutex<BufReader<StdinRaw>>>,
221+
inner: &'static Mutex<BufReader<StdinRaw>>,
222222
}
223223

224224
/// A locked reference to the `Stdin` handle.
@@ -293,13 +293,11 @@ pub struct StdinLock<'a> {
293293
/// ```
294294
#[stable(feature = "rust1", since = "1.0.0")]
295295
pub fn stdin() -> Stdin {
296-
static INSTANCE: SyncOnceCell<Arc<Mutex<BufReader<StdinRaw>>>> = SyncOnceCell::new();
296+
static INSTANCE: SyncOnceCell<Mutex<BufReader<StdinRaw>>> = SyncOnceCell::new();
297297
Stdin {
298-
inner: INSTANCE
299-
.get_or_init(|| {
300-
Arc::new(Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin_raw())))
301-
})
302-
.clone(),
298+
inner: INSTANCE.get_or_init(|| {
299+
Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin_raw()))
300+
}),
303301
}
304302
}
305303

@@ -475,7 +473,7 @@ pub struct Stdout {
475473
// FIXME: this should be LineWriter or BufWriter depending on the state of
476474
// stdout (tty or not). Note that if this is not line buffered it
477475
// should also flush-on-panic or some form of flush-on-abort.
478-
inner: Arc<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>>,
476+
inner: &'static ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>,
479477
}
480478

481479
/// A locked reference to the `Stdout` handle.
@@ -533,27 +531,25 @@ pub struct StdoutLock<'a> {
533531
/// ```
534532
#[stable(feature = "rust1", since = "1.0.0")]
535533
pub fn stdout() -> Stdout {
536-
static INSTANCE: SyncOnceCell<Arc<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>>> =
534+
static INSTANCE: SyncOnceCell<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> =
537535
SyncOnceCell::new();
538536
Stdout {
539-
inner: INSTANCE
540-
.get_or_init(|| unsafe {
541-
let _ = sys_common::at_exit(|| {
542-
if let Some(instance) = INSTANCE.get() {
543-
// Flush the data and disable buffering during shutdown
544-
// by replacing the line writer by one with zero
545-
// buffering capacity.
546-
// We use try_lock() instead of lock(), because someone
547-
// might have leaked a StdoutLock, which would
548-
// otherwise cause a deadlock here.
549-
if let Some(lock) = instance.try_lock() {
550-
*lock.borrow_mut() = LineWriter::with_capacity(0, stdout_raw());
551-
}
537+
inner: INSTANCE.get_or_init(|| unsafe {
538+
let _ = sys_common::at_exit(|| {
539+
if let Some(instance) = INSTANCE.get() {
540+
// Flush the data and disable buffering during shutdown
541+
// by replacing the line writer by one with zero
542+
// buffering capacity.
543+
// We use try_lock() instead of lock(), because someone
544+
// might have leaked a StdoutLock, which would
545+
// otherwise cause a deadlock here.
546+
if let Some(lock) = instance.try_lock() {
547+
*lock.borrow_mut() = LineWriter::with_capacity(0, stdout_raw());
552548
}
553-
});
554-
Arc::new(ReentrantMutex::new(RefCell::new(LineWriter::new(stdout_raw()))))
555-
})
556-
.clone(),
549+
}
550+
});
551+
ReentrantMutex::new(RefCell::new(LineWriter::new(stdout_raw())))
552+
}),
557553
}
558554
}
559555

0 commit comments

Comments
 (0)