Skip to content

Commit

Permalink
lock per key fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Alfonsi <[email protected]>
  • Loading branch information
Peter Alfonsi authored and sgup432 committed Jun 4, 2024
1 parent 810de9a commit 6eb3e05
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,11 @@ ICache<K, V> getDiskCache() {
private void writeLock(ICacheKey<K> key) {
if (threadLocal.get() != null) {
LockWrapper lockWrapper = threadLocal.get();
lockWrapper.writeLock.close();
if (lockWrapper.refCount.decrementAndGet() == 0) {
locks.remove(key);
if (lockWrapper != null && lockWrapper.writeLock.isHeldByCurrentThread()) {
lockWrapper.writeLock.close();
if (lockWrapper.refCount.decrementAndGet() == 0) {
locks.remove(key);
}
}
}
LockWrapper lockWrapper = locks.computeIfAbsent(key, key1 -> {
Expand All @@ -195,18 +197,22 @@ private void readLock(ICacheKey<K> key) {

private void unlockWriteLock(ICacheKey<K> key) {
LockWrapper lockWrapper = locks.get(key);
lockWrapper.writeLock.close();
if (lockWrapper.refCount.decrementAndGet() == 0) {
locks.remove(key);
if (lockWrapper != null && lockWrapper.writeLock.isHeldByCurrentThread()) {
lockWrapper.writeLock.close();
if (lockWrapper.refCount.decrementAndGet() == 0) {
locks.remove(key);
}
}
threadLocal.remove();
}

private void unlockReadLock(ICacheKey<K> key) {
LockWrapper lockWrapper = locks.get(key);
lockWrapper.readLock.close();
if (lockWrapper.refCount.decrementAndGet() == 0) {
locks.remove(key);
if (lockWrapper != null && lockWrapper.readLock.isHeldByCurrentThread()) {
lockWrapper.readLock.close();
if (lockWrapper.refCount.decrementAndGet() == 0) {
locks.remove(key);
}
}
threadLocal.remove();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1368,7 +1368,9 @@ public void testNumLocksTiming() throws Exception {
System.out.println("Finished iter " + j);
}
}
} catch (Exception ignored) {}
} catch (Exception e) {
throw new RuntimeException(e);
}
countDownLatch.countDown();
});
threads[i].start();
Expand Down

0 comments on commit 6eb3e05

Please sign in to comment.