diff --git a/lightning/src/sync/debug_sync.rs b/lightning/src/sync/debug_sync.rs index 5968a79ee4d..776e35e8ce0 100644 --- a/lightning/src/sync/debug_sync.rs +++ b/lightning/src/sync/debug_sync.rs @@ -1,30 +1,34 @@ -pub use ::alloc::sync::Arc; +pub use alloc::sync::Arc; use core::ops::{Deref, DerefMut}; use core::time::Duration; use std::cell::RefCell; use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::Condvar as StdCondvar; use std::sync::Mutex as StdMutex; use std::sync::MutexGuard as StdMutexGuard; use std::sync::RwLock as StdRwLock; use std::sync::RwLockReadGuard as StdRwLockReadGuard; use std::sync::RwLockWriteGuard as StdRwLockWriteGuard; -use std::sync::Condvar as StdCondvar; pub use std::sync::WaitTimeoutResult; use crate::prelude::*; -use super::{LockTestExt, LockHeldState}; +use super::{LockHeldState, LockTestExt}; #[cfg(feature = "backtrace")] use {crate::prelude::hash_map, backtrace::Backtrace, std::sync::Once}; #[cfg(not(feature = "backtrace"))] -struct Backtrace{} +struct Backtrace {} #[cfg(not(feature = "backtrace"))] -impl Backtrace { fn new() -> Backtrace { Backtrace {} } } +impl Backtrace { + fn new() -> Backtrace { + Backtrace {} + } +} pub type LockResult = Result; @@ -37,22 +41,30 @@ impl Condvar { Condvar { inner: StdCondvar::new() } } - pub fn wait_while<'a, T, F: FnMut(&mut T) -> bool>(&'a self, guard: MutexGuard<'a, T>, condition: F) - -> LockResult> { + pub fn wait_while<'a, T, F: FnMut(&mut T) -> bool>( + &'a self, guard: MutexGuard<'a, T>, condition: F, + ) -> LockResult> { let mutex: &'a Mutex = guard.mutex; - self.inner.wait_while(guard.into_inner(), condition).map(|lock| MutexGuard { mutex, lock }) + self.inner + .wait_while(guard.into_inner(), condition) + .map(|lock| MutexGuard { mutex, lock }) .map_err(|_| ()) } #[allow(unused)] - pub fn wait_timeout_while<'a, T, F: FnMut(&mut T) -> bool>(&'a self, guard: MutexGuard<'a, T>, dur: Duration, condition: F) - -> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> { + pub fn wait_timeout_while<'a, T, F: FnMut(&mut T) -> bool>( + &'a self, guard: MutexGuard<'a, T>, dur: Duration, condition: F, + ) -> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> { let mutex = guard.mutex; - self.inner.wait_timeout_while(guard.into_inner(), dur, condition).map_err(|_| ()) + self.inner + .wait_timeout_while(guard.into_inner(), dur, condition) + .map_err(|_| ()) .map(|(lock, e)| (MutexGuard { mutex, lock }, e)) } - pub fn notify_all(&self) { self.inner.notify_all(); } + pub fn notify_all(&self) { + self.inner.notify_all(); + } } thread_local! { @@ -99,14 +111,19 @@ fn locate_call_symbol(backtrace: &Backtrace) -> (String, Option) { symbol_after_latest_debug_sync = Some(symbol); found_debug_sync = false; } - } else { found_debug_sync = true; } + } else { + found_debug_sync = true; + } } } } let symbol = symbol_after_latest_debug_sync.unwrap_or_else(|| { panic!("Couldn't find lock call symbol in trace {:?}", backtrace); }); - (format!("{}:{}", symbol.filename().unwrap().display(), symbol.lineno().unwrap()), symbol.colno()) + ( + format!("{}:{}", symbol.filename().unwrap().display(), symbol.lineno().unwrap()), + symbol.colno(), + ) } impl LockMetadata { @@ -124,16 +141,20 @@ impl LockMetadata { { let (lock_constr_location, lock_constr_colno) = locate_call_symbol(&res._lock_construction_bt); - LOCKS_INIT.call_once(|| { unsafe { LOCKS = Some(StdMutex::new(new_hash_map())); } }); + LOCKS_INIT.call_once(|| unsafe { + LOCKS = Some(StdMutex::new(new_hash_map())); + }); let mut locks = unsafe { LOCKS.as_ref() }.unwrap().lock().unwrap(); match locks.entry(lock_constr_location) { hash_map::Entry::Occupied(e) => { assert_eq!(lock_constr_colno, locate_call_symbol(&e.get()._lock_construction_bt).1, "Because Windows doesn't support column number results in backtraces, we cannot construct two mutexes on the same line or we risk lockorder detection false positives."); - return Arc::clone(e.get()) + return Arc::clone(e.get()); + }, + hash_map::Entry::Vacant(e) => { + e.insert(Arc::clone(&res)); }, - hash_map::Entry::Vacant(e) => { e.insert(Arc::clone(&res)); }, } } res @@ -213,7 +234,8 @@ impl LockMetadata { let mut locked_before = this.locked_before.lock().unwrap(); for (locked_idx, locked) in held.borrow().iter() { if !locked_before.contains_key(locked_idx) { - let lockdep = LockDep { lock: Arc::clone(locked), _lockdep_trace: Backtrace::new() }; + let lockdep = + LockDep { lock: Arc::clone(locked), _lockdep_trace: Backtrace::new() }; locked_before.insert(*locked_idx, lockdep); } } @@ -282,7 +304,8 @@ impl Mutex { } pub fn try_lock<'a>(&'a self) -> LockResult> { - let res = self.inner.try_lock().map(|lock| MutexGuard { mutex: self, lock }).map_err(|_| ()); + let res = + self.inner.try_lock().map(|lock| MutexGuard { mutex: self, lock }).map_err(|_| ()); if res.is_ok() { LockMetadata::try_locked(&self.deps); } @@ -376,7 +399,11 @@ impl RwLock { } pub fn try_write<'a>(&'a self) -> LockResult> { - let res = self.inner.try_write().map(|guard| RwLockWriteGuard { lock: self, guard }).map_err(|_| ()); + let res = self + .inner + .try_write() + .map(|guard| RwLockWriteGuard { lock: self, guard }) + .map_err(|_| ()); if res.is_ok() { LockMetadata::try_locked(&self.deps); } diff --git a/lightning/src/sync/fairrwlock.rs b/lightning/src/sync/fairrwlock.rs index 23b8c23db28..d97370090cd 100644 --- a/lightning/src/sync/fairrwlock.rs +++ b/lightning/src/sync/fairrwlock.rs @@ -1,6 +1,6 @@ -use std::sync::{LockResult, RwLock, RwLockReadGuard, RwLockWriteGuard, TryLockResult}; -use std::sync::atomic::{AtomicUsize, Ordering}; use super::{LockHeldState, LockTestExt}; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::{LockResult, RwLock, RwLockReadGuard, RwLockWriteGuard, TryLockResult}; /// Rust libstd's RwLock does not provide any fairness guarantees (and, in fact, when used on /// Linux with pthreads under the hood, readers trivially and completely starve writers). diff --git a/lightning/src/sync/mod.rs b/lightning/src/sync/mod.rs index 348bd90274a..0d0a76e3f2e 100644 --- a/lightning/src/sync/mod.rs +++ b/lightning/src/sync/mod.rs @@ -31,24 +31,35 @@ mod test_lockorder_checks; #[cfg(all(feature = "std", any(ldk_bench, not(test))))] pub(crate) mod fairrwlock; #[cfg(all(feature = "std", any(ldk_bench, not(test))))] -pub use {std::sync::{Arc, Mutex, Condvar, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}, fairrwlock::FairRwLock}; +pub use { + fairrwlock::FairRwLock, + std::sync::{Arc, Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}, +}; #[cfg(all(feature = "std", any(ldk_bench, not(test))))] mod ext_impl { use super::*; impl<'a, T: 'a> LockTestExt<'a> for Mutex { #[inline] - fn held_by_thread(&self) -> LockHeldState { LockHeldState::Unsupported } + 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() } + 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 } + 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() } + fn unsafe_well_ordered_double_lock_self(&'a self) -> RwLockWriteGuard { + self.write().unwrap() + } } } diff --git a/lightning/src/sync/nostd_sync.rs b/lightning/src/sync/nostd_sync.rs index 0f92bd6caa3..03fa65b69c6 100644 --- a/lightning/src/sync/nostd_sync.rs +++ b/lightning/src/sync/nostd_sync.rs @@ -1,12 +1,12 @@ -pub use ::alloc::sync::Arc; +use super::{LockHeldState, LockTestExt}; +pub use alloc::sync::Arc; +use core::cell::{Ref, RefCell, RefMut}; use core::ops::{Deref, DerefMut}; -use core::cell::{RefCell, Ref, RefMut}; -use super::{LockTestExt, LockHeldState}; pub type LockResult = Result; pub struct Mutex { - inner: RefCell + inner: RefCell, } #[must_use = "if unused the Mutex will immediately unlock"] @@ -45,16 +45,21 @@ impl Mutex { impl<'a, T: 'a> LockTestExt<'a> for Mutex { #[inline] fn held_by_thread(&self) -> LockHeldState { - if self.inner.try_borrow_mut().is_err() { return LockHeldState::HeldByThread; } - else { return LockHeldState::NotHeldByThread; } + if self.inner.try_borrow_mut().is_err() { + return LockHeldState::HeldByThread; + } else { + return LockHeldState::NotHeldByThread; + } } type ExclLock = MutexGuard<'a, T>; #[inline] - fn unsafe_well_ordered_double_lock_self(&'a self) -> MutexGuard { self.lock().unwrap() } + fn unsafe_well_ordered_double_lock_self(&'a self) -> MutexGuard { + self.lock().unwrap() + } } pub struct RwLock { - inner: RefCell + inner: RefCell, } pub struct RwLockReadGuard<'a, T: ?Sized + 'a> { @@ -103,7 +108,7 @@ impl RwLock { pub fn try_write<'a>(&'a self) -> LockResult> { match self.inner.try_borrow_mut() { Ok(lock) => Ok(RwLockWriteGuard { lock }), - Err(_) => Err(()) + Err(_) => Err(()), } } } @@ -111,12 +116,17 @@ impl RwLock { impl<'a, T: 'a> LockTestExt<'a> for RwLock { #[inline] fn held_by_thread(&self) -> LockHeldState { - if self.inner.try_borrow_mut().is_err() { return LockHeldState::HeldByThread; } - else { return LockHeldState::NotHeldByThread; } + if self.inner.try_borrow_mut().is_err() { + return LockHeldState::HeldByThread; + } else { + return LockHeldState::NotHeldByThread; + } } type ExclLock = RwLockWriteGuard<'a, T>; #[inline] - fn unsafe_well_ordered_double_lock_self(&'a self) -> RwLockWriteGuard { self.write().unwrap() } + fn unsafe_well_ordered_double_lock_self(&'a self) -> RwLockWriteGuard { + self.write().unwrap() + } } pub type FairRwLock = RwLock; diff --git a/lightning/src/sync/test_lockorder_checks.rs b/lightning/src/sync/test_lockorder_checks.rs index 96e497d4439..91826342f8a 100644 --- a/lightning/src/sync/test_lockorder_checks.rs +++ b/lightning/src/sync/test_lockorder_checks.rs @@ -1,4 +1,4 @@ -use crate::sync::debug_sync::{RwLock, Mutex}; +use crate::sync::debug_sync::{Mutex, RwLock}; use super::{LockHeldState, LockTestExt}; diff --git a/rustfmt_excluded_files b/rustfmt_excluded_files index d3c5f934a16..ef7fa85e913 100644 --- a/rustfmt_excluded_files +++ b/rustfmt_excluded_files @@ -72,11 +72,6 @@ ./lightning/src/routing/scoring.rs ./lightning/src/routing/test_utils.rs ./lightning/src/routing/utxo.rs -./lightning/src/sync/debug_sync.rs -./lightning/src/sync/fairrwlock.rs -./lightning/src/sync/mod.rs -./lightning/src/sync/nostd_sync.rs -./lightning/src/sync/test_lockorder_checks.rs ./lightning/src/util/atomic_counter.rs ./lightning/src/util/base32.rs ./lightning/src/util/byte_utils.rs