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](https://github.com/rust-lang/rust/pull/95035), [on Windows](https://github.com/rust-lang/rust/pull/77380), and various BSDs and some tier 3 platforms have already been using a non-allocating const-constructible implementation. As of https://github.com/rust-lang/rust/pull/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: https://github.com/rust-lang/rust/issues/93740
This commit is contained in:
bors 2022-06-19 08:20:36 +00:00
commit 15fc228d0d
15 changed files with 26 additions and 6 deletions

View File

@ -122,8 +122,10 @@ impl Condvar {
/// let condvar = Condvar::new();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_locks", since = "1.63.0")]
#[must_use]
pub fn new() -> Condvar {
#[inline]
pub const fn new() -> Condvar {
Condvar { inner: sys::Condvar::new() }
}

View File

@ -213,7 +213,9 @@ impl<T> Mutex<T> {
/// let mutex = Mutex::new(0);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new(t: T) -> Mutex<T> {
#[rustc_const_stable(feature = "const_locks", since = "1.63.0")]
#[inline]
pub const fn new(t: T) -> Mutex<T> {
Mutex {
inner: sys::MovableMutex::new(),
poison: poison::Flag::new(),

View File

@ -19,6 +19,7 @@ pub struct Flag {
// all cases.
impl Flag {
#[inline]
pub const fn new() -> Flag {
Flag { failed: AtomicBool::new(false) }
}

View File

@ -146,7 +146,9 @@ impl<T> RwLock<T> {
/// let lock = RwLock::new(5);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new(t: T) -> RwLock<T> {
#[rustc_const_stable(feature = "const_locks", since = "1.63.0")]
#[inline]
pub const fn new(t: T) -> RwLock<T> {
RwLock {
inner: sys::MovableRwLock::new(),
poison: poison::Flag::new(),

View File

@ -26,6 +26,7 @@ fn new_mtx() -> Result<abi::ID, ItronError> {
}
impl Mutex {
#[inline]
pub const fn new() -> Mutex {
Mutex { mtx: SpinIdOnceCell::new() }
}

View File

@ -23,6 +23,7 @@ fn new_rwl() -> Result<abi::ID, ItronError> {
}
impl RwLock {
#[inline]
pub const fn new() -> RwLock {
RwLock { rwl: SpinIdOnceCell::new() }
}

View File

@ -6,6 +6,7 @@ pub struct Condvar {}
pub type MovableCondvar = Condvar;
impl Condvar {
#[inline]
pub const fn new() -> Condvar {
Condvar {}
}

View File

@ -11,6 +11,7 @@ unsafe impl Send for Mutex {}
unsafe impl Sync for Mutex {} // no threads on this platform
impl Mutex {
#[inline]
pub const fn new() -> Mutex {
Mutex { locked: Cell::new(false) }
}

View File

@ -11,6 +11,7 @@ unsafe impl Send for RwLock {}
unsafe impl Sync for RwLock {} // no threads on this platform
impl RwLock {
#[inline]
pub const fn new() -> RwLock {
RwLock { mode: Cell::new(0) }
}

View File

@ -14,6 +14,7 @@ unsafe impl Send for Condvar {}
unsafe impl Sync for Condvar {}
impl Condvar {
#[inline]
pub const fn new() -> Condvar {
Condvar { inner: UnsafeCell::new(c::CONDITION_VARIABLE_INIT) }
}

View File

@ -33,6 +33,7 @@ pub unsafe fn raw(m: &Mutex) -> c::PSRWLOCK {
}
impl Mutex {
#[inline]
pub const fn new() -> Mutex {
Mutex { srwlock: UnsafeCell::new(c::SRWLOCK_INIT) }
}

View File

@ -11,6 +11,7 @@ unsafe impl Send for RwLock {}
unsafe impl Sync for RwLock {}
impl RwLock {
#[inline]
pub const fn new() -> RwLock {
RwLock { inner: UnsafeCell::new(c::SRWLOCK_INIT) }
}

View File

@ -14,7 +14,8 @@ pub struct Condvar {
impl Condvar {
/// Creates a new condition variable for use.
pub fn new() -> Self {
#[inline]
pub const fn new() -> Self {
Self { inner: imp::MovableCondvar::new(), check: CondvarCheck::new() }
}

View File

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

View File

@ -10,6 +10,7 @@
impl StaticRwLock {
/// Creates a new rwlock for use.
#[inline]
pub const fn new() -> Self {
Self(imp::RwLock::new())
}
@ -73,7 +74,8 @@ fn drop(&mut self) {
impl MovableRwLock {
/// Creates a new reader-writer lock for use.
pub fn new() -> Self {
#[inline]
pub const fn new() -> Self {
Self(imp::MovableRwLock::new())
}