X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fsync%2Fmod.rs;h=1b2b9a739b8c5d3078204917f55b0fef86e87f13;hb=3acf7e2c9d4896ddfb48e883023cb76f1c4de9a0;hp=f7226a5fa34eef114d27c6569207dbc68354b950;hpb=230331f3e8efe9cee0ad2dc1644051bd9679a01f;p=rust-lightning diff --git a/lightning/src/sync/mod.rs b/lightning/src/sync/mod.rs index f7226a5f..1b2b9a73 100644 --- a/lightning/src/sync/mod.rs +++ b/lightning/src/sync/mod.rs @@ -1,3 +1,25 @@ +#[allow(dead_code)] // Depending on the compilation flags some variants are never used +#[derive(Debug, PartialEq, Eq)] +pub(crate) enum LockHeldState { + HeldByThread, + NotHeldByThread, + #[cfg(any(feature = "_bench_unstable", not(test)))] + Unsupported, +} + +pub(crate) trait LockTestExt<'a> { + fn held_by_thread(&self) -> LockHeldState; + type ExclLock; + /// If two instances of the same mutex are being taken at the same time, it's very easy to have + /// a lockorder inversion and risk deadlock. Thus, we default to disabling such locks. + /// + /// However, sometimes they cannot be avoided. In such cases, this method exists to take a + /// mutex while avoiding a test failure. It is deliberately verbose and includes the term + /// "unsafe" to indicate that special care needs to be taken to ensure no deadlocks are + /// possible. + fn unsafe_well_ordered_double_lock_self(&'a self) -> Self::ExclLock; +} + #[cfg(all(feature = "std", not(feature = "_bench_unstable"), test))] mod debug_sync; #[cfg(all(feature = "std", not(feature = "_bench_unstable"), test))] @@ -7,9 +29,28 @@ pub use debug_sync::*; mod test_lockorder_checks; #[cfg(all(feature = "std", any(feature = "_bench_unstable", not(test))))] -pub use ::std::sync::{Arc, Mutex, Condvar, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}; +pub(crate) mod fairrwlock; +#[cfg(all(feature = "std", any(feature = "_bench_unstable", not(test))))] +pub use {std::sync::{Arc, Mutex, Condvar, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}, fairrwlock::FairRwLock}; + #[cfg(all(feature = "std", any(feature = "_bench_unstable", not(test))))] -pub use crate::util::fairrwlock::FairRwLock; +mod ext_impl { + use super::*; + impl<'a, T: 'a> LockTestExt<'a> for Mutex { + #[inline] + fn held_by_thread(&self) -> LockHeldState { LockHeldState::Unsupported } + type ExclLock = MutexGuard<'a, T>; + #[inline] + fn unsafe_well_ordered_double_lock_self(&'a self) -> MutexGuard { self.lock().unwrap() } + } + impl<'a, T: 'a> LockTestExt<'a> for RwLock { + #[inline] + fn held_by_thread(&self) -> LockHeldState { LockHeldState::Unsupported } + type ExclLock = RwLockWriteGuard<'a, T>; + #[inline] + fn unsafe_well_ordered_double_lock_self(&'a self) -> RwLockWriteGuard { self.write().unwrap() } + } +} #[cfg(not(feature = "std"))] mod nostd_sync;