Skip to content

Commit 15fc228

Browse files
committed
Auto merge of rust-lang#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](rust-lang#95035), [on Windows](rust-lang#77380), and various BSDs and some tier 3 platforms have already been using a non-allocating const-constructible implementation. As of rust-lang#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: rust-lang#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

+3-1
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

+3-1
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

+1
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

+3-1
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

+1
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

+1
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

+1
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

+1
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

+1
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

+1
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
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub unsafe fn raw(m: &Mutex) -> c::PSRWLOCK {
3333
}
3434

3535
impl Mutex {
36+
#[inline]
3637
pub const fn new() -> Mutex {
3738
Mutex { srwlock: UnsafeCell::new(c::SRWLOCK_INIT) }
3839
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ unsafe impl Send for RwLock {}
1111
unsafe impl Sync for RwLock {}
1212

1313
impl RwLock {
14+
#[inline]
1415
pub const fn new() -> RwLock {
1516
RwLock { inner: UnsafeCell::new(c::SRWLOCK_INIT) }
1617
}

library/std/src/sys_common/condvar.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ pub struct Condvar {
1414

1515
impl Condvar {
1616
/// Creates a new condition variable for use.
17-
pub fn new() -> Self {
17+
#[inline]
18+
pub const fn new() -> Self {
1819
Self { inner: imp::MovableCondvar::new(), check: CondvarCheck::new() }
1920
}
2021

library/std/src/sys_common/mutex.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ unsafe impl Sync for StaticMutex {}
1515

1616
impl StaticMutex {
1717
/// Creates a new mutex for use.
18+
#[inline]
1819
pub const fn new() -> Self {
1920
Self(imp::Mutex::new())
2021
}
@@ -60,7 +61,8 @@ unsafe impl Sync for MovableMutex {}
6061

6162
impl MovableMutex {
6263
/// Creates a new mutex.
63-
pub fn new() -> Self {
64+
#[inline]
65+
pub const fn new() -> Self {
6466
Self(imp::MovableMutex::new())
6567
}
6668

library/std/src/sys_common/rwlock.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub struct StaticRwLock(imp::RwLock);
1010

1111
impl StaticRwLock {
1212
/// Creates a new rwlock for use.
13+
#[inline]
1314
pub const fn new() -> Self {
1415
Self(imp::RwLock::new())
1516
}
@@ -73,7 +74,8 @@ pub struct MovableRwLock(imp::MovableRwLock);
7374

7475
impl MovableRwLock {
7576
/// Creates a new reader-writer lock for use.
76-
pub fn new() -> Self {
77+
#[inline]
78+
pub const fn new() -> Self {
7779
Self(imp::MovableRwLock::new())
7880
}
7981

0 commit comments

Comments
 (0)