Skip to content

Commit 35c8718

Browse files
committed
Use the term "lock" for Mutex/RwLock instead of "mutex"
1 parent 3648784 commit 35c8718

File tree

1 file changed

+53
-53
lines changed

1 file changed

+53
-53
lines changed

lightning/src/debug_sync.rs

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -43,43 +43,43 @@ impl Condvar {
4343
}
4444

4545
thread_local! {
46-
/// 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());
46+
/// We track the set of locks currently held by a reference to their `LockMetadata`
47+
static LOCKS_HELD: RefCell<HashSet<Arc<LockMetadata>>> = 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.
53-
struct MutexMetadata {
54-
mutex_idx: u64,
55-
locked_before: StdMutex<HashSet<Arc<MutexMetadata>>>,
53+
struct LockMetadata {
54+
lock_idx: u64,
55+
locked_before: StdMutex<HashSet<Arc<LockMetadata>>>,
5656
#[cfg(feature = "backtrace")]
57-
mutex_construction_bt: Backtrace,
57+
lock_construction_bt: Backtrace,
5858
}
59-
impl PartialEq for MutexMetadata {
60-
fn eq(&self, o: &MutexMetadata) -> bool { self.mutex_idx == o.mutex_idx }
59+
impl PartialEq for LockMetadata {
60+
fn eq(&self, o: &LockMetadata) -> bool { self.lock_idx == o.lock_idx }
6161
}
62-
impl Eq for MutexMetadata {}
63-
impl std::hash::Hash for MutexMetadata {
64-
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) { hasher.write_u64(self.mutex_idx); }
62+
impl Eq for LockMetadata {}
63+
impl std::hash::Hash for LockMetadata {
64+
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) { hasher.write_u64(self.lock_idx); }
6565
}
6666

67-
impl MutexMetadata {
68-
fn new() -> MutexMetadata {
69-
MutexMetadata {
67+
impl LockMetadata {
68+
fn new() -> LockMetadata {
69+
LockMetadata {
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)
78-
fn _pre_lock(this: &Arc<MutexMetadata>, read: bool) -> bool {
78+
fn _pre_lock(this: &Arc<LockMetadata>, 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));
@@ -108,11 +108,11 @@ impl MutexMetadata {
108108
inserted
109109
}
110110

111-
fn pre_lock(this: &Arc<MutexMetadata>) { Self::_pre_lock(this, false); }
112-
fn pre_read_lock(this: &Arc<MutexMetadata>) -> bool { Self::_pre_lock(this, true) }
111+
fn pre_lock(this: &Arc<LockMetadata>) { Self::_pre_lock(this, false); }
112+
fn pre_read_lock(this: &Arc<LockMetadata>) -> bool { Self::_pre_lock(this, true) }
113113

114-
fn try_locked(this: &Arc<MutexMetadata>) {
115-
MUTEXES_HELD.with(|held| {
114+
fn try_locked(this: &Arc<LockMetadata>) {
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.
@@ -126,7 +126,7 @@ impl MutexMetadata {
126126

127127
pub struct Mutex<T: Sized> {
128128
inner: StdMutex<T>,
129-
deps: Arc<MutexMetadata>,
129+
deps: Arc<LockMetadata>,
130130
}
131131

132132
#[must_use = "if unused the Mutex will immediately unlock"]
@@ -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
}
@@ -170,44 +170,44 @@ impl<T: Sized> DerefMut for MutexGuard<'_, T> {
170170

171171
impl<T> Mutex<T> {
172172
pub fn new(inner: T) -> Mutex<T> {
173-
Mutex { inner: StdMutex::new(inner), deps: Arc::new(MutexMetadata::new()) }
173+
Mutex { inner: StdMutex::new(inner), deps: Arc::new(LockMetadata::new()) }
174174
}
175175

176176
pub fn lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
177-
MutexMetadata::pre_lock(&self.deps);
177+
LockMetadata::pre_lock(&self.deps);
178178
self.inner.lock().map(|lock| MutexGuard { mutex: self, lock }).map_err(|_| ())
179179
}
180180

181181
pub fn try_lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
182182
let res = self.inner.try_lock().map(|lock| MutexGuard { mutex: self, lock }).map_err(|_| ());
183183
if res.is_ok() {
184-
MutexMetadata::try_locked(&self.deps);
184+
LockMetadata::try_locked(&self.deps);
185185
}
186186
res
187187
}
188188
}
189189

190190
pub struct RwLock<T: Sized> {
191191
inner: StdRwLock<T>,
192-
deps: Arc<MutexMetadata>,
192+
deps: Arc<LockMetadata>,
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,43 +229,43 @@ 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

250250
impl<T> RwLock<T> {
251251
pub fn new(inner: T) -> RwLock<T> {
252-
RwLock { inner: StdRwLock::new(inner), deps: Arc::new(MutexMetadata::new()) }
252+
RwLock { inner: StdRwLock::new(inner), deps: Arc::new(LockMetadata::new()) }
253253
}
254254

255255
pub fn read<'a>(&'a self) -> LockResult<RwLockReadGuard<'a, T>> {
256-
let first_lock = MutexMetadata::pre_read_lock(&self.deps);
257-
self.inner.read().map(|lock| RwLockReadGuard { mutex: self, lock, first_lock }).map_err(|_| ())
256+
let first_lock = LockMetadata::pre_read_lock(&self.deps);
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>> {
261-
MutexMetadata::pre_lock(&self.deps);
262-
self.inner.write().map(|lock| RwLockWriteGuard { mutex: self, lock }).map_err(|_| ())
261+
LockMetadata::pre_lock(&self.deps);
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() {
268-
MutexMetadata::try_locked(&self.deps);
268+
LockMetadata::try_locked(&self.deps);
269269
}
270270
res
271271
}

0 commit comments

Comments
 (0)