Cache now no longer locked from reading while writing.

Fixed another case where writing is followed by consecutive reading.
This commit is contained in:
Harrison Deng 2020-04-30 14:40:28 -05:00
parent c60e94728e
commit 387393632d
2 changed files with 17 additions and 3 deletions

View File

@ -22,14 +22,15 @@ class CacheUsageStack<K, V> {
} }
private void removeValueFromStack(CacheValue<K, V> value) { private void removeValueFromStack(CacheValue<K, V> value) {
if (value == null) throw new IllegalStateException("Current size is supposed to be " + size);
if (value.front != null) { if (value.front != null) {
value.front.back = value.back; value.front.back = value.back;
} else if (first == value) { } else {
first = value.back; first = value.back;
} }
if (value.back != null) { if (value.back != null) {
value.back.front = value.front; value.back.front = value.front;
} else if (last == value) { } else {
last = value.front; last = value.front;
} }
value.front = null; value.front = null;
@ -66,6 +67,7 @@ class CacheUsageStack<K, V> {
lock.lock(); lock.lock();
try { try {
addValueToStackTop(value); addValueToStackTop(value);
value.detached = false;
} finally { } finally {
lock.unlock(); lock.unlock();
} }
@ -98,4 +100,15 @@ class CacheUsageStack<K, V> {
public int size() { public int size() {
return size; return size;
} }
@Override
public String toString() {
StringBuilder stringBulder = new StringBuilder();
CacheValue<K, V> current = first;
while (current != null) {
stringBulder.append(first + "\n");
current = current.back;
}
return stringBulder.toString();
}
} }

View File

@ -19,5 +19,6 @@ class CacheValue<KeyType, ValueType> {
/** /**
* Whether or not this value is detached from the cache's hashmap. * Whether or not this value is detached from the cache's hashmap.
*/ */
public volatile boolean detached; public volatile boolean detached = true;
} }