Skip to content

Commit

Permalink
cache: fix assert node.next==empty (#1790)
Browse files Browse the repository at this point in the history
  • Loading branch information
javeme authored Apr 15, 2022
1 parent 73a1f1f commit 734d554
Showing 1 changed file with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ public LinkNode<K, V> enqueue(LinkNode<K, V> node) {

while (true) {
LinkNode<K, V> last = this.rear.prev;
assert last != this.empty : last;

// TODO: should we lock the new `node`?
List<Lock> locks = this.lock(last, this.rear);
Expand All @@ -405,13 +406,13 @@ public LinkNode<K, V> enqueue(LinkNode<K, V> node) {
}

/*
* Link the node to the rear before to the last if we
* have not locked the node itself, because dumpKeys()
* Link the node to the `rear` before to the `last` if we
* have not locked the `node` itself, because dumpKeys()
* may get the new node with next=null.
* TODO: it also depends on memory barrier.
*/

// Build the link between `node` and the rear
// Build the link between `node` and the `rear`
node.next = this.rear;
assert this.rear.prev == last : this.rear.prev;
this.rear.prev = node;
Expand Down Expand Up @@ -445,12 +446,12 @@ public LinkNode<K, V> dequeue() {
continue;
}

// Break the link between the head and `first`
assert first.next != null;
// Break the link between the `head` and `first`
assert first.next != null && first.next != this.empty;
this.head.next = first.next;
first.next.prev = this.head;

// Clear the links of the first node
// Clear the links of the `first` node
first.prev = this.empty;
first.next = this.empty;

Expand All @@ -470,9 +471,8 @@ public LinkNode<K, V> remove(LinkNode<K, V> node) {

while (true) {
LinkNode<K, V> prev = node.prev;
if (prev == this.empty) {
assert node.next == this.empty;
// Ignore the node if it has been removed
if (prev == this.empty || node.next == this.empty) {
// Ignore the `node` if it has been removed
return null;
}

Expand All @@ -487,9 +487,10 @@ public LinkNode<K, V> remove(LinkNode<K, V> node) {
continue;
}
assert node.next != null : node;
assert node.next != this.empty : node.next;
assert node.next != node.prev : node.next;

// Build the link between node.prev and node.next
// Break `node` & Build the link between node.prev~node.next
node.prev.next = node.next;
node.next.prev = node.prev;

Expand Down

0 comments on commit 734d554

Please sign in to comment.