Avoid redundant reads of head in peakNode

Motivation:

There is not need todo redunant reads of head in peakNode as we can just spin on next() until it becomes visible.

Modifications:

Remove redundant reads of head in peakNode. This is based on @nitsanw's patch for akka.
See https://github.com/akka/akka/pull/15596

Result:

Less volatile access.
This commit is contained in:
Norman Maurer 2014-08-18 08:35:25 +02:00
parent 2c9d1dafac
commit 09100e5043

View File

@ -97,22 +97,19 @@ final class MpscLinkedQueue<E> extends MpscLinkedQueueTailRef<E> implements Queu
* Returns the node right next to the head, which contains the first element of this queue. * Returns the node right next to the head, which contains the first element of this queue.
*/ */
private MpscLinkedQueueNode<E> peekNode() { private MpscLinkedQueueNode<E> peekNode() {
for (;;) { MpscLinkedQueueNode<E> head = headRef();
final MpscLinkedQueueNode<E> head = headRef(); MpscLinkedQueueNode<E> next = head.next();
final MpscLinkedQueueNode<E> next = head.next(); if (next == null && head != tailRef()) {
if (next != null) { // if tail != head this is not going to change until consumer makes progress
// we can avoid reading the head and just spin on next until it shows up
//
// See https://github.com/akka/akka/pull/15596
do {
next = head.next();
} while (next == null);
}
return next; return next;
} }
if (head == tailRef()) {
return null;
}
// If we are here, it means:
// * offer() is adding the first element, and
// * it's between replaceTail(newTail) and oldTail.setNext(newTail).
// (i.e. next == oldTail and oldTail.next == null and head == oldTail != newTail)
}
}
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")