Skip to content

Commit

Permalink
Refactor MpscLinkedQueue.poll
Browse files Browse the repository at this point in the history
Handle empty queue first, then share most of the
implementation for non-empty scenarios (spin and
non-spin).
  • Loading branch information
olivergillespie committed Nov 21, 2024
1 parent e46ea36 commit b8582b9
Showing 1 changed file with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,19 @@ public boolean offer(final T e) {
public T poll() {
LinkedQueueNode<T> currConsumerNode = lpConsumerNode(); // don't load twice, it's alright
LinkedQueueNode<T> nextNode = currConsumerNode.lvNext();
if (nextNode != null) {
// we have to null out the value because we are going to hang on to the node
final T nextValue = nextNode.getAndNullValue();
spConsumerNode(nextNode);
return nextValue;
final T nextValue;
if (nextNode == null && currConsumerNode == lvProducerNode()) {
return null;
}
else if (currConsumerNode != lvProducerNode()) {
if (nextNode == null) {
// spin, we are no longer wait free
while ((nextNode = currConsumerNode.lvNext()) == null) { } // NOPMD
// got the next node...

// we have to null out the value because we are going to hang on to the node
final T nextValue = nextNode.getAndNullValue();
spConsumerNode(nextNode);
return nextValue;
}
return null;
// we have to null out the value because we are going to hang on to the node
nextValue = nextNode.getAndNullValue();
spConsumerNode(nextNode);
return nextValue;
}

@Override
Expand Down

0 comments on commit b8582b9

Please sign in to comment.