Permalink
Browse files

Simplified locking methods

They sort the buckets in numerical order, rather than going through a
bunch of branches.
  • Loading branch information...
1 parent c3607a4 commit 126af279621d43ec26068cf3c0ffaa996cf25230 @manugoyal manugoyal committed Dec 29, 2015
Showing with 19 additions and 42 deletions.
  1. +19 −42 src/cuckoohash_map.hh
View
@@ -695,17 +695,13 @@ private:
void lock_two(const size_t hp, size_t i1, size_t i2) const {
i1 = lock_ind(i1);
i2 = lock_ind(i2);
- if (i1 < i2) {
- locks_[i1].lock();
- check_hashpower(hp, i1);
- locks_[i2].lock();
- } else if (i2 < i1) {
+ if (i2 < i1) {
+ std::swap(i1, i2);
+ }
+ locks_[i1].lock();
+ check_hashpower(hp, i1);
+ if (i2 != i1) {
locks_[i2].lock();
- check_hashpower(hp, i2);
- locks_[i1].lock();
- } else {
- locks_[i1].lock();
- check_hashpower(hp, i1);
}
}
@@ -735,39 +731,20 @@ private:
} else if (i1 == i3) {
lock_two(hp, i1, i2);
} else {
- if (i1 < i2) {
- if (i2 < i3) {
- locks_[i1].lock();
- check_hashpower(hp, i1);
- locks_[i2].lock();
- locks_[i3].lock();
- } else if (i1 < i3) {
- locks_[i1].lock();
- check_hashpower(hp, i1);
- locks_[i3].lock();
- locks_[i2].lock();
- } else {
- locks_[i3].lock();
- check_hashpower(hp, i3);
- locks_[i1].lock();
- locks_[i2].lock();
- }
- } else if (i2 < i3) {
- locks_[i2].lock();
- check_hashpower(hp, i2);
- if (i1 < i3) {
- locks_[i1].lock();
- locks_[i3].lock();
- } else {
- locks_[i3].lock();
- locks_[i1].lock();
- }
- } else {
- locks_[i3].lock();
- check_hashpower(hp, i3);
- locks_[i2].lock();
- locks_[i1].lock();
+ if (i2 < i1) {
+ std::swap(i1, i2);
+ }
+ if (i3 < i2) {
+ std::swap(i2, i3);
}
+ // Now i3 is the largest, but i2 could now be less than i1
+ if (i2 < i1) {
+ std::swap(i1, i2);
+ }
+ locks_[i1].lock();
+ check_hashpower(hp, i1);
+ locks_[i2].lock();
+ locks_[i3].lock();
}
}

0 comments on commit 126af27

Please sign in to comment.