Skip to content

Commit 90244b7

Browse files
Rollup merge of rust-lang#110543 - joboet:reentrant_lock, r=m-ou-se
Make `ReentrantLock` public Implements the ACP rust-lang/libs-team#193. `@rustbot` label +T-libs-api +S-waiting-on-ACP
2 parents bf9c7a6 + 2aa8a1d commit 90244b7

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
@@ -11,8 +11,9 @@ use crate::fs::File;
1111
use crate::io::{
1212
self, BorrowedCursor, BufReader, IoSlice, IoSliceMut, LineWriter, Lines, SpecReadByte,
1313
};
14+
use crate::panic::{RefUnwindSafe, UnwindSafe};
1415
use crate::sync::atomic::{AtomicBool, Ordering};
15-
use crate::sync::{Arc, Mutex, MutexGuard, OnceLock, ReentrantMutex, ReentrantMutexGuard};
16+
use crate::sync::{Arc, Mutex, MutexGuard, OnceLock, ReentrantLock, ReentrantLockGuard};
1617
use crate::sys::stdio;
1718

1819
type LocalStream = Arc<Mutex<Vec<u8>>>;
@@ -545,7 +546,7 @@ pub struct Stdout {
545546
// FIXME: this should be LineWriter or BufWriter depending on the state of
546547
// stdout (tty or not). Note that if this is not line buffered it
547548
// should also flush-on-panic or some form of flush-on-abort.
548-
inner: &'static ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>,
549+
inner: &'static ReentrantLock<RefCell<LineWriter<StdoutRaw>>>,
549550
}
550551

551552
/// A locked reference to the [`Stdout`] handle.
@@ -567,10 +568,10 @@ pub struct Stdout {
567568
#[must_use = "if unused stdout will immediately unlock"]
568569
#[stable(feature = "rust1", since = "1.0.0")]
569570
pub struct StdoutLock<'a> {
570-
inner: ReentrantMutexGuard<'a, RefCell<LineWriter<StdoutRaw>>>,
571+
inner: ReentrantLockGuard<'a, RefCell<LineWriter<StdoutRaw>>>,
571572
}
572573

573-
static STDOUT: OnceLock<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> = OnceLock::new();
574+
static STDOUT: OnceLock<ReentrantLock<RefCell<LineWriter<StdoutRaw>>>> = OnceLock::new();
574575

575576
/// Constructs a new handle to the standard output of the current process.
576577
///
@@ -624,7 +625,7 @@ static STDOUT: OnceLock<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> = OnceLo
624625
pub fn stdout() -> Stdout {
625626
Stdout {
626627
inner: STDOUT
627-
.get_or_init(|| ReentrantMutex::new(RefCell::new(LineWriter::new(stdout_raw())))),
628+
.get_or_init(|| ReentrantLock::new(RefCell::new(LineWriter::new(stdout_raw())))),
628629
}
629630
}
630631

@@ -635,7 +636,7 @@ pub fn cleanup() {
635636
let mut initialized = false;
636637
let stdout = STDOUT.get_or_init(|| {
637638
initialized = true;
638-
ReentrantMutex::new(RefCell::new(LineWriter::with_capacity(0, stdout_raw())))
639+
ReentrantLock::new(RefCell::new(LineWriter::with_capacity(0, stdout_raw())))
639640
});
640641

641642
if !initialized {
@@ -678,6 +679,12 @@ impl Stdout {
678679
}
679680
}
680681

682+
#[stable(feature = "catch_unwind", since = "1.9.0")]
683+
impl UnwindSafe for Stdout {}
684+
685+
#[stable(feature = "catch_unwind", since = "1.9.0")]
686+
impl RefUnwindSafe for Stdout {}
687+
681688
#[stable(feature = "std_debug", since = "1.16.0")]
682689
impl fmt::Debug for Stdout {
683690
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -737,6 +744,12 @@ impl Write for &Stdout {
737744
}
738745
}
739746

747+
#[stable(feature = "catch_unwind", since = "1.9.0")]
748+
impl UnwindSafe for StdoutLock<'_> {}
749+
750+
#[stable(feature = "catch_unwind", since = "1.9.0")]
751+
impl RefUnwindSafe for StdoutLock<'_> {}
752+
740753
#[stable(feature = "rust1", since = "1.0.0")]
741754
impl Write for StdoutLock<'_> {
742755
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
@@ -786,7 +799,7 @@ impl fmt::Debug for StdoutLock<'_> {
786799
/// standard library or via raw Windows API calls, will fail.
787800
#[stable(feature = "rust1", since = "1.0.0")]
788801
pub struct Stderr {
789-
inner: &'static ReentrantMutex<RefCell<StderrRaw>>,
802+
inner: &'static ReentrantLock<RefCell<StderrRaw>>,
790803
}
791804

792805
/// A locked reference to the [`Stderr`] handle.
@@ -808,7 +821,7 @@ pub struct Stderr {
808821
#[must_use = "if unused stderr will immediately unlock"]
809822
#[stable(feature = "rust1", since = "1.0.0")]
810823
pub struct StderrLock<'a> {
811-
inner: ReentrantMutexGuard<'a, RefCell<StderrRaw>>,
824+
inner: ReentrantLockGuard<'a, RefCell<StderrRaw>>,
812825
}
813826

814827
/// Constructs a new handle to the standard error of the current process.
@@ -862,8 +875,8 @@ pub fn stderr() -> Stderr {
862875
// Note that unlike `stdout()` we don't use `at_exit` here to register a
863876
// destructor. Stderr is not buffered, so there's no need to run a
864877
// destructor for flushing the buffer
865-
static INSTANCE: ReentrantMutex<RefCell<StderrRaw>> =
866-
ReentrantMutex::new(RefCell::new(stderr_raw()));
878+
static INSTANCE: ReentrantLock<RefCell<StderrRaw>> =
879+
ReentrantLock::new(RefCell::new(stderr_raw()));
867880

868881
Stderr { inner: &INSTANCE }
869882
}
@@ -898,6 +911,12 @@ impl Stderr {
898911
}
899912
}
900913

914+
#[stable(feature = "catch_unwind", since = "1.9.0")]
915+
impl UnwindSafe for Stderr {}
916+
917+
#[stable(feature = "catch_unwind", since = "1.9.0")]
918+
impl RefUnwindSafe for Stderr {}
919+
901920
#[stable(feature = "std_debug", since = "1.16.0")]
902921
impl fmt::Debug for Stderr {
903922
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -957,6 +976,12 @@ impl Write for &Stderr {
957976
}
958977
}
959978

979+
#[stable(feature = "catch_unwind", since = "1.9.0")]
980+
impl UnwindSafe for StderrLock<'_> {}
981+
982+
#[stable(feature = "catch_unwind", since = "1.9.0")]
983+
impl RefUnwindSafe for StderrLock<'_> {}
984+
960985
#[stable(feature = "rust1", since = "1.0.0")]
961986
impl Write for StderrLock<'_> {
962987
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
@@ -184,7 +184,8 @@ pub use self::lazy_lock::LazyLock;
184184
#[stable(feature = "once_cell", since = "1.70.0")]
185185
pub use self::once_lock::OnceLock;
186186

187-
pub(crate) use self::remutex::{ReentrantMutex, ReentrantMutexGuard};
187+
#[unstable(feature = "reentrant_lock", issue = "121440")]
188+
pub use self::reentrant_lock::{ReentrantLock, ReentrantLockGuard};
188189

189190
pub mod mpsc;
190191

@@ -196,5 +197,5 @@ mod mutex;
196197
pub(crate) mod once;
197198
mod once_lock;
198199
mod poison;
199-
mod remutex;
200+
mod reentrant_lock;
200201
mod rwlock;

0 commit comments

Comments
 (0)