Repository: ignite Updated Branches: refs/heads/ignite-2314 [created] 4f55c84f0
IGNITE-2314: WIP. Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/f8011bb5 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/f8011bb5 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/f8011bb5 Branch: refs/heads/ignite-2314 Commit: f8011bb5b1a34e8c3e7b1ccca022f19c6824f1cb Parents: 4a1a80c Author: vozerov-gridgain <voze...@gridgain.com> Authored: Tue Dec 29 12:51:26 2015 +0300 Committer: vozerov-gridgain <voze...@gridgain.com> Committed: Tue Dec 29 12:51:26 2015 +0300 ---------------------------------------------------------------------- .../util/ManyToOneConcurrentLinkedQueue.java | 90 ++++++++++++++++++++ 1 file changed, 90 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/f8011bb5/modules/core/src/main/java/org/apache/ignite/internal/util/ManyToOneConcurrentLinkedQueue.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/ManyToOneConcurrentLinkedQueue.java b/modules/core/src/main/java/org/apache/ignite/internal/util/ManyToOneConcurrentLinkedQueue.java new file mode 100644 index 0000000..d30a5da --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/ManyToOneConcurrentLinkedQueue.java @@ -0,0 +1,90 @@ +package org.apache.ignite.internal.util; + +import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; + +/** + * + */ +public class ManyToOneConcurrentLinkedQueue<E> +{ + protected static final AtomicReferenceFieldUpdater<ManyToOneConcurrentLinkedQueue, Node> TAIL_UPD = + AtomicReferenceFieldUpdater.newUpdater(ManyToOneConcurrentLinkedQueue.class, Node.class, "tail"); + + protected volatile ManyToOneConcurrentLinkedQueue.Node tail; + + private Node head; + + public ManyToOneConcurrentLinkedQueue() + { + head = new Node(null); + TAIL_UPD.lazySet(this, head); + } + + public boolean offer(final E e) + { + if (null == e) + { + throw new NullPointerException("element cannot be null"); + } + + final Node newTail = new Node(e); + final Node prevTail = swapTail(newTail); + prevTail.setNextOrdered(newTail); + + return true; + } + + public E poll() + { + Object value = null; + + final Node node = head.next; + + if (null != node) + { + value = node.value; + node.value = null; + head = node; + } + + return (E)value; + } + + @SuppressWarnings("unchecked") + private Node swapTail(final Node newTail) + { + return TAIL_UPD.getAndSet(this, newTail); + } + + /** + * Node with data. + */ + private static class Node + { + public static final AtomicReferenceFieldUpdater<Node, Node> NODE_UPD = + AtomicReferenceFieldUpdater.newUpdater(Node.class, Node.class, "next"); + + Object value; + volatile Node next; + + /** + * Constructor. + * + * @param value Value. + */ + private Node(Object value) + { + this.value = value; + } + + /** + * Set next node. + * + * @param next Next node. + */ + void setNextOrdered(Node next) + { + NODE_UPD.lazySet(this, next); + } + } +} \ No newline at end of file