Skip to content

Commit d6e110d

Browse files
committed
Tiny lock_bucket simplifications
1 parent 4f61bbe commit d6e110d

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

core/src/parking_lot.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -320,12 +320,11 @@ fn hash(key: usize, bits: u32) -> usize {
320320
/// The returned bucket must be unlocked again in order to not cause deadlocks.
321321
#[inline]
322322
fn lock_bucket(key: usize) -> &'static Bucket {
323-
let mut bucket;
324323
loop {
325324
let hashtable = get_hashtable();
326325

327326
let hash = hash(key, hashtable.hash_bits);
328-
bucket = &hashtable.entries[hash];
327+
let bucket = &hashtable.entries[hash];
329328

330329
// Lock the bucket
331330
bucket.mutex.lock();
@@ -347,13 +346,12 @@ fn lock_bucket(key: usize) -> &'static Bucket {
347346
/// The returned bucket must be unlocked again in order to not cause deadlocks.
348347
#[inline]
349348
fn lock_bucket_checked(key: &AtomicUsize) -> (usize, &'static Bucket) {
350-
let mut bucket;
351349
loop {
352350
let hashtable = get_hashtable();
353351
let current_key = key.load(Ordering::Relaxed);
354352

355353
let hash = hash(current_key, hashtable.hash_bits);
356-
bucket = &hashtable.entries[hash];
354+
let bucket = &hashtable.entries[hash];
357355

358356
// Lock the bucket
359357
bucket.mutex.lock();
@@ -380,18 +378,18 @@ fn lock_bucket_checked(key: &AtomicUsize) -> (usize, &'static Bucket) {
380378
/// careful to only unlock it once in this case, always use `unlock_bucket_pair`.
381379
#[inline]
382380
fn lock_bucket_pair(key1: usize, key2: usize) -> (&'static Bucket, &'static Bucket) {
383-
let mut bucket1;
384381
loop {
385382
let hashtable = get_hashtable();
386383

387-
// Get the lowest bucket first
388384
let hash1 = hash(key1, hashtable.hash_bits);
389385
let hash2 = hash(key2, hashtable.hash_bits);
390-
if hash1 <= hash2 {
391-
bucket1 = &hashtable.entries[hash1];
386+
387+
// Get the bucket at the lowest hash/index first
388+
let bucket1 = if hash1 <= hash2 {
389+
&hashtable.entries[hash1]
392390
} else {
393-
bucket1 = &hashtable.entries[hash2];
394-
}
391+
&hashtable.entries[hash2]
392+
};
395393

396394
// Lock the first bucket
397395
bucket1.mutex.lock();

0 commit comments

Comments
 (0)