Skip to content

Commit

Permalink
Auto merge of rust-lang#86799 - tlyu:stdio-locked, r=joshtriplett
Browse files Browse the repository at this point in the history
add owned locked stdio handles

Add stderr_locked, stdin_locked, and stdout_locked free functions
to obtain owned locked stdio handles in a single step. Also add
into_lock methods to consume a stdio handle and return an owned
lock. These methods will make it easier to use locked stdio
handles without having to deal with lifetime problems or keeping
bindings to the unlocked handles around.

Fixes rust-lang#85383; enables rust-lang#86412.

r? `@joshtriplett`
`@rustbot` label +A-io +C-enhancement +D-newcomer-roadblock +T-libs-api
  • Loading branch information
bors committed Jul 3, 2021
2 parents 7014963 + c58ceb7 commit a8b8558
Show file tree
Hide file tree
Showing 3 changed files with 337 additions and 1 deletion.
2 changes: 2 additions & 0 deletions library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ pub use self::error::{Error, ErrorKind, Result};
pub use self::stdio::set_output_capture;
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::stdio::{stderr, stdin, stdout, Stderr, Stdin, Stdout};
#[unstable(feature = "stdio_locked", issue = "none")]
pub use self::stdio::{stderr_locked, stdin_locked, stdout_locked};
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::stdio::{StderrLock, StdinLock, StdoutLock};
#[unstable(feature = "print_internals", issue = "none")]
Expand Down
217 changes: 216 additions & 1 deletion library/std/src/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,48 @@ pub fn stdin() -> Stdin {
}
}

/// Constructs a new locked handle to the standard input of the current
/// process.
///
/// Each handle returned is a guard granting locked access to a shared
/// global buffer whose access is synchronized via a mutex. If you need
/// more explicit control over locking, for example, in a multi-threaded
/// program, use the [`io::stdin`] function to obtain an unlocked handle,
/// along with the [`Stdin::lock`] method.
///
/// The lock is released when the returned guard goes out of scope. The
/// returned guard also implements the [`Read`] and [`BufRead`] traits for
/// accessing the underlying data.
///
/// **Note**: The mutex locked by this handle is not reentrant. Even in a
/// single-threaded program, calling other code that accesses [`Stdin`]
/// could cause a deadlock or panic, if this locked handle is held across
/// that call.
///
/// ### Note: Windows Portability Consideration
/// When operating in a console, the Windows implementation of this stream does not support
/// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
/// an error.
///
/// # Examples
///
/// ```no_run
/// #![feature(stdio_locked)]
/// use std::io::{self, Read};
///
/// fn main() -> io::Result<()> {
/// let mut buffer = String::new();
/// let mut handle = io::stdin_locked();
///
/// handle.read_to_string(&mut buffer)?;
/// Ok(())
/// }
/// ```
#[unstable(feature = "stdio_locked", issue = "none")]
pub fn stdin_locked() -> StdinLock<'static> {
stdin().into_locked()
}

impl Stdin {
/// Locks this handle to the standard input stream, returning a readable
/// guard.
Expand All @@ -334,7 +376,7 @@ impl Stdin {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn lock(&self) -> StdinLock<'_> {
StdinLock { inner: self.inner.lock().unwrap_or_else(|e| e.into_inner()) }
self.lock_any()
}

/// Locks this handle and reads a line of input, appending it to the specified buffer.
Expand Down Expand Up @@ -367,6 +409,43 @@ impl Stdin {
pub fn read_line(&self, buf: &mut String) -> io::Result<usize> {
self.lock().read_line(buf)
}

// Locks this handle with any lifetime. This depends on the
// implementation detail that the underlying `Mutex` is static.
fn lock_any<'a>(&self) -> StdinLock<'a> {
StdinLock { inner: self.inner.lock().unwrap_or_else(|e| e.into_inner()) }
}

/// Consumes this handle to the standard input stream, locking the
/// shared global buffer associated with the stream and returning a
/// readable guard.
///
/// The lock is released when the returned guard goes out of scope. The
/// returned guard also implements the [`Read`] and [`BufRead`] traits
/// for accessing the underlying data.
///
/// It is often simpler to directly get a locked handle using the
/// [`stdin_locked`] function instead, unless nearby code also needs to
/// use an unlocked handle.
///
/// # Examples
///
/// ```no_run
/// #![feature(stdio_locked)]
/// use std::io::{self, Read};
///
/// fn main() -> io::Result<()> {
/// let mut buffer = String::new();
/// let mut handle = io::stdin().into_locked();
///
/// handle.read_to_string(&mut buffer)?;
/// Ok(())
/// }
/// ```
#[unstable(feature = "stdio_locked", issue = "none")]
pub fn into_locked(self) -> StdinLock<'static> {
self.lock_any()
}
}

#[stable(feature = "std_debug", since = "1.16.0")]
Expand Down Expand Up @@ -558,6 +637,42 @@ pub fn stdout() -> Stdout {
}
}

/// Constructs a new locked handle to the standard output of the current
/// process.
///
/// Each handle returned is a guard granting locked access to a shared
/// global buffer whose access is synchronized via a mutex. If you need
/// more explicit control over locking, for example, in a multi-threaded
/// program, use the [`io::stdout`] function to obtain an unlocked handle,
/// along with the [`Stdout::lock`] method.
///
/// The lock is released when the returned guard goes out of scope. The
/// returned guard also implements the [`Write`] trait for writing data.
///
/// ### Note: Windows Portability Consideration
/// When operating in a console, the Windows implementation of this stream does not support
/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
/// an error.
///
/// # Examples
///
/// ```no_run
/// #![feature(stdio_locked)]
/// use std::io::{self, Write};
///
/// fn main() -> io::Result<()> {
/// let mut handle = io::stdout_locked();
///
/// handle.write_all(b"hello world")?;
///
/// Ok(())
/// }
/// ```
#[unstable(feature = "stdio_locked", issue = "none")]
pub fn stdout_locked() -> StdoutLock<'static> {
stdout().into_locked()
}

pub fn cleanup() {
if let Some(instance) = STDOUT.get() {
// Flush the data and disable buffering during shutdown
Expand Down Expand Up @@ -595,8 +710,45 @@ impl Stdout {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn lock(&self) -> StdoutLock<'_> {
self.lock_any()
}

// Locks this handle with any lifetime. This depends on the
// implementation detail that the underlying `ReentrantMutex` is
// static.
fn lock_any<'a>(&self) -> StdoutLock<'a> {
StdoutLock { inner: self.inner.lock() }
}

/// Consumes this handle to the standard output stream, locking the
/// shared global buffer associated with the stream and returning a
/// writable guard.
///
/// The lock is released when the returned lock goes out of scope. The
/// returned guard also implements the [`Write`] trait for writing data.
///
/// It is often simpler to directly get a locked handle using the
/// [`io::stdout_locked`] function instead, unless nearby code also
/// needs to use an unlocked handle.
///
/// # Examples
///
/// ```no_run
/// #![feature(stdio_locked)]
/// use std::io::{self, Write};
///
/// fn main() -> io::Result<()> {
/// let mut handle = io::stdout().into_locked();
///
/// handle.write_all(b"hello world")?;
///
/// Ok(())
/// }
/// ```
#[unstable(feature = "stdio_locked", issue = "none")]
pub fn into_locked(self) -> StdoutLock<'static> {
self.lock_any()
}
}

#[stable(feature = "std_debug", since = "1.16.0")]
Expand Down Expand Up @@ -769,6 +921,35 @@ pub fn stderr() -> Stderr {
}
}

/// Constructs a new locked handle to the standard error of the current
/// process.
///
/// This handle is not buffered.
///
/// ### Note: Windows Portability Consideration
/// When operating in a console, the Windows implementation of this stream does not support
/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
/// an error.
///
/// # Example
///
/// ```no_run
/// #![feature(stdio_locked)]
/// use std::io::{self, Write};
///
/// fn main() -> io::Result<()> {
/// let mut handle = io::stderr_locked();
///
/// handle.write_all(b"hello world")?;
///
/// Ok(())
/// }
/// ```
#[unstable(feature = "stdio_locked", issue = "none")]
pub fn stderr_locked() -> StderrLock<'static> {
stderr().into_locked()
}

impl Stderr {
/// Locks this handle to the standard error stream, returning a writable
/// guard.
Expand All @@ -792,8 +973,42 @@ impl Stderr {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn lock(&self) -> StderrLock<'_> {
self.lock_any()
}

// Locks this handle with any lifetime. This depends on the
// implementation detail that the underlying `ReentrantMutex` is
// static.
fn lock_any<'a>(&self) -> StderrLock<'a> {
StderrLock { inner: self.inner.lock() }
}

/// Locks and consumes this handle to the standard error stream,
/// returning a writable guard.
///
/// The lock is released when the returned guard goes out of scope. The
/// returned guard also implements the [`Write`] trait for writing
/// data.
///
/// # Examples
///
/// ```
/// #![feature(stdio_locked)]
/// use std::io::{self, Write};
///
/// fn foo() -> io::Result<()> {
/// let stderr = io::stderr();
/// let mut handle = stderr.into_locked();
///
/// handle.write_all(b"hello world")?;
///
/// Ok(())
/// }
/// ```
#[unstable(feature = "stdio_locked", issue = "none")]
pub fn into_locked(self) -> StderrLock<'static> {
self.lock_any()
}
}

#[stable(feature = "std_debug", since = "1.16.0")]
Expand Down
Loading

0 comments on commit a8b8558

Please sign in to comment.