Fork SpscLinkedQueue and SpscLinkedAtomicQueue from JCTools

Motivation:

See #3746.

Modifications:

Fork SpscLinkedQueue and SpscLinkedAtomicQueue from JCTools based on 7846450e28

Result:

Add SpscLinkedQueue and SpscLinkedAtomicQueue and apply it in LocalChannel.
This commit is contained in:
Xiaoyan Lin 2016-02-18 11:28:25 -08:00 committed by Norman Maurer
parent 86272afb30
commit 1b231cbfe8
9 changed files with 1061 additions and 1 deletions

View File

@ -0,0 +1,110 @@
/*
* Copyright 2016 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.netty.util.internal;
import java.util.AbstractQueue;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicReference;
/**
* Forked from <a href="https://github.com/JCTools/JCTools">JCTools</a>.
*/
abstract class BaseLinkedAtomicQueue<E> extends AbstractQueue<E> {
private final AtomicReference<LinkedQueueAtomicNode<E>> producerNode;
private final AtomicReference<LinkedQueueAtomicNode<E>> consumerNode;
public BaseLinkedAtomicQueue() {
producerNode = new AtomicReference<LinkedQueueAtomicNode<E>>();
consumerNode = new AtomicReference<LinkedQueueAtomicNode<E>>();
}
protected final LinkedQueueAtomicNode<E> lvProducerNode() {
return producerNode.get();
}
protected final LinkedQueueAtomicNode<E> lpProducerNode() {
return producerNode.get();
}
protected final void spProducerNode(LinkedQueueAtomicNode<E> node) {
producerNode.lazySet(node);
}
protected final LinkedQueueAtomicNode<E> xchgProducerNode(LinkedQueueAtomicNode<E> node) {
return producerNode.getAndSet(node);
}
protected final LinkedQueueAtomicNode<E> lvConsumerNode() {
return consumerNode.get();
}
protected final LinkedQueueAtomicNode<E> lpConsumerNode() {
return consumerNode.get();
}
protected final void spConsumerNode(LinkedQueueAtomicNode<E> node) {
consumerNode.lazySet(node);
}
@Override
public final Iterator<E> iterator() {
throw new UnsupportedOperationException();
}
/**
* {@inheritDoc} <br>
* <p>
* IMPLEMENTATION NOTES:<br>
* This is an O(n) operation as we run through all the nodes and count them.<br>
*
* @see java.util.Queue#size()
*/
@Override
public final int size() {
LinkedQueueAtomicNode<E> chaserNode = lvConsumerNode();
final LinkedQueueAtomicNode<E> producerNode = lvProducerNode();
int size = 0;
// must chase the nodes all the way to the producer node, but there's no need to chase a moving target.
while (chaserNode != producerNode && size < Integer.MAX_VALUE) {
LinkedQueueAtomicNode<E> next;
while ((next = chaserNode.lvNext()) == null) {
continue;
}
chaserNode = next;
size++;
}
return size;
}
/**
* {@inheritDoc} <br>
* <p>
* IMPLEMENTATION NOTES:<br>
* Queue is empty when producerNode is the same as consumerNode. An alternative implementation would be to observe
* the producerNode.value is null, which also means an empty queue because only the consumerNode.value is allowed to
* be null.
*
* @see MessagePassingQueue#isEmpty()
*/
@Override
public final boolean isEmpty() {
return lvConsumerNode() == lvProducerNode();
}
}

View File

@ -0,0 +1,158 @@
/*
* Copyright 2016 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.netty.util.internal;
import java.util.AbstractQueue;
import java.util.Iterator;
/**
* Forked from <a href="https://github.com/JCTools/JCTools">JCTools</a>.
*
* A base data structure for concurrent linked queues.
*
* @param <E>
*/
abstract class BaseLinkedQueue<E> extends BaseLinkedQueueConsumerNodeRef<E> {
long p01, p02, p03, p04, p05, p06, p07;
long p10, p11, p12, p13, p14, p15, p16, p17;
@Override
public final Iterator<E> iterator() {
throw new UnsupportedOperationException();
}
/**
* {@inheritDoc} <br>
* <p>
* IMPLEMENTATION NOTES:<br>
* This is an O(n) operation as we run through all the nodes and count them.<br>
*
* @see java.util.Queue#size()
*/
@Override
public final int size() {
// Read consumer first, this is important because if the producer is node is 'older' than the consumer the
// consumer may overtake it (consume past it). This will lead to an infinite loop below.
LinkedQueueNode<E> chaserNode = lvConsumerNode();
final LinkedQueueNode<E> producerNode = lvProducerNode();
int size = 0;
// must chase the nodes all the way to the producer node, but there's no need to chase a moving target.
while (chaserNode != producerNode && size < Integer.MAX_VALUE) {
LinkedQueueNode<E> next;
while ((next = chaserNode.lvNext()) == null) {
continue;
}
chaserNode = next;
size++;
}
return size;
}
/**
* {@inheritDoc} <br>
* <p>
* IMPLEMENTATION NOTES:<br>
* Queue is empty when producerNode is the same as consumerNode. An alternative implementation would be to observe
* the producerNode.value is null, which also means an empty queue because only the consumerNode.value is allowed to
* be null.
*
* @see MessagePassingQueue#isEmpty()
*/
@Override
public final boolean isEmpty() {
return lvConsumerNode() == lvProducerNode();
}
@Override
public int capacity() {
return UNBOUNDED_CAPACITY;
}
}
abstract class BaseLinkedQueuePad0<E> extends AbstractQueue<E> implements MessagePassingQueue<E> {
long p00, p01, p02, p03, p04, p05, p06, p07;
long p10, p11, p12, p13, p14, p15, p16;
}
abstract class BaseLinkedQueueProducerNodeRef<E> extends BaseLinkedQueuePad0<E> {
protected static final long P_NODE_OFFSET;
static {
try {
P_NODE_OFFSET = PlatformDependent0.objectFieldOffset(
BaseLinkedQueueProducerNodeRef.class.getDeclaredField("producerNode"));
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
}
protected LinkedQueueNode<E> producerNode;
protected final void spProducerNode(LinkedQueueNode<E> node) {
producerNode = node;
}
@SuppressWarnings("unchecked")
protected final LinkedQueueNode<E> lvProducerNode() {
return (LinkedQueueNode<E>) PlatformDependent0.getObjectVolatile(this, P_NODE_OFFSET);
}
protected final LinkedQueueNode<E> lpProducerNode() {
return producerNode;
}
}
abstract class BaseLinkedQueuePad1<E> extends BaseLinkedQueueProducerNodeRef<E> {
long p01, p02, p03, p04, p05, p06, p07;
long p10, p11, p12, p13, p14, p15, p16, p17;
}
abstract class BaseLinkedQueueConsumerNodeRef<E> extends BaseLinkedQueuePad1<E> {
protected static final long C_NODE_OFFSET;
static {
try {
C_NODE_OFFSET = PlatformDependent0.objectFieldOffset(
BaseLinkedQueueConsumerNodeRef.class.getDeclaredField("consumerNode"));
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
}
protected LinkedQueueNode<E> consumerNode;
protected final void spConsumerNode(LinkedQueueNode<E> node) {
consumerNode = node;
}
@SuppressWarnings("unchecked")
protected final LinkedQueueNode<E> lvConsumerNode() {
return (LinkedQueueNode<E>) PlatformDependent0.getObjectVolatile(this, C_NODE_OFFSET);
}
protected final LinkedQueueNode<E> lpConsumerNode() {
return consumerNode;
}
}

View File

@ -0,0 +1,71 @@
/*
* Copyright 2016 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.netty.util.internal;
import java.util.concurrent.atomic.AtomicReference;
/**
* Forked from <a href="https://github.com/JCTools/JCTools">JCTools</a>.
*/
public final class LinkedQueueAtomicNode<E> extends AtomicReference<LinkedQueueAtomicNode<E>> {
/** */
private static final long serialVersionUID = 2404266111789071508L;
private E value;
LinkedQueueAtomicNode() {
}
LinkedQueueAtomicNode(E val) {
spValue(val);
}
/**
* Gets the current value and nulls out the reference to it from this node.
*
* @return value
*/
public E getAndNullValue() {
E temp = lpValue();
spValue(null);
return temp;
}
public E lpValue() {
return value;
}
public void spValue(E newValue) {
value = newValue;
}
public void soNext(LinkedQueueAtomicNode<E> n) {
lazySet(n);
}
public LinkedQueueAtomicNode<E> lvNext() {
return get();
}
}

View File

@ -0,0 +1,80 @@
/*
* Copyright 2016 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.netty.util.internal;
/**
* Forked from <a href="https://github.com/JCTools/JCTools">JCTools</a>.
*/
final class LinkedQueueNode<E> {
private static final long NEXT_OFFSET;
static {
try {
NEXT_OFFSET = PlatformDependent0.objectFieldOffset(LinkedQueueNode.class.getDeclaredField("next"));
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
}
private E value;
private volatile LinkedQueueNode<E> next;
LinkedQueueNode() {
this(null);
}
LinkedQueueNode(E val) {
spValue(val);
}
/**
* Gets the current value and nulls out the reference to it from this node.
*
* @return value
*/
public E getAndNullValue() {
E temp = lpValue();
spValue(null);
return temp;
}
public E lpValue() {
return value;
}
public void spValue(E newValue) {
value = newValue;
}
public void soNext(LinkedQueueNode<E> n) {
PlatformDependent0.putOrderedObject(this, NEXT_OFFSET, n);
}
public LinkedQueueNode<E> lvNext() {
return next;
}
}

View File

@ -0,0 +1,297 @@
/*
* Copyright 2016 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.netty.util.internal;
import java.util.Queue;
/**
* Forked from <a href="https://github.com/JCTools/JCTools">JCTools</a>.
*
* This is a tagging interface for the queues in this library which implement a subset of the {@link Queue}
* interface sufficient for concurrent message passing.<br>
* Message passing queues provide happens before semantics to messages passed through, namely that writes made
* by the producer before offering the message are visible to the consuming thread after the message has been
* polled out of the queue.
*
* @param <T>
* the event/message type
*/
public interface MessagePassingQueue<T> {
int UNBOUNDED_CAPACITY = -1;
interface Supplier<T> {
/**
* This method will return the next value to be written to the queue. As such the queue
* implementations are commited to insert the value once the call is made.
* <p>
* Users should be aware that underlying queue implementations may upfront claim parts of the queue
* for batch operations and this will effect the view on the queue from the supplier method. In
* particular size and any offer methods may take the view that the full batch has already happened.
*
* @return new element, NEVER null
*/
T get();
}
interface Consumer<T> {
/**
* This method will process an element already removed from the queue. This method is expected to
* never throw an exception.
* <p>
* Users should be aware that underlying queue implementations may upfront claim parts of the queue
* for batch operations and this will effect the view on the queue from the accept method. In
* particular size and any poll/peek methods may take the view that the full batch has already
* happened.
*
* @param e not null
*/
void accept(T e);
}
interface WaitStrategy {
/**
* This method can implement static or dynamic backoff. Dynamic backoff will rely on the counter for
* estimating how long the caller has been idling. The expected usage is:
*
* <pre>
* <code>
* int ic = 0;
* while(true) {
* if(!isGodotArrived()) {
* ic = w.idle(ic);
* continue;
* }
* ic = 0;
* // party with Godot until he goes again
* }
* </code>
* </pre>
*
* @param idleCounter idle calls counter, managed by the idle method until reset
* @return new counter value to be used on subsequent idle cycle
*/
int idle(int idleCounter);
}
interface ExitCondition {
/**
* This method should be implemented such that the flag read or determination cannot be hoisted out of
* a loop which notmally means a volatile load, but with JDK9 VarHandles may mean getOpaque.
*
* @return true as long as we should keep running
*/
boolean keepRunning();
}
/**
* Called from a producer thread subject to the restrictions appropriate to the implementation and
* according to the {@link Queue#offer(Object)} interface.
*
* @param e not null, will throw NPE if it is
* @return true if element was inserted into the queue, false iff full
*/
boolean offer(T e);
/**
* Called from the consumer thread subject to the restrictions appropriate to the implementation and
* according to the {@link Queue#poll()} interface.
*
* @return a message from the queue if one is available, null iff empty
*/
T poll();
/**
* Called from the consumer thread subject to the restrictions appropriate to the implementation and
* according to the {@link Queue#peek()} interface.
*
* @return a message from the queue if one is available, null iff empty
*/
T peek();
/**
* This method's accuracy is subject to concurrent modifications happening as the size is estimated and as
* such is a best effort rather than absolute value. For some implementations this method may be O(n)
* rather than O(1).
*
* @return number of messages in the queue, between 0 and {@link Integer#MAX_VALUE} but less or equals to
* capacity (if bounded).
*/
int size();
/**
* Removes all items from the queue. Called from the consumer thread subject to the restrictions
* appropriate to the implementation and according to the {@link Queue#clear()} interface.
*/
void clear();
/**
* This method's accuracy is subject to concurrent modifications happening as the observation is carried
* out.
*
* @return true if empty, false otherwise
*/
boolean isEmpty();
/**
* @return the capacity of this queue or UNBOUNDED_CAPACITY if not bounded
*/
int capacity();
/**
* Called from a producer thread subject to the restrictions appropriate to the implementation. As opposed
* to {@link Queue#offer(Object)} this method may return false without the queue being full.
*
* @param e not null, will throw NPE if it is
* @return true if element was inserted into the queue, false if unable to offer
*/
boolean relaxedOffer(T e);
/**
* Called from the consumer thread subject to the restrictions appropriate to the implementation. As
* opposed to {@link Queue#poll()} this method may return null without the queue being empty.
*
* @return a message from the queue if one is available, null if unable to poll
*/
T relaxedPoll();
/**
* Called from the consumer thread subject to the restrictions appropriate to the implementation. As
* opposed to {@link Queue#peek()} this method may return null without the queue being empty.
*
* @return a message from the queue if one is available, null if unable to peek
*/
T relaxedPeek();
/**
* Remove all available item from the queue and hand to consume. This should be semantically similar to:
* <pre><code>
* M m;
* while((m = relaxedPoll()) != null){
* c.accept(m);
* }
* </code></pre>
* There's no strong commitment to the queue being empty at the end of a drain. Called from a
* consumer thread subject to the restrictions appropriate to the implementation.
*
* @return the number of polled elements
*/
int drain(Consumer<T> c);
/**
* Stuff the queue with elements from the supplier. Semantically similar to:
* <pre><code>
* while(relaxedOffer(s.get());
* </code></pre>
* There's no strong commitment to the queue being full at the end of a fill. Called from a
* producer thread subject to the restrictions appropriate to the implementation.
*
* @return the number of offered elements
*/
int fill(Supplier<T> s);
/**
* Remove up to <i>limit</i> elements from the queue and hand to consume. This should be semantically
* similar to:
*
* <pre><code>
* M m;
* while((m = relaxedPoll()) != null){
* c.accept(m);
* }
* </code></pre>
*
* There's no strong commitment to the queue being empty at the end of a drain. Called from a consumer
* thread subject to the restrictions appropriate to the implementation.
*
* @return the number of polled elements
*/
int drain(Consumer<T> c, int limit);
/**
* Stuff the queue with up to <i>limit</i> elements from the supplier. Semantically similar to:
*
* <pre>
* <code>
* for(int i=0; i < limit && relaxedOffer(s.get(); i++);
* </code>
* </pre>
*
* There's no strong commitment to the queue being full at the end of a fill. Called from a producer
* thread subject to the restrictions appropriate to the implementation.
*
* @return the number of offered elements
*/
int fill(Supplier<T> s, int limit);
/**
* Remove elements from the queue and hand to consume forever. Semantically similar to:
*
* <pre>
* <code>
* int idleCounter = 0;
* while (exit.keepRunning()) {
* E e = relaxedPoll();
* if(e==null){
* idleCounter = wait.idle(idleCounter);
* continue;
* }
* idleCounter = 0;
* c.accept(e);
* }
* </code>
* </pre>
*
* Called from a consumer thread subject to the restrictions appropriate to the implementation.
*
*/
void drain(Consumer<T> c, WaitStrategy wait, ExitCondition exit);
/**
* Stuff the queue with elements from the supplier forever. Semantically similar to:
*
* <pre>
* <code>
* int idleCounter = 0;
* while (exit.keepRunning()) {
* E e = s.get();
* while (!relaxedOffer(e)) {
* idleCounter = wait.idle(idleCounter);
* continue;
* }
* idleCounter = 0;
* }
* </code>
* </pre>
*
* Called from a producer thread subject to the restrictions appropriate to the implementation.
*
*/
void fill(Supplier<T> s, WaitStrategy wait, ExitCondition exit);
}

View File

@ -468,6 +468,17 @@ public final class PlatformDependent {
return new MpscLinkedQueue<T>();
}
/**
* Create a new {@link Queue} which is safe to use for single producer (one thread!) and a single
* consumer (one thread!).
*/
public static <T> Queue<T> newSpscQueue() {
if (hasUnsafe()) {
return new SpscLinkedQueue<T>();
}
return new SpscLinkedAtomicQueue<T>();
}
/**
* Create a new {@link Queue} which is safe to use for multiple producers (different threads) and a single
* consumer (one thread!) with the given fixes {@code capacity}.

View File

@ -0,0 +1,118 @@
/*
* Copyright 2016 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.netty.util.internal;
/**
* Forked from <a href="https://github.com/JCTools/JCTools">JCTools</a>.
*
* This is a weakened version of the MPSC algorithm as presented <a
* href="http://www.1024cores.net/home/lock-free-algorithms/queues/non-intrusive-mpsc-node-based-queue"> on 1024
* Cores</a> by D. Vyukov. The original has been adapted to Java and it's quirks with regards to memory model and
* layout:
* <ol>
* <li>As this is an SPSC we have no need for XCHG, an ordered store is enough.
* </ol>
* The queue is initialized with a stub node which is set to both the producer and consumer node references. From this
* point follow the notes on offer/poll.
*
* @param <E>
*/
public final class SpscLinkedAtomicQueue<E> extends BaseLinkedAtomicQueue<E> {
public SpscLinkedAtomicQueue() {
super();
LinkedQueueAtomicNode<E> node = new LinkedQueueAtomicNode<E>();
spProducerNode(node);
spConsumerNode(node);
node.soNext(null); // this ensures correct construction: StoreStore
}
/**
* {@inheritDoc} <br>
*
* IMPLEMENTATION NOTES:<br>
* Offer is allowed from a SINGLE thread.<br>
* Offer allocates a new node (holding the offered value) and:
* <ol>
* <li>Sets that node as the producerNode.next
* <li>Sets the new node as the producerNode
* </ol>
* From this follows that producerNode.next is always null and for all other nodes node.next is not null.
*
* @see MessagePassingQueue#offer(Object)
* @see java.util.Queue#offer(java.lang.Object)
*/
@Override
public boolean offer(final E nextValue) {
if (nextValue == null) {
throw new IllegalArgumentException("null elements not allowed");
}
final LinkedQueueAtomicNode<E> nextNode = new LinkedQueueAtomicNode<E>(nextValue);
lpProducerNode().soNext(nextNode);
spProducerNode(nextNode);
return true;
}
/**
* {@inheritDoc} <br>
*
* IMPLEMENTATION NOTES:<br>
* Poll is allowed from a SINGLE thread.<br>
* Poll reads the next node from the consumerNode and:
* <ol>
* <li>If it is null, the queue is empty.
* <li>If it is not null set it as the consumer node and return it's now evacuated value.
* </ol>
* This means the consumerNode.value is always null, which is also the starting point for the queue. Because null
* values are not allowed to be offered this is the only node with it's value set to null at any one time.
*
*/
@Override
public E poll() {
final LinkedQueueAtomicNode<E> nextNode = lpConsumerNode().lvNext();
if (nextNode != null) {
// we have to null out the value because we are going to hang on to the node
final E nextValue = nextNode.getAndNullValue();
spConsumerNode(nextNode);
return nextValue;
}
return null;
}
@Override
public E peek() {
final LinkedQueueAtomicNode<E> nextNode = lpConsumerNode().lvNext();
if (nextNode != null) {
return nextNode.lpValue();
} else {
return null;
}
}
}

View File

@ -0,0 +1,215 @@
/*
* Copyright 2016 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.netty.util.internal;
/**
* Forked from <a href="https://github.com/JCTools/JCTools">JCTools</a>.
*
* This is a weakened version of the MPSC algorithm as presented
* <a href="http://www.1024cores.net/home/lock-free-algorithms/queues/non-intrusive-mpsc-node-based-queue"> on
* 1024 Cores</a> by D. Vyukov. The original has been adapted to Java and it's quirks with regards to memory
* model and layout:
* <ol>
* <li>Use inheritance to ensure no false sharing occurs between producer/consumer node reference fields.
* <li>As this is an SPSC we have no need for XCHG, an ordered store is enough.
* </ol>
* The queue is initialized with a stub node which is set to both the producer and consumer node references.
* From this point follow the notes on offer/poll.
*
* @param <E>
*/
public class SpscLinkedQueue<E> extends BaseLinkedQueue<E> {
public SpscLinkedQueue() {
spProducerNode(new LinkedQueueNode<E>());
spConsumerNode(producerNode);
consumerNode.soNext(null); // this ensures correct construction: StoreStore
}
/**
* {@inheritDoc} <br>
*
* IMPLEMENTATION NOTES:<br>
* Offer is allowed from a SINGLE thread.<br>
* Offer allocates a new node (holding the offered value) and:
* <ol>
* <li>Sets that node as the producerNode.next
* <li>Sets the new node as the producerNode
* </ol>
* From this follows that producerNode.next is always null and for all other nodes node.next is not null.
*
* @see MessagePassingQueue#offer(Object)
* @see java.util.Queue#offer(java.lang.Object)
*/
@Override
public boolean offer(final E nextValue) {
if (nextValue == null) {
throw new IllegalArgumentException("null elements not allowed");
}
final LinkedQueueNode<E> nextNode = new LinkedQueueNode<E>(nextValue);
producerNode.soNext(nextNode);
producerNode = nextNode;
return true;
}
/**
* {@inheritDoc} <br>
*
* IMPLEMENTATION NOTES:<br>
* Poll is allowed from a SINGLE thread.<br>
* Poll reads the next node from the consumerNode and:
* <ol>
* <li>If it is null, the queue is empty.
* <li>If it is not null set it as the consumer node and return it's now evacuated value.
* </ol>
* This means the consumerNode.value is always null, which is also the starting point for the queue.
* Because null values are not allowed to be offered this is the only node with it's value set to null at
* any one time.
*
*/
@Override
public E poll() {
final LinkedQueueNode<E> nextNode = consumerNode.lvNext();
if (nextNode != null) {
// we have to null out the value because we are going to hang on to the node
final E nextValue = nextNode.getAndNullValue();
consumerNode = nextNode;
return nextValue;
}
return null;
}
@Override
public E peek() {
final LinkedQueueNode<E> nextNode = consumerNode.lvNext();
if (nextNode != null) {
return nextNode.lpValue();
} else {
return null;
}
}
@Override
public boolean relaxedOffer(E e) {
return offer(e);
}
@Override
public E relaxedPoll() {
return poll();
}
@Override
public E relaxedPeek() {
return peek();
}
@Override
public int drain(Consumer<E> c) {
long result = 0; // use long to force safepoint into loop below
int drained;
do {
drained = drain(c, 4096);
result += drained;
} while (drained == 4096 && result <= Integer.MAX_VALUE - 4096);
return (int) result;
}
@Override
public int fill(Supplier<E> s) {
long result = 0; // result is a long because we want to have a safepoint check at regular intervals
do {
fill(s, 4096);
result += 4096;
} while (result <= Integer.MAX_VALUE - 4096);
return (int) result;
}
@Override
public int drain(Consumer<E> c, int limit) {
LinkedQueueNode<E> chaserNode = this.consumerNode;
for (int i = 0; i < limit; i++) {
chaserNode = chaserNode.lvNext();
if (chaserNode == null) {
return i;
}
// we have to null out the value because we are going to hang on to the node
final E nextValue = chaserNode.getAndNullValue();
this.consumerNode = chaserNode;
c.accept(nextValue);
}
return limit;
}
@Override
public int fill(Supplier<E> s, int limit) {
LinkedQueueNode<E> chaserNode = producerNode;
for (int i = 0; i < limit; i++) {
final LinkedQueueNode<E> nextNode = new LinkedQueueNode<E>(s.get());
chaserNode.soNext(nextNode);
chaserNode = nextNode;
this.producerNode = chaserNode;
}
return limit;
}
@Override
public void drain(Consumer<E> c, WaitStrategy wait, ExitCondition exit) {
LinkedQueueNode<E> chaserNode = this.consumerNode;
int idleCounter = 0;
while (exit.keepRunning()) {
for (int i = 0; i < 4096; i++) {
final LinkedQueueNode<E> next = chaserNode.lvNext();
if (next == null) {
idleCounter = wait.idle(idleCounter);
continue;
}
chaserNode = next;
idleCounter = 0;
// we have to null out the value because we are going to hang on to the node
final E nextValue = chaserNode.getAndNullValue();
this.consumerNode = chaserNode;
c.accept(nextValue);
}
}
}
@Override
public void fill(Supplier<E> s, WaitStrategy wait, ExitCondition exit) {
LinkedQueueNode<E> chaserNode = producerNode;
while (exit.keepRunning()) {
for (int i = 0; i < 4096; i++) {
final LinkedQueueNode<E> nextNode = new LinkedQueueNode<E>(s.get());
chaserNode.soNext(nextNode);
chaserNode = nextNode;
this.producerNode = chaserNode;
}
}
}
}

View File

@ -55,7 +55,7 @@ public class LocalChannel extends AbstractChannel {
private final ChannelConfig config = new DefaultChannelConfig(this);
// To further optimize this we could write our own SPSC queue.
private final Queue<Object> inboundBuffer = PlatformDependent.newMpscQueue();
private final Queue<Object> inboundBuffer = PlatformDependent.newSpscQueue();
private final Runnable readTask = new Runnable() {
@Override
public void run() {