Skip to content

Commit b8582b9

Browse files
Refactor MpscLinkedQueue.poll
Handle empty queue first, then share most of the implementation for non-empty scenarios (spin and non-spin).
1 parent e46ea36 commit b8582b9

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/main/java/io/reactivex/rxjava3/internal/queue/MpscLinkedQueue.java

+8-12
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,19 @@ public boolean offer(final T e) {
8787
public T poll() {
8888
LinkedQueueNode<T> currConsumerNode = lpConsumerNode(); // don't load twice, it's alright
8989
LinkedQueueNode<T> nextNode = currConsumerNode.lvNext();
90-
if (nextNode != null) {
91-
// we have to null out the value because we are going to hang on to the node
92-
final T nextValue = nextNode.getAndNullValue();
93-
spConsumerNode(nextNode);
94-
return nextValue;
90+
final T nextValue;
91+
if (nextNode == null && currConsumerNode == lvProducerNode()) {
92+
return null;
9593
}
96-
else if (currConsumerNode != lvProducerNode()) {
94+
if (nextNode == null) {
9795
// spin, we are no longer wait free
9896
while ((nextNode = currConsumerNode.lvNext()) == null) { } // NOPMD
9997
// got the next node...
100-
101-
// we have to null out the value because we are going to hang on to the node
102-
final T nextValue = nextNode.getAndNullValue();
103-
spConsumerNode(nextNode);
104-
return nextValue;
10598
}
106-
return null;
99+
// we have to null out the value because we are going to hang on to the node
100+
nextValue = nextNode.getAndNullValue();
101+
spConsumerNode(nextNode);
102+
return nextValue;
107103
}
108104

109105
@Override

0 commit comments

Comments
 (0)