From 93e109d886da4cd1ac12637b037195c83e7bc6fe Mon Sep 17 00:00:00 2001 From: David LeGare Date: Wed, 2 Mar 2022 16:21:15 +0000 Subject: Update lock_api to 0.4.6 Test: cd external/rust/crates && atest --host -c Change-Id: Ieaac4c489656751a71ea398bb05725212cbb6dc7 --- .cargo_vcs_info.json | 7 ++-- Android.bp | 2 +- Cargo.toml | 2 +- Cargo.toml.orig | 2 +- METADATA | 10 ++--- src/mutex.rs | 6 +-- src/remutex.rs | 39 +++++++++--------- src/rwlock.rs | 111 ++++++++++++++++++++++++++++++++------------------- 8 files changed, 105 insertions(+), 74 deletions(-) diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index 58935f2..9ed45c2 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,6 @@ { "git": { - "sha1": "18001b819c1539c06c176c671bbe54e70b5c3d69" - } -} + "sha1": "a75875b0bf904287a9749e8eabea919b5e9dd8a9" + }, + "path_in_vcs": "lock_api" +} \ No newline at end of file diff --git a/Android.bp b/Android.bp index 06ca975..67d3a3b 100644 --- a/Android.bp +++ b/Android.bp @@ -42,7 +42,7 @@ rust_library { host_supported: true, crate_name: "lock_api", cargo_env_compat: true, - cargo_pkg_version: "0.4.5", + cargo_pkg_version: "0.4.6", srcs: ["src/lib.rs"], edition: "2018", rustlibs: [ diff --git a/Cargo.toml b/Cargo.toml index 4382f30..63cf8c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ [package] edition = "2018" name = "lock_api" -version = "0.4.5" +version = "0.4.6" authors = ["Amanieu d'Antras "] description = "Wrappers to create fully-featured Mutex and RwLock types. Compatible with no_std." keywords = ["mutex", "rwlock", "lock", "no_std"] diff --git a/Cargo.toml.orig b/Cargo.toml.orig index 96ffdfc..e6a805f 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "lock_api" -version = "0.4.5" +version = "0.4.6" authors = ["Amanieu d'Antras "] description = "Wrappers to create fully-featured Mutex and RwLock types. Compatible with no_std." license = "Apache-2.0/MIT" diff --git a/METADATA b/METADATA index ebfc44b..1caadde 100644 --- a/METADATA +++ b/METADATA @@ -7,13 +7,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/lock_api/lock_api-0.4.5.crate" + value: "https://static.crates.io/crates/lock_api/lock_api-0.4.6.crate" } - version: "0.4.5" + version: "0.4.6" license_type: NOTICE last_upgrade_date { - year: 2021 - month: 9 - day: 22 + year: 2022 + month: 3 + day: 1 } } diff --git a/src/mutex.rs b/src/mutex.rs index f64fc13..81c25fb 100644 --- a/src/mutex.rs +++ b/src/mutex.rs @@ -663,8 +663,8 @@ impl<'a, R: RawMutex + 'a, T: fmt::Display + ?Sized + 'a> fmt::Display for Mutex unsafe impl<'a, R: RawMutex + 'a, T: ?Sized + 'a> StableAddress for MutexGuard<'a, R, T> {} /// An RAII mutex guard returned by the `Arc` locking operations on `Mutex`. -/// -/// This is similar to the `MutexGuard` struct, except instead of using a reference to unlock the `Mutex` it +/// +/// This is similar to the `MutexGuard` struct, except instead of using a reference to unlock the `Mutex` it /// uses an `Arc`. This has several advantages, most notably that it has an `'static` lifetime. #[cfg(feature = "arc_lock")] #[must_use = "if unused the Mutex will immediately unlock"] @@ -713,7 +713,7 @@ impl ArcMutexGuard { // SAFETY: make sure the Arc gets it reference decremented let mut s = ManuallyDrop::new(s); - unsafe { ptr::drop_in_place(&mut s.mutex) }; + unsafe { ptr::drop_in_place(&mut s.mutex) }; } /// Temporarily unlocks the mutex to execute the given function. diff --git a/src/remutex.rs b/src/remutex.rs index 8493a24..dd992b4 100644 --- a/src/remutex.rs +++ b/src/remutex.rs @@ -401,7 +401,7 @@ impl ReentrantMutex { } /// # Safety - /// + /// /// The lock must be held before calling this method. #[cfg(feature = "arc_lock")] #[inline] @@ -413,7 +413,7 @@ impl ReentrantMutex { } /// Acquires a reentrant mutex through an `Arc`. - /// + /// /// This method is similar to the `lock` method; however, it requires the `ReentrantMutex` to be inside of an /// `Arc` and the resulting mutex guard has no lifetime requirements. #[cfg(feature = "arc_lock")] @@ -425,7 +425,7 @@ impl ReentrantMutex { } /// Attempts to acquire a reentrant mutex through an `Arc`. - /// + /// /// This method is similar to the `try_lock` method; however, it requires the `ReentrantMutex` to be inside /// of an `Arc` and the resulting mutex guard has no lifetime requirements. #[cfg(feature = "arc_lock")] @@ -490,12 +490,15 @@ impl ReentrantMutex { } /// Attempts to acquire this lock until a timeout is reached, through an `Arc`. - /// + /// /// This method is similar to the `try_lock_for` method; however, it requires the `ReentrantMutex` to be /// inside of an `Arc` and the resulting mutex guard has no lifetime requirements. #[cfg(feature = "arc_lock")] #[inline] - pub fn try_lock_arc_for(self: &Arc, timeout: R::Duration) -> Option> { + pub fn try_lock_arc_for( + self: &Arc, + timeout: R::Duration, + ) -> Option> { if self.raw.try_lock_for(timeout) { // SAFETY: locking guarantee is upheld Some(unsafe { self.guard_arc() }) @@ -505,12 +508,15 @@ impl ReentrantMutex { } /// Attempts to acquire this lock until a timeout is reached, through an `Arc`. - /// + /// /// This method is similar to the `try_lock_until` method; however, it requires the `ReentrantMutex` to be /// inside of an `Arc` and the resulting mutex guard has no lifetime requirements. #[cfg(feature = "arc_lock")] #[inline] - pub fn try_lock_arc_until(self: &Arc, timeout: R::Instant) -> Option> { + pub fn try_lock_arc_until( + self: &Arc, + timeout: R::Instant, + ) -> Option> { if self.raw.try_lock_until(timeout) { // SAFETY: locking guarantee is upheld Some(unsafe { self.guard_arc() }) @@ -736,7 +742,6 @@ impl<'a, R: RawMutexFair + 'a, G: GetThreadId + 'a, T: ?Sized + 'a> s.remutex.raw.bump(); } } - } impl<'a, R: RawMutex + 'a, G: GetThreadId + 'a, T: ?Sized + 'a> Deref @@ -783,9 +788,9 @@ unsafe impl<'a, R: RawMutex + 'a, G: GetThreadId + 'a, T: ?Sized + 'a> StableAdd { } -/// An RAII mutex guard returned by the `Arc` locking operations on `ReentrantMutex`. -/// -/// This is similar to the `ReentrantMutexGuard` struct, except instead of using a reference to unlock the +/// An RAII mutex guard returned by the `Arc` locking operations on `ReentrantMutex`. +/// +/// This is similar to the `ReentrantMutexGuard` struct, except instead of using a reference to unlock the /// `Mutex` it uses an `Arc`. This has several advantages, most notably that it has an `'static` /// lifetime. #[cfg(feature = "arc_lock")] @@ -821,9 +826,7 @@ impl ArcReentrantMutexGuard { } #[cfg(feature = "arc_lock")] -impl - ArcReentrantMutexGuard -{ +impl ArcReentrantMutexGuard { /// Unlocks the mutex using a fair unlock protocol. /// /// This is functionally identical to the `unlock_fair` method on [`ReentrantMutexGuard`]. @@ -868,9 +871,7 @@ impl } #[cfg(feature = "arc_lock")] -impl Deref - for ArcReentrantMutexGuard -{ +impl Deref for ArcReentrantMutexGuard { type Target = T; #[inline] fn deref(&self) -> &T { @@ -879,9 +880,7 @@ impl Deref } #[cfg(feature = "arc_lock")] -impl Drop - for ArcReentrantMutexGuard -{ +impl Drop for ArcReentrantMutexGuard { #[inline] fn drop(&mut self) { // Safety: A ReentrantMutexGuard always holds the lock. diff --git a/src/rwlock.rs b/src/rwlock.rs index c404934..9bfa1da 100644 --- a/src/rwlock.rs +++ b/src/rwlock.rs @@ -84,6 +84,18 @@ pub unsafe trait RawRwLock { } !acquired_lock } + + /// Check if this `RwLock` is currently exclusively locked. + fn is_locked_exclusive(&self) -> bool { + let acquired_lock = self.try_lock_shared(); + if acquired_lock { + // Safety: A shared lock was successfully acquired above. + unsafe { + self.unlock_shared(); + } + } + !acquired_lock + } } /// Additional methods for RwLocks which support fair unlocking. @@ -502,6 +514,12 @@ impl RwLock { self.raw.is_locked() } + /// Check if this `RwLock` is currently exclusively locked. + #[inline] + pub fn is_locked_exclusive(&self) -> bool { + self.raw.is_locked_exclusive() + } + /// Forcibly unlocks a read lock. /// /// This is useful when combined with `mem::forget` to hold a lock without @@ -590,7 +608,7 @@ impl RwLock { } /// Locks this `RwLock` with read access, through an `Arc`. - /// + /// /// This method is similar to the `read` method; however, it requires the `RwLock` to be inside of an `Arc` /// and the resulting read guard has no lifetime requirements. #[cfg(feature = "arc_lock")] @@ -602,8 +620,8 @@ impl RwLock { } /// Attempts to lock this `RwLock` with read access, through an `Arc`. - /// - /// This method is similar to the `try_read` method; however, it requires the `RwLock` to be inside of an + /// + /// This method is similar to the `try_read` method; however, it requires the `RwLock` to be inside of an /// `Arc` and the resulting read guard has no lifetime requirements. #[cfg(feature = "arc_lock")] #[inline] @@ -617,7 +635,7 @@ impl RwLock { } /// Locks this `RwLock` with write access, through an `Arc`. - /// + /// /// This method is similar to the `write` method; however, it requires the `RwLock` to be inside of an `Arc` /// and the resulting write guard has no lifetime requirements. #[cfg(feature = "arc_lock")] @@ -629,8 +647,8 @@ impl RwLock { } /// Attempts to lock this `RwLock` with writ access, through an `Arc`. - /// - /// This method is similar to the `try_write` method; however, it requires the `RwLock` to be inside of an + /// + /// This method is similar to the `try_write` method; however, it requires the `RwLock` to be inside of an /// `Arc` and the resulting write guard has no lifetime requirements. #[cfg(feature = "arc_lock")] #[inline] @@ -744,12 +762,15 @@ impl RwLock { } /// Attempts to acquire this `RwLock` with read access until a timeout is reached, through an `Arc`. - /// + /// /// This method is similar to the `try_read_for` method; however, it requires the `RwLock` to be inside of an /// `Arc` and the resulting read guard has no lifetime requirements. #[cfg(feature = "arc_lock")] #[inline] - pub fn try_read_arc_for(self: &Arc, timeout: R::Duration) -> Option> { + pub fn try_read_arc_for( + self: &Arc, + timeout: R::Duration, + ) -> Option> { if self.raw.try_lock_shared_for(timeout) { // SAFETY: locking guarantee is upheld Some(unsafe { self.read_guard_arc() }) @@ -759,12 +780,15 @@ impl RwLock { } /// Attempts to acquire this `RwLock` with read access until a timeout is reached, through an `Arc`. - /// + /// /// This method is similar to the `try_read_until` method; however, it requires the `RwLock` to be inside of /// an `Arc` and the resulting read guard has no lifetime requirements. #[cfg(feature = "arc_lock")] #[inline] - pub fn try_read_arc_until(self: &Arc, timeout: R::Instant) -> Option> { + pub fn try_read_arc_until( + self: &Arc, + timeout: R::Instant, + ) -> Option> { if self.raw.try_lock_shared_until(timeout) { // SAFETY: locking guarantee is upheld Some(unsafe { self.read_guard_arc() }) @@ -774,12 +798,15 @@ impl RwLock { } /// Attempts to acquire this `RwLock` with write access until a timeout is reached, through an `Arc`. - /// + /// /// This method is similar to the `try_write_for` method; however, it requires the `RwLock` to be inside of /// an `Arc` and the resulting write guard has no lifetime requirements. #[cfg(feature = "arc_lock")] #[inline] - pub fn try_write_arc_for(self: &Arc, timeout: R::Duration) -> Option> { + pub fn try_write_arc_for( + self: &Arc, + timeout: R::Duration, + ) -> Option> { if self.raw.try_lock_exclusive_for(timeout) { // SAFETY: locking guarantee is upheld Some(unsafe { self.write_guard_arc() }) @@ -789,12 +816,15 @@ impl RwLock { } /// Attempts to acquire this `RwLock` with read access until a timeout is reached, through an `Arc`. - /// + /// /// This method is similar to the `try_write_until` method; however, it requires the `RwLock` to be inside of /// an `Arc` and the resulting read guard has no lifetime requirements. #[cfg(feature = "arc_lock")] #[inline] - pub fn try_write_arc_until(self: &Arc, timeout: R::Instant) -> Option> { + pub fn try_write_arc_until( + self: &Arc, + timeout: R::Instant, + ) -> Option> { if self.raw.try_lock_exclusive_until(timeout) { // SAFETY: locking guarantee is upheld Some(unsafe { self.write_guard_arc() }) @@ -848,7 +878,7 @@ impl RwLock { } /// Locks this `RwLock` with shared read access, through an `Arc`. - /// + /// /// This method is similar to the `read_recursive` method; however, it requires the `RwLock` to be inside of /// an `Arc` and the resulting read guard has no lifetime requirements. #[cfg(feature = "arc_lock")] @@ -860,7 +890,7 @@ impl RwLock { } /// Attempts to lock this `RwLock` with shared read access, through an `Arc`. - /// + /// /// This method is similar to the `try_read_recursive` method; however, it requires the `RwLock` to be inside /// of an `Arc` and the resulting read guard has no lifetime requirements. #[cfg(feature = "arc_lock")] @@ -924,7 +954,10 @@ impl RwLock { /// inside of an `Arc` and the resulting read guard has no lifetime requirements. #[cfg(feature = "arc_lock")] #[inline] - pub fn try_read_arc_recursive_for(self: &Arc, timeout: R::Duration) -> Option> { + pub fn try_read_arc_recursive_for( + self: &Arc, + timeout: R::Duration, + ) -> Option> { if self.raw.try_lock_shared_recursive_for(timeout) { // SAFETY: locking guarantee is upheld Some(unsafe { self.read_guard_arc() }) @@ -939,7 +972,10 @@ impl RwLock { /// inside of an `Arc` and the resulting read guard has no lifetime requirements. #[cfg(feature = "arc_lock")] #[inline] - pub fn try_read_arc_recursive_until(self: &Arc, timeout: R::Instant) -> Option> { + pub fn try_read_arc_recursive_until( + self: &Arc, + timeout: R::Instant, + ) -> Option> { if self.raw.try_lock_shared_recursive_until(timeout) { // SAFETY: locking guarantee is upheld Some(unsafe { self.read_guard_arc() }) @@ -995,19 +1031,19 @@ impl RwLock { } /// # Safety - /// + /// /// The lock must be held when calling this method. #[cfg(feature = "arc_lock")] #[inline] unsafe fn upgradable_guard_arc(self: &Arc) -> ArcRwLockUpgradableReadGuard { ArcRwLockUpgradableReadGuard { rwlock: self.clone(), - marker: PhantomData + marker: PhantomData, } } /// Locks this `RwLock` with upgradable read access, through an `Arc`. - /// + /// /// This method is similar to the `upgradable_read` method; however, it requires the `RwLock` to be /// inside of an `Arc` and the resulting read guard has no lifetime requirements. #[cfg(feature = "arc_lock")] @@ -1019,7 +1055,7 @@ impl RwLock { } /// Attempts to lock this `RwLock` with upgradable read access, through an `Arc`. - /// + /// /// This method is similar to the `try_upgradable_read` method; however, it requires the `RwLock` to be /// inside of an `Arc` and the resulting read guard has no lifetime requirements. #[cfg(feature = "arc_lock")] @@ -1074,7 +1110,7 @@ impl RwLock { } /// Attempts to lock this `RwLock` with upgradable access until a timeout is reached, through an `Arc`. - /// + /// /// This method is similar to the `try_upgradable_read_for` method; however, it requires the `RwLock` to be /// inside of an `Arc` and the resulting read guard has no lifetime requirements. #[cfg(feature = "arc_lock")] @@ -1092,7 +1128,7 @@ impl RwLock { } /// Attempts to lock this `RwLock` with upgradable access until a timeout is reached, through an `Arc`. - /// + /// /// This method is similar to the `try_upgradable_read_until` method; however, it requires the `RwLock` to be /// inside of an `Arc` and the resulting read guard has no lifetime requirements. #[cfg(feature = "arc_lock")] @@ -1318,9 +1354,9 @@ impl<'a, R: RawRwLock + 'a, T: fmt::Display + ?Sized + 'a> fmt::Display #[cfg(feature = "owning_ref")] unsafe impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> StableAddress for RwLockReadGuard<'a, R, T> {} -/// An RAII rwlock guard returned by the `Arc` locking operations on `RwLock`. -/// -/// This is similar to the `RwLockReadGuard` struct, except instead of using a reference to unlock the `RwLock` +/// An RAII rwlock guard returned by the `Arc` locking operations on `RwLock`. +/// +/// This is similar to the `RwLockReadGuard` struct, except instead of using a reference to unlock the `RwLock` /// it uses an `Arc`. This has several advantages, most notably that it has an `'static` lifetime. #[cfg(feature = "arc_lock")] #[must_use = "if unused the RwLock will immediately unlock"] @@ -1426,9 +1462,7 @@ impl fmt::Debug for ArcRwLockReadGuard fmt::Display - for ArcRwLockReadGuard -{ +impl fmt::Display for ArcRwLockReadGuard { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { (**self).fmt(f) } @@ -1655,8 +1689,8 @@ impl<'a, R: RawRwLock + 'a, T: fmt::Display + ?Sized + 'a> fmt::Display #[cfg(feature = "owning_ref")] unsafe impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> StableAddress for RwLockWriteGuard<'a, R, T> {} -/// An RAII rwlock guard returned by the `Arc` locking operations on `RwLock`. -/// This is similar to the `RwLockWriteGuard` struct, except instead of using a reference to unlock the `RwLock` +/// An RAII rwlock guard returned by the `Arc` locking operations on `RwLock`. +/// This is similar to the `RwLockWriteGuard` struct, except instead of using a reference to unlock the `RwLock` /// it uses an `Arc`. This has several advantages, most notably that it has an `'static` lifetime. #[cfg(feature = "arc_lock")] #[must_use = "if unused the RwLock will immediately unlock"] @@ -1770,7 +1804,7 @@ impl ArcRwLockWriteGuard { /// Temporarily yields the `RwLock` to a waiting thread if there is one. /// - /// This method is functionally equivalent to the `bump` method on [`RwLockWriteGuard`]. + /// This method is functionally equivalent to the `bump` method on [`RwLockWriteGuard`]. #[inline] pub fn bump(s: &mut Self) { // Safety: An RwLockWriteGuard always holds an exclusive lock. @@ -1816,9 +1850,7 @@ impl fmt::Debug for ArcRwLockWriteGuard fmt::Display - for ArcRwLockWriteGuard -{ +impl fmt::Display for ArcRwLockWriteGuard { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { (**self).fmt(f) } @@ -2059,7 +2091,7 @@ unsafe impl<'a, R: RawRwLockUpgrade + 'a, T: ?Sized + 'a> StableAddress /// An RAII rwlock guard returned by the `Arc` locking operations on `RwLock`. /// This is similar to the `RwLockUpgradableReadGuard` struct, except instead of using a reference to unlock the -/// `RwLock` it uses an `Arc`. This has several advantages, most notably that it has an `'static` +/// `RwLock` it uses an `Arc`. This has several advantages, most notably that it has an `'static` /// lifetime. #[cfg(feature = "arc_lock")] #[must_use = "if unused the RwLock will immediately unlock"] @@ -2069,7 +2101,7 @@ pub struct ArcRwLockUpgradableReadGuard { } #[cfg(feature = "arc_lock")] -impl ArcRwLockUpgradableReadGuard { +impl ArcRwLockUpgradableReadGuard { /// Returns a reference to the rwlock, contained in its original `Arc`. pub fn rwlock(s: &Self) -> &Arc> { &s.rwlock @@ -2144,7 +2176,7 @@ impl ArcRwLockUpgradableReadGuard { // SAFETY: make sure we decrement the refcount properly let mut s = ManuallyDrop::new(s); - unsafe { ptr::drop_in_place(&mut s.rwlock) }; + unsafe { ptr::drop_in_place(&mut s.rwlock) }; } /// Temporarily unlocks the `RwLock` to execute the given function. @@ -2291,7 +2323,6 @@ impl fmt::Display } } - /// An RAII read lock guard returned by `RwLockReadGuard::map`, which can point to a /// subfield of the protected data. /// -- cgit v1.2.3