X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fsync%2Fdebug_sync.rs;h=5968a79ee4daa50439a3cac96478c49d3803458b;hb=a27088d6aa00da10ad3191f1b02acb4ec3d6d22e;hp=11557be82afec418be0abce3b38117ce04582fd0;hpb=783e8188a7bd6470d5926bf5d1bae1350996801a;p=rust-lightning diff --git a/lightning/src/sync/debug_sync.rs b/lightning/src/sync/debug_sync.rs index 11557be8..5968a79e 100644 --- a/lightning/src/sync/debug_sync.rs +++ b/lightning/src/sync/debug_sync.rs @@ -12,7 +12,9 @@ use std::sync::RwLockReadGuard as StdRwLockReadGuard; use std::sync::RwLockWriteGuard as StdRwLockWriteGuard; use std::sync::Condvar as StdCondvar; -use crate::prelude::HashMap; +pub use std::sync::WaitTimeoutResult; + +use crate::prelude::*; use super::{LockTestExt, LockHeldState}; @@ -35,15 +37,19 @@ impl Condvar { Condvar { inner: StdCondvar::new() } } - pub fn wait<'a, T>(&'a self, guard: MutexGuard<'a, T>) -> 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(guard.into_inner()).map(|lock| MutexGuard { mutex, lock }).map_err(|_| ()) + self.inner.wait_while(guard.into_inner(), condition).map(|lock| MutexGuard { mutex, lock }) + .map_err(|_| ()) } #[allow(unused)] - pub fn wait_timeout<'a, T>(&'a self, guard: MutexGuard<'a, T>, dur: Duration) -> LockResult<(MutexGuard<'a, T>, ())> { + 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(guard.into_inner(), dur).map(|(lock, _)| (MutexGuard { mutex, lock }, ())).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(); } @@ -51,7 +57,7 @@ impl Condvar { thread_local! { /// We track the set of locks currently held by a reference to their `LockMetadata` - static LOCKS_HELD: RefCell>> = RefCell::new(HashMap::new()); + static LOCKS_HELD: RefCell>> = RefCell::new(new_hash_map()); } static LOCK_IDX: AtomicUsize = AtomicUsize::new(0); @@ -97,7 +103,9 @@ fn locate_call_symbol(backtrace: &Backtrace) -> (String, Option) { } } } - let symbol = symbol_after_latest_debug_sync.expect("Couldn't find lock call symbol"); + 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()) } @@ -107,7 +115,7 @@ impl LockMetadata { let lock_idx = LOCK_IDX.fetch_add(1, Ordering::Relaxed) as u64; let res = Arc::new(LockMetadata { - locked_before: StdMutex::new(HashMap::new()), + locked_before: StdMutex::new(new_hash_map()), lock_idx, _lock_construction_bt: backtrace, }); @@ -116,7 +124,7 @@ 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(HashMap::new())); } }); + 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) => {