Class MpscLinkedAtomicQueue<E>

All Implemented Interfaces:
Iterable<E>, Collection<E>, Queue<E>, MessagePassingQueue<E>

public class MpscLinkedAtomicQueue<E> extends BaseLinkedAtomicQueue<E>
NOTE: This class was automatically generated by org.jctools.queues.atomic.JavaParsingAtomicLinkedQueueGenerator which can found in the jctools-build module. The original source file is MpscLinkedQueue.java. This is a Java port of the MPSC algorithm as presented on 1024 Cores by D. Vyukov. The original has been adapted to Java and it's quirks with regards to memory model and layout:
  1. Use inheritance to ensure no false sharing occurs between producer/consumer node reference fields.
  2. Use XCHG functionality to the best of the JDK ability (see differences in JDK7/8 impls).
  3. Conform to Queue contract on poll. The original semantics are available via relaxedPoll.
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.
  • Constructor Details

    • MpscLinkedAtomicQueue

      public MpscLinkedAtomicQueue()
  • Method Details

    • offer

      public boolean offer(E e)
      Called from a producer thread subject to the restrictions appropriate to the implementation and according to the Queue.offer(Object) interface.

      IMPLEMENTATION NOTES:
      Offer is allowed from multiple threads.
      Offer allocates a new node and:

      1. Swaps it atomically with current producer node (only one producer 'wins')
      2. Sets the new node as the node following from the swapped producer node
      This works because each producer is guaranteed to 'plant' a new node and link the old node. No 2 producers can get the same producer node as part of XCHG guarantee.
      Parameters:
      e - not null, will throw NPE if it is
      Returns:
      true if element was inserted into the queue, false iff full
      See Also:
    • remove

      public boolean remove(Object o)

      This method is only safe to call from the (single) consumer thread, and is subject to best effort when racing with producers. This method is potentially blocking when "bubble"s in the queue are visible.

      Specified by:
      remove in interface Collection<E>
      Overrides:
      remove in class AbstractCollection<E>
    • fill

      public int fill(MessagePassingQueue.Supplier<E> s)
      Description copied from interface: MessagePassingQueue
      Stuff the queue with elements from the supplier. Semantically similar to:
       while(relaxedOffer(s.get());
       
      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.

      Unbounded queues will fill up the queue with a fixed amount rather than fill up to oblivion. WARNING: Explicit assumptions are made with regards to MessagePassingQueue.Supplier.get() make sure you have read and understood these before using this method.

      Returns:
      the number of offered elements
    • fill

      public int fill(MessagePassingQueue.Supplier<E> s, int limit)
      Description copied from interface: MessagePassingQueue
      Stuff the queue with up to limit elements from the supplier. Semantically similar to:

      
         for(int i=0; i < limit && relaxedOffer(s.get()); i++);
       

      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. WARNING: Explicit assumptions are made with regards to MessagePassingQueue.Supplier.get() make sure you have read and understood these before using this method.

      Returns:
      the number of offered elements
    • fill

      Description copied from interface: MessagePassingQueue
      Stuff the queue with elements from the supplier forever. Semantically similar to:

       
        int idleCounter = 0;
        while (exit.keepRunning()) {
            E e = s.get();
            while (!relaxedOffer(e)) {
                idleCounter = wait.idle(idleCounter);
                continue;
            }
            idleCounter = 0;
        }
       
       

      Called from a producer thread subject to the restrictions appropriate to the implementation. The main difference being that implementors MUST assure room in the queue is available BEFORE calling MessagePassingQueue.Supplier.get(). WARNING: Explicit assumptions are made with regards to MessagePassingQueue.Supplier.get() make sure you have read and understood these before using this method.

    • getNextConsumerNode

      private LinkedQueueAtomicNode<E> getNextConsumerNode(LinkedQueueAtomicNode<E> currConsumerNode)