From 5dbdc605b0db6dc326615cd09036212a22c8ea65 Mon Sep 17 00:00:00 2001 From: Nitsan Wakart Date: Mon, 28 Jul 2014 14:19:34 +0200 Subject: [PATCH] Update AbstractNodeQueue.java Avoid spinning on head when a peek/poll observes a null next but the queue is not empty. --- .../main/java/akka/dispatch/AbstractNodeQueue.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/akka-actor/src/main/java/akka/dispatch/AbstractNodeQueue.java b/akka-actor/src/main/java/akka/dispatch/AbstractNodeQueue.java index 67ce05170b..5768e20987 100644 --- a/akka-actor/src/main/java/akka/dispatch/AbstractNodeQueue.java +++ b/akka-actor/src/main/java/akka/dispatch/AbstractNodeQueue.java @@ -29,12 +29,16 @@ public abstract class AbstractNodeQueue extends AtomicReference peekNode() { - for(;;) { - final Node tail = ((Node)Unsafe.instance.getObjectVolatile(this, tailOffset)); - final Node next = tail.next(); - if (next != null || get() == tail) - return next; + final Node tail = ((Node)Unsafe.instance.getObjectVolatile(this, tailOffset)); + Node next = tail.next(); + if (next == null && get() != tail) { + // 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 the f**k up + do { + next = tail.next(); + } while (next == null); } + return next; } public final T peek() {