1 pub use ::alloc::sync::Arc;
2 use core::ops::{Deref, DerefMut};
3 use core::time::Duration;
5 use std::cell::RefCell;
7 use std::sync::atomic::{AtomicUsize, Ordering};
8 use std::sync::Mutex as StdMutex;
9 use std::sync::MutexGuard as StdMutexGuard;
10 use std::sync::RwLock as StdRwLock;
11 use std::sync::RwLockReadGuard as StdRwLockReadGuard;
12 use std::sync::RwLockWriteGuard as StdRwLockWriteGuard;
13 use std::sync::Condvar as StdCondvar;
15 use crate::prelude::HashMap;
17 use super::{LockTestExt, LockHeldState};
19 #[cfg(feature = "backtrace")]
20 use {crate::prelude::hash_map, backtrace::Backtrace, std::sync::Once};
22 #[cfg(not(feature = "backtrace"))]
24 #[cfg(not(feature = "backtrace"))]
25 impl Backtrace { fn new() -> Backtrace { Backtrace {} } }
27 pub type LockResult<Guard> = Result<Guard, ()>;
34 pub fn new() -> Condvar {
35 Condvar { inner: StdCondvar::new() }
38 pub fn wait<'a, T>(&'a self, guard: MutexGuard<'a, T>) -> LockResult<MutexGuard<'a, T>> {
39 let mutex: &'a Mutex<T> = guard.mutex;
40 self.inner.wait(guard.into_inner()).map(|lock| MutexGuard { mutex, lock }).map_err(|_| ())
44 pub fn wait_timeout<'a, T>(&'a self, guard: MutexGuard<'a, T>, dur: Duration) -> LockResult<(MutexGuard<'a, T>, ())> {
45 let mutex = guard.mutex;
46 self.inner.wait_timeout(guard.into_inner(), dur).map(|(lock, _)| (MutexGuard { mutex, lock }, ())).map_err(|_| ())
49 pub fn notify_all(&self) { self.inner.notify_all(); }
53 /// We track the set of locks currently held by a reference to their `LockMetadata`
54 static LOCKS_HELD: RefCell<HashMap<u64, Arc<LockMetadata>>> = RefCell::new(HashMap::new());
56 static LOCK_IDX: AtomicUsize = AtomicUsize::new(0);
58 #[cfg(feature = "backtrace")]
59 static mut LOCKS: Option<StdMutex<HashMap<String, Arc<LockMetadata>>>> = None;
60 #[cfg(feature = "backtrace")]
61 static LOCKS_INIT: Once = Once::new();
63 /// Metadata about a single lock, by id, the set of things locked-before it, and the backtrace of
64 /// when the Mutex itself was constructed.
67 locked_before: StdMutex<HashMap<u64, LockDep>>,
68 _lock_construction_bt: Backtrace,
72 lock: Arc<LockMetadata>,
73 /// lockdep_trace is unused unless we're building with `backtrace`, so we mark it _
74 _lockdep_trace: Backtrace,
77 // Locates the frame preceding the earliest `debug_sync` frame in the call stack. This ensures we
78 // can properly detect a lock's construction and acquiral callsites, since the latter may contain
79 // multiple `debug_sync` frames.
80 #[cfg(feature = "backtrace")]
81 fn locate_call_symbol(backtrace: &Backtrace) -> (String, Option<u32>) {
82 // Find the earliest `debug_sync` frame (or that is in our tests) and use the frame preceding it
83 // as the callsite. Note that the first few frames may be in the `backtrace` crate, so we have
85 let sync_mutex_constr_regex = regex::Regex::new(r"lightning.*debug_sync").unwrap();
86 let mut found_debug_sync = false;
87 let mut symbol_after_latest_debug_sync = None;
88 for frame in backtrace.frames().iter() {
89 for symbol in frame.symbols().iter() {
90 if let Some(symbol_name) = symbol.name().map(|name| name.as_str()).flatten() {
91 if !sync_mutex_constr_regex.is_match(symbol_name) {
93 symbol_after_latest_debug_sync = Some(symbol);
94 found_debug_sync = false;
96 } else { found_debug_sync = true; }
100 let symbol = symbol_after_latest_debug_sync.expect("Couldn't find lock call symbol");
101 (format!("{}:{}", symbol.filename().unwrap().display(), symbol.lineno().unwrap()), symbol.colno())
105 fn new() -> Arc<LockMetadata> {
106 let backtrace = Backtrace::new();
107 let lock_idx = LOCK_IDX.fetch_add(1, Ordering::Relaxed) as u64;
109 let res = Arc::new(LockMetadata {
110 locked_before: StdMutex::new(HashMap::new()),
112 _lock_construction_bt: backtrace,
115 #[cfg(feature = "backtrace")]
117 let (lock_constr_location, lock_constr_colno) =
118 locate_call_symbol(&res._lock_construction_bt);
119 LOCKS_INIT.call_once(|| { unsafe { LOCKS = Some(StdMutex::new(HashMap::new())); } });
120 let mut locks = unsafe { LOCKS.as_ref() }.unwrap().lock().unwrap();
121 match locks.entry(lock_constr_location) {
122 hash_map::Entry::Occupied(e) => {
123 assert_eq!(lock_constr_colno,
124 locate_call_symbol(&e.get()._lock_construction_bt).1,
125 "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.");
126 return Arc::clone(e.get())
128 hash_map::Entry::Vacant(e) => { e.insert(Arc::clone(&res)); },
134 fn pre_lock(this: &Arc<LockMetadata>, _double_lock_self_allowed: bool) {
135 LOCKS_HELD.with(|held| {
136 // For each lock that is currently held, check that no lock's `locked_before` set
137 // includes the lock we're about to hold, which would imply a lockorder inversion.
138 for (locked_idx, _locked) in held.borrow().iter() {
139 if *locked_idx == this.lock_idx {
140 // Note that with `feature = "backtrace"` set, we may be looking at different
141 // instances of the same lock. Still, doing so is quite risky, a total order
142 // must be maintained, and doing so across a set of otherwise-identical mutexes
143 // is fraught with issues.
144 #[cfg(feature = "backtrace")]
145 debug_assert!(_double_lock_self_allowed,
146 "Tried to acquire a lock while it was held!\nLock constructed at {}",
147 locate_call_symbol(&this._lock_construction_bt).0);
148 #[cfg(not(feature = "backtrace"))]
149 panic!("Tried to acquire a lock while it was held!");
152 for (_locked_idx, locked) in held.borrow().iter() {
153 for (locked_dep_idx, _locked_dep) in locked.locked_before.lock().unwrap().iter() {
154 let is_dep_this_lock = *locked_dep_idx == this.lock_idx;
155 let has_same_construction = *locked_dep_idx == locked.lock_idx;
156 if is_dep_this_lock && !has_same_construction {
157 #[allow(unused_mut, unused_assignments)]
158 let mut has_same_callsite = false;
159 #[cfg(feature = "backtrace")] {
160 has_same_callsite = _double_lock_self_allowed &&
161 locate_call_symbol(&_locked_dep._lockdep_trace) ==
162 locate_call_symbol(&Backtrace::new());
164 if !has_same_callsite {
165 #[cfg(feature = "backtrace")]
166 panic!("Tried to violate existing lockorder.\nMutex that should be locked after the current lock was created at the following backtrace.\nNote that to get a backtrace for the lockorder violation, you should set RUST_BACKTRACE=1\nLock being taken constructed at: {} ({}):\n{:?}\nLock constructed at: {} ({})\n{:?}\n\nLock dep created at:\n{:?}\n\n",
167 locate_call_symbol(&this._lock_construction_bt).0,
168 this.lock_idx, this._lock_construction_bt,
169 locate_call_symbol(&locked._lock_construction_bt).0,
170 locked.lock_idx, locked._lock_construction_bt,
171 _locked_dep._lockdep_trace);
172 #[cfg(not(feature = "backtrace"))]
173 panic!("Tried to violate existing lockorder. Build with the backtrace feature for more info.");
177 // Insert any already-held locks in our locked-before set.
178 let mut locked_before = this.locked_before.lock().unwrap();
179 if !locked_before.contains_key(&locked.lock_idx) {
180 let lockdep = LockDep { lock: Arc::clone(locked), _lockdep_trace: Backtrace::new() };
181 locked_before.insert(lockdep.lock.lock_idx, lockdep);
184 held.borrow_mut().insert(this.lock_idx, Arc::clone(this));
188 fn held_by_thread(this: &Arc<LockMetadata>) -> LockHeldState {
189 let mut res = LockHeldState::NotHeldByThread;
190 LOCKS_HELD.with(|held| {
191 for (locked_idx, _locked) in held.borrow().iter() {
192 if *locked_idx == this.lock_idx {
193 res = LockHeldState::HeldByThread;
200 fn try_locked(this: &Arc<LockMetadata>) {
201 LOCKS_HELD.with(|held| {
202 // Since a try-lock will simply fail if the lock is held already, we do not
203 // consider try-locks to ever generate lockorder inversions. However, if a try-lock
204 // succeeds, we do consider it to have created lockorder dependencies.
205 let mut locked_before = this.locked_before.lock().unwrap();
206 for (locked_idx, locked) in held.borrow().iter() {
207 if !locked_before.contains_key(locked_idx) {
208 let lockdep = LockDep { lock: Arc::clone(locked), _lockdep_trace: Backtrace::new() };
209 locked_before.insert(*locked_idx, lockdep);
212 held.borrow_mut().insert(this.lock_idx, Arc::clone(this));
217 pub struct Mutex<T: Sized> {
219 deps: Arc<LockMetadata>,
221 impl<T: Sized> Mutex<T> {
222 pub(crate) fn into_inner(self) -> LockResult<T> {
223 self.inner.into_inner().map_err(|_| ())
227 #[must_use = "if unused the Mutex will immediately unlock"]
228 pub struct MutexGuard<'a, T: Sized + 'a> {
230 lock: StdMutexGuard<'a, T>,
233 impl<'a, T: Sized> MutexGuard<'a, T> {
234 fn into_inner(self) -> StdMutexGuard<'a, T> {
235 // Somewhat unclear why we cannot move out of self.lock, but doing so gets E0509.
237 let v: StdMutexGuard<'a, T> = std::ptr::read(&self.lock);
238 std::mem::forget(self);
244 impl<T: Sized> Drop for MutexGuard<'_, T> {
246 LOCKS_HELD.with(|held| {
247 held.borrow_mut().remove(&self.mutex.deps.lock_idx);
252 impl<T: Sized> Deref for MutexGuard<'_, T> {
255 fn deref(&self) -> &T {
260 impl<T: Sized> DerefMut for MutexGuard<'_, T> {
261 fn deref_mut(&mut self) -> &mut T {
262 self.lock.deref_mut()
267 pub fn new(inner: T) -> Mutex<T> {
268 Mutex { inner: StdMutex::new(inner), deps: LockMetadata::new() }
271 pub fn lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
272 LockMetadata::pre_lock(&self.deps, false);
273 self.inner.lock().map(|lock| MutexGuard { mutex: self, lock }).map_err(|_| ())
276 pub fn try_lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
277 let res = self.inner.try_lock().map(|lock| MutexGuard { mutex: self, lock }).map_err(|_| ());
279 LockMetadata::try_locked(&self.deps);
285 impl<'a, T: 'a> LockTestExt<'a> for Mutex<T> {
287 fn held_by_thread(&self) -> LockHeldState {
288 LockMetadata::held_by_thread(&self.deps)
290 type ExclLock = MutexGuard<'a, T>;
292 fn unsafe_well_ordered_double_lock_self(&'a self) -> MutexGuard<T> {
293 LockMetadata::pre_lock(&self.deps, true);
294 self.inner.lock().map(|lock| MutexGuard { mutex: self, lock }).unwrap()
298 pub struct RwLock<T: Sized> {
300 deps: Arc<LockMetadata>,
303 pub struct RwLockReadGuard<'a, T: Sized + 'a> {
305 guard: StdRwLockReadGuard<'a, T>,
308 pub struct RwLockWriteGuard<'a, T: Sized + 'a> {
310 guard: StdRwLockWriteGuard<'a, T>,
313 impl<T: Sized> Deref for RwLockReadGuard<'_, T> {
316 fn deref(&self) -> &T {
321 impl<T: Sized> Drop for RwLockReadGuard<'_, T> {
323 LOCKS_HELD.with(|held| {
324 held.borrow_mut().remove(&self.lock.deps.lock_idx);
329 impl<T: Sized> Deref for RwLockWriteGuard<'_, T> {
332 fn deref(&self) -> &T {
337 impl<T: Sized> Drop for RwLockWriteGuard<'_, T> {
339 LOCKS_HELD.with(|held| {
340 held.borrow_mut().remove(&self.lock.deps.lock_idx);
345 impl<T: Sized> DerefMut for RwLockWriteGuard<'_, T> {
346 fn deref_mut(&mut self) -> &mut T {
347 self.guard.deref_mut()
352 pub fn new(inner: T) -> RwLock<T> {
353 RwLock { inner: StdRwLock::new(inner), deps: LockMetadata::new() }
356 pub fn read<'a>(&'a self) -> LockResult<RwLockReadGuard<'a, T>> {
357 // Note that while we could be taking a recursive read lock here, Rust's `RwLock` may
358 // deadlock trying to take a second read lock if another thread is waiting on the write
359 // lock. This behavior is platform dependent, but our in-tree `FairRwLock` guarantees
361 LockMetadata::pre_lock(&self.deps, false);
362 self.inner.read().map(|guard| RwLockReadGuard { lock: self, guard }).map_err(|_| ())
365 pub fn write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
366 LockMetadata::pre_lock(&self.deps, false);
367 self.inner.write().map(|guard| RwLockWriteGuard { lock: self, guard }).map_err(|_| ())
370 pub fn try_write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
371 let res = self.inner.try_write().map(|guard| RwLockWriteGuard { lock: self, guard }).map_err(|_| ());
373 LockMetadata::try_locked(&self.deps);
379 impl<'a, T: 'a> LockTestExt<'a> for RwLock<T> {
381 fn held_by_thread(&self) -> LockHeldState {
382 LockMetadata::held_by_thread(&self.deps)
384 type ExclLock = RwLockWriteGuard<'a, T>;
386 fn unsafe_well_ordered_double_lock_self(&'a self) -> RwLockWriteGuard<'a, T> {
387 LockMetadata::pre_lock(&self.deps, true);
388 self.inner.write().map(|guard| RwLockWriteGuard { lock: self, guard }).unwrap()
392 pub type FairRwLock<T> = RwLock<T>;