Skip to content

Commit 4597bc7

Browse files
committed
std: make ReentrantLock public
1 parent 8b8110e commit 4597bc7

File tree

5 files changed

+375
-207
lines changed

5 files changed

+375
-207
lines changed

library/std/src/io/stdio.rs

+35-10
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ use crate::cell::{Cell, RefCell};
99
use crate::fmt;
1010
use crate::fs::File;
1111
use crate::io::{self, BorrowedCursor, BufReader, IoSlice, IoSliceMut, LineWriter, Lines};
12+
use crate::panic::{RefUnwindSafe, UnwindSafe};
1213
use crate::sync::atomic::{AtomicBool, Ordering};
13-
use crate::sync::{Arc, Mutex, MutexGuard, OnceLock, ReentrantMutex, ReentrantMutexGuard};
14+
use crate::sync::{Arc, Mutex, MutexGuard, OnceLock, ReentrantLock, ReentrantLockGuard};
1415
use crate::sys::stdio;
1516

1617
type LocalStream = Arc<Mutex<Vec<u8>>>;
@@ -536,7 +537,7 @@ pub struct Stdout {
536537
// FIXME: this should be LineWriter or BufWriter depending on the state of
537538
// stdout (tty or not). Note that if this is not line buffered it
538539
// should also flush-on-panic or some form of flush-on-abort.
539-
inner: &'static ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>,
540+
inner: &'static ReentrantLock<RefCell<LineWriter<StdoutRaw>>>,
540541
}
541542

542543
/// A locked reference to the [`Stdout`] handle.
@@ -558,10 +559,10 @@ pub struct Stdout {
558559
#[must_use = "if unused stdout will immediately unlock"]
559560
#[stable(feature = "rust1", since = "1.0.0")]
560561
pub struct StdoutLock<'a> {
561-
inner: ReentrantMutexGuard<'a, RefCell<LineWriter<StdoutRaw>>>,
562+
inner: ReentrantLockGuard<'a, RefCell<LineWriter<StdoutRaw>>>,
562563
}
563564

564-
static STDOUT: OnceLock<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> = OnceLock::new();
565+
static STDOUT: OnceLock<ReentrantLock<RefCell<LineWriter<StdoutRaw>>>> = OnceLock::new();
565566

566567
/// Constructs a new handle to the standard output of the current process.
567568
///
@@ -614,7 +615,7 @@ static STDOUT: OnceLock<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> = OnceLo
614615
pub fn stdout() -> Stdout {
615616
Stdout {
616617
inner: STDOUT
617-
.get_or_init(|| ReentrantMutex::new(RefCell::new(LineWriter::new(stdout_raw())))),
618+
.get_or_init(|| ReentrantLock::new(RefCell::new(LineWriter::new(stdout_raw())))),
618619
}
619620
}
620621

@@ -625,7 +626,7 @@ pub fn cleanup() {
625626
let mut initialized = false;
626627
let stdout = STDOUT.get_or_init(|| {
627628
initialized = true;
628-
ReentrantMutex::new(RefCell::new(LineWriter::with_capacity(0, stdout_raw())))
629+
ReentrantLock::new(RefCell::new(LineWriter::with_capacity(0, stdout_raw())))
629630
});
630631

631632
if !initialized {
@@ -668,6 +669,12 @@ impl Stdout {
668669
}
669670
}
670671

672+
#[stable(feature = "catch_unwind", since = "1.9.0")]
673+
impl UnwindSafe for Stdout {}
674+
675+
#[stable(feature = "catch_unwind", since = "1.9.0")]
676+
impl RefUnwindSafe for Stdout {}
677+
671678
#[stable(feature = "std_debug", since = "1.16.0")]
672679
impl fmt::Debug for Stdout {
673680
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -727,6 +734,12 @@ impl Write for &Stdout {
727734
}
728735
}
729736

737+
#[stable(feature = "catch_unwind", since = "1.9.0")]
738+
impl UnwindSafe for StdoutLock<'_> {}
739+
740+
#[stable(feature = "catch_unwind", since = "1.9.0")]
741+
impl RefUnwindSafe for StdoutLock<'_> {}
742+
730743
#[stable(feature = "rust1", since = "1.0.0")]
731744
impl Write for StdoutLock<'_> {
732745
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
@@ -776,7 +789,7 @@ impl fmt::Debug for StdoutLock<'_> {
776789
/// standard library or via raw Windows API calls, will fail.
777790
#[stable(feature = "rust1", since = "1.0.0")]
778791
pub struct Stderr {
779-
inner: &'static ReentrantMutex<RefCell<StderrRaw>>,
792+
inner: &'static ReentrantLock<RefCell<StderrRaw>>,
780793
}
781794

782795
/// A locked reference to the [`Stderr`] handle.
@@ -798,7 +811,7 @@ pub struct Stderr {
798811
#[must_use = "if unused stderr will immediately unlock"]
799812
#[stable(feature = "rust1", since = "1.0.0")]
800813
pub struct StderrLock<'a> {
801-
inner: ReentrantMutexGuard<'a, RefCell<StderrRaw>>,
814+
inner: ReentrantLockGuard<'a, RefCell<StderrRaw>>,
802815
}
803816

804817
/// Constructs a new handle to the standard error of the current process.
@@ -851,8 +864,8 @@ pub fn stderr() -> Stderr {
851864
// Note that unlike `stdout()` we don't use `at_exit` here to register a
852865
// destructor. Stderr is not buffered, so there's no need to run a
853866
// destructor for flushing the buffer
854-
static INSTANCE: ReentrantMutex<RefCell<StderrRaw>> =
855-
ReentrantMutex::new(RefCell::new(stderr_raw()));
867+
static INSTANCE: ReentrantLock<RefCell<StderrRaw>> =
868+
ReentrantLock::new(RefCell::new(stderr_raw()));
856869

857870
Stderr { inner: &INSTANCE }
858871
}
@@ -887,6 +900,12 @@ impl Stderr {
887900
}
888901
}
889902

903+
#[stable(feature = "catch_unwind", since = "1.9.0")]
904+
impl UnwindSafe for Stderr {}
905+
906+
#[stable(feature = "catch_unwind", since = "1.9.0")]
907+
impl RefUnwindSafe for Stderr {}
908+
890909
#[stable(feature = "std_debug", since = "1.16.0")]
891910
impl fmt::Debug for Stderr {
892911
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -946,6 +965,12 @@ impl Write for &Stderr {
946965
}
947966
}
948967

968+
#[stable(feature = "catch_unwind", since = "1.9.0")]
969+
impl UnwindSafe for StderrLock<'_> {}
970+
971+
#[stable(feature = "catch_unwind", since = "1.9.0")]
972+
impl RefUnwindSafe for StderrLock<'_> {}
973+
949974
#[stable(feature = "rust1", since = "1.0.0")]
950975
impl Write for StderrLock<'_> {
951976
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {

library/std/src/sync/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ pub use self::lazy_lock::LazyLock;
180180
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
181181
pub use self::once_lock::OnceLock;
182182

183-
pub(crate) use self::remutex::{ReentrantMutex, ReentrantMutexGuard};
183+
#[unstable(feature = "reentrant_lock", issue = "none")]
184+
pub use self::reentrant_lock::{ReentrantLock, ReentrantLockGuard};
184185

185186
pub mod mpsc;
186187

@@ -192,5 +193,5 @@ mod mutex;
192193
pub(crate) mod once;
193194
mod once_lock;
194195
mod poison;
195-
mod remutex;
196+
mod reentrant_lock;
196197
mod rwlock;

0 commit comments

Comments
 (0)