Skip to content

Commit

Permalink
change lock id type
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Killat committed Apr 26, 2021
1 parent 43fdc62 commit 8398545
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
22 changes: 12 additions & 10 deletions include/id_aware_lock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
#include <atomic>
#include "semaphore.hpp"

using lock_id_t = uint32_t; //guaranteed to fit into an int64_t

//can be used to build a recursive mutex if id is a unique thread id
class IdAwareLock
{
public:
//guaranteed to fit into an int64_t
using id_t = uint32_t;

private:
using state_t = int64_t;
Expand Down Expand Up @@ -41,14 +43,13 @@ class IdAwareLock
IdAwareLock(uint32_t maxSpinIterations = 1)
: MAX_SPINNING_ACQUIRE_ITERATIONS(maxSpinIterations > 0 ? maxSpinIterations : 1)
{
//futexWord = reinterpret_cast<int *>(&state);
}

IdAwareLock(const IdAwareLock &) = delete;
IdAwareLock(IdAwareLock &&) = delete;

//only positive values (>0), can foolproof this later
void lock(lock_id_t id = 0)
void lock(id_t id = 0)
{
//try to acquire the lock by spinning
for (uint32_t i = 0; i < MAX_SPINNING_ACQUIRE_ITERATIONS; ++i)
Expand All @@ -61,13 +62,12 @@ class IdAwareLock
}
else if (knownState == CONTESTED)
{
if (lockingId == id) // recursive locking
if (getLockingId() == id) // for recursive locking
{
return;
}
//contested, do not try to spin any more and sleep instead
//(promotes fairness with respect to threads trying to acquire the lock)
//sleepIfContested(); //could use a semaphore to wait as well
semaphore.wait();
break;
}
Expand All @@ -76,15 +76,15 @@ class IdAwareLock
//spinning failed, assume the lock is contested and change its state accordingly,
//sleep while it is actually contested or locked

//change to contested, waiting for it to be unlocked
while (exchangeState(CONTESTED) != UNLOCKED)
{
//note that the contested state can be a false positive, i.e. might not be contested anymore when
//we set it to contested, but then we do not sleep here,
//we set it to contested, but then we do not sleep here since we already issued a post,
//i.e. this is just a pessimistic but safe assumption which optimzes the logic

//note that we also do not sleep when someone sets it back to UNLOCKED before the exchange
//and just set it to CONTESTED (false positive) and return, having acquired the lock
//sleepIfContested();
semaphore.wait();
}

Expand All @@ -93,21 +93,23 @@ class IdAwareLock

void unlock()
{
lockingId.store(0); //slightly out of sync, but has to be done before exchange
lockingId.store(UNLOCKED); //slightly out of sync, but has to be done before exchange

//change the lock state back to unlocked and wake someone if it was contested
if (exchangeState(UNLOCKED) == CONTESTED)
{
semaphore.post();
}
}

void unlock(lock_id_t id)
void unlock(id_t id)
{
if (getLockingId() != id)
{
std::cout << "incorrect unlock id" << std::endl;
std::terminate(); //protocol error
}

lockingId.store(UNLOCKED); //slightly out of sync, but has to be done before exchange

//change the lock state back to unlocked and wake someone if it was contested
Expand Down
2 changes: 1 addition & 1 deletion test_locks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void test(int iterations = 1000000, int n = 4)
users.store(0, std::memory_order_relaxed);
mutexError.store(0, std::memory_order_relaxed);

lock_id_t id = 1;
IdAwareLock::id_t id = 1;
for (int i = 0; i < n; ++i)
{
threads.emplace_back(work<LockType>, std::ref(lock), id++, 1, iterations);
Expand Down

0 comments on commit 8398545

Please sign in to comment.