Skip to content

Commit

Permalink
minor optimization for hand-over-hand locking
Browse files Browse the repository at this point in the history
  • Loading branch information
Hao-Wei committed Feb 11, 2024
1 parent 648b0d9 commit 2538401
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions benchmarks/concurrentKNN/octTree/flock/spin_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// is_locked() -> bool

#include <parlay/parallel.h> // needed for worker_id
#include "epoch.h" // for worker_id()
#include <flock/epoch.h> // for worker_id()

#include <atomic>
#include<chrono>
Expand Down Expand Up @@ -82,9 +82,10 @@ struct lock {
lock_entry newl = current.take_lock();
if (lck.compare_exchange_strong(current, newl)) {
RT result = f();
if (no_release == nullptr)
lck.compare_exchange_strong(newl, newl.release_lock());
// lck = newl.release_lock(); // release lock
if (no_release == nullptr) {
if(lck.load().is_self_locked()) // lock might have been released early during f()
lck = newl.release_lock(); // release lock
}
else *no_release = true;
return std::optional<RT>(result);
} else {
Expand Down Expand Up @@ -124,7 +125,7 @@ struct lock {

void unlock() {
//std::cout << "released lock:" << this << ", " << is_locked() << std::endl;
if (!is_locked()) abort();
assert(lck.load().is_self_locked());
lck = lck.load().release_lock();
}

Expand Down

0 comments on commit 2538401

Please sign in to comment.