From 9962e4b4971421fde6017fa3fc2af6ec3ffae39d Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Tue, 17 Sep 2024 13:03:25 +0200 Subject: [PATCH 01/10] `rustfmt`: Run on `sync/debug_sync.rs` --- lightning/src/sync/debug_sync.rs | 67 ++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 20 deletions(-) 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); } From edcdc565c4a569f2b7f88d1fb8fd645dfd780de8 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Tue, 17 Sep 2024 13:03:59 +0200 Subject: [PATCH 02/10] `rustfmt`: Drop `sync/debug_sync.rs` from exclusion list --- rustfmt_excluded_files | 1 - 1 file changed, 1 deletion(-) diff --git a/rustfmt_excluded_files b/rustfmt_excluded_files index d3c5f934a16..c0631296884 100644 --- a/rustfmt_excluded_files +++ b/rustfmt_excluded_files @@ -72,7 +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 From 2cb1664cf6141a6d8492f58892badbdee6cae4c3 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Tue, 17 Sep 2024 13:04:35 +0200 Subject: [PATCH 03/10] `rustfmt`: Run on `sync/fairrwlock.rs` --- lightning/src/sync/fairrwlock.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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). From a161834f9013d4eb8da2c75b79d3414dc0c99893 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Tue, 17 Sep 2024 13:04:55 +0200 Subject: [PATCH 04/10] `rustfmt`: Drop `sync/fairrwlock.rs` from exclusion list --- rustfmt_excluded_files | 1 - 1 file changed, 1 deletion(-) diff --git a/rustfmt_excluded_files b/rustfmt_excluded_files index c0631296884..053de00fefd 100644 --- a/rustfmt_excluded_files +++ b/rustfmt_excluded_files @@ -72,7 +72,6 @@ ./lightning/src/routing/scoring.rs ./lightning/src/routing/test_utils.rs ./lightning/src/routing/utxo.rs -./lightning/src/sync/fairrwlock.rs ./lightning/src/sync/mod.rs ./lightning/src/sync/nostd_sync.rs ./lightning/src/sync/test_lockorder_checks.rs From e3fb566e0581261b72d9a27ce51f713ec7734962 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Tue, 17 Sep 2024 13:05:40 +0200 Subject: [PATCH 05/10] `rustfmt`: Run on `sync/nostd_sync.rs` --- lightning/src/sync/nostd_sync.rs | 34 +++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 12 deletions(-) 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; From cffbeb09b95ea79e28e43bc9d3f54ba609bb120a Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Tue, 17 Sep 2024 13:06:00 +0200 Subject: [PATCH 06/10] `rustfmt`: Drop `sync/nostd_sync.rs` from exclusion list --- rustfmt_excluded_files | 1 - 1 file changed, 1 deletion(-) diff --git a/rustfmt_excluded_files b/rustfmt_excluded_files index 053de00fefd..c9733a31e62 100644 --- a/rustfmt_excluded_files +++ b/rustfmt_excluded_files @@ -73,7 +73,6 @@ ./lightning/src/routing/test_utils.rs ./lightning/src/routing/utxo.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 From 6f1949b0d68b09f277e2409fc6cd03f2ceb259c8 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Tue, 17 Sep 2024 13:06:29 +0200 Subject: [PATCH 07/10] `rustfmt`: Run on `sync/test_lockorder_checks.rs` --- lightning/src/sync/test_lockorder_checks.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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}; From a3de52f847c3ae367aeb36c0790965146cf18b51 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Tue, 17 Sep 2024 13:06:51 +0200 Subject: [PATCH 08/10] `rustfmt`: Drop `sync/test_lockorder_checks.rs` from exclusion list --- rustfmt_excluded_files | 1 - 1 file changed, 1 deletion(-) diff --git a/rustfmt_excluded_files b/rustfmt_excluded_files index c9733a31e62..d3f717d762a 100644 --- a/rustfmt_excluded_files +++ b/rustfmt_excluded_files @@ -73,7 +73,6 @@ ./lightning/src/routing/test_utils.rs ./lightning/src/routing/utxo.rs ./lightning/src/sync/mod.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 From 8746a809774355a743358b8b705cfada1afe1a4e Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Tue, 17 Sep 2024 13:07:28 +0200 Subject: [PATCH 09/10] `rustfmt`: Run on `sync/mod.rs` --- lightning/src/sync/mod.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) 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() + } } } From 555bd7536fd7baac10a745f76897b694a127f993 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Tue, 17 Sep 2024 13:07:52 +0200 Subject: [PATCH 10/10] `rustfmt`: Drop `sync/mod.rs` from exclusion list --- rustfmt_excluded_files | 1 - 1 file changed, 1 deletion(-) diff --git a/rustfmt_excluded_files b/rustfmt_excluded_files index d3f717d762a..ef7fa85e913 100644 --- a/rustfmt_excluded_files +++ b/rustfmt_excluded_files @@ -72,7 +72,6 @@ ./lightning/src/routing/scoring.rs ./lightning/src/routing/test_utils.rs ./lightning/src/routing/utxo.rs -./lightning/src/sync/mod.rs ./lightning/src/util/atomic_counter.rs ./lightning/src/util/base32.rs ./lightning/src/util/byte_utils.rs