diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-19 09:26:03 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-19 09:26:03 +0000 |
commit | 9918693037dce8aa4bb6f08741b6812923486c18 (patch) | |
tree | 21d2b40bec7e6a7ea664acee056eb3d08e15a1cf /vendor/lock_api/src/mutex.rs | |
parent | Releasing progress-linux version 1.75.0+dfsg1-5~progress7.99u1. (diff) | |
download | rustc-9918693037dce8aa4bb6f08741b6812923486c18.tar.xz rustc-9918693037dce8aa4bb6f08741b6812923486c18.zip |
Merging upstream version 1.76.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/lock_api/src/mutex.rs')
-rw-r--r-- | vendor/lock_api/src/mutex.rs | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/vendor/lock_api/src/mutex.rs b/vendor/lock_api/src/mutex.rs index 29533c9a5..80eadfa9f 100644 --- a/vendor/lock_api/src/mutex.rs +++ b/vendor/lock_api/src/mutex.rs @@ -189,11 +189,16 @@ impl<R, T> Mutex<R, T> { } impl<R: RawMutex, T: ?Sized> Mutex<R, T> { + /// Creates a new `MutexGuard` without checking if the mutex is locked. + /// /// # Safety /// - /// The lock must be held when calling this method. + /// This method must only be called if the thread logically holds the lock. + /// + /// Calling this function when a guard has already been produced is undefined behaviour unless + /// the guard was forgotten with `mem::forget`. #[inline] - unsafe fn guard(&self) -> MutexGuard<'_, R, T> { + pub unsafe fn make_guard_unchecked(&self) -> MutexGuard<'_, R, T> { MutexGuard { mutex: self, marker: PhantomData, @@ -213,7 +218,7 @@ impl<R: RawMutex, T: ?Sized> Mutex<R, T> { pub fn lock(&self) -> MutexGuard<'_, R, T> { self.raw.lock(); // SAFETY: The lock is held, as required. - unsafe { self.guard() } + unsafe { self.make_guard_unchecked() } } /// Attempts to acquire this lock. @@ -227,7 +232,7 @@ impl<R: RawMutex, T: ?Sized> Mutex<R, T> { pub fn try_lock(&self) -> Option<MutexGuard<'_, R, T>> { if self.raw.try_lock() { // SAFETY: The lock is held, as required. - Some(unsafe { self.guard() }) + Some(unsafe { self.make_guard_unchecked() }) } else { None } @@ -294,12 +299,17 @@ impl<R: RawMutex, T: ?Sized> Mutex<R, T> { self.data.get() } + /// Creates a new `ArcMutexGuard` without checking if the mutex is locked. + /// /// # Safety /// - /// The lock needs to be held for the behavior of this function to be defined. + /// This method must only be called if the thread logically holds the lock. + /// + /// Calling this function when a guard has already been produced is undefined behaviour unless + /// the guard was forgotten with `mem::forget`. #[cfg(feature = "arc_lock")] #[inline] - unsafe fn guard_arc(self: &Arc<Self>) -> ArcMutexGuard<R, T> { + unsafe fn make_arc_guard_unchecked(self: &Arc<Self>) -> ArcMutexGuard<R, T> { ArcMutexGuard { mutex: self.clone(), marker: PhantomData, @@ -315,7 +325,7 @@ impl<R: RawMutex, T: ?Sized> Mutex<R, T> { pub fn lock_arc(self: &Arc<Self>) -> ArcMutexGuard<R, T> { self.raw.lock(); // SAFETY: the locking guarantee is upheld - unsafe { self.guard_arc() } + unsafe { self.make_arc_guard_unchecked() } } /// Attempts to acquire a lock through an `Arc`. @@ -327,7 +337,7 @@ impl<R: RawMutex, T: ?Sized> Mutex<R, T> { pub fn try_lock_arc(self: &Arc<Self>) -> Option<ArcMutexGuard<R, T>> { if self.raw.try_lock() { // SAFETY: locking guarantee is upheld - Some(unsafe { self.guard_arc() }) + Some(unsafe { self.make_arc_guard_unchecked() }) } else { None } @@ -362,7 +372,7 @@ impl<R: RawMutexTimed, T: ?Sized> Mutex<R, T> { pub fn try_lock_for(&self, timeout: R::Duration) -> Option<MutexGuard<'_, R, T>> { if self.raw.try_lock_for(timeout) { // SAFETY: The lock is held, as required. - Some(unsafe { self.guard() }) + Some(unsafe { self.make_guard_unchecked() }) } else { None } @@ -377,7 +387,7 @@ impl<R: RawMutexTimed, T: ?Sized> Mutex<R, T> { pub fn try_lock_until(&self, timeout: R::Instant) -> Option<MutexGuard<'_, R, T>> { if self.raw.try_lock_until(timeout) { // SAFETY: The lock is held, as required. - Some(unsafe { self.guard() }) + Some(unsafe { self.make_guard_unchecked() }) } else { None } @@ -392,7 +402,7 @@ impl<R: RawMutexTimed, T: ?Sized> Mutex<R, T> { pub fn try_lock_arc_for(self: &Arc<Self>, timeout: R::Duration) -> Option<ArcMutexGuard<R, T>> { if self.raw.try_lock_for(timeout) { // SAFETY: locking guarantee is upheld - Some(unsafe { self.guard_arc() }) + Some(unsafe { self.make_arc_guard_unchecked() }) } else { None } @@ -410,7 +420,7 @@ impl<R: RawMutexTimed, T: ?Sized> Mutex<R, T> { ) -> Option<ArcMutexGuard<R, T>> { if self.raw.try_lock_until(timeout) { // SAFETY: locking guarantee is upheld - Some(unsafe { self.guard_arc() }) + Some(unsafe { self.make_arc_guard_unchecked() }) } else { None } |