Skip to content

Commit 15fc228

Browse files
committed
Auto merge of #97791 - m-ou-se:const-locks, r=m-ou-se
Make {Mutex, Condvar, RwLock}::new() const. This makes it possible to have `static M: Mutex<_> = Mutex::new(..);` 🎉 Our implementations [on Linux](#95035), [on Windows](#77380), and various BSDs and some tier 3 platforms have already been using a non-allocating const-constructible implementation. As of #97647, the remaining platforms (most notably macOS) now have a const-constructible implementation as well. This means we can finally make these functions publicly const. Tracking issue: #93740
2 parents 5fb8a39 + edae495 commit 15fc228

File tree

15 files changed

+26
-6
lines changed

15 files changed

+26
-6
lines changed

library/std/src/sync/condvar.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ impl Condvar {
122122
/// let condvar = Condvar::new();
123123
/// ```
124124
#[stable(feature = "rust1", since = "1.0.0")]
125+
#[rustc_const_stable(feature = "const_locks", since = "1.63.0")]
125126
#[must_use]
126-
pub fn new() -> Condvar {
127+
#[inline]
128+
pub const fn new() -> Condvar {
127129
Condvar { inner: sys::Condvar::new() }
128130
}
129131

library/std/src/sync/mutex.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ impl<T> Mutex<T> {
213213
/// let mutex = Mutex::new(0);
214214
/// ```
215215
#[stable(feature = "rust1", since = "1.0.0")]
216-
pub fn new(t: T) -> Mutex<T> {
216+
#[rustc_const_stable(feature = "const_locks", since = "1.63.0")]
217+
#[inline]
218+
pub const fn new(t: T) -> Mutex<T> {
217219
Mutex {
218220
inner: sys::MovableMutex::new(),
219221
poison: poison::Flag::new(),

library/std/src/sync/poison.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct Flag {
1919
// all cases.
2020

2121
impl Flag {
22+
#[inline]
2223
pub const fn new() -> Flag {
2324
Flag { failed: AtomicBool::new(false) }
2425
}

library/std/src/sync/rwlock.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ impl<T> RwLock<T> {
146146
/// let lock = RwLock::new(5);
147147
/// ```
148148
#[stable(feature = "rust1", since = "1.0.0")]
149-
pub fn new(t: T) -> RwLock<T> {
149+
#[rustc_const_stable(feature = "const_locks", since = "1.63.0")]
150+
#[inline]
151+
pub const fn new(t: T) -> RwLock<T> {
150152
RwLock {
151153
inner: sys::MovableRwLock::new(),
152154
poison: poison::Flag::new(),

library/std/src/sys/itron/mutex.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ fn new_mtx() -> Result<abi::ID, ItronError> {
2626
}
2727

2828
impl Mutex {
29+
#[inline]
2930
pub const fn new() -> Mutex {
3031
Mutex { mtx: SpinIdOnceCell::new() }
3132
}

library/std/src/sys/solid/rwlock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ fn new_rwl() -> Result<abi::ID, ItronError> {
2323
}
2424

2525
impl RwLock {
26+
#[inline]
2627
pub const fn new() -> RwLock {
2728
RwLock { rwl: SpinIdOnceCell::new() }
2829
}

library/std/src/sys/unsupported/locks/condvar.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub struct Condvar {}
66
pub type MovableCondvar = Condvar;
77

88
impl Condvar {
9+
#[inline]
910
pub const fn new() -> Condvar {
1011
Condvar {}
1112
}

library/std/src/sys/unsupported/locks/mutex.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ unsafe impl Send for Mutex {}
1111
unsafe impl Sync for Mutex {} // no threads on this platform
1212

1313
impl Mutex {
14+
#[inline]
1415
pub const fn new() -> Mutex {
1516
Mutex { locked: Cell::new(false) }
1617
}

library/std/src/sys/unsupported/locks/rwlock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ unsafe impl Send for RwLock {}
1111
unsafe impl Sync for RwLock {} // no threads on this platform
1212

1313
impl RwLock {
14+
#[inline]
1415
pub const fn new() -> RwLock {
1516
RwLock { mode: Cell::new(0) }
1617
}

library/std/src/sys/windows/locks/condvar.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ unsafe impl Send for Condvar {}
1414
unsafe impl Sync for Condvar {}
1515

1616
impl Condvar {
17+
#[inline]
1718
pub const fn new() -> Condvar {
1819
Condvar { inner: UnsafeCell::new(c::CONDITION_VARIABLE_INIT) }
1920
}

0 commit comments

Comments
 (0)