X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fsync%2Fmod.rs;h=348bd90274ae1632b1cec63cf98e9249d9ac7425;hb=7c9463668a4f71663746e57bdb09ee6b91797d5a;hp=50ef40e295f50d0a0d9095a0f3dfe4c73916a340;hpb=d0b8f455fe86d3e55231d35d35dd96693abe2049;p=rust-lightning diff --git a/lightning/src/sync/mod.rs b/lightning/src/sync/mod.rs index 50ef40e2..348bd902 100644 --- a/lightning/src/sync/mod.rs +++ b/lightning/src/sync/mod.rs @@ -3,37 +3,52 @@ pub(crate) enum LockHeldState { HeldByThread, NotHeldByThread, - #[cfg(any(feature = "_bench_unstable", not(test)))] + #[cfg(any(ldk_bench, not(test)))] Unsupported, } -pub(crate) trait LockTestExt { +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))] +#[cfg(all(feature = "std", not(ldk_bench), test))] mod debug_sync; -#[cfg(all(feature = "std", not(feature = "_bench_unstable"), test))] +#[cfg(all(feature = "std", not(ldk_bench), test))] pub use debug_sync::*; -#[cfg(all(feature = "std", not(feature = "_bench_unstable"), test))] +#[cfg(all(feature = "std", not(ldk_bench), test))] // Note that to make debug_sync's regex work this must not contain `debug_string` in the module name mod test_lockorder_checks; -#[cfg(all(feature = "std", any(feature = "_bench_unstable", not(test))))] +#[cfg(all(feature = "std", any(ldk_bench, not(test))))] pub(crate) mod fairrwlock; -#[cfg(all(feature = "std", any(feature = "_bench_unstable", not(test))))] +#[cfg(all(feature = "std", any(ldk_bench, 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))))] +#[cfg(all(feature = "std", any(ldk_bench, not(test))))] mod ext_impl { use super::*; - impl LockTestExt for Mutex { + 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 LockTestExt for RwLock { + 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() } } }