Skip to content

Commit 8b39f29

Browse files
committed
Use the term "lock" for Mutex/RwLock instead of "mutex"
1 parent c73b50f commit 8b39f29

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

lightning/src/debug_sync.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -44,42 +44,42 @@ impl Condvar {
4444

4545
thread_local! {
4646
/// We track the set of locks currently held by a reference to their `MutexMetadata`
47-
static MUTEXES_HELD: RefCell<HashSet<Arc<MutexMetadata>>> = RefCell::new(HashSet::new());
47+
static LOCKS_HELD: RefCell<HashSet<Arc<MutexMetadata>>> = RefCell::new(HashSet::new());
4848
}
49-
static MUTEX_IDX: AtomicUsize = AtomicUsize::new(0);
49+
static LOCK_IDX: AtomicUsize = AtomicUsize::new(0);
5050

51-
/// Metadata about a single mutex, by id, the set of things locked-before it, and the backtrace of
51+
/// Metadata about a single lock, by id, the set of things locked-before it, and the backtrace of
5252
/// when the Mutex itself was constructed.
5353
struct MutexMetadata {
54-
mutex_idx: u64,
54+
lock_idx: u64,
5555
locked_before: StdMutex<HashSet<Arc<MutexMetadata>>>,
5656
#[cfg(feature = "backtrace")]
57-
mutex_construction_bt: Backtrace,
57+
lock_construction_bt: Backtrace,
5858
}
5959
impl PartialEq for MutexMetadata {
60-
fn eq(&self, o: &MutexMetadata) -> bool { self.mutex_idx == o.mutex_idx }
60+
fn eq(&self, o: &MutexMetadata) -> bool { self.lock_idx == o.lock_idx }
6161
}
6262
impl Eq for MutexMetadata {}
6363
impl std::hash::Hash for MutexMetadata {
64-
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) { hasher.write_u64(self.mutex_idx); }
64+
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) { hasher.write_u64(self.lock_idx); }
6565
}
6666

6767
impl MutexMetadata {
6868
fn new() -> MutexMetadata {
6969
MutexMetadata {
7070
locked_before: StdMutex::new(HashSet::new()),
71-
mutex_idx: MUTEX_IDX.fetch_add(1, Ordering::Relaxed) as u64,
71+
lock_idx: LOCK_IDX.fetch_add(1, Ordering::Relaxed) as u64,
7272
#[cfg(feature = "backtrace")]
73-
mutex_construction_bt: Backtrace::new(),
73+
lock_construction_bt: Backtrace::new(),
7474
}
7575
}
7676

7777
// Returns whether we were a recursive lock (only relevant for read)
7878
fn _pre_lock(this: &Arc<MutexMetadata>, read: bool) -> bool {
7979
let mut inserted = false;
80-
MUTEXES_HELD.with(|held| {
81-
// For each mutex which is currently locked, check that no mutex's locked-before
82-
// set includes the mutex we're about to lock, which would imply a lockorder
80+
LOCKS_HELD.with(|held| {
81+
// For each lock which is currently locked, check that no lock's locked-before
82+
// set includes the lock we're about to lock, which would imply a lockorder
8383
// inversion.
8484
for locked in held.borrow().iter() {
8585
if read && *locked == *this {
@@ -89,17 +89,17 @@ impl MutexMetadata {
8989
}
9090
for locked in held.borrow().iter() {
9191
if !read && *locked == *this {
92-
panic!("Tried to lock a mutex while it was held!");
92+
panic!("Tried to lock a lock while it was held!");
9393
}
9494
for locked_dep in locked.locked_before.lock().unwrap().iter() {
9595
if *locked_dep == *this {
9696
#[cfg(feature = "backtrace")]
97-
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\n{:?}", locked.mutex_construction_bt);
97+
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\n{:?}", locked.lock_construction_bt);
9898
#[cfg(not(feature = "backtrace"))]
9999
panic!("Tried to violate existing lockorder. Build with the backtrace feature for more info.");
100100
}
101101
}
102-
// Insert any already-held mutexes in our locked-before set.
102+
// Insert any already-held locks in our locked-before set.
103103
this.locked_before.lock().unwrap().insert(Arc::clone(locked));
104104
}
105105
held.borrow_mut().insert(Arc::clone(this));
@@ -112,7 +112,7 @@ impl MutexMetadata {
112112
fn pre_read_lock(this: &Arc<MutexMetadata>) -> bool { Self::_pre_lock(this, true) }
113113

114114
fn try_locked(this: &Arc<MutexMetadata>) {
115-
MUTEXES_HELD.with(|held| {
115+
LOCKS_HELD.with(|held| {
116116
// Since a try-lock will simply fail if the lock is held already, we do not
117117
// consider try-locks to ever generate lockorder inversions. However, if a try-lock
118118
// succeeds, we do consider it to have created lockorder dependencies.
@@ -148,7 +148,7 @@ impl<'a, T: Sized> MutexGuard<'a, T> {
148148

149149
impl<T: Sized> Drop for MutexGuard<'_, T> {
150150
fn drop(&mut self) {
151-
MUTEXES_HELD.with(|held| {
151+
LOCKS_HELD.with(|held| {
152152
held.borrow_mut().remove(&self.mutex.deps);
153153
});
154154
}
@@ -193,21 +193,21 @@ pub struct RwLock<T: Sized> {
193193
}
194194

195195
pub struct RwLockReadGuard<'a, T: Sized + 'a> {
196-
mutex: &'a RwLock<T>,
196+
lock: &'a RwLock<T>,
197197
first_lock: bool,
198-
lock: StdRwLockReadGuard<'a, T>,
198+
guard: StdRwLockReadGuard<'a, T>,
199199
}
200200

201201
pub struct RwLockWriteGuard<'a, T: Sized + 'a> {
202-
mutex: &'a RwLock<T>,
203-
lock: StdRwLockWriteGuard<'a, T>,
202+
lock: &'a RwLock<T>,
203+
guard: StdRwLockWriteGuard<'a, T>,
204204
}
205205

206206
impl<T: Sized> Deref for RwLockReadGuard<'_, T> {
207207
type Target = T;
208208

209209
fn deref(&self) -> &T {
210-
&self.lock.deref()
210+
&self.guard.deref()
211211
}
212212
}
213213

@@ -219,8 +219,8 @@ impl<T: Sized> Drop for RwLockReadGuard<'_, T> {
219219
// always be true.
220220
return;
221221
}
222-
MUTEXES_HELD.with(|held| {
223-
held.borrow_mut().remove(&self.mutex.deps);
222+
LOCKS_HELD.with(|held| {
223+
held.borrow_mut().remove(&self.lock.deps);
224224
});
225225
}
226226
}
@@ -229,21 +229,21 @@ impl<T: Sized> Deref for RwLockWriteGuard<'_, T> {
229229
type Target = T;
230230

231231
fn deref(&self) -> &T {
232-
&self.lock.deref()
232+
&self.guard.deref()
233233
}
234234
}
235235

236236
impl<T: Sized> Drop for RwLockWriteGuard<'_, T> {
237237
fn drop(&mut self) {
238-
MUTEXES_HELD.with(|held| {
239-
held.borrow_mut().remove(&self.mutex.deps);
238+
LOCKS_HELD.with(|held| {
239+
held.borrow_mut().remove(&self.lock.deps);
240240
});
241241
}
242242
}
243243

244244
impl<T: Sized> DerefMut for RwLockWriteGuard<'_, T> {
245245
fn deref_mut(&mut self) -> &mut T {
246-
self.lock.deref_mut()
246+
self.guard.deref_mut()
247247
}
248248
}
249249

@@ -254,16 +254,16 @@ impl<T> RwLock<T> {
254254

255255
pub fn read<'a>(&'a self) -> LockResult<RwLockReadGuard<'a, T>> {
256256
let first_lock = MutexMetadata::pre_read_lock(&self.deps);
257-
self.inner.read().map(|lock| RwLockReadGuard { mutex: self, lock, first_lock }).map_err(|_| ())
257+
self.inner.read().map(|guard| RwLockReadGuard { lock: self, guard, first_lock }).map_err(|_| ())
258258
}
259259

260260
pub fn write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
261261
MutexMetadata::pre_lock(&self.deps);
262-
self.inner.write().map(|lock| RwLockWriteGuard { mutex: self, lock }).map_err(|_| ())
262+
self.inner.write().map(|guard| RwLockWriteGuard { lock: self, guard }).map_err(|_| ())
263263
}
264264

265265
pub fn try_write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
266-
let res = self.inner.try_write().map(|lock| RwLockWriteGuard { mutex: self, lock }).map_err(|_| ());
266+
let res = self.inner.try_write().map(|guard| RwLockWriteGuard { lock: self, guard }).map_err(|_| ());
267267
if res.is_ok() {
268268
MutexMetadata::try_locked(&self.deps);
269269
}

0 commit comments

Comments
 (0)